binutils/dwarf.c abbrev list leak
[binutils-gdb.git] / binutils / dwarf.c
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #include <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>
34
35 #ifdef HAVE_LIBDEBUGINFOD
36 #include <elfutils/debuginfod.h>
37 #endif
38
39 #include <limits.h>
40 #ifndef CHAR_BIT
41 #define CHAR_BIT 8
42 #endif
43
44 #ifndef ENABLE_CHECKING
45 #define ENABLE_CHECKING 0
46 #endif
47
48 #undef MAX
49 #undef MIN
50 #define MAX(a, b) ((a) > (b) ? (a) : (b))
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52
53 static const char *regname (unsigned int regno, int row);
54 static const char *regname_internal_by_table_only (unsigned int regno);
55
56 static int have_frame_base;
57 static int need_base_address;
58
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
65
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
69 {
70 DWO_NAME,
71 DWO_DIR,
72 DWO_ID
73 } dwo_type;
74
75 typedef struct dwo_info
76 {
77 dwo_type type;
78 const char * value;
79 uint64_t cu_offset;
80 struct dwo_info * next;
81 } dwo_info;
82
83 static dwo_info *first_dwo_info = NULL;
84 static bool need_dwo_info;
85
86 separate_info * first_separate_info = NULL;
87
88 unsigned int eh_addr_size;
89
90 int do_debug_info;
91 int do_debug_abbrevs;
92 int do_debug_lines;
93 int do_debug_pubnames;
94 int do_debug_pubtypes;
95 int do_debug_aranges;
96 int do_debug_ranges;
97 int do_debug_frames;
98 int do_debug_frames_interp;
99 int do_debug_macinfo;
100 int do_debug_str;
101 int do_debug_str_offsets;
102 int do_debug_loc;
103 int do_gdb_index;
104 int do_trace_info;
105 int do_trace_abbrevs;
106 int do_trace_aranges;
107 int do_debug_addr;
108 int do_debug_cu_index;
109 int do_wide;
110 int do_debug_links;
111 int do_follow_links = DEFAULT_FOR_FOLLOW_LINKS;
112 #ifdef HAVE_LIBDEBUGINFOD
113 int use_debuginfod = 1;
114 #endif
115 bool do_checks;
116
117 int dwarf_cutoff_level = -1;
118 unsigned long dwarf_start_die;
119
120 int dwarf_check = 0;
121
122 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
123 sections. For version 1 package files, each set is stored in SHNDX_POOL
124 as a zero-terminated list of section indexes comprising one set of debug
125 sections from a .dwo file. */
126
127 static unsigned int *shndx_pool = NULL;
128 static unsigned int shndx_pool_size = 0;
129 static unsigned int shndx_pool_used = 0;
130
131 /* For version 2 package files, each set contains an array of section offsets
132 and an array of section sizes, giving the offset and size of the
133 contribution from a CU or TU within one of the debug sections.
134 When displaying debug info from a package file, we need to use these
135 tables to locate the corresponding contributions to each section. */
136
137 struct cu_tu_set
138 {
139 uint64_t signature;
140 uint64_t section_offsets[DW_SECT_MAX];
141 size_t section_sizes[DW_SECT_MAX];
142 };
143
144 static int cu_count = 0;
145 static int tu_count = 0;
146 static struct cu_tu_set *cu_sets = NULL;
147 static struct cu_tu_set *tu_sets = NULL;
148
149 static bool load_cu_tu_indexes (void *);
150
151 /* An array that indicates for a given level of CU nesting whether
152 the latest DW_AT_type seen for that level was a signed type or
153 an unsigned type. */
154 #define MAX_CU_NESTING (1 << 8)
155 static bool level_type_signed[MAX_CU_NESTING];
156
157 /* Values for do_debug_lines. */
158 #define FLAG_DEBUG_LINES_RAW 1
159 #define FLAG_DEBUG_LINES_DECODED 2
160
161 static unsigned int
162 size_of_encoded_value (int encoding)
163 {
164 switch (encoding & 0x7)
165 {
166 default: /* ??? */
167 case 0: return eh_addr_size;
168 case 2: return 2;
169 case 3: return 4;
170 case 4: return 8;
171 }
172 }
173
174 static uint64_t
175 get_encoded_value (unsigned char **pdata,
176 int encoding,
177 struct dwarf_section *section,
178 unsigned char * end)
179 {
180 unsigned char * data = * pdata;
181 unsigned int size = size_of_encoded_value (encoding);
182 uint64_t val;
183
184 if (data >= end || size > (size_t) (end - data))
185 {
186 warn (_("Encoded value extends past end of section\n"));
187 * pdata = end;
188 return 0;
189 }
190
191 /* PR 17512: file: 002-829853-0.004. */
192 if (size > 8)
193 {
194 warn (_("Encoded size of %d is too large to read\n"), size);
195 * pdata = end;
196 return 0;
197 }
198
199 /* PR 17512: file: 1085-5603-0.004. */
200 if (size == 0)
201 {
202 warn (_("Encoded size of 0 is too small to read\n"));
203 * pdata = end;
204 return 0;
205 }
206
207 if (encoding & DW_EH_PE_signed)
208 val = byte_get_signed (data, size);
209 else
210 val = byte_get (data, size);
211
212 if ((encoding & 0x70) == DW_EH_PE_pcrel)
213 val += section->address + (data - section->start);
214
215 * pdata = data + size;
216 return val;
217 }
218
219 /* Print a uint64_t value (typically an address, offset or length) in
220 hexadecimal format, followed by a space. The precision displayed is
221 determined by the NUM_BYTES parameter. */
222
223 static void
224 print_hex (uint64_t value, unsigned num_bytes)
225 {
226 if (num_bytes == 0)
227 num_bytes = 2;
228
229 printf ("%0*" PRIx64 " ", num_bytes * 2,
230 value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
231 }
232
233 /* Like print_hex, but no trailing space. */
234
235 static void
236 print_hex_ns (uint64_t value, unsigned num_bytes)
237 {
238 if (num_bytes == 0)
239 num_bytes = 2;
240
241 printf ("%0*" PRIx64, num_bytes * 2,
242 value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
243 }
244
245 /* Print a view number in hexadecimal value, with the same width as
246 print_hex would have printed it. */
247
248 static void
249 print_view (uint64_t value, unsigned num_bytes)
250 {
251 if (num_bytes == 0)
252 num_bytes = 2;
253
254 printf ("v%0*" PRIx64 " ", num_bytes * 2 - 1,
255 value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
256 }
257
258 static const char *
259 null_name (const char *p)
260 {
261 if (p == NULL)
262 p = _("unknown");
263 return p;
264 }
265
266 /* Read in a LEB128 encoded value starting at address DATA.
267 If SIGN is true, return a signed LEB128 value.
268 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
269 If STATUS_RETURN is not NULL, return with bit 0 (LSB) set if the
270 terminating byte was not found and with bit 1 set if the value
271 overflows a uint64_t.
272 No bytes will be read at address END or beyond. */
273
274 uint64_t
275 read_leb128 (unsigned char *data,
276 const unsigned char *const end,
277 bool sign,
278 unsigned int *length_return,
279 int *status_return)
280 {
281 uint64_t result = 0;
282 unsigned int num_read = 0;
283 unsigned int shift = 0;
284 int status = 1;
285
286 while (data < end)
287 {
288 unsigned char byte = *data++;
289 unsigned char lost, mask;
290
291 num_read++;
292
293 if (shift < CHAR_BIT * sizeof (result))
294 {
295 result |= ((uint64_t) (byte & 0x7f)) << shift;
296 /* These bits overflowed. */
297 lost = byte ^ (result >> shift);
298 /* And this is the mask of possible overflow bits. */
299 mask = 0x7f ^ ((uint64_t) 0x7f << shift >> shift);
300 shift += 7;
301 }
302 else
303 {
304 lost = byte;
305 mask = 0x7f;
306 }
307 if ((lost & mask) != (sign && (int64_t) result < 0 ? mask : 0))
308 status |= 2;
309
310 if ((byte & 0x80) == 0)
311 {
312 status &= ~1;
313 if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
314 result |= -((uint64_t) 1 << shift);
315 break;
316 }
317 }
318
319 if (length_return != NULL)
320 *length_return = num_read;
321 if (status_return != NULL)
322 *status_return = status;
323
324 return result;
325 }
326
327 /* Read AMOUNT bytes from PTR and store them in VAL.
328 Checks to make sure that the read will not reach or pass END.
329 FUNC chooses whether the value read is unsigned or signed, and may
330 be either byte_get or byte_get_signed. If INC is true, PTR is
331 incremented after reading the value.
332 This macro cannot protect against PTR values derived from user input.
333 The C standard sections 6.5.6 and 6.5.8 say attempts to do so using
334 pointers is undefined behaviour. */
335 #define SAFE_BYTE_GET_INTERNAL(VAL, PTR, AMOUNT, END, FUNC, INC) \
336 do \
337 { \
338 size_t amount = (AMOUNT); \
339 if (sizeof (VAL) < amount) \
340 { \
341 error (ngettext ("internal error: attempt to read %d byte " \
342 "of data in to %d sized variable", \
343 "internal error: attempt to read %d bytes " \
344 "of data in to %d sized variable", \
345 amount), \
346 (int) amount, (int) sizeof (VAL)); \
347 amount = sizeof (VAL); \
348 } \
349 if (ENABLE_CHECKING) \
350 assert ((PTR) <= (END)); \
351 size_t avail = (END) - (PTR); \
352 if ((PTR) > (END)) \
353 avail = 0; \
354 if (amount > avail) \
355 amount = avail; \
356 if (amount == 0) \
357 (VAL) = 0; \
358 else \
359 (VAL) = (FUNC) ((PTR), amount); \
360 if (INC) \
361 (PTR) += amount; \
362 } \
363 while (0)
364
365 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END) \
366 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, false)
367
368 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
369 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, true)
370
371 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END) \
372 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, false)
373
374 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
375 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, true)
376
377 typedef struct State_Machine_Registers
378 {
379 uint64_t address;
380 unsigned int view;
381 unsigned int file;
382 unsigned int line;
383 unsigned int column;
384 int is_stmt;
385 int basic_block;
386 unsigned char op_index;
387 unsigned char end_sequence;
388 /* This variable hold the number of the last entry seen
389 in the File Table. */
390 unsigned int last_file_entry;
391 } SMR;
392
393 static SMR state_machine_regs;
394
395 static void
396 reset_state_machine (int is_stmt)
397 {
398 state_machine_regs.address = 0;
399 state_machine_regs.view = 0;
400 state_machine_regs.op_index = 0;
401 state_machine_regs.file = 1;
402 state_machine_regs.line = 1;
403 state_machine_regs.column = 0;
404 state_machine_regs.is_stmt = is_stmt;
405 state_machine_regs.basic_block = 0;
406 state_machine_regs.end_sequence = 0;
407 state_machine_regs.last_file_entry = 0;
408 }
409
410 /* Handled an extend line op.
411 Returns the number of bytes read. */
412
413 static size_t
414 process_extended_line_op (unsigned char * data,
415 int is_stmt,
416 unsigned char * end)
417 {
418 unsigned char op_code;
419 size_t len, header_len;
420 unsigned char *name;
421 unsigned char *orig_data = data;
422 uint64_t adr, val;
423
424 READ_ULEB (len, data, end);
425 header_len = data - orig_data;
426
427 if (len == 0 || data >= end || len > (size_t) (end - data))
428 {
429 warn (_("Badly formed extended line op encountered!\n"));
430 return header_len;
431 }
432
433 op_code = *data++;
434
435 printf (_(" Extended opcode %d: "), op_code);
436
437 switch (op_code)
438 {
439 case DW_LNE_end_sequence:
440 printf (_("End of Sequence\n\n"));
441 reset_state_machine (is_stmt);
442 break;
443
444 case DW_LNE_set_address:
445 /* PR 17512: file: 002-100480-0.004. */
446 if (len - 1 > 8)
447 {
448 warn (_("Length (%zu) of DW_LNE_set_address op is too long\n"),
449 len - 1);
450 adr = 0;
451 }
452 else
453 SAFE_BYTE_GET (adr, data, len - 1, end);
454 printf (_("set Address to %#" PRIx64 "\n"), adr);
455 state_machine_regs.address = adr;
456 state_machine_regs.view = 0;
457 state_machine_regs.op_index = 0;
458 break;
459
460 case DW_LNE_define_file:
461 printf (_("define new File Table entry\n"));
462 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
463 printf (" %d\t", ++state_machine_regs.last_file_entry);
464
465 {
466 size_t l;
467
468 name = data;
469 l = strnlen ((char *) data, end - data);
470 data += l;
471 if (data < end)
472 data++;
473 READ_ULEB (val, data, end);
474 printf ("%" PRIu64 "\t", val);
475 READ_ULEB (val, data, end);
476 printf ("%" PRIu64 "\t", val);
477 READ_ULEB (val, data, end);
478 printf ("%" PRIu64 "\t", val);
479 printf ("%.*s\n\n", (int) l, name);
480 }
481
482 if (((size_t) (data - orig_data) != len + header_len) || data >= end)
483 warn (_("DW_LNE_define_file: Bad opcode length\n"));
484 break;
485
486 case DW_LNE_set_discriminator:
487 READ_ULEB (val, data, end);
488 printf (_("set Discriminator to %" PRIu64 "\n"), val);
489 break;
490
491 /* HP extensions. */
492 case DW_LNE_HP_negate_is_UV_update:
493 printf ("DW_LNE_HP_negate_is_UV_update\n");
494 break;
495 case DW_LNE_HP_push_context:
496 printf ("DW_LNE_HP_push_context\n");
497 break;
498 case DW_LNE_HP_pop_context:
499 printf ("DW_LNE_HP_pop_context\n");
500 break;
501 case DW_LNE_HP_set_file_line_column:
502 printf ("DW_LNE_HP_set_file_line_column\n");
503 break;
504 case DW_LNE_HP_set_routine_name:
505 printf ("DW_LNE_HP_set_routine_name\n");
506 break;
507 case DW_LNE_HP_set_sequence:
508 printf ("DW_LNE_HP_set_sequence\n");
509 break;
510 case DW_LNE_HP_negate_post_semantics:
511 printf ("DW_LNE_HP_negate_post_semantics\n");
512 break;
513 case DW_LNE_HP_negate_function_exit:
514 printf ("DW_LNE_HP_negate_function_exit\n");
515 break;
516 case DW_LNE_HP_negate_front_end_logical:
517 printf ("DW_LNE_HP_negate_front_end_logical\n");
518 break;
519 case DW_LNE_HP_define_proc:
520 printf ("DW_LNE_HP_define_proc\n");
521 break;
522 case DW_LNE_HP_source_file_correlation:
523 {
524 unsigned char *edata = data + len - 1;
525
526 printf ("DW_LNE_HP_source_file_correlation\n");
527
528 while (data < edata)
529 {
530 unsigned int opc;
531
532 READ_ULEB (opc, data, edata);
533
534 switch (opc)
535 {
536 case DW_LNE_HP_SFC_formfeed:
537 printf (" DW_LNE_HP_SFC_formfeed\n");
538 break;
539 case DW_LNE_HP_SFC_set_listing_line:
540 READ_ULEB (val, data, edata);
541 printf (" DW_LNE_HP_SFC_set_listing_line (%" PRIu64 ")\n",
542 val);
543 break;
544 case DW_LNE_HP_SFC_associate:
545 printf (" DW_LNE_HP_SFC_associate ");
546 READ_ULEB (val, data, edata);
547 printf ("(%" PRIu64 , val);
548 READ_ULEB (val, data, edata);
549 printf (",%" PRIu64, val);
550 READ_ULEB (val, data, edata);
551 printf (",%" PRIu64 ")\n", val);
552 break;
553 default:
554 printf (_(" UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
555 data = edata;
556 break;
557 }
558 }
559 }
560 break;
561
562 default:
563 {
564 unsigned int rlen = len - 1;
565
566 if (op_code >= DW_LNE_lo_user
567 /* The test against DW_LNW_hi_user is redundant due to
568 the limited range of the unsigned char data type used
569 for op_code. */
570 /*&& op_code <= DW_LNE_hi_user*/)
571 printf (_("user defined: "));
572 else
573 printf (_("UNKNOWN: "));
574 printf (_("length %d ["), rlen);
575 for (; rlen; rlen--)
576 printf (" %02x", *data++);
577 printf ("]\n");
578 }
579 break;
580 }
581
582 return len + header_len;
583 }
584
585 static const unsigned char *
586 fetch_indirect_string (uint64_t offset)
587 {
588 struct dwarf_section *section = &debug_displays [str].section;
589 const unsigned char * ret;
590
591 if (section->start == NULL)
592 return (const unsigned char *) _("<no .debug_str section>");
593
594 if (offset >= section->size)
595 {
596 warn (_("DW_FORM_strp offset too big: %#" PRIx64 "\n"), offset);
597 return (const unsigned char *) _("<offset is too big>");
598 }
599
600 ret = section->start + offset;
601 /* Unfortunately we cannot rely upon the .debug_str section ending with a
602 NUL byte. Since our caller is expecting to receive a well formed C
603 string we test for the lack of a terminating byte here. */
604 if (strnlen ((const char *) ret, section->size - offset)
605 == section->size - offset)
606 ret = (const unsigned char *)
607 _("<no NUL byte at end of .debug_str section>");
608
609 return ret;
610 }
611
612 static const unsigned char *
613 fetch_indirect_line_string (uint64_t offset)
614 {
615 struct dwarf_section *section = &debug_displays [line_str].section;
616 const unsigned char * ret;
617
618 if (section->start == NULL)
619 return (const unsigned char *) _("<no .debug_line_str section>");
620
621 if (offset >= section->size)
622 {
623 warn (_("DW_FORM_line_strp offset too big: %#" PRIx64 "\n"), offset);
624 return (const unsigned char *) _("<offset is too big>");
625 }
626
627 ret = section->start + offset;
628 /* Unfortunately we cannot rely upon the .debug_line_str section ending
629 with a NUL byte. Since our caller is expecting to receive a well formed
630 C string we test for the lack of a terminating byte here. */
631 if (strnlen ((const char *) ret, section->size - offset)
632 == section->size - offset)
633 ret = (const unsigned char *)
634 _("<no NUL byte at end of .debug_line_str section>");
635
636 return ret;
637 }
638
639 static const char *
640 fetch_indexed_string (uint64_t idx,
641 struct cu_tu_set *this_set,
642 uint64_t offset_size,
643 bool dwo,
644 uint64_t str_offsets_base)
645 {
646 enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
647 enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
648 struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
649 struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
650 uint64_t index_offset;
651 uint64_t str_offset;
652 const char * ret;
653
654 if (index_section->start == NULL)
655 return (dwo ? _("<no .debug_str_offsets.dwo section>")
656 : _("<no .debug_str_offsets section>"));
657
658 if (str_section->start == NULL)
659 return (dwo ? _("<no .debug_str.dwo section>")
660 : _("<no .debug_str section>"));
661
662 if (_mul_overflow (idx, offset_size, &index_offset)
663 || (this_set != NULL
664 && ((index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS])
665 < this_set->section_offsets [DW_SECT_STR_OFFSETS]))
666 || (index_offset += str_offsets_base) < str_offsets_base
667 || index_offset + offset_size < offset_size
668 || index_offset + offset_size > index_section->size)
669 {
670 warn (_("string index of %" PRIu64 " converts to an offset of %#" PRIx64
671 " which is too big for section %s"),
672 idx, index_offset, str_section->name);
673
674 return _("<string index too big>");
675 }
676
677 str_offset = byte_get (index_section->start + index_offset, offset_size);
678
679 str_offset -= str_section->address;
680 if (str_offset >= str_section->size)
681 {
682 warn (_("indirect offset too big: %#" PRIx64 "\n"), str_offset);
683 return _("<indirect index offset is too big>");
684 }
685
686 ret = (const char *) str_section->start + str_offset;
687
688 /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
689 Since our caller is expecting to receive a well formed C string we test
690 for the lack of a terminating byte here. */
691 if (strnlen (ret, str_section->size - str_offset)
692 == str_section->size - str_offset)
693 return _("<no NUL byte at end of section>");
694
695 return ret;
696 }
697
698 static uint64_t
699 fetch_indexed_addr (uint64_t offset, uint32_t num_bytes)
700 {
701 struct dwarf_section *section = &debug_displays [debug_addr].section;
702
703 if (section->start == NULL)
704 {
705 warn (_("Cannot fetch indexed address: the .debug_addr section is missing\n"));
706 return 0;
707 }
708
709 if (offset + num_bytes > section->size)
710 {
711 warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
712 section->name, offset);
713 return 0;
714 }
715
716 return byte_get (section->start + offset, num_bytes);
717 }
718
719 /* Fetch a value from a debug section that has been indexed by
720 something in another section (eg DW_FORM_loclistx or DW_FORM_rnglistx).
721 Returns -1 if the value could not be found. */
722
723 static uint64_t
724 fetch_indexed_value (uint64_t idx,
725 enum dwarf_section_display_enum sec_enum,
726 uint64_t base_address)
727 {
728 struct dwarf_section *section = &debug_displays [sec_enum].section;
729
730 if (section->start == NULL)
731 {
732 warn (_("Unable to locate %s section\n"), section->uncompressed_name);
733 return -1;
734 }
735
736 if (section->size < 4)
737 {
738 warn (_("Section %s is too small to contain an value indexed from another section!\n"),
739 section->name);
740 return -1;
741 }
742
743 uint32_t pointer_size, bias;
744
745 if (byte_get (section->start, 4) == 0xffffffff)
746 {
747 pointer_size = 8;
748 bias = 20;
749 }
750 else
751 {
752 pointer_size = 4;
753 bias = 12;
754 }
755
756 uint64_t offset = idx * pointer_size;
757
758 /* Offsets are biased by the size of the section header
759 or base address. */
760 if (base_address)
761 offset += base_address;
762 else
763 offset += bias;
764
765 if (offset + pointer_size > section->size)
766 {
767 warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
768 section->name, offset);
769 return -1;
770 }
771
772 return byte_get (section->start + offset, pointer_size);
773 }
774
775 /* FIXME: There are better and more efficient ways to handle
776 these structures. For now though, I just want something that
777 is simple to implement. */
778 /* Records a single attribute in an abbrev. */
779 typedef struct abbrev_attr
780 {
781 unsigned long attribute;
782 unsigned long form;
783 int64_t implicit_const;
784 struct abbrev_attr *next;
785 }
786 abbrev_attr;
787
788 /* Records a single abbrev. */
789 typedef struct abbrev_entry
790 {
791 unsigned long number;
792 unsigned long tag;
793 int children;
794 struct abbrev_attr * first_attr;
795 struct abbrev_attr * last_attr;
796 struct abbrev_entry * next;
797 }
798 abbrev_entry;
799
800 /* Records a set of abbreviations. */
801 typedef struct abbrev_list
802 {
803 abbrev_entry * first_abbrev;
804 abbrev_entry * last_abbrev;
805 unsigned char * raw;
806 struct abbrev_list * next;
807 unsigned char * start_of_next_abbrevs;
808 }
809 abbrev_list;
810
811 /* Records all the abbrevs found so far. */
812 static struct abbrev_list * abbrev_lists = NULL;
813
814 typedef struct abbrev_map
815 {
816 uint64_t start;
817 uint64_t end;
818 abbrev_list *list;
819 } abbrev_map;
820
821 /* Maps between CU offsets and abbrev sets. */
822 static abbrev_map * cu_abbrev_map = NULL;
823 static unsigned long num_abbrev_map_entries = 0;
824 static unsigned long next_free_abbrev_map_entry = 0;
825
826 #define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
827 #define ABBREV_MAP_ENTRIES_INCREMENT 8
828
829 static void
830 record_abbrev_list_for_cu (uint64_t start, uint64_t end,
831 abbrev_list *list, abbrev_list *free_list)
832 {
833 if (free_list != NULL)
834 {
835 list->next = abbrev_lists;
836 abbrev_lists = list;
837 }
838
839 if (cu_abbrev_map == NULL)
840 {
841 num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
842 cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
843 }
844 else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
845 {
846 num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
847 cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
848 }
849
850 cu_abbrev_map[next_free_abbrev_map_entry].start = start;
851 cu_abbrev_map[next_free_abbrev_map_entry].end = end;
852 cu_abbrev_map[next_free_abbrev_map_entry].list = list;
853 next_free_abbrev_map_entry ++;
854 }
855
856 static abbrev_list *
857 free_abbrev_list (abbrev_list *list)
858 {
859 abbrev_entry *abbrv = list->first_abbrev;
860
861 while (abbrv)
862 {
863 abbrev_attr *attr = abbrv->first_attr;
864
865 while (attr)
866 {
867 abbrev_attr *next_attr = attr->next;
868 free (attr);
869 attr = next_attr;
870 }
871
872 abbrev_entry *next_abbrev = abbrv->next;
873 free (abbrv);
874 abbrv = next_abbrev;
875 }
876
877 abbrev_list *next = list->next;
878 free (list);
879 return next;
880 }
881
882 static void
883 free_all_abbrevs (void)
884 {
885 while (abbrev_lists)
886 abbrev_lists = free_abbrev_list (abbrev_lists);
887
888 free (cu_abbrev_map);
889 cu_abbrev_map = NULL;
890 next_free_abbrev_map_entry = 0;
891 }
892
893 static abbrev_list *
894 find_abbrev_list_by_raw_abbrev (unsigned char *raw)
895 {
896 abbrev_list * list;
897
898 for (list = abbrev_lists; list != NULL; list = list->next)
899 if (list->raw == raw)
900 return list;
901
902 return NULL;
903 }
904
905 /* Find the abbreviation map for the CU that includes OFFSET.
906 OFFSET is an absolute offset from the start of the .debug_info section. */
907 /* FIXME: This function is going to slow down readelf & objdump.
908 Not caching abbrevs is likely the answer. */
909
910 static abbrev_map *
911 find_abbrev_map_by_offset (uint64_t offset)
912 {
913 unsigned long i;
914
915 for (i = 0; i < next_free_abbrev_map_entry; i++)
916 if (cu_abbrev_map[i].start <= offset
917 && cu_abbrev_map[i].end > offset)
918 return cu_abbrev_map + i;
919
920 return NULL;
921 }
922
923 static void
924 add_abbrev (unsigned long number,
925 unsigned long tag,
926 int children,
927 abbrev_list * list)
928 {
929 abbrev_entry * entry;
930
931 entry = (abbrev_entry *) xmalloc (sizeof (*entry));
932
933 entry->number = number;
934 entry->tag = tag;
935 entry->children = children;
936 entry->first_attr = NULL;
937 entry->last_attr = NULL;
938 entry->next = NULL;
939
940 assert (list != NULL);
941
942 if (list->first_abbrev == NULL)
943 list->first_abbrev = entry;
944 else
945 list->last_abbrev->next = entry;
946
947 list->last_abbrev = entry;
948 }
949
950 static void
951 add_abbrev_attr (unsigned long attribute,
952 unsigned long form,
953 int64_t implicit_const,
954 abbrev_list *list)
955 {
956 abbrev_attr *attr;
957
958 attr = (abbrev_attr *) xmalloc (sizeof (*attr));
959
960 attr->attribute = attribute;
961 attr->form = form;
962 attr->implicit_const = implicit_const;
963 attr->next = NULL;
964
965 assert (list != NULL && list->last_abbrev != NULL);
966
967 if (list->last_abbrev->first_attr == NULL)
968 list->last_abbrev->first_attr = attr;
969 else
970 list->last_abbrev->last_attr->next = attr;
971
972 list->last_abbrev->last_attr = attr;
973 }
974
975 /* Return processed (partial) contents of a .debug_abbrev section.
976 Returns NULL on errors. */
977
978 static abbrev_list *
979 process_abbrev_set (struct dwarf_section *section,
980 unsigned char *start,
981 unsigned char *end)
982 {
983 abbrev_list *list = xmalloc (sizeof (*list));
984 list->first_abbrev = NULL;
985 list->last_abbrev = NULL;
986 list->raw = start;
987 list->next = NULL;
988
989 while (start < end)
990 {
991 unsigned long entry;
992 unsigned long tag;
993 unsigned long attribute;
994 int children;
995
996 READ_ULEB (entry, start, end);
997
998 /* A single zero is supposed to end the set according
999 to the standard. If there's more, then signal that to
1000 the caller. */
1001 if (start == end || entry == 0)
1002 {
1003 list->start_of_next_abbrevs = start != end ? start : NULL;
1004 return list;
1005 }
1006
1007 READ_ULEB (tag, start, end);
1008 if (start == end)
1009 return free_abbrev_list (list);
1010
1011 children = *start++;
1012
1013 add_abbrev (entry, tag, children, list);
1014
1015 do
1016 {
1017 unsigned long form;
1018 /* Initialize it due to a false compiler warning. */
1019 int64_t implicit_const = -1;
1020
1021 READ_ULEB (attribute, start, end);
1022 if (start == end)
1023 break;
1024
1025 READ_ULEB (form, start, end);
1026 if (start == end)
1027 break;
1028
1029 if (form == DW_FORM_implicit_const)
1030 {
1031 READ_SLEB (implicit_const, start, end);
1032 if (start == end)
1033 break;
1034 }
1035
1036 add_abbrev_attr (attribute, form, implicit_const, list);
1037 }
1038 while (attribute != 0);
1039 }
1040
1041 /* Report the missing single zero which ends the section. */
1042 error (_("%s section not zero terminated\n"), section->name);
1043
1044 return free_abbrev_list (list);
1045 }
1046
1047 /* Return a sequence of abbrevs in SECTION starting at ABBREV_BASE
1048 plus ABBREV_OFFSET and finishing at ABBREV_BASE + ABBREV_SIZE.
1049 If FREE_LIST is non-NULL search the already decoded abbrevs on
1050 abbrev_lists first and if found set *FREE_LIST to NULL. If
1051 searching doesn't find a matching abbrev, set *FREE_LIST to the
1052 newly allocated list. If FREE_LIST is NULL, no search is done and
1053 the returned abbrev_list is always newly allocated. */
1054
1055 static abbrev_list *
1056 find_and_process_abbrev_set (struct dwarf_section *section,
1057 uint64_t abbrev_base,
1058 uint64_t abbrev_size,
1059 uint64_t abbrev_offset,
1060 abbrev_list **free_list)
1061 {
1062 if (free_list)
1063 *free_list = NULL;
1064
1065 if (abbrev_base >= section->size
1066 || abbrev_size > section->size - abbrev_base)
1067 {
1068 /* PR 17531: file:4bcd9ce9. */
1069 warn (_("Debug info is corrupted, abbrev size (%#" PRIx64 ")"
1070 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1071 abbrev_base + abbrev_size, section->size);
1072 return NULL;
1073 }
1074 if (abbrev_offset >= abbrev_size)
1075 {
1076 warn (_("Debug info is corrupted, abbrev offset (%#" PRIx64 ")"
1077 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1078 abbrev_offset, abbrev_size);
1079 return NULL;
1080 }
1081
1082 unsigned char *start = section->start + abbrev_base + abbrev_offset;
1083 unsigned char *end = section->start + abbrev_base + abbrev_size;
1084 abbrev_list *list = NULL;
1085 if (free_list)
1086 list = find_abbrev_list_by_raw_abbrev (start);
1087 if (list == NULL)
1088 {
1089 list = process_abbrev_set (section, start, end);
1090 if (free_list)
1091 *free_list = list;
1092 }
1093 return list;
1094 }
1095
1096 static const char *
1097 get_TAG_name (uint64_t tag)
1098 {
1099 const char *name = NULL;
1100
1101 if ((unsigned int) tag == tag)
1102 name = get_DW_TAG_name ((unsigned int) tag);
1103 if (name == NULL)
1104 {
1105 static char buffer[100];
1106
1107 if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1108 snprintf (buffer, sizeof (buffer),
1109 _("User TAG value: %#" PRIx64), tag);
1110 else
1111 snprintf (buffer, sizeof (buffer),
1112 _("Unknown TAG value: %#" PRIx64), tag);
1113 return buffer;
1114 }
1115
1116 return name;
1117 }
1118
1119 static const char *
1120 get_FORM_name (unsigned long form)
1121 {
1122 const char *name = NULL;
1123
1124 if (form == 0)
1125 return "DW_FORM value: 0";
1126
1127 if ((unsigned int) form == form)
1128 name = get_DW_FORM_name ((unsigned int) form);
1129 if (name == NULL)
1130 {
1131 static char buffer[100];
1132
1133 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1134 return buffer;
1135 }
1136
1137 return name;
1138 }
1139
1140 static const char *
1141 get_IDX_name (unsigned long idx)
1142 {
1143 const char *name = NULL;
1144
1145 if ((unsigned int) idx == idx)
1146 name = get_DW_IDX_name ((unsigned int) idx);
1147 if (name == NULL)
1148 {
1149 static char buffer[100];
1150
1151 snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1152 return buffer;
1153 }
1154
1155 return name;
1156 }
1157
1158 static unsigned char *
1159 display_block (unsigned char *data,
1160 uint64_t length,
1161 const unsigned char * const end, char delimiter)
1162 {
1163 size_t maxlen;
1164
1165 printf (_("%c%" PRIu64 " byte block: "), delimiter, length);
1166 if (data > end)
1167 return (unsigned char *) end;
1168
1169 maxlen = end - data;
1170 length = length > maxlen ? maxlen : length;
1171
1172 while (length --)
1173 printf ("%" PRIx64 " ", byte_get (data++, 1));
1174
1175 return data;
1176 }
1177
1178 static int
1179 decode_location_expression (unsigned char * data,
1180 unsigned int pointer_size,
1181 unsigned int offset_size,
1182 int dwarf_version,
1183 uint64_t length,
1184 uint64_t cu_offset,
1185 struct dwarf_section * section)
1186 {
1187 unsigned op;
1188 uint64_t uvalue;
1189 int64_t svalue;
1190 unsigned char *end = data + length;
1191 int need_frame_base = 0;
1192
1193 while (data < end)
1194 {
1195 op = *data++;
1196
1197 switch (op)
1198 {
1199 case DW_OP_addr:
1200 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1201 printf ("DW_OP_addr: %" PRIx64, uvalue);
1202 break;
1203 case DW_OP_deref:
1204 printf ("DW_OP_deref");
1205 break;
1206 case DW_OP_const1u:
1207 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1208 printf ("DW_OP_const1u: %" PRIu64, uvalue);
1209 break;
1210 case DW_OP_const1s:
1211 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1212 printf ("DW_OP_const1s: %" PRId64, svalue);
1213 break;
1214 case DW_OP_const2u:
1215 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1216 printf ("DW_OP_const2u: %" PRIu64, uvalue);
1217 break;
1218 case DW_OP_const2s:
1219 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1220 printf ("DW_OP_const2s: %" PRId64, svalue);
1221 break;
1222 case DW_OP_const4u:
1223 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1224 printf ("DW_OP_const4u: %" PRIu64, uvalue);
1225 break;
1226 case DW_OP_const4s:
1227 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1228 printf ("DW_OP_const4s: %" PRId64, svalue);
1229 break;
1230 case DW_OP_const8u:
1231 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1232 printf ("DW_OP_const8u: %" PRIu64, uvalue);
1233 break;
1234 case DW_OP_const8s:
1235 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 8, end);
1236 printf ("DW_OP_const8s: %" PRId64, svalue);
1237 break;
1238 case DW_OP_constu:
1239 READ_ULEB (uvalue, data, end);
1240 printf ("DW_OP_constu: %" PRIu64, uvalue);
1241 break;
1242 case DW_OP_consts:
1243 READ_SLEB (svalue, data, end);
1244 printf ("DW_OP_consts: %" PRId64, svalue);
1245 break;
1246 case DW_OP_dup:
1247 printf ("DW_OP_dup");
1248 break;
1249 case DW_OP_drop:
1250 printf ("DW_OP_drop");
1251 break;
1252 case DW_OP_over:
1253 printf ("DW_OP_over");
1254 break;
1255 case DW_OP_pick:
1256 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1257 printf ("DW_OP_pick: %" PRIu64, uvalue);
1258 break;
1259 case DW_OP_swap:
1260 printf ("DW_OP_swap");
1261 break;
1262 case DW_OP_rot:
1263 printf ("DW_OP_rot");
1264 break;
1265 case DW_OP_xderef:
1266 printf ("DW_OP_xderef");
1267 break;
1268 case DW_OP_abs:
1269 printf ("DW_OP_abs");
1270 break;
1271 case DW_OP_and:
1272 printf ("DW_OP_and");
1273 break;
1274 case DW_OP_div:
1275 printf ("DW_OP_div");
1276 break;
1277 case DW_OP_minus:
1278 printf ("DW_OP_minus");
1279 break;
1280 case DW_OP_mod:
1281 printf ("DW_OP_mod");
1282 break;
1283 case DW_OP_mul:
1284 printf ("DW_OP_mul");
1285 break;
1286 case DW_OP_neg:
1287 printf ("DW_OP_neg");
1288 break;
1289 case DW_OP_not:
1290 printf ("DW_OP_not");
1291 break;
1292 case DW_OP_or:
1293 printf ("DW_OP_or");
1294 break;
1295 case DW_OP_plus:
1296 printf ("DW_OP_plus");
1297 break;
1298 case DW_OP_plus_uconst:
1299 READ_ULEB (uvalue, data, end);
1300 printf ("DW_OP_plus_uconst: %" PRIu64, uvalue);
1301 break;
1302 case DW_OP_shl:
1303 printf ("DW_OP_shl");
1304 break;
1305 case DW_OP_shr:
1306 printf ("DW_OP_shr");
1307 break;
1308 case DW_OP_shra:
1309 printf ("DW_OP_shra");
1310 break;
1311 case DW_OP_xor:
1312 printf ("DW_OP_xor");
1313 break;
1314 case DW_OP_bra:
1315 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1316 printf ("DW_OP_bra: %" PRId64, svalue);
1317 break;
1318 case DW_OP_eq:
1319 printf ("DW_OP_eq");
1320 break;
1321 case DW_OP_ge:
1322 printf ("DW_OP_ge");
1323 break;
1324 case DW_OP_gt:
1325 printf ("DW_OP_gt");
1326 break;
1327 case DW_OP_le:
1328 printf ("DW_OP_le");
1329 break;
1330 case DW_OP_lt:
1331 printf ("DW_OP_lt");
1332 break;
1333 case DW_OP_ne:
1334 printf ("DW_OP_ne");
1335 break;
1336 case DW_OP_skip:
1337 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1338 printf ("DW_OP_skip: %" PRId64, svalue);
1339 break;
1340
1341 case DW_OP_lit0:
1342 case DW_OP_lit1:
1343 case DW_OP_lit2:
1344 case DW_OP_lit3:
1345 case DW_OP_lit4:
1346 case DW_OP_lit5:
1347 case DW_OP_lit6:
1348 case DW_OP_lit7:
1349 case DW_OP_lit8:
1350 case DW_OP_lit9:
1351 case DW_OP_lit10:
1352 case DW_OP_lit11:
1353 case DW_OP_lit12:
1354 case DW_OP_lit13:
1355 case DW_OP_lit14:
1356 case DW_OP_lit15:
1357 case DW_OP_lit16:
1358 case DW_OP_lit17:
1359 case DW_OP_lit18:
1360 case DW_OP_lit19:
1361 case DW_OP_lit20:
1362 case DW_OP_lit21:
1363 case DW_OP_lit22:
1364 case DW_OP_lit23:
1365 case DW_OP_lit24:
1366 case DW_OP_lit25:
1367 case DW_OP_lit26:
1368 case DW_OP_lit27:
1369 case DW_OP_lit28:
1370 case DW_OP_lit29:
1371 case DW_OP_lit30:
1372 case DW_OP_lit31:
1373 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1374 break;
1375
1376 case DW_OP_reg0:
1377 case DW_OP_reg1:
1378 case DW_OP_reg2:
1379 case DW_OP_reg3:
1380 case DW_OP_reg4:
1381 case DW_OP_reg5:
1382 case DW_OP_reg6:
1383 case DW_OP_reg7:
1384 case DW_OP_reg8:
1385 case DW_OP_reg9:
1386 case DW_OP_reg10:
1387 case DW_OP_reg11:
1388 case DW_OP_reg12:
1389 case DW_OP_reg13:
1390 case DW_OP_reg14:
1391 case DW_OP_reg15:
1392 case DW_OP_reg16:
1393 case DW_OP_reg17:
1394 case DW_OP_reg18:
1395 case DW_OP_reg19:
1396 case DW_OP_reg20:
1397 case DW_OP_reg21:
1398 case DW_OP_reg22:
1399 case DW_OP_reg23:
1400 case DW_OP_reg24:
1401 case DW_OP_reg25:
1402 case DW_OP_reg26:
1403 case DW_OP_reg27:
1404 case DW_OP_reg28:
1405 case DW_OP_reg29:
1406 case DW_OP_reg30:
1407 case DW_OP_reg31:
1408 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1409 regname (op - DW_OP_reg0, 1));
1410 break;
1411
1412 case DW_OP_breg0:
1413 case DW_OP_breg1:
1414 case DW_OP_breg2:
1415 case DW_OP_breg3:
1416 case DW_OP_breg4:
1417 case DW_OP_breg5:
1418 case DW_OP_breg6:
1419 case DW_OP_breg7:
1420 case DW_OP_breg8:
1421 case DW_OP_breg9:
1422 case DW_OP_breg10:
1423 case DW_OP_breg11:
1424 case DW_OP_breg12:
1425 case DW_OP_breg13:
1426 case DW_OP_breg14:
1427 case DW_OP_breg15:
1428 case DW_OP_breg16:
1429 case DW_OP_breg17:
1430 case DW_OP_breg18:
1431 case DW_OP_breg19:
1432 case DW_OP_breg20:
1433 case DW_OP_breg21:
1434 case DW_OP_breg22:
1435 case DW_OP_breg23:
1436 case DW_OP_breg24:
1437 case DW_OP_breg25:
1438 case DW_OP_breg26:
1439 case DW_OP_breg27:
1440 case DW_OP_breg28:
1441 case DW_OP_breg29:
1442 case DW_OP_breg30:
1443 case DW_OP_breg31:
1444 READ_SLEB (svalue, data, end);
1445 printf ("DW_OP_breg%d (%s): %" PRId64,
1446 op - DW_OP_breg0, regname (op - DW_OP_breg0, 1), svalue);
1447 break;
1448
1449 case DW_OP_regx:
1450 READ_ULEB (uvalue, data, end);
1451 printf ("DW_OP_regx: %" PRIu64 " (%s)",
1452 uvalue, regname (uvalue, 1));
1453 break;
1454 case DW_OP_fbreg:
1455 need_frame_base = 1;
1456 READ_SLEB (svalue, data, end);
1457 printf ("DW_OP_fbreg: %" PRId64, svalue);
1458 break;
1459 case DW_OP_bregx:
1460 READ_ULEB (uvalue, data, end);
1461 READ_SLEB (svalue, data, end);
1462 printf ("DW_OP_bregx: %" PRIu64 " (%s) %" PRId64,
1463 uvalue, regname (uvalue, 1), svalue);
1464 break;
1465 case DW_OP_piece:
1466 READ_ULEB (uvalue, data, end);
1467 printf ("DW_OP_piece: %" PRIu64, uvalue);
1468 break;
1469 case DW_OP_deref_size:
1470 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1471 printf ("DW_OP_deref_size: %" PRIu64, uvalue);
1472 break;
1473 case DW_OP_xderef_size:
1474 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1475 printf ("DW_OP_xderef_size: %" PRIu64, uvalue);
1476 break;
1477 case DW_OP_nop:
1478 printf ("DW_OP_nop");
1479 break;
1480
1481 /* DWARF 3 extensions. */
1482 case DW_OP_push_object_address:
1483 printf ("DW_OP_push_object_address");
1484 break;
1485 case DW_OP_call2:
1486 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1487 this ought to be an 8-byte wide computation. */
1488 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1489 printf ("DW_OP_call2: <%#" PRIx64 ">", svalue + cu_offset);
1490 break;
1491 case DW_OP_call4:
1492 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1493 this ought to be an 8-byte wide computation. */
1494 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1495 printf ("DW_OP_call4: <%#" PRIx64 ">", svalue + cu_offset);
1496 break;
1497 case DW_OP_call_ref:
1498 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1499 this ought to be an 8-byte wide computation. */
1500 if (dwarf_version == -1)
1501 {
1502 printf (_("(DW_OP_call_ref in frame info)"));
1503 /* No way to tell where the next op is, so just bail. */
1504 return need_frame_base;
1505 }
1506 if (dwarf_version == 2)
1507 {
1508 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1509 }
1510 else
1511 {
1512 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1513 }
1514 printf ("DW_OP_call_ref: <%#" PRIx64 ">", uvalue);
1515 break;
1516 case DW_OP_form_tls_address:
1517 printf ("DW_OP_form_tls_address");
1518 break;
1519 case DW_OP_call_frame_cfa:
1520 printf ("DW_OP_call_frame_cfa");
1521 break;
1522 case DW_OP_bit_piece:
1523 printf ("DW_OP_bit_piece: ");
1524 READ_ULEB (uvalue, data, end);
1525 printf (_("size: %" PRIu64 " "), uvalue);
1526 READ_ULEB (uvalue, data, end);
1527 printf (_("offset: %" PRIu64 " "), uvalue);
1528 break;
1529
1530 /* DWARF 4 extensions. */
1531 case DW_OP_stack_value:
1532 printf ("DW_OP_stack_value");
1533 break;
1534
1535 case DW_OP_implicit_value:
1536 printf ("DW_OP_implicit_value");
1537 READ_ULEB (uvalue, data, end);
1538 data = display_block (data, uvalue, end, ' ');
1539 break;
1540
1541 /* GNU extensions. */
1542 case DW_OP_GNU_push_tls_address:
1543 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1544 break;
1545 case DW_OP_GNU_uninit:
1546 printf ("DW_OP_GNU_uninit");
1547 /* FIXME: Is there data associated with this OP ? */
1548 break;
1549 case DW_OP_GNU_encoded_addr:
1550 {
1551 int encoding = 0;
1552 uint64_t addr;
1553
1554 if (data < end)
1555 encoding = *data++;
1556 addr = get_encoded_value (&data, encoding, section, end);
1557
1558 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1559 print_hex_ns (addr, pointer_size);
1560 }
1561 break;
1562 case DW_OP_implicit_pointer:
1563 case DW_OP_GNU_implicit_pointer:
1564 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1565 this ought to be an 8-byte wide computation. */
1566 if (dwarf_version == -1)
1567 {
1568 printf (_("(%s in frame info)"),
1569 (op == DW_OP_implicit_pointer
1570 ? "DW_OP_implicit_pointer"
1571 : "DW_OP_GNU_implicit_pointer"));
1572 /* No way to tell where the next op is, so just bail. */
1573 return need_frame_base;
1574 }
1575 if (dwarf_version == 2)
1576 {
1577 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1578 }
1579 else
1580 {
1581 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1582 }
1583 READ_SLEB (svalue, data, end);
1584 printf ("%s: <%#" PRIx64 "> %" PRId64,
1585 (op == DW_OP_implicit_pointer
1586 ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1587 uvalue, svalue);
1588 break;
1589 case DW_OP_entry_value:
1590 case DW_OP_GNU_entry_value:
1591 READ_ULEB (uvalue, data, end);
1592 /* PR 17531: file: 0cc9cd00. */
1593 if (uvalue > (size_t) (end - data))
1594 uvalue = end - data;
1595 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1596 : "DW_OP_GNU_entry_value"));
1597 if (decode_location_expression (data, pointer_size, offset_size,
1598 dwarf_version, uvalue,
1599 cu_offset, section))
1600 need_frame_base = 1;
1601 putchar (')');
1602 data += uvalue;
1603 break;
1604 case DW_OP_const_type:
1605 case DW_OP_GNU_const_type:
1606 READ_ULEB (uvalue, data, end);
1607 printf ("%s: <%#" PRIx64 "> ",
1608 (op == DW_OP_const_type ? "DW_OP_const_type"
1609 : "DW_OP_GNU_const_type"),
1610 cu_offset + uvalue);
1611 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1612 data = display_block (data, uvalue, end, ' ');
1613 break;
1614 case DW_OP_regval_type:
1615 case DW_OP_GNU_regval_type:
1616 READ_ULEB (uvalue, data, end);
1617 printf ("%s: %" PRIu64 " (%s)",
1618 (op == DW_OP_regval_type ? "DW_OP_regval_type"
1619 : "DW_OP_GNU_regval_type"),
1620 uvalue, regname (uvalue, 1));
1621 READ_ULEB (uvalue, data, end);
1622 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1623 break;
1624 case DW_OP_deref_type:
1625 case DW_OP_GNU_deref_type:
1626 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1627 printf ("%s: %" PRId64,
1628 (op == DW_OP_deref_type ? "DW_OP_deref_type"
1629 : "DW_OP_GNU_deref_type"),
1630 uvalue);
1631 READ_ULEB (uvalue, data, end);
1632 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1633 break;
1634 case DW_OP_convert:
1635 case DW_OP_GNU_convert:
1636 READ_ULEB (uvalue, data, end);
1637 printf ("%s <%#" PRIx64 ">",
1638 (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1639 uvalue ? cu_offset + uvalue : uvalue);
1640 break;
1641 case DW_OP_reinterpret:
1642 case DW_OP_GNU_reinterpret:
1643 READ_ULEB (uvalue, data, end);
1644 printf ("%s <%#" PRIx64 ">",
1645 (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1646 : "DW_OP_GNU_reinterpret"),
1647 uvalue ? cu_offset + uvalue : uvalue);
1648 break;
1649 case DW_OP_GNU_parameter_ref:
1650 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1651 printf ("DW_OP_GNU_parameter_ref: <%#" PRIx64 ">",
1652 cu_offset + uvalue);
1653 break;
1654 case DW_OP_addrx:
1655 READ_ULEB (uvalue, data, end);
1656 printf ("DW_OP_addrx <%#" PRIx64 ">", uvalue);
1657 break;
1658 case DW_OP_GNU_addr_index:
1659 READ_ULEB (uvalue, data, end);
1660 printf ("DW_OP_GNU_addr_index <%#" PRIx64 ">", uvalue);
1661 break;
1662 case DW_OP_GNU_const_index:
1663 READ_ULEB (uvalue, data, end);
1664 printf ("DW_OP_GNU_const_index <%#" PRIx64 ">", uvalue);
1665 break;
1666 case DW_OP_GNU_variable_value:
1667 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1668 this ought to be an 8-byte wide computation. */
1669 if (dwarf_version == -1)
1670 {
1671 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1672 /* No way to tell where the next op is, so just bail. */
1673 return need_frame_base;
1674 }
1675 if (dwarf_version == 2)
1676 {
1677 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1678 }
1679 else
1680 {
1681 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1682 }
1683 printf ("DW_OP_GNU_variable_value: <%#" PRIx64 ">", uvalue);
1684 break;
1685
1686 /* HP extensions. */
1687 case DW_OP_HP_is_value:
1688 printf ("DW_OP_HP_is_value");
1689 /* FIXME: Is there data associated with this OP ? */
1690 break;
1691 case DW_OP_HP_fltconst4:
1692 printf ("DW_OP_HP_fltconst4");
1693 /* FIXME: Is there data associated with this OP ? */
1694 break;
1695 case DW_OP_HP_fltconst8:
1696 printf ("DW_OP_HP_fltconst8");
1697 /* FIXME: Is there data associated with this OP ? */
1698 break;
1699 case DW_OP_HP_mod_range:
1700 printf ("DW_OP_HP_mod_range");
1701 /* FIXME: Is there data associated with this OP ? */
1702 break;
1703 case DW_OP_HP_unmod_range:
1704 printf ("DW_OP_HP_unmod_range");
1705 /* FIXME: Is there data associated with this OP ? */
1706 break;
1707 case DW_OP_HP_tls:
1708 printf ("DW_OP_HP_tls");
1709 /* FIXME: Is there data associated with this OP ? */
1710 break;
1711
1712 /* PGI (STMicroelectronics) extensions. */
1713 case DW_OP_PGI_omp_thread_num:
1714 /* Pushes the thread number for the current thread as it would be
1715 returned by the standard OpenMP library function:
1716 omp_get_thread_num(). The "current thread" is the thread for
1717 which the expression is being evaluated. */
1718 printf ("DW_OP_PGI_omp_thread_num");
1719 break;
1720
1721 default:
1722 if (op >= DW_OP_lo_user
1723 && op <= DW_OP_hi_user)
1724 printf (_("(User defined location op %#x)"), op);
1725 else
1726 printf (_("(Unknown location op %#x)"), op);
1727 /* No way to tell where the next op is, so just bail. */
1728 return need_frame_base;
1729 }
1730
1731 /* Separate the ops. */
1732 if (data < end)
1733 printf ("; ");
1734 }
1735
1736 return need_frame_base;
1737 }
1738
1739 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1740 This is used for DWARF package files. */
1741
1742 static struct cu_tu_set *
1743 find_cu_tu_set_v2 (uint64_t cu_offset, int do_types)
1744 {
1745 struct cu_tu_set *p;
1746 unsigned int nsets;
1747 unsigned int dw_sect;
1748
1749 if (do_types)
1750 {
1751 p = tu_sets;
1752 nsets = tu_count;
1753 dw_sect = DW_SECT_TYPES;
1754 }
1755 else
1756 {
1757 p = cu_sets;
1758 nsets = cu_count;
1759 dw_sect = DW_SECT_INFO;
1760 }
1761 while (nsets > 0)
1762 {
1763 if (p->section_offsets [dw_sect] == cu_offset)
1764 return p;
1765 p++;
1766 nsets--;
1767 }
1768 return NULL;
1769 }
1770
1771 static const char *
1772 fetch_alt_indirect_string (uint64_t offset)
1773 {
1774 separate_info * i;
1775
1776 if (! do_follow_links)
1777 return "";
1778
1779 if (first_separate_info == NULL)
1780 return _("<no links available>");
1781
1782 for (i = first_separate_info; i != NULL; i = i->next)
1783 {
1784 struct dwarf_section * section;
1785 const char * ret;
1786
1787 if (! load_debug_section (separate_debug_str, i->handle))
1788 continue;
1789
1790 section = &debug_displays [separate_debug_str].section;
1791
1792 if (section->start == NULL)
1793 continue;
1794
1795 if (offset >= section->size)
1796 continue;
1797
1798 ret = (const char *) (section->start + offset);
1799 /* Unfortunately we cannot rely upon the .debug_str section ending with a
1800 NUL byte. Since our caller is expecting to receive a well formed C
1801 string we test for the lack of a terminating byte here. */
1802 if (strnlen ((const char *) ret, section->size - offset)
1803 == section->size - offset)
1804 return _("<no NUL byte at end of alt .debug_str section>");
1805
1806 return ret;
1807 }
1808
1809 warn (_("DW_FORM_GNU_strp_alt offset (%#" PRIx64 ")"
1810 " too big or no string sections available\n"), offset);
1811 return _("<offset is too big>");
1812 }
1813
1814 static const char *
1815 get_AT_name (unsigned long attribute)
1816 {
1817 const char *name;
1818
1819 if (attribute == 0)
1820 return "DW_AT value: 0";
1821
1822 /* One value is shared by the MIPS and HP extensions: */
1823 if (attribute == DW_AT_MIPS_fde)
1824 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1825
1826 name = get_DW_AT_name (attribute);
1827
1828 if (name == NULL)
1829 {
1830 static char buffer[100];
1831
1832 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1833 attribute);
1834 return buffer;
1835 }
1836
1837 return name;
1838 }
1839
1840 static void
1841 add_dwo_info (const char * value, uint64_t cu_offset, dwo_type type)
1842 {
1843 dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1844
1845 dwinfo->type = type;
1846 dwinfo->value = value;
1847 dwinfo->cu_offset = cu_offset;
1848 dwinfo->next = first_dwo_info;
1849 first_dwo_info = dwinfo;
1850 }
1851
1852 static void
1853 add_dwo_name (const char * name, uint64_t cu_offset)
1854 {
1855 add_dwo_info (name, cu_offset, DWO_NAME);
1856 }
1857
1858 static void
1859 add_dwo_dir (const char * dir, uint64_t cu_offset)
1860 {
1861 add_dwo_info (dir, cu_offset, DWO_DIR);
1862 }
1863
1864 static void
1865 add_dwo_id (const char * id, uint64_t cu_offset)
1866 {
1867 add_dwo_info (id, cu_offset, DWO_ID);
1868 }
1869
1870 static void
1871 free_dwo_info (void)
1872 {
1873 dwo_info * dwinfo;
1874 dwo_info * next;
1875
1876 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1877 {
1878 next = dwinfo->next;
1879 free (dwinfo);
1880 }
1881 first_dwo_info = NULL;
1882 }
1883
1884 /* Ensure that START + UVALUE is less than END.
1885 Return an adjusted UVALUE if necessary to ensure this relationship. */
1886
1887 static inline uint64_t
1888 check_uvalue (const unsigned char *start,
1889 uint64_t uvalue,
1890 const unsigned char *end)
1891 {
1892 uint64_t max_uvalue = end - start;
1893
1894 /* See PR 17512: file: 008-103549-0.001:0.1.
1895 and PR 24829 for examples of where these tests are triggered. */
1896 if (uvalue > max_uvalue)
1897 {
1898 warn (_("Corrupt attribute block length: %#" PRIx64 "\n"), uvalue);
1899 uvalue = max_uvalue;
1900 }
1901
1902 return uvalue;
1903 }
1904
1905 static unsigned char *
1906 skip_attr_bytes (unsigned long form,
1907 unsigned char *data,
1908 unsigned char *end,
1909 uint64_t pointer_size,
1910 uint64_t offset_size,
1911 int dwarf_version,
1912 uint64_t *value_return)
1913 {
1914 int64_t svalue;
1915 uint64_t uvalue = 0;
1916 uint64_t inc = 0;
1917
1918 * value_return = 0;
1919
1920 switch (form)
1921 {
1922 case DW_FORM_ref_addr:
1923 if (dwarf_version == 2)
1924 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1925 else if (dwarf_version > 2)
1926 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1927 else
1928 return NULL;
1929 break;
1930
1931 case DW_FORM_addr:
1932 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1933 break;
1934
1935 case DW_FORM_strp:
1936 case DW_FORM_line_strp:
1937 case DW_FORM_sec_offset:
1938 case DW_FORM_GNU_ref_alt:
1939 case DW_FORM_GNU_strp_alt:
1940 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1941 break;
1942
1943 case DW_FORM_flag_present:
1944 uvalue = 1;
1945 break;
1946
1947 case DW_FORM_ref1:
1948 case DW_FORM_flag:
1949 case DW_FORM_data1:
1950 case DW_FORM_strx1:
1951 case DW_FORM_addrx1:
1952 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1953 break;
1954
1955 case DW_FORM_strx3:
1956 case DW_FORM_addrx3:
1957 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
1958 break;
1959
1960 case DW_FORM_ref2:
1961 case DW_FORM_data2:
1962 case DW_FORM_strx2:
1963 case DW_FORM_addrx2:
1964 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1965 break;
1966
1967 case DW_FORM_ref4:
1968 case DW_FORM_data4:
1969 case DW_FORM_strx4:
1970 case DW_FORM_addrx4:
1971 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1972 break;
1973
1974 case DW_FORM_sdata:
1975 READ_SLEB (svalue, data, end);
1976 uvalue = svalue;
1977 break;
1978
1979 case DW_FORM_ref_udata:
1980 case DW_FORM_udata:
1981 case DW_FORM_GNU_str_index:
1982 case DW_FORM_strx:
1983 case DW_FORM_GNU_addr_index:
1984 case DW_FORM_addrx:
1985 case DW_FORM_loclistx:
1986 case DW_FORM_rnglistx:
1987 READ_ULEB (uvalue, data, end);
1988 break;
1989
1990 case DW_FORM_ref8:
1991 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1992 break;
1993
1994 case DW_FORM_data8:
1995 case DW_FORM_ref_sig8:
1996 inc = 8;
1997 break;
1998
1999 case DW_FORM_data16:
2000 inc = 16;
2001 break;
2002
2003 case DW_FORM_string:
2004 inc = strnlen ((char *) data, end - data) + 1;
2005 break;
2006
2007 case DW_FORM_block:
2008 case DW_FORM_exprloc:
2009 READ_ULEB (uvalue, data, end);
2010 inc = uvalue;
2011 break;
2012
2013 case DW_FORM_block1:
2014 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2015 inc = uvalue;
2016 break;
2017
2018 case DW_FORM_block2:
2019 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2020 inc = uvalue;
2021 break;
2022
2023 case DW_FORM_block4:
2024 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2025 inc = uvalue;
2026 break;
2027
2028 case DW_FORM_indirect:
2029 READ_ULEB (form, data, end);
2030 if (form == DW_FORM_implicit_const)
2031 SKIP_ULEB (data, end);
2032 return skip_attr_bytes (form, data, end, pointer_size, offset_size,
2033 dwarf_version, value_return);
2034
2035 default:
2036 return NULL;
2037 }
2038
2039 * value_return = uvalue;
2040 if (inc <= (size_t) (end - data))
2041 data += inc;
2042 else
2043 data = end;
2044 return data;
2045 }
2046
2047 /* Given form FORM with value UVALUE, locate and return the abbreviation
2048 associated with it. */
2049
2050 static abbrev_entry *
2051 get_type_abbrev_from_form (unsigned long form,
2052 unsigned long uvalue,
2053 uint64_t cu_offset,
2054 unsigned char *cu_end,
2055 const struct dwarf_section *section,
2056 unsigned long *abbrev_num_return,
2057 unsigned char **data_return,
2058 abbrev_map **map_return)
2059 {
2060 unsigned long abbrev_number;
2061 abbrev_map * map;
2062 abbrev_entry * entry;
2063 unsigned char * data;
2064
2065 if (abbrev_num_return != NULL)
2066 * abbrev_num_return = 0;
2067 if (data_return != NULL)
2068 * data_return = NULL;
2069
2070 switch (form)
2071 {
2072 case DW_FORM_GNU_ref_alt:
2073 case DW_FORM_ref_sig8:
2074 /* FIXME: We are unable to handle this form at the moment. */
2075 return NULL;
2076
2077 case DW_FORM_ref_addr:
2078 if (uvalue >= section->size)
2079 {
2080 warn (_("Unable to resolve ref_addr form: uvalue %lx "
2081 "> section size %" PRIx64 " (%s)\n"),
2082 uvalue, section->size, section->name);
2083 return NULL;
2084 }
2085 break;
2086
2087 case DW_FORM_ref_sup4:
2088 case DW_FORM_ref_sup8:
2089 break;
2090
2091 case DW_FORM_ref1:
2092 case DW_FORM_ref2:
2093 case DW_FORM_ref4:
2094 case DW_FORM_ref8:
2095 case DW_FORM_ref_udata:
2096 if (uvalue + cu_offset < uvalue
2097 || uvalue + cu_offset > (size_t) (cu_end - section->start))
2098 {
2099 warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %" PRIx64
2100 " > CU size %tx\n"),
2101 uvalue, cu_offset, cu_end - section->start);
2102 return NULL;
2103 }
2104 uvalue += cu_offset;
2105 break;
2106
2107 /* FIXME: Are there other DW_FORMs that can be used by types ? */
2108
2109 default:
2110 warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2111 return NULL;
2112 }
2113
2114 data = (unsigned char *) section->start + uvalue;
2115 map = find_abbrev_map_by_offset (uvalue);
2116
2117 if (map == NULL)
2118 {
2119 warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2120 return NULL;
2121 }
2122 if (map->list == NULL)
2123 {
2124 warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2125 return NULL;
2126 }
2127
2128 if (map_return != NULL)
2129 {
2130 if (form == DW_FORM_ref_addr)
2131 *map_return = map;
2132 else
2133 *map_return = NULL;
2134 }
2135
2136 READ_ULEB (abbrev_number, data, section->start + section->size);
2137
2138 for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2139 if (entry->number == abbrev_number)
2140 break;
2141
2142 if (abbrev_num_return != NULL)
2143 * abbrev_num_return = abbrev_number;
2144
2145 if (data_return != NULL)
2146 * data_return = data;
2147
2148 if (entry == NULL)
2149 warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2150
2151 return entry;
2152 }
2153
2154 /* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2155 can be determined to be a signed type. The data for ENTRY can be
2156 found starting at DATA. */
2157
2158 static void
2159 get_type_signedness (abbrev_entry *entry,
2160 const struct dwarf_section *section,
2161 unsigned char *data,
2162 unsigned char *end,
2163 uint64_t cu_offset,
2164 uint64_t pointer_size,
2165 uint64_t offset_size,
2166 int dwarf_version,
2167 bool *is_signed,
2168 unsigned int nesting)
2169 {
2170 abbrev_attr * attr;
2171
2172 * is_signed = false;
2173
2174 #define MAX_NESTING 20
2175 if (nesting > MAX_NESTING)
2176 {
2177 /* FIXME: Warn - or is this expected ?
2178 NB/ We need to avoid infinite recursion. */
2179 return;
2180 }
2181
2182 for (attr = entry->first_attr;
2183 attr != NULL && attr->attribute;
2184 attr = attr->next)
2185 {
2186 unsigned char * orig_data = data;
2187 uint64_t uvalue = 0;
2188
2189 data = skip_attr_bytes (attr->form, data, end, pointer_size,
2190 offset_size, dwarf_version, & uvalue);
2191 if (data == NULL)
2192 return;
2193
2194 switch (attr->attribute)
2195 {
2196 case DW_AT_linkage_name:
2197 case DW_AT_name:
2198 if (do_wide)
2199 {
2200 if (attr->form == DW_FORM_strp)
2201 printf (", %s", fetch_indirect_string (uvalue));
2202 else if (attr->form == DW_FORM_string)
2203 printf (", %.*s", (int) (end - orig_data), orig_data);
2204 }
2205 break;
2206
2207 case DW_AT_type:
2208 /* Recurse. */
2209 {
2210 abbrev_entry *type_abbrev;
2211 unsigned char *type_data;
2212 abbrev_map *map;
2213
2214 type_abbrev = get_type_abbrev_from_form (attr->form,
2215 uvalue,
2216 cu_offset,
2217 end,
2218 section,
2219 NULL /* abbrev num return */,
2220 &type_data,
2221 &map);
2222 if (type_abbrev == NULL)
2223 break;
2224
2225 get_type_signedness (type_abbrev, section, type_data,
2226 map ? section->start + map->end : end,
2227 map ? map->start : cu_offset,
2228 pointer_size, offset_size, dwarf_version,
2229 is_signed, nesting + 1);
2230 }
2231 break;
2232
2233 case DW_AT_encoding:
2234 /* Determine signness. */
2235 switch (uvalue)
2236 {
2237 case DW_ATE_address:
2238 /* FIXME - some architectures have signed addresses. */
2239 case DW_ATE_boolean:
2240 case DW_ATE_unsigned:
2241 case DW_ATE_unsigned_char:
2242 case DW_ATE_unsigned_fixed:
2243 * is_signed = false;
2244 break;
2245
2246 default:
2247 case DW_ATE_complex_float:
2248 case DW_ATE_float:
2249 case DW_ATE_signed:
2250 case DW_ATE_signed_char:
2251 case DW_ATE_imaginary_float:
2252 case DW_ATE_decimal_float:
2253 case DW_ATE_signed_fixed:
2254 * is_signed = true;
2255 break;
2256 }
2257 break;
2258 }
2259 }
2260 }
2261
2262 static void
2263 read_and_print_leb128 (unsigned char *data,
2264 unsigned int *bytes_read,
2265 unsigned const char *end,
2266 bool is_signed)
2267 {
2268 int status;
2269 uint64_t val = read_leb128 (data, end, is_signed, bytes_read, &status);
2270 if (status != 0)
2271 report_leb_status (status);
2272 else if (is_signed)
2273 printf ("%" PRId64, val);
2274 else
2275 printf ("%" PRIu64, val);
2276 }
2277
2278 static void
2279 display_discr_list (unsigned long form,
2280 uint64_t uvalue,
2281 unsigned char *data,
2282 int level)
2283 {
2284 unsigned char *end = data;
2285
2286 if (uvalue == 0)
2287 {
2288 printf ("[default]");
2289 return;
2290 }
2291
2292 switch (form)
2293 {
2294 case DW_FORM_block:
2295 case DW_FORM_block1:
2296 case DW_FORM_block2:
2297 case DW_FORM_block4:
2298 /* Move data pointer back to the start of the byte array. */
2299 data -= uvalue;
2300 break;
2301 default:
2302 printf ("<corrupt>\n");
2303 warn (_("corrupt discr_list - not using a block form\n"));
2304 return;
2305 }
2306
2307 if (uvalue < 2)
2308 {
2309 printf ("<corrupt>\n");
2310 warn (_("corrupt discr_list - block not long enough\n"));
2311 return;
2312 }
2313
2314 bool is_signed = (level > 0 && level <= MAX_CU_NESTING
2315 ? level_type_signed [level - 1] : false);
2316
2317 printf ("(");
2318 while (data < end)
2319 {
2320 unsigned char discriminant;
2321 unsigned int bytes_read;
2322
2323 SAFE_BYTE_GET_AND_INC (discriminant, data, 1, end);
2324
2325 switch (discriminant)
2326 {
2327 case DW_DSC_label:
2328 printf ("label ");
2329 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2330 data += bytes_read;
2331 break;
2332
2333 case DW_DSC_range:
2334 printf ("range ");
2335 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2336 data += bytes_read;
2337
2338 printf ("..");
2339 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2340 data += bytes_read;
2341 break;
2342
2343 default:
2344 printf ("<corrupt>\n");
2345 warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
2346 discriminant);
2347 return;
2348 }
2349
2350 if (data < end)
2351 printf (", ");
2352 }
2353
2354 if (is_signed)
2355 printf (")(signed)");
2356 else
2357 printf (")(unsigned)");
2358 }
2359
2360 static unsigned char *
2361 read_and_display_attr_value (unsigned long attribute,
2362 unsigned long form,
2363 int64_t implicit_const,
2364 unsigned char *start,
2365 unsigned char *data,
2366 unsigned char *end,
2367 uint64_t cu_offset,
2368 uint64_t pointer_size,
2369 uint64_t offset_size,
2370 int dwarf_version,
2371 debug_info *debug_info_p,
2372 int do_loc,
2373 struct dwarf_section *section,
2374 struct cu_tu_set *this_set,
2375 char delimiter,
2376 int level)
2377 {
2378 int64_t svalue;
2379 uint64_t uvalue = 0;
2380 uint64_t uvalue_hi = 0;
2381 unsigned char *block_start = NULL;
2382 unsigned char *orig_data = data;
2383
2384 if (data > end || (data == end && form != DW_FORM_flag_present))
2385 {
2386 warn (_("Corrupt attribute\n"));
2387 return data;
2388 }
2389
2390 if (do_wide && ! do_loc)
2391 {
2392 /* PR 26847: Display the name of the form. */
2393 const char * name = get_FORM_name (form);
2394
2395 /* For convenience we skip the DW_FORM_ prefix to the name. */
2396 if (name[0] == 'D')
2397 name += 8; /* strlen ("DW_FORM_") */
2398 printf ("%c(%s)", delimiter, name);
2399 }
2400
2401 switch (form)
2402 {
2403 case DW_FORM_ref_addr:
2404 if (dwarf_version == 2)
2405 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2406 else if (dwarf_version > 2)
2407 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2408 else
2409 error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
2410 break;
2411
2412 case DW_FORM_addr:
2413 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2414 break;
2415
2416 case DW_FORM_strp_sup:
2417 case DW_FORM_strp:
2418 case DW_FORM_line_strp:
2419 case DW_FORM_sec_offset:
2420 case DW_FORM_GNU_ref_alt:
2421 case DW_FORM_GNU_strp_alt:
2422 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2423 break;
2424
2425 case DW_FORM_flag_present:
2426 uvalue = 1;
2427 break;
2428
2429 case DW_FORM_ref1:
2430 case DW_FORM_flag:
2431 case DW_FORM_data1:
2432 case DW_FORM_strx1:
2433 case DW_FORM_addrx1:
2434 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2435 break;
2436
2437 case DW_FORM_ref2:
2438 case DW_FORM_data2:
2439 case DW_FORM_strx2:
2440 case DW_FORM_addrx2:
2441 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2442 break;
2443
2444 case DW_FORM_strx3:
2445 case DW_FORM_addrx3:
2446 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2447 break;
2448
2449 case DW_FORM_ref_sup4:
2450 case DW_FORM_ref4:
2451 case DW_FORM_data4:
2452 case DW_FORM_strx4:
2453 case DW_FORM_addrx4:
2454 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2455 break;
2456
2457 case DW_FORM_ref_sup8:
2458 case DW_FORM_ref8:
2459 case DW_FORM_data8:
2460 case DW_FORM_ref_sig8:
2461 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2462 break;
2463
2464 case DW_FORM_data16:
2465 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2466 SAFE_BYTE_GET_AND_INC (uvalue_hi, data, 8, end);
2467 if (byte_get != byte_get_little_endian)
2468 {
2469 uint64_t utmp = uvalue;
2470 uvalue = uvalue_hi;
2471 uvalue_hi = utmp;
2472 }
2473 break;
2474
2475 case DW_FORM_sdata:
2476 READ_SLEB (svalue, data, end);
2477 uvalue = svalue;
2478 break;
2479
2480 case DW_FORM_GNU_str_index:
2481 case DW_FORM_strx:
2482 case DW_FORM_ref_udata:
2483 case DW_FORM_udata:
2484 case DW_FORM_GNU_addr_index:
2485 case DW_FORM_addrx:
2486 case DW_FORM_loclistx:
2487 case DW_FORM_rnglistx:
2488 READ_ULEB (uvalue, data, end);
2489 break;
2490
2491 case DW_FORM_indirect:
2492 READ_ULEB (form, data, end);
2493 if (!do_loc)
2494 printf ("%c%s", delimiter, get_FORM_name (form));
2495 if (form == DW_FORM_implicit_const)
2496 READ_SLEB (implicit_const, data, end);
2497 return read_and_display_attr_value (attribute, form, implicit_const,
2498 start, data, end,
2499 cu_offset, pointer_size,
2500 offset_size, dwarf_version,
2501 debug_info_p, do_loc,
2502 section, this_set, delimiter, level);
2503
2504 case DW_FORM_implicit_const:
2505 uvalue = implicit_const;
2506 break;
2507
2508 default:
2509 break;
2510 }
2511
2512 switch (form)
2513 {
2514 case DW_FORM_ref_addr:
2515 if (!do_loc)
2516 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2517 break;
2518
2519 case DW_FORM_GNU_ref_alt:
2520 if (!do_loc)
2521 {
2522 if (do_wide)
2523 /* We have already printed the form name. */
2524 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2525 else
2526 printf ("%c<alt %#" PRIx64 ">", delimiter, uvalue);
2527 }
2528 /* FIXME: Follow the reference... */
2529 break;
2530
2531 case DW_FORM_ref1:
2532 case DW_FORM_ref2:
2533 case DW_FORM_ref4:
2534 case DW_FORM_ref_sup4:
2535 case DW_FORM_ref_udata:
2536 if (!do_loc)
2537 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2538 break;
2539
2540 case DW_FORM_data4:
2541 case DW_FORM_addr:
2542 case DW_FORM_sec_offset:
2543 if (!do_loc)
2544 printf ("%c%#" PRIx64, delimiter, uvalue);
2545 break;
2546
2547 case DW_FORM_flag_present:
2548 case DW_FORM_flag:
2549 case DW_FORM_data1:
2550 case DW_FORM_data2:
2551 case DW_FORM_sdata:
2552 if (!do_loc)
2553 printf ("%c%" PRId64, delimiter, uvalue);
2554 break;
2555
2556 case DW_FORM_udata:
2557 if (!do_loc)
2558 printf ("%c%" PRIu64, delimiter, uvalue);
2559 break;
2560
2561 case DW_FORM_implicit_const:
2562 if (!do_loc)
2563 printf ("%c%" PRId64, delimiter, implicit_const);
2564 break;
2565
2566 case DW_FORM_ref_sup8:
2567 case DW_FORM_ref8:
2568 case DW_FORM_data8:
2569 if (!do_loc)
2570 {
2571 uint64_t utmp = uvalue;
2572 if (form == DW_FORM_ref8)
2573 utmp += cu_offset;
2574 printf ("%c%#" PRIx64, delimiter, utmp);
2575 }
2576 break;
2577
2578 case DW_FORM_data16:
2579 if (!do_loc)
2580 {
2581 if (uvalue_hi == 0)
2582 printf (" %#" PRIx64, uvalue);
2583 else
2584 printf (" %#" PRIx64 "%016" PRIx64, uvalue_hi, uvalue);
2585 }
2586 break;
2587
2588 case DW_FORM_string:
2589 if (!do_loc)
2590 printf ("%c%.*s", delimiter, (int) (end - data), data);
2591 data += strnlen ((char *) data, end - data);
2592 if (data < end)
2593 data++;
2594 break;
2595
2596 case DW_FORM_block:
2597 case DW_FORM_exprloc:
2598 READ_ULEB (uvalue, data, end);
2599 do_block:
2600 block_start = data;
2601 if (block_start >= end)
2602 {
2603 warn (_("Block ends prematurely\n"));
2604 uvalue = 0;
2605 block_start = end;
2606 }
2607
2608 uvalue = check_uvalue (block_start, uvalue, end);
2609
2610 data = block_start + uvalue;
2611 if (!do_loc)
2612 {
2613 unsigned char op;
2614
2615 SAFE_BYTE_GET (op, block_start, sizeof (op), end);
2616 if (op != DW_OP_addrx)
2617 data = display_block (block_start, uvalue, end, delimiter);
2618 }
2619 break;
2620
2621 case DW_FORM_block1:
2622 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2623 goto do_block;
2624
2625 case DW_FORM_block2:
2626 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2627 goto do_block;
2628
2629 case DW_FORM_block4:
2630 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2631 goto do_block;
2632
2633 case DW_FORM_strp:
2634 if (!do_loc)
2635 {
2636 if (do_wide)
2637 /* We have already displayed the form name. */
2638 printf (_("%c(offset: %#" PRIx64 "): %s"),
2639 delimiter, uvalue, fetch_indirect_string (uvalue));
2640 else
2641 printf (_("%c(indirect string, offset: %#" PRIx64 "): %s"),
2642 delimiter, uvalue, fetch_indirect_string (uvalue));
2643 }
2644 break;
2645
2646 case DW_FORM_line_strp:
2647 if (!do_loc)
2648 {
2649 if (do_wide)
2650 /* We have already displayed the form name. */
2651 printf (_("%c(offset: %#" PRIx64 "): %s"),
2652 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2653 else
2654 printf (_("%c(indirect line string, offset: %#" PRIx64 "): %s"),
2655 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2656 }
2657 break;
2658
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)
2666 {
2667 const char *suffix = section ? strrchr (section->name, '.') : NULL;
2668 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2669 const char *strng;
2670
2671 strng = fetch_indexed_string (uvalue, this_set, offset_size, dwo,
2672 debug_info_p ? debug_info_p->str_offsets_base : 0);
2673 if (do_wide)
2674 /* We have already displayed the form name. */
2675 printf (_("%c(offset: %#" PRIx64 "): %s"),
2676 delimiter, uvalue, strng);
2677 else
2678 printf (_("%c(indexed string: %#" PRIx64 "): %s"),
2679 delimiter, uvalue, strng);
2680 }
2681 break;
2682
2683 case DW_FORM_GNU_strp_alt:
2684 if (!do_loc)
2685 {
2686 if (do_wide)
2687 /* We have already displayed the form name. */
2688 printf (_("%c(offset: %#" PRIx64 ") %s"),
2689 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2690 else
2691 printf (_("%c(alt indirect string, offset: %#" PRIx64 ") %s"),
2692 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2693 }
2694 break;
2695
2696 case DW_FORM_indirect:
2697 /* Handled above. */
2698 break;
2699
2700 case DW_FORM_ref_sig8:
2701 if (!do_loc)
2702 printf ("%c%s: %#" PRIx64, delimiter, do_wide ? "" : "signature",
2703 uvalue);
2704 break;
2705
2706 case DW_FORM_GNU_addr_index:
2707 case DW_FORM_addrx:
2708 case DW_FORM_addrx1:
2709 case DW_FORM_addrx2:
2710 case DW_FORM_addrx3:
2711 case DW_FORM_addrx4:
2712 case DW_FORM_loclistx:
2713 case DW_FORM_rnglistx:
2714 if (!do_loc)
2715 {
2716 uint64_t base, idx;
2717 const char *suffix = strrchr (section->name, '.');
2718 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2719
2720 if (form == DW_FORM_loclistx)
2721 {
2722 if (dwo)
2723 {
2724 idx = fetch_indexed_value (uvalue, loclists_dwo, 0);
2725 if (idx != (uint64_t) -1)
2726 idx += (offset_size == 8) ? 20 : 12;
2727 }
2728 else if (debug_info_p == NULL || dwarf_version > 4)
2729 {
2730 idx = fetch_indexed_value (uvalue, loclists, 0);
2731 }
2732 else
2733 {
2734 /* We want to compute:
2735 idx = fetch_indexed_value (uvalue, loclists, debug_info_p->loclists_base);
2736 idx += debug_info_p->loclists_base;
2737 Fortunately we already have that sum cached in the
2738 loc_offsets array. */
2739 if (uvalue < debug_info_p->num_loc_offsets)
2740 idx = debug_info_p->loc_offsets [uvalue];
2741 else
2742 {
2743 warn (_("loc_offset %" PRIu64 " too big\n"), uvalue);
2744 idx = -1;
2745 }
2746 }
2747 }
2748 else if (form == DW_FORM_rnglistx)
2749 {
2750 if (dwo)
2751 {
2752 idx = fetch_indexed_value (uvalue, rnglists_dwo, 0);
2753 if (idx != (uint64_t) -1)
2754 idx += (offset_size == 8) ? 20 : 12;
2755 }
2756 else
2757 {
2758 if (debug_info_p == NULL)
2759 base = 0;
2760 else
2761 base = debug_info_p->rnglists_base;
2762 /* We do not have a cached value this time, so we perform the
2763 computation manually. */
2764 idx = fetch_indexed_value (uvalue, rnglists, base);
2765 if (idx != (uint64_t) -1)
2766 idx += base;
2767 }
2768 }
2769 else
2770 {
2771 if (debug_info_p == NULL)
2772 base = 0;
2773 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2774 base = 0;
2775 else
2776 base = debug_info_p->addr_base;
2777
2778 base += uvalue * pointer_size;
2779 idx = fetch_indexed_addr (base, pointer_size);
2780 }
2781
2782 /* We have already displayed the form name. */
2783 if (idx != (uint64_t) -1)
2784 printf (_("%c(index: %#" PRIx64 "): %#" PRIx64),
2785 delimiter, uvalue, idx);
2786 }
2787 break;
2788
2789 case DW_FORM_strp_sup:
2790 if (!do_loc)
2791 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2792 break;
2793
2794 default:
2795 warn (_("Unrecognized form: %#lx"), form);
2796 /* What to do? Consume a byte maybe? */
2797 ++data;
2798 break;
2799 }
2800
2801 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
2802 && num_debug_info_entries == 0
2803 && debug_info_p != NULL)
2804 {
2805 switch (attribute)
2806 {
2807 case DW_AT_loclists_base:
2808 if (debug_info_p->loclists_base)
2809 warn (_("CU @ %#" PRIx64 " has multiple loclists_base values "
2810 "(%#" PRIx64 " and %#" PRIx64 ")"),
2811 debug_info_p->cu_offset,
2812 debug_info_p->loclists_base, uvalue);
2813 svalue = uvalue;
2814 if (svalue < 0)
2815 {
2816 warn (_("CU @ %#" PRIx64 " has has a negative loclists_base "
2817 "value of %#" PRIx64 " - treating as zero"),
2818 debug_info_p->cu_offset, svalue);
2819 uvalue = 0;
2820 }
2821 debug_info_p->loclists_base = uvalue;
2822 break;
2823
2824 case DW_AT_rnglists_base:
2825 if (debug_info_p->rnglists_base)
2826 warn (_("CU @ %#" PRIx64 " has multiple rnglists_base values "
2827 "(%#" PRIx64 " and %#" PRIx64 ")"),
2828 debug_info_p->cu_offset,
2829 debug_info_p->rnglists_base, uvalue);
2830 svalue = uvalue;
2831 if (svalue < 0)
2832 {
2833 warn (_("CU @ %#" PRIx64 " has has a negative rnglists_base "
2834 "value of %#" PRIx64 " - treating as zero"),
2835 debug_info_p->cu_offset, svalue);
2836 uvalue = 0;
2837 }
2838 debug_info_p->rnglists_base = uvalue;
2839 break;
2840
2841 case DW_AT_str_offsets_base:
2842 if (debug_info_p->str_offsets_base)
2843 warn (_("CU @ %#" PRIx64 " has multiple str_offsets_base values "
2844 "%#" PRIx64 " and %#" PRIx64 ")"),
2845 debug_info_p->cu_offset,
2846 debug_info_p->str_offsets_base, uvalue);
2847 svalue = uvalue;
2848 if (svalue < 0)
2849 {
2850 warn (_("CU @ %#" PRIx64 " has has a negative stroffsets_base "
2851 "value of %#" PRIx64 " - treating as zero"),
2852 debug_info_p->cu_offset, svalue);
2853 uvalue = 0;
2854 }
2855 debug_info_p->str_offsets_base = uvalue;
2856 break;
2857
2858 case DW_AT_frame_base:
2859 have_frame_base = 1;
2860 /* Fall through. */
2861 case DW_AT_location:
2862 case DW_AT_GNU_locviews:
2863 case DW_AT_string_length:
2864 case DW_AT_return_addr:
2865 case DW_AT_data_member_location:
2866 case DW_AT_vtable_elem_location:
2867 case DW_AT_segment:
2868 case DW_AT_static_link:
2869 case DW_AT_use_location:
2870 case DW_AT_call_value:
2871 case DW_AT_GNU_call_site_value:
2872 case DW_AT_call_data_value:
2873 case DW_AT_GNU_call_site_data_value:
2874 case DW_AT_call_target:
2875 case DW_AT_GNU_call_site_target:
2876 case DW_AT_call_target_clobbered:
2877 case DW_AT_GNU_call_site_target_clobbered:
2878 if ((dwarf_version < 4
2879 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2880 || form == DW_FORM_sec_offset
2881 || form == DW_FORM_loclistx)
2882 {
2883 /* Process location list. */
2884 unsigned int lmax = debug_info_p->max_loc_offsets;
2885 unsigned int num = debug_info_p->num_loc_offsets;
2886
2887 if (lmax == 0 || num >= lmax)
2888 {
2889 lmax += 1024;
2890 debug_info_p->loc_offsets = (uint64_t *)
2891 xcrealloc (debug_info_p->loc_offsets,
2892 lmax, sizeof (*debug_info_p->loc_offsets));
2893 debug_info_p->loc_views = (uint64_t *)
2894 xcrealloc (debug_info_p->loc_views,
2895 lmax, sizeof (*debug_info_p->loc_views));
2896 debug_info_p->have_frame_base = (int *)
2897 xcrealloc (debug_info_p->have_frame_base,
2898 lmax, sizeof (*debug_info_p->have_frame_base));
2899 debug_info_p->max_loc_offsets = lmax;
2900 }
2901 if (form == DW_FORM_loclistx)
2902 uvalue = fetch_indexed_value (num, loclists, debug_info_p->loclists_base);
2903 else if (this_set != NULL)
2904 uvalue += this_set->section_offsets [DW_SECT_LOC];
2905
2906 debug_info_p->have_frame_base [num] = have_frame_base;
2907 if (attribute != DW_AT_GNU_locviews)
2908 {
2909 uvalue += debug_info_p->loclists_base;
2910
2911 /* Corrupt DWARF info can produce more offsets than views.
2912 See PR 23062 for an example. */
2913 if (debug_info_p->num_loc_offsets
2914 > debug_info_p->num_loc_views)
2915 warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2916 else
2917 {
2918 debug_info_p->loc_offsets [num] = uvalue;
2919 debug_info_p->num_loc_offsets++;
2920 }
2921 }
2922 else
2923 {
2924 if (debug_info_p->num_loc_views > num)
2925 {
2926 warn (_("The number of views (%u) is greater than the number of locations (%u)\n"),
2927 debug_info_p->num_loc_views, num);
2928 debug_info_p->num_loc_views = num;
2929 }
2930 else
2931 num = debug_info_p->num_loc_views;
2932 if (num > debug_info_p->num_loc_offsets)
2933 warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
2934 else
2935 {
2936 debug_info_p->loc_views [num] = uvalue;
2937 debug_info_p->num_loc_views++;
2938 }
2939 }
2940 }
2941 break;
2942
2943 case DW_AT_low_pc:
2944 if (need_base_address)
2945 debug_info_p->base_address = uvalue;
2946 break;
2947
2948 case DW_AT_GNU_addr_base:
2949 case DW_AT_addr_base:
2950 debug_info_p->addr_base = uvalue;
2951 break;
2952
2953 case DW_AT_GNU_ranges_base:
2954 debug_info_p->ranges_base = uvalue;
2955 break;
2956
2957 case DW_AT_ranges:
2958 if ((dwarf_version < 4
2959 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2960 || form == DW_FORM_sec_offset
2961 || form == DW_FORM_rnglistx)
2962 {
2963 /* Process range list. */
2964 unsigned int lmax = debug_info_p->max_range_lists;
2965 unsigned int num = debug_info_p->num_range_lists;
2966
2967 if (lmax == 0 || num >= lmax)
2968 {
2969 lmax += 1024;
2970 debug_info_p->range_lists = (uint64_t *)
2971 xcrealloc (debug_info_p->range_lists,
2972 lmax, sizeof (*debug_info_p->range_lists));
2973 debug_info_p->range_versions = (unsigned int *)
2974 xcrealloc (debug_info_p->range_versions,
2975 lmax, sizeof (*debug_info_p->range_versions));
2976 debug_info_p->max_range_lists = lmax;
2977 }
2978
2979 if (form == DW_FORM_rnglistx)
2980 uvalue = fetch_indexed_value (uvalue, rnglists, 0);
2981
2982 debug_info_p->range_lists [num] = uvalue;
2983 debug_info_p->range_versions [num] = dwarf_version;
2984 debug_info_p->num_range_lists++;
2985 }
2986 break;
2987
2988 case DW_AT_GNU_dwo_name:
2989 case DW_AT_dwo_name:
2990 if (need_dwo_info)
2991 switch (form)
2992 {
2993 case DW_FORM_strp:
2994 add_dwo_name ((const char *) fetch_indirect_string (uvalue), cu_offset);
2995 break;
2996 case DW_FORM_GNU_strp_alt:
2997 add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue), cu_offset);
2998 break;
2999 case DW_FORM_GNU_str_index:
3000 case DW_FORM_strx:
3001 case DW_FORM_strx1:
3002 case DW_FORM_strx2:
3003 case DW_FORM_strx3:
3004 case DW_FORM_strx4:
3005 add_dwo_name (fetch_indexed_string (uvalue, this_set, offset_size, false,
3006 debug_info_p->str_offsets_base),
3007 cu_offset);
3008 break;
3009 case DW_FORM_string:
3010 add_dwo_name ((const char *) orig_data, cu_offset);
3011 break;
3012 default:
3013 warn (_("Unsupported form (%s) for attribute %s\n"),
3014 get_FORM_name (form), get_AT_name (attribute));
3015 break;
3016 }
3017 break;
3018
3019 case DW_AT_comp_dir:
3020 /* FIXME: Also extract a build-id in a CU/TU. */
3021 if (need_dwo_info)
3022 switch (form)
3023 {
3024 case DW_FORM_strp:
3025 add_dwo_dir ((const char *) fetch_indirect_string (uvalue), cu_offset);
3026 break;
3027 case DW_FORM_GNU_strp_alt:
3028 add_dwo_dir (fetch_alt_indirect_string (uvalue), cu_offset);
3029 break;
3030 case DW_FORM_line_strp:
3031 add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue), cu_offset);
3032 break;
3033 case DW_FORM_GNU_str_index:
3034 case DW_FORM_strx:
3035 case DW_FORM_strx1:
3036 case DW_FORM_strx2:
3037 case DW_FORM_strx3:
3038 case DW_FORM_strx4:
3039 add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, false,
3040 debug_info_p->str_offsets_base),
3041 cu_offset);
3042 break;
3043 case DW_FORM_string:
3044 add_dwo_dir ((const char *) orig_data, cu_offset);
3045 break;
3046 default:
3047 warn (_("Unsupported form (%s) for attribute %s\n"),
3048 get_FORM_name (form), get_AT_name (attribute));
3049 break;
3050 }
3051 break;
3052
3053 case DW_AT_GNU_dwo_id:
3054 if (need_dwo_info)
3055 switch (form)
3056 {
3057 case DW_FORM_data8:
3058 /* FIXME: Record the length of the ID as well ? */
3059 add_dwo_id ((const char *) (data - 8), cu_offset);
3060 break;
3061 default:
3062 warn (_("Unsupported form (%s) for attribute %s\n"),
3063 get_FORM_name (form), get_AT_name (attribute));
3064 break;
3065 }
3066 break;
3067
3068 default:
3069 break;
3070 }
3071 }
3072
3073 if (do_loc || attribute == 0)
3074 return data;
3075
3076 /* For some attributes we can display further information. */
3077 switch (attribute)
3078 {
3079 case DW_AT_type:
3080 if (level >= 0 && level < MAX_CU_NESTING
3081 && uvalue < (size_t) (end - start))
3082 {
3083 bool is_signed = false;
3084 abbrev_entry *type_abbrev;
3085 unsigned char *type_data;
3086 abbrev_map *map;
3087
3088 type_abbrev = get_type_abbrev_from_form (form, uvalue,
3089 cu_offset, end,
3090 section, NULL,
3091 &type_data, &map);
3092 if (type_abbrev != NULL)
3093 {
3094 get_type_signedness (type_abbrev, section, type_data,
3095 map ? section->start + map->end : end,
3096 map ? map->start : cu_offset,
3097 pointer_size, offset_size, dwarf_version,
3098 & is_signed, 0);
3099 }
3100 level_type_signed[level] = is_signed;
3101 }
3102 break;
3103
3104 case DW_AT_inline:
3105 printf ("\t");
3106 switch (uvalue)
3107 {
3108 case DW_INL_not_inlined:
3109 printf (_("(not inlined)"));
3110 break;
3111 case DW_INL_inlined:
3112 printf (_("(inlined)"));
3113 break;
3114 case DW_INL_declared_not_inlined:
3115 printf (_("(declared as inline but ignored)"));
3116 break;
3117 case DW_INL_declared_inlined:
3118 printf (_("(declared as inline and inlined)"));
3119 break;
3120 default:
3121 printf (_(" (Unknown inline attribute value: %#" PRIx64 ")"),
3122 uvalue);
3123 break;
3124 }
3125 break;
3126
3127 case DW_AT_language:
3128 printf ("\t");
3129 switch (uvalue)
3130 {
3131 /* Ordered by the numeric value of these constants. */
3132 case DW_LANG_C89: printf ("(ANSI C)"); break;
3133 case DW_LANG_C: printf ("(non-ANSI C)"); break;
3134 case DW_LANG_Ada83: printf ("(Ada)"); break;
3135 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
3136 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
3137 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
3138 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
3139 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
3140 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
3141 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
3142 /* DWARF 2.1 values. */
3143 case DW_LANG_Java: printf ("(Java)"); break;
3144 case DW_LANG_C99: printf ("(ANSI C99)"); break;
3145 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
3146 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
3147 /* DWARF 3 values. */
3148 case DW_LANG_PLI: printf ("(PLI)"); break;
3149 case DW_LANG_ObjC: printf ("(Objective C)"); break;
3150 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
3151 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
3152 case DW_LANG_D: printf ("(D)"); break;
3153 /* DWARF 4 values. */
3154 case DW_LANG_Python: printf ("(Python)"); break;
3155 /* DWARF 5 values. */
3156 case DW_LANG_OpenCL: printf ("(OpenCL)"); break;
3157 case DW_LANG_Go: printf ("(Go)"); break;
3158 case DW_LANG_Modula3: printf ("(Modula 3)"); break;
3159 case DW_LANG_Haskell: printf ("(Haskell)"); break;
3160 case DW_LANG_C_plus_plus_03: printf ("(C++03)"); break;
3161 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
3162 case DW_LANG_OCaml: printf ("(OCaml)"); break;
3163 case DW_LANG_Rust: printf ("(Rust)"); break;
3164 case DW_LANG_C11: printf ("(C11)"); break;
3165 case DW_LANG_Swift: printf ("(Swift)"); break;
3166 case DW_LANG_Julia: printf ("(Julia)"); break;
3167 case DW_LANG_Dylan: printf ("(Dylan)"); break;
3168 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
3169 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
3170 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
3171 case DW_LANG_RenderScript: printf ("(RenderScript)"); break;
3172 /* MIPS extension. */
3173 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
3174 /* UPC extension. */
3175 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
3176 default:
3177 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
3178 printf (_("(implementation defined: %#" PRIx64 ")"), uvalue);
3179 else
3180 printf (_("(unknown: %#" PRIx64 ")"), uvalue);
3181 break;
3182 }
3183 break;
3184
3185 case DW_AT_encoding:
3186 printf ("\t");
3187 switch (uvalue)
3188 {
3189 case DW_ATE_void: printf ("(void)"); break;
3190 case DW_ATE_address: printf ("(machine address)"); break;
3191 case DW_ATE_boolean: printf ("(boolean)"); break;
3192 case DW_ATE_complex_float: printf ("(complex float)"); break;
3193 case DW_ATE_float: printf ("(float)"); break;
3194 case DW_ATE_signed: printf ("(signed)"); break;
3195 case DW_ATE_signed_char: printf ("(signed char)"); break;
3196 case DW_ATE_unsigned: printf ("(unsigned)"); break;
3197 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
3198 /* DWARF 2.1 values: */
3199 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
3200 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
3201 /* DWARF 3 values: */
3202 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
3203 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
3204 case DW_ATE_edited: printf ("(edited)"); break;
3205 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
3206 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
3207 /* DWARF 4 values: */
3208 case DW_ATE_UTF: printf ("(unicode string)"); break;
3209 /* DWARF 5 values: */
3210 case DW_ATE_UCS: printf ("(UCS)"); break;
3211 case DW_ATE_ASCII: printf ("(ASCII)"); break;
3212
3213 /* HP extensions: */
3214 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
3215 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
3216 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
3217 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
3218 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
3219 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
3220 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
3221
3222 default:
3223 if (uvalue >= DW_ATE_lo_user
3224 && uvalue <= DW_ATE_hi_user)
3225 printf (_("(user defined type)"));
3226 else
3227 printf (_("(unknown type)"));
3228 break;
3229 }
3230 break;
3231
3232 case DW_AT_accessibility:
3233 printf ("\t");
3234 switch (uvalue)
3235 {
3236 case DW_ACCESS_public: printf ("(public)"); break;
3237 case DW_ACCESS_protected: printf ("(protected)"); break;
3238 case DW_ACCESS_private: printf ("(private)"); break;
3239 default:
3240 printf (_("(unknown accessibility)"));
3241 break;
3242 }
3243 break;
3244
3245 case DW_AT_visibility:
3246 printf ("\t");
3247 switch (uvalue)
3248 {
3249 case DW_VIS_local: printf ("(local)"); break;
3250 case DW_VIS_exported: printf ("(exported)"); break;
3251 case DW_VIS_qualified: printf ("(qualified)"); break;
3252 default: printf (_("(unknown visibility)")); break;
3253 }
3254 break;
3255
3256 case DW_AT_endianity:
3257 printf ("\t");
3258 switch (uvalue)
3259 {
3260 case DW_END_default: printf ("(default)"); break;
3261 case DW_END_big: printf ("(big)"); break;
3262 case DW_END_little: printf ("(little)"); break;
3263 default:
3264 if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
3265 printf (_("(user specified)"));
3266 else
3267 printf (_("(unknown endianity)"));
3268 break;
3269 }
3270 break;
3271
3272 case DW_AT_virtuality:
3273 printf ("\t");
3274 switch (uvalue)
3275 {
3276 case DW_VIRTUALITY_none: printf ("(none)"); break;
3277 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
3278 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
3279 default: printf (_("(unknown virtuality)")); break;
3280 }
3281 break;
3282
3283 case DW_AT_identifier_case:
3284 printf ("\t");
3285 switch (uvalue)
3286 {
3287 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
3288 case DW_ID_up_case: printf ("(up_case)"); break;
3289 case DW_ID_down_case: printf ("(down_case)"); break;
3290 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
3291 default: printf (_("(unknown case)")); break;
3292 }
3293 break;
3294
3295 case DW_AT_calling_convention:
3296 printf ("\t");
3297 switch (uvalue)
3298 {
3299 case DW_CC_normal: printf ("(normal)"); break;
3300 case DW_CC_program: printf ("(program)"); break;
3301 case DW_CC_nocall: printf ("(nocall)"); break;
3302 case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
3303 case DW_CC_pass_by_value: printf ("(pass by value)"); break;
3304 case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
3305 case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
3306 default:
3307 if (uvalue >= DW_CC_lo_user
3308 && uvalue <= DW_CC_hi_user)
3309 printf (_("(user defined)"));
3310 else
3311 printf (_("(unknown convention)"));
3312 }
3313 break;
3314
3315 case DW_AT_ordering:
3316 printf ("\t");
3317 switch (uvalue)
3318 {
3319 case 255:
3320 case -1: printf (_("(undefined)")); break;
3321 case 0: printf ("(row major)"); break;
3322 case 1: printf ("(column major)"); break;
3323 }
3324 break;
3325
3326 case DW_AT_decimal_sign:
3327 printf ("\t");
3328 switch (uvalue)
3329 {
3330 case DW_DS_unsigned: printf (_("(unsigned)")); break;
3331 case DW_DS_leading_overpunch: printf (_("(leading overpunch)")); break;
3332 case DW_DS_trailing_overpunch: printf (_("(trailing overpunch)")); break;
3333 case DW_DS_leading_separate: printf (_("(leading separate)")); break;
3334 case DW_DS_trailing_separate: printf (_("(trailing separate)")); break;
3335 default: printf (_("(unrecognised)")); break;
3336 }
3337 break;
3338
3339 case DW_AT_defaulted:
3340 printf ("\t");
3341 switch (uvalue)
3342 {
3343 case DW_DEFAULTED_no: printf (_("(no)")); break;
3344 case DW_DEFAULTED_in_class: printf (_("(in class)")); break;
3345 case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
3346 default: printf (_("(unrecognised)")); break;
3347 }
3348 break;
3349
3350 case DW_AT_discr_list:
3351 printf ("\t");
3352 display_discr_list (form, uvalue, data, level);
3353 break;
3354
3355 case DW_AT_frame_base:
3356 have_frame_base = 1;
3357 /* Fall through. */
3358 case DW_AT_location:
3359 case DW_AT_loclists_base:
3360 case DW_AT_rnglists_base:
3361 case DW_AT_str_offsets_base:
3362 case DW_AT_string_length:
3363 case DW_AT_return_addr:
3364 case DW_AT_data_member_location:
3365 case DW_AT_vtable_elem_location:
3366 case DW_AT_segment:
3367 case DW_AT_static_link:
3368 case DW_AT_use_location:
3369 case DW_AT_call_value:
3370 case DW_AT_GNU_call_site_value:
3371 case DW_AT_call_data_value:
3372 case DW_AT_GNU_call_site_data_value:
3373 case DW_AT_call_target:
3374 case DW_AT_GNU_call_site_target:
3375 case DW_AT_call_target_clobbered:
3376 case DW_AT_GNU_call_site_target_clobbered:
3377 if ((dwarf_version < 4
3378 && (form == DW_FORM_data4 || form == DW_FORM_data8))
3379 || form == DW_FORM_sec_offset
3380 || form == DW_FORM_loclistx)
3381 {
3382 if (attribute != DW_AT_rnglists_base
3383 && attribute != DW_AT_str_offsets_base)
3384 printf (_(" (location list)"));
3385 }
3386 /* Fall through. */
3387 case DW_AT_allocated:
3388 case DW_AT_associated:
3389 case DW_AT_data_location:
3390 case DW_AT_stride:
3391 case DW_AT_upper_bound:
3392 case DW_AT_lower_bound:
3393 case DW_AT_rank:
3394 if (block_start)
3395 {
3396 int need_frame_base;
3397
3398 printf ("\t(");
3399 need_frame_base = decode_location_expression (block_start,
3400 pointer_size,
3401 offset_size,
3402 dwarf_version,
3403 uvalue,
3404 cu_offset, section);
3405 printf (")");
3406 if (need_frame_base && !have_frame_base)
3407 printf (_(" [without DW_AT_frame_base]"));
3408 }
3409 break;
3410
3411 case DW_AT_data_bit_offset:
3412 case DW_AT_byte_size:
3413 case DW_AT_bit_size:
3414 case DW_AT_string_length_byte_size:
3415 case DW_AT_string_length_bit_size:
3416 case DW_AT_bit_stride:
3417 if (form == DW_FORM_exprloc)
3418 {
3419 printf ("\t(");
3420 (void) decode_location_expression (block_start, pointer_size,
3421 offset_size, dwarf_version,
3422 uvalue, cu_offset, section);
3423 printf (")");
3424 }
3425 break;
3426
3427 case DW_AT_import:
3428 {
3429 unsigned long abbrev_number;
3430 abbrev_entry *entry;
3431
3432 entry = get_type_abbrev_from_form (form, uvalue, cu_offset, end,
3433 section, & abbrev_number, NULL, NULL);
3434 if (entry == NULL)
3435 {
3436 if (form != DW_FORM_GNU_ref_alt)
3437 warn (_("Offset %#" PRIx64 " used as value for DW_AT_import attribute of DIE at offset %#tx is too big.\n"),
3438 uvalue,
3439 orig_data - section->start);
3440 }
3441 else
3442 {
3443 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
3444 printf (" (%s)", get_TAG_name (entry->tag));
3445 printf ("]");
3446 }
3447 }
3448 break;
3449
3450 default:
3451 break;
3452 }
3453
3454 return data;
3455 }
3456
3457 static unsigned char *
3458 read_and_display_attr (unsigned long attribute,
3459 unsigned long form,
3460 int64_t implicit_const,
3461 unsigned char *start,
3462 unsigned char *data,
3463 unsigned char *end,
3464 uint64_t cu_offset,
3465 uint64_t pointer_size,
3466 uint64_t offset_size,
3467 int dwarf_version,
3468 debug_info *debug_info_p,
3469 int do_loc,
3470 struct dwarf_section *section,
3471 struct cu_tu_set *this_set,
3472 int level)
3473 {
3474 if (!do_loc)
3475 printf (" %-18s:", get_AT_name (attribute));
3476 data = read_and_display_attr_value (attribute, form, implicit_const,
3477 start, data, end,
3478 cu_offset, pointer_size, offset_size,
3479 dwarf_version, debug_info_p,
3480 do_loc, section, this_set, ' ', level);
3481 if (!do_loc)
3482 printf ("\n");
3483 return data;
3484 }
3485
3486 /* Like load_debug_section, but if the ordinary call fails, and we are
3487 following debug links, then attempt to load the requested section
3488 from one of the separate debug info files. */
3489
3490 static bool
3491 load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
3492 void * handle)
3493 {
3494 if (load_debug_section (sec_enum, handle))
3495 {
3496 if (debug_displays[sec_enum].section.filename == NULL)
3497 {
3498 /* See if we can associate a filename with this section. */
3499 separate_info * i;
3500
3501 for (i = first_separate_info; i != NULL; i = i->next)
3502 if (i->handle == handle)
3503 {
3504 debug_displays[sec_enum].section.filename = i->filename;
3505 break;
3506 }
3507 }
3508
3509 return true;
3510 }
3511
3512 if (do_follow_links)
3513 {
3514 separate_info * i;
3515
3516 for (i = first_separate_info; i != NULL; i = i->next)
3517 {
3518 if (load_debug_section (sec_enum, i->handle))
3519 {
3520 debug_displays[sec_enum].section.filename = i->filename;
3521
3522 /* FIXME: We should check to see if any of the remaining debug info
3523 files also contain this section, and, umm, do something about it. */
3524 return true;
3525 }
3526 }
3527 }
3528
3529 return false;
3530 }
3531
3532 static void
3533 introduce (struct dwarf_section * section, bool raw)
3534 {
3535 if (raw)
3536 {
3537 if (do_follow_links && section->filename)
3538 printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3539 section->name, section->filename);
3540 else
3541 printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3542 }
3543 else
3544 {
3545 if (do_follow_links && section->filename)
3546 printf (_("Contents of the %s section (loaded from %s):\n\n"),
3547 section->name, section->filename);
3548 else
3549 printf (_("Contents of the %s section:\n\n"), section->name);
3550 }
3551 }
3552
3553 /* Free memory allocated for one unit in debug_information. */
3554
3555 static void
3556 free_debug_information (debug_info *ent)
3557 {
3558 if (ent->max_loc_offsets)
3559 {
3560 free (ent->loc_offsets);
3561 free (ent->loc_views);
3562 free (ent->have_frame_base);
3563 }
3564 if (ent->max_range_lists)
3565 {
3566 free (ent->range_versions);
3567 free (ent->range_lists);
3568 }
3569 }
3570
3571 /* Process the contents of a .debug_info section.
3572 If do_loc is TRUE then we are scanning for location lists and dwo tags
3573 and we do not want to display anything to the user.
3574 If do_types is TRUE, we are processing a .debug_types section instead of
3575 a .debug_info section.
3576 The information displayed is restricted by the values in DWARF_START_DIE
3577 and DWARF_CUTOFF_LEVEL.
3578 Returns TRUE upon success. Otherwise an error or warning message is
3579 printed and FALSE is returned. */
3580
3581 static bool
3582 process_debug_info (struct dwarf_section * section,
3583 void *file,
3584 enum dwarf_section_display_enum abbrev_sec,
3585 bool do_loc,
3586 bool do_types)
3587 {
3588 unsigned char *start = section->start;
3589 unsigned char *end = start + section->size;
3590 unsigned char *section_begin;
3591 unsigned int unit;
3592 unsigned int num_units = 0;
3593
3594 /* First scan the section to get the number of comp units.
3595 Length sanity checks are done here. */
3596 for (section_begin = start, num_units = 0; section_begin < end;
3597 num_units ++)
3598 {
3599 uint64_t length;
3600
3601 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3602 will be the length. For a 64-bit DWARF section, it'll be
3603 the escape code 0xffffffff followed by an 8 byte length. */
3604 SAFE_BYTE_GET_AND_INC (length, section_begin, 4, end);
3605
3606 if (length == 0xffffffff)
3607 SAFE_BYTE_GET_AND_INC (length, section_begin, 8, end);
3608 else if (length >= 0xfffffff0 && length < 0xffffffff)
3609 {
3610 warn (_("Reserved length value (%#" PRIx64 ") found in section %s\n"),
3611 length, section->name);
3612 return false;
3613 }
3614
3615 /* Negative values are illegal, they may even cause infinite
3616 looping. This can happen if we can't accurately apply
3617 relocations to an object file, or if the file is corrupt. */
3618 if (length > (size_t) (end - section_begin))
3619 {
3620 warn (_("Corrupt unit length (got %#" PRIx64
3621 " expected at most %#tx) in section %s\n"),
3622 length, end - section_begin, section->name);
3623 return false;
3624 }
3625 section_begin += length;
3626 }
3627
3628 if (num_units == 0)
3629 {
3630 error (_("No comp units in %s section ?\n"), section->name);
3631 return false;
3632 }
3633
3634 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3635 && num_debug_info_entries == 0
3636 && ! do_types)
3637 {
3638
3639 /* Then allocate an array to hold the information. */
3640 debug_information = (debug_info *) cmalloc (num_units,
3641 sizeof (* debug_information));
3642 if (debug_information == NULL)
3643 {
3644 error (_("Not enough memory for a debug info array of %u entries\n"),
3645 num_units);
3646 alloc_num_debug_info_entries = num_debug_info_entries = 0;
3647 return false;
3648 }
3649
3650 /* PR 17531: file: 92ca3797.
3651 We cannot rely upon the debug_information array being initialised
3652 before it is used. A corrupt file could easily contain references
3653 to a unit for which information has not been made available. So
3654 we ensure that the array is zeroed here. */
3655 memset (debug_information, 0, num_units * sizeof (*debug_information));
3656
3657 alloc_num_debug_info_entries = num_units;
3658 }
3659
3660 if (!do_loc)
3661 {
3662 load_debug_section_with_follow (str, file);
3663 load_debug_section_with_follow (line_str, file);
3664 load_debug_section_with_follow (str_dwo, file);
3665 load_debug_section_with_follow (str_index, file);
3666 load_debug_section_with_follow (str_index_dwo, file);
3667 load_debug_section_with_follow (debug_addr, file);
3668 }
3669
3670 load_debug_section_with_follow (abbrev_sec, file);
3671 load_debug_section_with_follow (loclists, file);
3672 load_debug_section_with_follow (rnglists, file);
3673 load_debug_section_with_follow (loclists_dwo, file);
3674 load_debug_section_with_follow (rnglists_dwo, file);
3675
3676 if (debug_displays [abbrev_sec].section.start == NULL)
3677 {
3678 warn (_("Unable to locate %s section!\n"),
3679 debug_displays [abbrev_sec].section.uncompressed_name);
3680 return false;
3681 }
3682
3683 if (!do_loc && dwarf_start_die == 0)
3684 introduce (section, false);
3685
3686 free_all_abbrevs ();
3687
3688 /* In order to be able to resolve DW_FORM_ref_addr forms we need
3689 to load *all* of the abbrevs for all CUs in this .debug_info
3690 section. This does effectively mean that we (partially) read
3691 every CU header twice. */
3692 for (section_begin = start; start < end;)
3693 {
3694 DWARF2_Internal_CompUnit compunit;
3695 unsigned char *hdrptr;
3696 uint64_t abbrev_base;
3697 size_t abbrev_size;
3698 uint64_t cu_offset;
3699 unsigned int offset_size;
3700 struct cu_tu_set *this_set;
3701 unsigned char *end_cu;
3702
3703 hdrptr = start;
3704 cu_offset = start - section_begin;
3705
3706 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3707
3708 if (compunit.cu_length == 0xffffffff)
3709 {
3710 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3711 offset_size = 8;
3712 }
3713 else
3714 offset_size = 4;
3715 end_cu = hdrptr + compunit.cu_length;
3716
3717 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3718
3719 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3720
3721 if (compunit.cu_version < 5)
3722 {
3723 compunit.cu_unit_type = DW_UT_compile;
3724 /* Initialize it due to a false compiler warning. */
3725 compunit.cu_pointer_size = -1;
3726 }
3727 else
3728 {
3729 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3730 do_types = (compunit.cu_unit_type == DW_UT_type);
3731
3732 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3733 }
3734
3735 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size,
3736 end_cu);
3737
3738 if (compunit.cu_unit_type == DW_UT_split_compile
3739 || compunit.cu_unit_type == DW_UT_skeleton)
3740 {
3741 uint64_t dwo_id;
3742 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3743 }
3744
3745 if (this_set == NULL)
3746 {
3747 abbrev_base = 0;
3748 abbrev_size = debug_displays [abbrev_sec].section.size;
3749 }
3750 else
3751 {
3752 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3753 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3754 }
3755
3756 abbrev_list *list;
3757 abbrev_list *free_list;
3758 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3759 abbrev_base, abbrev_size,
3760 compunit.cu_abbrev_offset,
3761 &free_list);
3762 start = end_cu;
3763 if (list != NULL && list->first_abbrev != NULL)
3764 record_abbrev_list_for_cu (cu_offset, start - section_begin,
3765 list, free_list);
3766 else if (free_list != NULL)
3767 free_abbrev_list (free_list);
3768 }
3769
3770 for (start = section_begin, unit = 0; start < end; unit++)
3771 {
3772 DWARF2_Internal_CompUnit compunit;
3773 unsigned char *hdrptr;
3774 unsigned char *tags;
3775 int level, last_level, saved_level;
3776 uint64_t cu_offset;
3777 unsigned int offset_size;
3778 uint64_t signature = 0;
3779 uint64_t type_offset = 0;
3780 struct cu_tu_set *this_set;
3781 uint64_t abbrev_base;
3782 size_t abbrev_size;
3783 unsigned char *end_cu;
3784
3785 hdrptr = start;
3786 cu_offset = start - section_begin;
3787
3788 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3789
3790 if (compunit.cu_length == 0xffffffff)
3791 {
3792 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3793 offset_size = 8;
3794 }
3795 else
3796 offset_size = 4;
3797 end_cu = hdrptr + compunit.cu_length;
3798
3799 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3800
3801 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3802
3803 if (compunit.cu_version < 5)
3804 {
3805 compunit.cu_unit_type = DW_UT_compile;
3806 /* Initialize it due to a false compiler warning. */
3807 compunit.cu_pointer_size = -1;
3808 }
3809 else
3810 {
3811 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3812 do_types = (compunit.cu_unit_type == DW_UT_type);
3813
3814 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3815 }
3816
3817 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end_cu);
3818
3819 if (this_set == NULL)
3820 {
3821 abbrev_base = 0;
3822 abbrev_size = debug_displays [abbrev_sec].section.size;
3823 }
3824 else
3825 {
3826 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3827 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3828 }
3829
3830 if (compunit.cu_version < 5)
3831 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3832
3833 bool do_dwo_id = false;
3834 uint64_t dwo_id = 0;
3835 if (compunit.cu_unit_type == DW_UT_split_compile
3836 || compunit.cu_unit_type == DW_UT_skeleton)
3837 {
3838 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3839 do_dwo_id = true;
3840 }
3841
3842 /* PR 17512: file: 001-108546-0.001:0.1. */
3843 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3844 {
3845 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3846 compunit.cu_pointer_size, offset_size);
3847 compunit.cu_pointer_size = offset_size;
3848 }
3849
3850 if (do_types)
3851 {
3852 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, end_cu);
3853 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end_cu);
3854 }
3855
3856 if (dwarf_start_die >= (size_t) (end_cu - section_begin))
3857 {
3858 start = end_cu;
3859 continue;
3860 }
3861
3862 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3863 && num_debug_info_entries == 0
3864 && alloc_num_debug_info_entries > unit
3865 && ! do_types)
3866 {
3867 free_debug_information (&debug_information[unit]);
3868 memset (&debug_information[unit], 0, sizeof (*debug_information));
3869 debug_information[unit].pointer_size = compunit.cu_pointer_size;
3870 debug_information[unit].offset_size = offset_size;
3871 debug_information[unit].dwarf_version = compunit.cu_version;
3872 debug_information[unit].cu_offset = cu_offset;
3873 debug_information[unit].addr_base = DEBUG_INFO_UNAVAILABLE;
3874 debug_information[unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
3875 }
3876
3877 if (!do_loc && dwarf_start_die == 0)
3878 {
3879 printf (_(" Compilation Unit @ offset %#" PRIx64 ":\n"),
3880 cu_offset);
3881 printf (_(" Length: %#" PRIx64 " (%s)\n"),
3882 compunit.cu_length,
3883 offset_size == 8 ? "64-bit" : "32-bit");
3884 printf (_(" Version: %d\n"), compunit.cu_version);
3885 if (compunit.cu_version >= 5)
3886 {
3887 const char *name = get_DW_UT_name (compunit.cu_unit_type);
3888
3889 printf (_(" Unit Type: %s (%x)\n"),
3890 null_name (name),
3891 compunit.cu_unit_type);
3892 }
3893 printf (_(" Abbrev Offset: %#" PRIx64 "\n"),
3894 compunit.cu_abbrev_offset);
3895 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
3896 if (do_types)
3897 {
3898 printf (_(" Signature: %#" PRIx64 "\n"), signature);
3899 printf (_(" Type Offset: %#" PRIx64 "\n"), type_offset);
3900 }
3901 if (do_dwo_id)
3902 printf (_(" DWO ID: %#" PRIx64 "\n"), dwo_id);
3903 if (this_set != NULL)
3904 {
3905 uint64_t *offsets = this_set->section_offsets;
3906 size_t *sizes = this_set->section_sizes;
3907
3908 printf (_(" Section contributions:\n"));
3909 printf (_(" .debug_abbrev.dwo: %#" PRIx64 " %#zx\n"),
3910 offsets[DW_SECT_ABBREV], sizes[DW_SECT_ABBREV]);
3911 printf (_(" .debug_line.dwo: %#" PRIx64 " %#zx\n"),
3912 offsets[DW_SECT_LINE], sizes[DW_SECT_LINE]);
3913 printf (_(" .debug_loc.dwo: %#" PRIx64 " %#zx\n"),
3914 offsets[DW_SECT_LOC], sizes[DW_SECT_LOC]);
3915 printf (_(" .debug_str_offsets.dwo: %#" PRIx64 " %#zx\n"),
3916 offsets[DW_SECT_STR_OFFSETS], sizes[DW_SECT_STR_OFFSETS]);
3917 }
3918 }
3919
3920 tags = hdrptr;
3921 start = end_cu;
3922
3923 if (compunit.cu_version < 2 || compunit.cu_version > 5)
3924 {
3925 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
3926 "unsupported version number: %d.\n"),
3927 cu_offset, compunit.cu_version);
3928 continue;
3929 }
3930
3931 if (compunit.cu_unit_type != DW_UT_compile
3932 && compunit.cu_unit_type != DW_UT_partial
3933 && compunit.cu_unit_type != DW_UT_type
3934 && compunit.cu_unit_type != DW_UT_split_compile
3935 && compunit.cu_unit_type != DW_UT_skeleton)
3936 {
3937 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
3938 "unsupported unit type: %d.\n"),
3939 cu_offset, compunit.cu_unit_type);
3940 continue;
3941 }
3942
3943 /* Process the abbrevs used by this compilation unit. */
3944 abbrev_list *list;
3945 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3946 abbrev_base, abbrev_size,
3947 compunit.cu_abbrev_offset, NULL);
3948 level = 0;
3949 last_level = level;
3950 saved_level = -1;
3951 while (tags < start)
3952 {
3953 unsigned long abbrev_number;
3954 unsigned long die_offset;
3955 abbrev_entry *entry;
3956 abbrev_attr *attr;
3957 int do_printing = 1;
3958
3959 die_offset = tags - section_begin;
3960
3961 READ_ULEB (abbrev_number, tags, start);
3962
3963 /* A null DIE marks the end of a list of siblings or it may also be
3964 a section padding. */
3965 if (abbrev_number == 0)
3966 {
3967 /* Check if it can be a section padding for the last CU. */
3968 if (level == 0 && start == end)
3969 {
3970 unsigned char *chk;
3971
3972 for (chk = tags; chk < start; chk++)
3973 if (*chk != 0)
3974 break;
3975 if (chk == start)
3976 break;
3977 }
3978
3979 if (!do_loc && die_offset >= dwarf_start_die
3980 && (dwarf_cutoff_level == -1
3981 || level < dwarf_cutoff_level))
3982 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
3983 level, die_offset);
3984
3985 --level;
3986 if (level < 0)
3987 {
3988 static unsigned num_bogus_warns = 0;
3989
3990 if (num_bogus_warns < 3)
3991 {
3992 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
3993 die_offset, section->name);
3994 num_bogus_warns ++;
3995 if (num_bogus_warns == 3)
3996 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
3997 }
3998 }
3999 if (dwarf_start_die != 0 && level < saved_level)
4000 {
4001 if (list != NULL)
4002 free_abbrev_list (list);
4003 return true;
4004 }
4005 continue;
4006 }
4007
4008 if (!do_loc)
4009 {
4010 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
4011 do_printing = 0;
4012 else
4013 {
4014 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
4015 saved_level = level;
4016 do_printing = (dwarf_cutoff_level == -1
4017 || level < dwarf_cutoff_level);
4018 if (do_printing)
4019 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
4020 level, die_offset, abbrev_number);
4021 else if (dwarf_cutoff_level == -1
4022 || last_level < dwarf_cutoff_level)
4023 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
4024 last_level = level;
4025 }
4026 }
4027
4028 /* Scan through the abbreviation list until we reach the
4029 correct entry. */
4030 entry = NULL;
4031 if (list != NULL)
4032 for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
4033 if (entry->number == abbrev_number)
4034 break;
4035
4036 if (entry == NULL)
4037 {
4038 if (!do_loc && do_printing)
4039 {
4040 printf ("\n");
4041 fflush (stdout);
4042 }
4043 warn (_("DIE at offset %#lx refers to abbreviation number %lu which does not exist\n"),
4044 die_offset, abbrev_number);
4045 if (list != NULL)
4046 free_abbrev_list (list);
4047 return false;
4048 }
4049
4050 if (!do_loc && do_printing)
4051 printf (" (%s)\n", get_TAG_name (entry->tag));
4052
4053 switch (entry->tag)
4054 {
4055 default:
4056 need_base_address = 0;
4057 break;
4058 case DW_TAG_compile_unit:
4059 case DW_TAG_skeleton_unit:
4060 need_base_address = 1;
4061 need_dwo_info = do_loc;
4062 break;
4063 case DW_TAG_entry_point:
4064 case DW_TAG_subprogram:
4065 need_base_address = 0;
4066 /* Assuming that there is no DW_AT_frame_base. */
4067 have_frame_base = 0;
4068 break;
4069 }
4070
4071 debug_info *debug_info_p =
4072 (debug_information && unit < alloc_num_debug_info_entries)
4073 ? debug_information + unit : NULL;
4074
4075 assert (!debug_info_p
4076 || (debug_info_p->num_loc_offsets
4077 == debug_info_p->num_loc_views));
4078
4079 for (attr = entry->first_attr;
4080 attr && attr->attribute;
4081 attr = attr->next)
4082 {
4083 if (! do_loc && do_printing)
4084 /* Show the offset from where the tag was extracted. */
4085 printf (" <%tx>", tags - section_begin);
4086 tags = read_and_display_attr (attr->attribute,
4087 attr->form,
4088 attr->implicit_const,
4089 section_begin,
4090 tags,
4091 start,
4092 cu_offset,
4093 compunit.cu_pointer_size,
4094 offset_size,
4095 compunit.cu_version,
4096 debug_info_p,
4097 do_loc || ! do_printing,
4098 section,
4099 this_set,
4100 level);
4101 }
4102
4103 /* If a locview attribute appears before a location one,
4104 make sure we don't associate it with an earlier
4105 loclist. */
4106 if (debug_info_p)
4107 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
4108 {
4109 case 1:
4110 debug_info_p->loc_views [debug_info_p->num_loc_views] = -1;
4111 debug_info_p->num_loc_views++;
4112 assert (debug_info_p->num_loc_views
4113 == debug_info_p->num_loc_offsets);
4114 break;
4115
4116 case 0:
4117 break;
4118
4119 case -1:
4120 warn(_("DIE has locviews without loclist\n"));
4121 debug_info_p->num_loc_views--;
4122 break;
4123
4124 default:
4125 assert (0);
4126 }
4127
4128 if (entry->children)
4129 ++level;
4130 }
4131 if (list != NULL)
4132 free_abbrev_list (list);
4133 }
4134
4135 /* Set num_debug_info_entries here so that it can be used to check if
4136 we need to process .debug_loc and .debug_ranges sections. */
4137 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
4138 && num_debug_info_entries == 0
4139 && ! do_types)
4140 {
4141 if (num_units > alloc_num_debug_info_entries)
4142 num_debug_info_entries = alloc_num_debug_info_entries;
4143 else
4144 num_debug_info_entries = num_units;
4145 }
4146
4147 if (!do_loc)
4148 printf ("\n");
4149
4150 return true;
4151 }
4152
4153 /* Locate and scan the .debug_info section in the file and record the pointer
4154 sizes and offsets for the compilation units in it. Usually an executable
4155 will have just one pointer size, but this is not guaranteed, and so we try
4156 not to make any assumptions. Returns zero upon failure, or the number of
4157 compilation units upon success. */
4158
4159 static unsigned int
4160 load_debug_info (void * file)
4161 {
4162 /* If we have already tried and failed to load the .debug_info
4163 section then do not bother to repeat the task. */
4164 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4165 return 0;
4166
4167 /* If we already have the information there is nothing else to do. */
4168 if (num_debug_info_entries > 0)
4169 return num_debug_info_entries;
4170
4171 /* If this is a DWARF package file, load the CU and TU indexes. */
4172 (void) load_cu_tu_indexes (file);
4173
4174 if (load_debug_section_with_follow (info, file)
4175 && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
4176 return num_debug_info_entries;
4177
4178 if (load_debug_section_with_follow (info_dwo, file)
4179 && process_debug_info (&debug_displays [info_dwo].section, file,
4180 abbrev_dwo, true, false))
4181 return num_debug_info_entries;
4182
4183 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
4184 return 0;
4185 }
4186
4187 /* Read a DWARF .debug_line section header starting at DATA.
4188 Upon success returns an updated DATA pointer and the LINFO
4189 structure and the END_OF_SEQUENCE pointer will be filled in.
4190 Otherwise returns NULL. */
4191
4192 static unsigned char *
4193 read_debug_line_header (struct dwarf_section * section,
4194 unsigned char * data,
4195 unsigned char * end,
4196 DWARF2_Internal_LineInfo * linfo,
4197 unsigned char ** end_of_sequence)
4198 {
4199 unsigned char *hdrptr;
4200
4201 /* Extract information from the Line Number Program Header.
4202 (section 6.2.4 in the Dwarf3 doc). */
4203 hdrptr = data;
4204
4205 /* Get and check the length of the block. */
4206 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
4207
4208 if (linfo->li_length == 0xffffffff)
4209 {
4210 /* This section is 64-bit DWARF 3. */
4211 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
4212 linfo->li_offset_size = 8;
4213 }
4214 else
4215 linfo->li_offset_size = 4;
4216
4217 if (linfo->li_length > (size_t) (end - hdrptr))
4218 {
4219 /* If the length field has a relocation against it, then we should
4220 not complain if it is inaccurate (and probably negative). This
4221 happens in object files when the .debug_line section is actually
4222 comprised of several different .debug_line.* sections, (some of
4223 which may be removed by linker garbage collection), and a relocation
4224 is used to compute the correct length once that is done. */
4225 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
4226 {
4227 linfo->li_length = end - hdrptr;
4228 }
4229 else
4230 {
4231 warn (_("The length field (%#" PRIx64 ")"
4232 " in the debug_line header is wrong"
4233 " - the section is too small\n"),
4234 linfo->li_length);
4235 return NULL;
4236 }
4237 }
4238 end = hdrptr + linfo->li_length;
4239
4240 /* Get and check the version number. */
4241 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4242
4243 if (linfo->li_version != 2
4244 && linfo->li_version != 3
4245 && linfo->li_version != 4
4246 && linfo->li_version != 5)
4247 {
4248 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4249 "is currently supported.\n"));
4250 return NULL;
4251 }
4252
4253 if (linfo->li_version >= 5)
4254 {
4255 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
4256
4257 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4258 if (linfo->li_segment_size != 0)
4259 {
4260 warn (_("The %s section contains "
4261 "unsupported segment selector size: %d.\n"),
4262 section->name, linfo->li_segment_size);
4263 return NULL;
4264 }
4265 }
4266
4267 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4268 linfo->li_offset_size, end);
4269 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
4270
4271 if (linfo->li_version >= 4)
4272 {
4273 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
4274
4275 if (linfo->li_max_ops_per_insn == 0)
4276 {
4277 warn (_("Invalid maximum operations per insn.\n"));
4278 return NULL;
4279 }
4280 }
4281 else
4282 linfo->li_max_ops_per_insn = 1;
4283
4284 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
4285 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
4286 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4287 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
4288
4289 *end_of_sequence = end;
4290 return hdrptr;
4291 }
4292
4293 static unsigned char *
4294 display_formatted_table (unsigned char *data,
4295 unsigned char *start,
4296 unsigned char *end,
4297 const DWARF2_Internal_LineInfo *linfo,
4298 struct dwarf_section *section,
4299 bool is_dir)
4300 {
4301 unsigned char *format_start, format_count, *format, formati;
4302 uint64_t data_count, datai;
4303 unsigned int namepass, last_entry = 0;
4304 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
4305
4306 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4307 if (do_checks && format_count > 5)
4308 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4309 table_name, format_count);
4310
4311 format_start = data;
4312 for (formati = 0; formati < format_count; formati++)
4313 {
4314 SKIP_ULEB (data, end);
4315 SKIP_ULEB (data, end);
4316 if (data >= end)
4317 {
4318 warn (_("%s: Corrupt format description entry\n"), table_name);
4319 return data;
4320 }
4321 }
4322
4323 READ_ULEB (data_count, data, end);
4324 if (data_count == 0)
4325 {
4326 printf (_("\n The %s is empty.\n"), table_name);
4327 return data;
4328 }
4329 else if (data >= end
4330 || data_count > (size_t) (end - data))
4331 {
4332 warn (_("%s: Corrupt entry count %#" PRIx64 "\n"), table_name, data_count);
4333 return data;
4334 }
4335
4336 else if (format_count == 0)
4337 {
4338 warn (_("%s: format count is zero, but the table is not empty\n"),
4339 table_name);
4340 return end;
4341 }
4342
4343 printf (_("\n The %s (offset %#tx, lines %" PRIu64 ", columns %u):\n"),
4344 table_name, data - start, data_count, format_count);
4345
4346 printf (_(" Entry"));
4347 /* Delay displaying name as the last entry for better screen layout. */
4348 for (namepass = 0; namepass < 2; namepass++)
4349 {
4350 format = format_start;
4351 for (formati = 0; formati < format_count; formati++)
4352 {
4353 uint64_t content_type;
4354
4355 READ_ULEB (content_type, format, end);
4356 if ((content_type == DW_LNCT_path) == (namepass == 1))
4357 switch (content_type)
4358 {
4359 case DW_LNCT_path:
4360 printf (_("\tName"));
4361 break;
4362 case DW_LNCT_directory_index:
4363 printf (_("\tDir"));
4364 break;
4365 case DW_LNCT_timestamp:
4366 printf (_("\tTime"));
4367 break;
4368 case DW_LNCT_size:
4369 printf (_("\tSize"));
4370 break;
4371 case DW_LNCT_MD5:
4372 printf (_("\tMD5\t\t\t"));
4373 break;
4374 default:
4375 printf (_("\t(Unknown format content type %" PRIu64 ")"),
4376 content_type);
4377 }
4378 SKIP_ULEB (format, end);
4379 }
4380 }
4381 putchar ('\n');
4382
4383 for (datai = 0; datai < data_count; datai++)
4384 {
4385 unsigned char *datapass = data;
4386
4387 printf (" %d", last_entry++);
4388 /* Delay displaying name as the last entry for better screen layout. */
4389 for (namepass = 0; namepass < 2; namepass++)
4390 {
4391 format = format_start;
4392 data = datapass;
4393 for (formati = 0; formati < format_count; formati++)
4394 {
4395 uint64_t content_type, form;
4396
4397 READ_ULEB (content_type, format, end);
4398 READ_ULEB (form, format, end);
4399 data = read_and_display_attr_value (0, form, 0, start, data, end,
4400 0, 0, linfo->li_offset_size,
4401 linfo->li_version, NULL,
4402 ((content_type == DW_LNCT_path) != (namepass == 1)),
4403 section, NULL, '\t', -1);
4404 }
4405 }
4406
4407 if (data >= end && (datai < data_count - 1))
4408 {
4409 warn (_("\n%s: Corrupt entries list\n"), table_name);
4410 return data;
4411 }
4412 putchar ('\n');
4413 }
4414 return data;
4415 }
4416
4417 static int
4418 display_debug_sup (struct dwarf_section * section,
4419 void * file ATTRIBUTE_UNUSED)
4420 {
4421 unsigned char * start = section->start;
4422 unsigned char * end = section->start + section->size;
4423 unsigned int version;
4424 char is_supplementary;
4425 const unsigned char * sup_filename;
4426 size_t sup_filename_len;
4427 unsigned int num_read;
4428 int status;
4429 uint64_t checksum_len;
4430
4431
4432 introduce (section, true);
4433 if (section->size < 4)
4434 {
4435 error (_("corrupt .debug_sup section: size is too small\n"));
4436 return 0;
4437 }
4438
4439 /* Read the data. */
4440 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4441 if (version < 5)
4442 warn (_("corrupt .debug_sup section: version < 5"));
4443
4444 SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4445 if (is_supplementary != 0 && is_supplementary != 1)
4446 warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4447
4448 sup_filename = start;
4449 if (is_supplementary && sup_filename[0] != 0)
4450 warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4451
4452 sup_filename_len = strnlen ((const char *) start, end - start);
4453 if (sup_filename_len == (size_t) (end - start))
4454 {
4455 error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4456 return 0;
4457 }
4458 start += sup_filename_len + 1;
4459
4460 checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
4461 if (status)
4462 {
4463 error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4464 checksum_len = 0;
4465 }
4466 start += num_read;
4467 if (checksum_len > (size_t) (end - start))
4468 {
4469 error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4470 checksum_len = end - start;
4471 }
4472 else if (checksum_len < (size_t) (end - start))
4473 {
4474 warn (_("corrupt .debug_sup section: there are %#" PRIx64
4475 " extra, unused bytes at the end of the section\n"),
4476 (end - start) - checksum_len);
4477 }
4478
4479 printf (_(" Version: %u\n"), version);
4480 printf (_(" Is Supp: %u\n"), is_supplementary);
4481 printf (_(" Filename: %s\n"), sup_filename);
4482 printf (_(" Checksum Len: %" PRIu64 "\n"), checksum_len);
4483 if (checksum_len > 0)
4484 {
4485 printf (_(" Checksum: "));
4486 while (checksum_len--)
4487 printf ("0x%x ", * start++ );
4488 printf ("\n");
4489 }
4490 return 1;
4491 }
4492
4493 static int
4494 display_debug_lines_raw (struct dwarf_section * section,
4495 unsigned char * data,
4496 unsigned char * end,
4497 void * file)
4498 {
4499 unsigned char *start = section->start;
4500 int verbose_view = 0;
4501
4502 introduce (section, true);
4503
4504 while (data < end)
4505 {
4506 static DWARF2_Internal_LineInfo saved_linfo;
4507 DWARF2_Internal_LineInfo linfo;
4508 unsigned char *standard_opcodes;
4509 unsigned char *end_of_sequence;
4510 int i;
4511
4512 if (startswith (section->name, ".debug_line.")
4513 /* Note: the following does not apply to .debug_line.dwo sections.
4514 These are full debug_line sections. */
4515 && strcmp (section->name, ".debug_line.dwo") != 0)
4516 {
4517 /* Sections named .debug_line.<foo> are fragments of a .debug_line
4518 section containing just the Line Number Statements. They are
4519 created by the assembler and intended to be used alongside gcc's
4520 -ffunction-sections command line option. When the linker's
4521 garbage collection decides to discard a .text.<foo> section it
4522 can then also discard the line number information in .debug_line.<foo>.
4523
4524 Since the section is a fragment it does not have the details
4525 needed to fill out a LineInfo structure, so instead we use the
4526 details from the last full debug_line section that we processed. */
4527 end_of_sequence = end;
4528 standard_opcodes = NULL;
4529 linfo = saved_linfo;
4530 /* PR 17531: file: 0522b371. */
4531 if (linfo.li_line_range == 0)
4532 {
4533 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4534 return 0;
4535 }
4536 reset_state_machine (linfo.li_default_is_stmt);
4537 }
4538 else
4539 {
4540 unsigned char * hdrptr;
4541
4542 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4543 & end_of_sequence)) == NULL)
4544 return 0;
4545
4546 printf (_(" Offset: %#tx\n"), data - start);
4547 printf (_(" Length: %" PRId64 "\n"), linfo.li_length);
4548 printf (_(" DWARF Version: %d\n"), linfo.li_version);
4549 if (linfo.li_version >= 5)
4550 {
4551 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
4552 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
4553 }
4554 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
4555 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
4556 if (linfo.li_version >= 4)
4557 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4558 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
4559 printf (_(" Line Base: %d\n"), linfo.li_line_base);
4560 printf (_(" Line Range: %d\n"), linfo.li_line_range);
4561 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
4562
4563 /* PR 17512: file: 1665-6428-0.004. */
4564 if (linfo.li_line_range == 0)
4565 {
4566 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4567 linfo.li_line_range = 1;
4568 }
4569
4570 reset_state_machine (linfo.li_default_is_stmt);
4571
4572 /* Display the contents of the Opcodes table. */
4573 standard_opcodes = hdrptr;
4574
4575 /* PR 17512: file: 002-417945-0.004. */
4576 if (standard_opcodes + linfo.li_opcode_base >= end)
4577 {
4578 warn (_("Line Base extends beyond end of section\n"));
4579 return 0;
4580 }
4581
4582 printf (_("\n Opcodes:\n"));
4583
4584 for (i = 1; i < linfo.li_opcode_base; i++)
4585 printf (ngettext (" Opcode %d has %d arg\n",
4586 " Opcode %d has %d args\n",
4587 standard_opcodes[i - 1]),
4588 i, standard_opcodes[i - 1]);
4589
4590 /* Display the contents of the Directory table. */
4591 data = standard_opcodes + linfo.li_opcode_base - 1;
4592
4593 if (linfo.li_version >= 5)
4594 {
4595 load_debug_section_with_follow (line_str, file);
4596
4597 data = display_formatted_table (data, start, end, &linfo, section,
4598 true);
4599 data = display_formatted_table (data, start, end, &linfo, section,
4600 false);
4601 }
4602 else
4603 {
4604 if (*data == 0)
4605 printf (_("\n The Directory Table is empty.\n"));
4606 else
4607 {
4608 unsigned int last_dir_entry = 0;
4609
4610 printf (_("\n The Directory Table (offset %#tx):\n"),
4611 data - start);
4612
4613 while (data < end && *data != 0)
4614 {
4615 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
4616
4617 data += strnlen ((char *) data, end - data);
4618 if (data < end)
4619 data++;
4620 }
4621
4622 /* PR 17512: file: 002-132094-0.004. */
4623 if (data >= end - 1)
4624 break;
4625 }
4626
4627 /* Skip the NUL at the end of the table. */
4628 if (data < end)
4629 data++;
4630
4631 /* Display the contents of the File Name table. */
4632 if (data >= end || *data == 0)
4633 printf (_("\n The File Name Table is empty.\n"));
4634 else
4635 {
4636 printf (_("\n The File Name Table (offset %#tx):\n"),
4637 data - start);
4638 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
4639
4640 while (data < end && *data != 0)
4641 {
4642 unsigned char *name;
4643 uint64_t val;
4644
4645 printf (" %d\t", ++state_machine_regs.last_file_entry);
4646 name = data;
4647 data += strnlen ((char *) data, end - data);
4648 if (data < end)
4649 data++;
4650
4651 READ_ULEB (val, data, end);
4652 printf ("%" PRIu64 "\t", val);
4653 READ_ULEB (val, data, end);
4654 printf ("%" PRIu64 "\t", val);
4655 READ_ULEB (val, data, end);
4656 printf ("%" PRIu64 "\t", val);
4657 printf ("%.*s\n", (int)(end - name), name);
4658
4659 if (data >= end)
4660 {
4661 warn (_("Corrupt file name table entry\n"));
4662 break;
4663 }
4664 }
4665 }
4666
4667 /* Skip the NUL at the end of the table. */
4668 if (data < end)
4669 data++;
4670 }
4671
4672 putchar ('\n');
4673 saved_linfo = linfo;
4674 }
4675
4676 /* Now display the statements. */
4677 if (data >= end_of_sequence)
4678 printf (_(" No Line Number Statements.\n"));
4679 else
4680 {
4681 printf (_(" Line Number Statements:\n"));
4682
4683 while (data < end_of_sequence)
4684 {
4685 unsigned char op_code;
4686 int adv;
4687 uint64_t uladv;
4688
4689 printf (" [0x%08tx]", data - start);
4690
4691 op_code = *data++;
4692
4693 if (op_code >= linfo.li_opcode_base)
4694 {
4695 op_code -= linfo.li_opcode_base;
4696 uladv = (op_code / linfo.li_line_range);
4697 if (linfo.li_max_ops_per_insn == 1)
4698 {
4699 uladv *= linfo.li_min_insn_length;
4700 state_machine_regs.address += uladv;
4701 if (uladv)
4702 state_machine_regs.view = 0;
4703 printf (_(" Special opcode %d: "
4704 "advance Address by %" PRIu64
4705 " to %#" PRIx64 "%s"),
4706 op_code, uladv, state_machine_regs.address,
4707 verbose_view && uladv
4708 ? _(" (reset view)") : "");
4709 }
4710 else
4711 {
4712 unsigned addrdelta
4713 = ((state_machine_regs.op_index + uladv)
4714 / linfo.li_max_ops_per_insn)
4715 * linfo.li_min_insn_length;
4716
4717 state_machine_regs.address += addrdelta;
4718 state_machine_regs.op_index
4719 = (state_machine_regs.op_index + uladv)
4720 % linfo.li_max_ops_per_insn;
4721 if (addrdelta)
4722 state_machine_regs.view = 0;
4723 printf (_(" Special opcode %d: "
4724 "advance Address by %" PRIu64
4725 " to %#" PRIx64 "[%d]%s"),
4726 op_code, uladv, state_machine_regs.address,
4727 state_machine_regs.op_index,
4728 verbose_view && addrdelta
4729 ? _(" (reset view)") : "");
4730 }
4731 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4732 state_machine_regs.line += adv;
4733 printf (_(" and Line by %d to %d"),
4734 adv, state_machine_regs.line);
4735 if (verbose_view || state_machine_regs.view)
4736 printf (_(" (view %u)\n"), state_machine_regs.view);
4737 else
4738 putchar ('\n');
4739 state_machine_regs.view++;
4740 }
4741 else
4742 switch (op_code)
4743 {
4744 case DW_LNS_extended_op:
4745 data += process_extended_line_op (data,
4746 linfo.li_default_is_stmt,
4747 end);
4748 break;
4749
4750 case DW_LNS_copy:
4751 printf (_(" Copy"));
4752 if (verbose_view || state_machine_regs.view)
4753 printf (_(" (view %u)\n"), state_machine_regs.view);
4754 else
4755 putchar ('\n');
4756 state_machine_regs.view++;
4757 break;
4758
4759 case DW_LNS_advance_pc:
4760 READ_ULEB (uladv, data, end);
4761 if (linfo.li_max_ops_per_insn == 1)
4762 {
4763 uladv *= linfo.li_min_insn_length;
4764 state_machine_regs.address += uladv;
4765 if (uladv)
4766 state_machine_regs.view = 0;
4767 printf (_(" Advance PC by %" PRIu64
4768 " to %#" PRIx64 "%s\n"),
4769 uladv, state_machine_regs.address,
4770 verbose_view && uladv
4771 ? _(" (reset view)") : "");
4772 }
4773 else
4774 {
4775 unsigned addrdelta
4776 = ((state_machine_regs.op_index + uladv)
4777 / linfo.li_max_ops_per_insn)
4778 * linfo.li_min_insn_length;
4779 state_machine_regs.address
4780 += addrdelta;
4781 state_machine_regs.op_index
4782 = (state_machine_regs.op_index + uladv)
4783 % linfo.li_max_ops_per_insn;
4784 if (addrdelta)
4785 state_machine_regs.view = 0;
4786 printf (_(" Advance PC by %" PRIu64
4787 " to %#" PRIx64 "[%d]%s\n"),
4788 uladv, state_machine_regs.address,
4789 state_machine_regs.op_index,
4790 verbose_view && addrdelta
4791 ? _(" (reset view)") : "");
4792 }
4793 break;
4794
4795 case DW_LNS_advance_line:
4796 READ_SLEB (adv, data, end);
4797 state_machine_regs.line += adv;
4798 printf (_(" Advance Line by %d to %d\n"),
4799 adv, state_machine_regs.line);
4800 break;
4801
4802 case DW_LNS_set_file:
4803 READ_ULEB (uladv, data, end);
4804 printf (_(" Set File Name to entry %" PRIu64
4805 " in the File Name Table\n"), uladv);
4806 state_machine_regs.file = uladv;
4807 break;
4808
4809 case DW_LNS_set_column:
4810 READ_ULEB (uladv, data, end);
4811 printf (_(" Set column to %" PRIu64 "\n"), uladv);
4812 state_machine_regs.column = uladv;
4813 break;
4814
4815 case DW_LNS_negate_stmt:
4816 adv = state_machine_regs.is_stmt;
4817 adv = ! adv;
4818 printf (_(" Set is_stmt to %d\n"), adv);
4819 state_machine_regs.is_stmt = adv;
4820 break;
4821
4822 case DW_LNS_set_basic_block:
4823 printf (_(" Set basic block\n"));
4824 state_machine_regs.basic_block = 1;
4825 break;
4826
4827 case DW_LNS_const_add_pc:
4828 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4829 if (linfo.li_max_ops_per_insn)
4830 {
4831 uladv *= linfo.li_min_insn_length;
4832 state_machine_regs.address += uladv;
4833 if (uladv)
4834 state_machine_regs.view = 0;
4835 printf (_(" Advance PC by constant %" PRIu64
4836 " to %#" PRIx64 "%s\n"),
4837 uladv, state_machine_regs.address,
4838 verbose_view && uladv
4839 ? _(" (reset view)") : "");
4840 }
4841 else
4842 {
4843 unsigned addrdelta
4844 = ((state_machine_regs.op_index + uladv)
4845 / linfo.li_max_ops_per_insn)
4846 * linfo.li_min_insn_length;
4847 state_machine_regs.address
4848 += addrdelta;
4849 state_machine_regs.op_index
4850 = (state_machine_regs.op_index + uladv)
4851 % linfo.li_max_ops_per_insn;
4852 if (addrdelta)
4853 state_machine_regs.view = 0;
4854 printf (_(" Advance PC by constant %" PRIu64
4855 " to %#" PRIx64 "[%d]%s\n"),
4856 uladv, state_machine_regs.address,
4857 state_machine_regs.op_index,
4858 verbose_view && addrdelta
4859 ? _(" (reset view)") : "");
4860 }
4861 break;
4862
4863 case DW_LNS_fixed_advance_pc:
4864 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4865 state_machine_regs.address += uladv;
4866 state_machine_regs.op_index = 0;
4867 printf (_(" Advance PC by fixed size amount %" PRIu64
4868 " to %#" PRIx64 "\n"),
4869 uladv, state_machine_regs.address);
4870 /* Do NOT reset view. */
4871 break;
4872
4873 case DW_LNS_set_prologue_end:
4874 printf (_(" Set prologue_end to true\n"));
4875 break;
4876
4877 case DW_LNS_set_epilogue_begin:
4878 printf (_(" Set epilogue_begin to true\n"));
4879 break;
4880
4881 case DW_LNS_set_isa:
4882 READ_ULEB (uladv, data, end);
4883 printf (_(" Set ISA to %" PRIu64 "\n"), uladv);
4884 break;
4885
4886 default:
4887 printf (_(" Unknown opcode %d with operands: "), op_code);
4888
4889 if (standard_opcodes != NULL)
4890 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4891 {
4892 READ_ULEB (uladv, data, end);
4893 printf ("%#" PRIx64 "%s", uladv, i == 1 ? "" : ", ");
4894 }
4895 putchar ('\n');
4896 break;
4897 }
4898 }
4899 putchar ('\n');
4900 }
4901 }
4902
4903 return 1;
4904 }
4905
4906 typedef struct
4907 {
4908 char *name;
4909 unsigned int directory_index;
4910 unsigned int modification_date;
4911 unsigned int length;
4912 } File_Entry;
4913
4914 /* Output a decoded representation of the .debug_line section. */
4915
4916 static int
4917 display_debug_lines_decoded (struct dwarf_section * section,
4918 unsigned char * start,
4919 unsigned char * data,
4920 unsigned char * end,
4921 void * fileptr)
4922 {
4923 static DWARF2_Internal_LineInfo saved_linfo;
4924
4925 introduce (section, false);
4926
4927 while (data < end)
4928 {
4929 /* This loop amounts to one iteration per compilation unit. */
4930 DWARF2_Internal_LineInfo linfo;
4931 unsigned char *standard_opcodes;
4932 unsigned char *end_of_sequence;
4933 int i;
4934 File_Entry *file_table = NULL;
4935 unsigned int n_files = 0;
4936 char **directory_table = NULL;
4937 unsigned int n_directories = 0;
4938
4939 if (startswith (section->name, ".debug_line.")
4940 /* Note: the following does not apply to .debug_line.dwo sections.
4941 These are full debug_line sections. */
4942 && strcmp (section->name, ".debug_line.dwo") != 0)
4943 {
4944 /* See comment in display_debug_lines_raw(). */
4945 end_of_sequence = end;
4946 standard_opcodes = NULL;
4947 linfo = saved_linfo;
4948 /* PR 17531: file: 0522b371. */
4949 if (linfo.li_line_range == 0)
4950 {
4951 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4952 return 0;
4953 }
4954 reset_state_machine (linfo.li_default_is_stmt);
4955 }
4956 else
4957 {
4958 unsigned char *hdrptr;
4959
4960 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4961 & end_of_sequence)) == NULL)
4962 return 0;
4963
4964 /* PR 17531: file: 0522b371. */
4965 if (linfo.li_line_range == 0)
4966 {
4967 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4968 linfo.li_line_range = 1;
4969 }
4970 reset_state_machine (linfo.li_default_is_stmt);
4971
4972 /* Save a pointer to the contents of the Opcodes table. */
4973 standard_opcodes = hdrptr;
4974
4975 /* Traverse the Directory table just to count entries. */
4976 data = standard_opcodes + linfo.li_opcode_base - 1;
4977 /* PR 20440 */
4978 if (data >= end)
4979 {
4980 warn (_("opcode base of %d extends beyond end of section\n"),
4981 linfo.li_opcode_base);
4982 return 0;
4983 }
4984
4985 if (linfo.li_version >= 5)
4986 {
4987 unsigned char *format_start, *format;
4988 unsigned int format_count, formati, entryi;
4989
4990 load_debug_section_with_follow (line_str, fileptr);
4991
4992 /* Skip directories format. */
4993 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4994 if (do_checks && format_count > 1)
4995 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
4996 format_count);
4997 format_start = data;
4998 for (formati = 0; formati < format_count; formati++)
4999 {
5000 SKIP_ULEB (data, end);
5001 SKIP_ULEB (data, end);
5002 }
5003
5004 READ_ULEB (n_directories, data, end);
5005 if (data >= end)
5006 {
5007 warn (_("Corrupt directories list\n"));
5008 break;
5009 }
5010
5011 if (n_directories == 0)
5012 directory_table = NULL;
5013 else if (n_directories > section->size)
5014 {
5015 warn (_("number of directories (0x%x) exceeds size of section %s\n"),
5016 n_directories, section->name);
5017 return 0;
5018 }
5019 else
5020 directory_table = (char **)
5021 xcalloc (n_directories, sizeof (unsigned char *));
5022
5023 for (entryi = 0; entryi < n_directories; entryi++)
5024 {
5025 char **pathp = &directory_table[entryi];
5026
5027 format = format_start;
5028 for (formati = 0; formati < format_count; formati++)
5029 {
5030 uint64_t content_type, form;
5031 uint64_t uvalue;
5032
5033 READ_ULEB (content_type, format, end);
5034 READ_ULEB (form, format, end);
5035 if (data >= end)
5036 {
5037 warn (_("Corrupt directories list\n"));
5038 break;
5039 }
5040 switch (content_type)
5041 {
5042 case DW_LNCT_path:
5043 switch (form)
5044 {
5045 case DW_FORM_string:
5046 *pathp = (char *) data;
5047 break;
5048 case DW_FORM_line_strp:
5049 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5050 end);
5051 /* Remove const by the cast. */
5052 *pathp = (char *)
5053 fetch_indirect_line_string (uvalue);
5054 break;
5055 }
5056 break;
5057 }
5058 data = read_and_display_attr_value (0, form, 0, start,
5059 data, end, 0, 0,
5060 linfo.li_offset_size,
5061 linfo.li_version,
5062 NULL, 1, section,
5063 NULL, '\t', -1);
5064 }
5065 if (data >= end)
5066 {
5067 warn (_("Corrupt directories list\n"));
5068 break;
5069 }
5070 }
5071
5072 /* Skip files format. */
5073 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5074 if (do_checks && format_count > 5)
5075 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5076 format_count);
5077
5078 format_start = data;
5079 for (formati = 0; formati < format_count; formati++)
5080 {
5081 SKIP_ULEB (data, end);
5082 SKIP_ULEB (data, end);
5083 }
5084
5085 READ_ULEB (n_files, data, end);
5086 if (data >= end && n_files > 0)
5087 {
5088 warn (_("Corrupt file name list\n"));
5089 break;
5090 }
5091
5092 if (n_files == 0)
5093 file_table = NULL;
5094 else if (n_files > section->size)
5095 {
5096 warn (_("number of files (0x%x) exceeds size of section %s\n"),
5097 n_files, section->name);
5098 return 0;
5099 }
5100 else
5101 file_table = (File_Entry *) xcalloc (n_files,
5102 sizeof (File_Entry));
5103
5104 for (entryi = 0; entryi < n_files; entryi++)
5105 {
5106 File_Entry *file = &file_table[entryi];
5107
5108 format = format_start;
5109 for (formati = 0; formati < format_count; formati++)
5110 {
5111 uint64_t content_type, form;
5112 uint64_t uvalue;
5113 unsigned char *tmp;
5114
5115 READ_ULEB (content_type, format, end);
5116 READ_ULEB (form, format, end);
5117 if (data >= end)
5118 {
5119 warn (_("Corrupt file name list\n"));
5120 break;
5121 }
5122 switch (content_type)
5123 {
5124 case DW_LNCT_path:
5125 switch (form)
5126 {
5127 case DW_FORM_string:
5128 file->name = (char *) data;
5129 break;
5130 case DW_FORM_line_strp:
5131 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5132 end);
5133 /* Remove const by the cast. */
5134 file->name = (char *)
5135 fetch_indirect_line_string (uvalue);
5136 break;
5137 }
5138 break;
5139 case DW_LNCT_directory_index:
5140 switch (form)
5141 {
5142 case DW_FORM_data1:
5143 SAFE_BYTE_GET (file->directory_index, data, 1,
5144 end);
5145 break;
5146 case DW_FORM_data2:
5147 SAFE_BYTE_GET (file->directory_index, data, 2,
5148 end);
5149 break;
5150 case DW_FORM_udata:
5151 tmp = data;
5152 READ_ULEB (file->directory_index, tmp, end);
5153 break;
5154 }
5155 break;
5156 }
5157 data = read_and_display_attr_value (0, form, 0, start,
5158 data, end, 0, 0,
5159 linfo.li_offset_size,
5160 linfo.li_version,
5161 NULL, 1, section,
5162 NULL, '\t', -1);
5163 }
5164 if (data >= end)
5165 {
5166 warn (_("Corrupt file name list\n"));
5167 break;
5168 }
5169 }
5170 }
5171 else
5172 {
5173 if (*data != 0)
5174 {
5175 char *ptr_directory_table = (char *) data;
5176
5177 while (data < end && *data != 0)
5178 {
5179 data += strnlen ((char *) data, end - data);
5180 if (data < end)
5181 data++;
5182 n_directories++;
5183 }
5184
5185 /* PR 20440 */
5186 if (data >= end)
5187 {
5188 warn (_("directory table ends unexpectedly\n"));
5189 n_directories = 0;
5190 break;
5191 }
5192
5193 /* Go through the directory table again to save the directories. */
5194 directory_table = (char **)
5195 xmalloc (n_directories * sizeof (unsigned char *));
5196
5197 i = 0;
5198 while (*ptr_directory_table != 0)
5199 {
5200 directory_table[i] = ptr_directory_table;
5201 ptr_directory_table += strlen (ptr_directory_table) + 1;
5202 i++;
5203 }
5204 }
5205 /* Skip the NUL at the end of the table. */
5206 data++;
5207
5208 /* Traverse the File Name table just to count the entries. */
5209 if (data < end && *data != 0)
5210 {
5211 unsigned char *ptr_file_name_table = data;
5212
5213 while (data < end && *data != 0)
5214 {
5215 /* Skip Name, directory index, last modification
5216 time and length of file. */
5217 data += strnlen ((char *) data, end - data);
5218 if (data < end)
5219 data++;
5220 SKIP_ULEB (data, end);
5221 SKIP_ULEB (data, end);
5222 SKIP_ULEB (data, end);
5223 n_files++;
5224 }
5225
5226 if (data >= end)
5227 {
5228 warn (_("file table ends unexpectedly\n"));
5229 n_files = 0;
5230 break;
5231 }
5232
5233 /* Go through the file table again to save the strings. */
5234 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5235
5236 i = 0;
5237 while (*ptr_file_name_table != 0)
5238 {
5239 file_table[i].name = (char *) ptr_file_name_table;
5240 ptr_file_name_table
5241 += strlen ((char *) ptr_file_name_table) + 1;
5242
5243 /* We are not interested in directory, time or size. */
5244 READ_ULEB (file_table[i].directory_index,
5245 ptr_file_name_table, end);
5246 READ_ULEB (file_table[i].modification_date,
5247 ptr_file_name_table, end);
5248 READ_ULEB (file_table[i].length,
5249 ptr_file_name_table, end);
5250 i++;
5251 }
5252 i = 0;
5253 }
5254
5255 /* Skip the NUL at the end of the table. */
5256 data++;
5257 }
5258
5259 /* Print the Compilation Unit's name and a header. */
5260 if (file_table == NULL)
5261 printf (_("CU: No directory table\n"));
5262 else if (directory_table == NULL)
5263 printf (_("CU: %s:\n"), null_name (file_table[0].name));
5264 else
5265 {
5266 unsigned int ix = file_table[0].directory_index;
5267 const char *directory;
5268
5269 if (ix == 0 && linfo.li_version < 5)
5270 directory = ".";
5271 /* PR 20439 */
5272 else if (n_directories == 0)
5273 directory = _("<unknown>");
5274 else
5275 {
5276 if (linfo.li_version < 5)
5277 --ix;
5278 if (ix >= n_directories)
5279 {
5280 warn (_("directory index %u "
5281 ">= number of directories %u\n"),
5282 ix, n_directories);
5283 directory = _("<corrupt>");
5284 }
5285 else
5286 directory = directory_table[ix];
5287 }
5288 if (do_wide)
5289 printf (_("CU: %s/%s:\n"),
5290 null_name (directory),
5291 null_name (file_table[0].name));
5292 else
5293 printf ("%s:\n", null_name (file_table[0].name));
5294 }
5295
5296 if (n_files > 0)
5297 {
5298 if (do_wide)
5299 printf (_("File name Line number Starting address View Stmt\n"));
5300 else
5301 printf (_("File name Line number Starting address View Stmt\n"));
5302 }
5303 else
5304 printf (_("CU: Empty file name table\n"));
5305 saved_linfo = linfo;
5306 }
5307
5308 /* This loop iterates through the Dwarf Line Number Program. */
5309 while (data < end_of_sequence)
5310 {
5311 unsigned char op_code;
5312 int xop;
5313 int adv;
5314 unsigned long int uladv;
5315 int is_special_opcode = 0;
5316
5317 op_code = *data++;
5318 xop = op_code;
5319
5320 if (op_code >= linfo.li_opcode_base)
5321 {
5322 op_code -= linfo.li_opcode_base;
5323 uladv = (op_code / linfo.li_line_range);
5324 if (linfo.li_max_ops_per_insn == 1)
5325 {
5326 uladv *= linfo.li_min_insn_length;
5327 state_machine_regs.address += uladv;
5328 if (uladv)
5329 state_machine_regs.view = 0;
5330 }
5331 else
5332 {
5333 unsigned addrdelta
5334 = ((state_machine_regs.op_index + uladv)
5335 / linfo.li_max_ops_per_insn)
5336 * linfo.li_min_insn_length;
5337 state_machine_regs.address
5338 += addrdelta;
5339 state_machine_regs.op_index
5340 = (state_machine_regs.op_index + uladv)
5341 % linfo.li_max_ops_per_insn;
5342 if (addrdelta)
5343 state_machine_regs.view = 0;
5344 }
5345
5346 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5347 state_machine_regs.line += adv;
5348 is_special_opcode = 1;
5349 /* Increment view after printing this row. */
5350 }
5351 else
5352 switch (op_code)
5353 {
5354 case DW_LNS_extended_op:
5355 {
5356 unsigned int ext_op_code_len;
5357 unsigned char ext_op_code;
5358 unsigned char *op_code_end;
5359 unsigned char *op_code_data = data;
5360
5361 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5362 op_code_end = op_code_data + ext_op_code_len;
5363 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5364 {
5365 warn (_("Badly formed extended line op encountered!\n"));
5366 break;
5367 }
5368 ext_op_code = *op_code_data++;
5369 xop = ext_op_code;
5370 xop = -xop;
5371
5372 switch (ext_op_code)
5373 {
5374 case DW_LNE_end_sequence:
5375 /* Reset stuff after printing this row. */
5376 break;
5377 case DW_LNE_set_address:
5378 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5379 op_code_data,
5380 op_code_end - op_code_data,
5381 op_code_end);
5382 state_machine_regs.op_index = 0;
5383 state_machine_regs.view = 0;
5384 break;
5385 case DW_LNE_define_file:
5386 file_table = (File_Entry *) xrealloc
5387 (file_table, (n_files + 1) * sizeof (File_Entry));
5388
5389 ++state_machine_regs.last_file_entry;
5390 /* Source file name. */
5391 file_table[n_files].name = (char *) op_code_data;
5392 op_code_data += strlen ((char *) op_code_data) + 1;
5393 /* Directory index. */
5394 READ_ULEB (file_table[n_files].directory_index,
5395 op_code_data, op_code_end);
5396 /* Last modification time. */
5397 READ_ULEB (file_table[n_files].modification_date,
5398 op_code_data, op_code_end);
5399 /* File length. */
5400 READ_ULEB (file_table[n_files].length,
5401 op_code_data, op_code_end);
5402 n_files++;
5403 break;
5404
5405 case DW_LNE_set_discriminator:
5406 case DW_LNE_HP_set_sequence:
5407 /* Simply ignored. */
5408 break;
5409
5410 default:
5411 printf (_("UNKNOWN (%u): length %ld\n"),
5412 ext_op_code, (long int) (op_code_data - data));
5413 break;
5414 }
5415 data = op_code_end;
5416 break;
5417 }
5418 case DW_LNS_copy:
5419 /* Increment view after printing this row. */
5420 break;
5421
5422 case DW_LNS_advance_pc:
5423 READ_ULEB (uladv, data, end);
5424 if (linfo.li_max_ops_per_insn == 1)
5425 {
5426 uladv *= linfo.li_min_insn_length;
5427 state_machine_regs.address += uladv;
5428 if (uladv)
5429 state_machine_regs.view = 0;
5430 }
5431 else
5432 {
5433 unsigned addrdelta
5434 = ((state_machine_regs.op_index + uladv)
5435 / linfo.li_max_ops_per_insn)
5436 * linfo.li_min_insn_length;
5437 state_machine_regs.address
5438 += addrdelta;
5439 state_machine_regs.op_index
5440 = (state_machine_regs.op_index + uladv)
5441 % linfo.li_max_ops_per_insn;
5442 if (addrdelta)
5443 state_machine_regs.view = 0;
5444 }
5445 break;
5446
5447 case DW_LNS_advance_line:
5448 READ_SLEB (adv, data, end);
5449 state_machine_regs.line += adv;
5450 break;
5451
5452 case DW_LNS_set_file:
5453 READ_ULEB (uladv, data, end);
5454 state_machine_regs.file = uladv;
5455
5456 unsigned file = state_machine_regs.file;
5457 if (linfo.li_version < 5)
5458 --file;
5459
5460 if (file_table == NULL || n_files == 0)
5461 printf (_("\n [Use file table entry %d]\n"), file);
5462 /* PR 20439 */
5463 else if (file >= n_files)
5464 {
5465 warn (_("file index %u >= number of files %u\n"),
5466 file, n_files);
5467 printf (_("\n <over large file table index %u>"), file);
5468 }
5469 else
5470 {
5471 unsigned dir = file_table[file].directory_index;
5472 if (dir == 0 && linfo.li_version < 5)
5473 /* If directory index is 0, that means compilation
5474 current directory. bfd/dwarf2.c shows
5475 DW_AT_comp_dir here but in keeping with the
5476 readelf practice of minimal interpretation of
5477 file data, we show "./". */
5478 printf ("\n./%s:[++]\n",
5479 null_name (file_table[file].name));
5480 else if (directory_table == NULL || n_directories == 0)
5481 printf (_("\n [Use file %s "
5482 "in directory table entry %d]\n"),
5483 null_name (file_table[file].name), dir);
5484 else
5485 {
5486 if (linfo.li_version < 5)
5487 --dir;
5488 /* PR 20439 */
5489 if (dir >= n_directories)
5490 {
5491 warn (_("directory index %u "
5492 ">= number of directories %u\n"),
5493 dir, n_directories);
5494 printf (_("\n <over large directory table entry "
5495 "%u>\n"), dir);
5496 }
5497 else
5498 printf ("\n%s/%s:\n",
5499 null_name (directory_table[dir]),
5500 null_name (file_table[file].name));
5501 }
5502 }
5503 break;
5504
5505 case DW_LNS_set_column:
5506 READ_ULEB (uladv, data, end);
5507 state_machine_regs.column = uladv;
5508 break;
5509
5510 case DW_LNS_negate_stmt:
5511 adv = state_machine_regs.is_stmt;
5512 adv = ! adv;
5513 state_machine_regs.is_stmt = adv;
5514 break;
5515
5516 case DW_LNS_set_basic_block:
5517 state_machine_regs.basic_block = 1;
5518 break;
5519
5520 case DW_LNS_const_add_pc:
5521 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5522 if (linfo.li_max_ops_per_insn == 1)
5523 {
5524 uladv *= linfo.li_min_insn_length;
5525 state_machine_regs.address += uladv;
5526 if (uladv)
5527 state_machine_regs.view = 0;
5528 }
5529 else
5530 {
5531 unsigned addrdelta
5532 = ((state_machine_regs.op_index + uladv)
5533 / linfo.li_max_ops_per_insn)
5534 * linfo.li_min_insn_length;
5535 state_machine_regs.address
5536 += addrdelta;
5537 state_machine_regs.op_index
5538 = (state_machine_regs.op_index + uladv)
5539 % linfo.li_max_ops_per_insn;
5540 if (addrdelta)
5541 state_machine_regs.view = 0;
5542 }
5543 break;
5544
5545 case DW_LNS_fixed_advance_pc:
5546 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5547 state_machine_regs.address += uladv;
5548 state_machine_regs.op_index = 0;
5549 /* Do NOT reset view. */
5550 break;
5551
5552 case DW_LNS_set_prologue_end:
5553 break;
5554
5555 case DW_LNS_set_epilogue_begin:
5556 break;
5557
5558 case DW_LNS_set_isa:
5559 READ_ULEB (uladv, data, end);
5560 printf (_(" Set ISA to %lu\n"), uladv);
5561 break;
5562
5563 default:
5564 printf (_(" Unknown opcode %d with operands: "), op_code);
5565
5566 if (standard_opcodes != NULL)
5567 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5568 {
5569 uint64_t val;
5570
5571 READ_ULEB (val, data, end);
5572 printf ("%#" PRIx64 "%s", val, i == 1 ? "" : ", ");
5573 }
5574 putchar ('\n');
5575 break;
5576 }
5577
5578 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5579 to the DWARF address/line matrix. */
5580 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5581 || (xop == DW_LNS_copy))
5582 {
5583 const unsigned int MAX_FILENAME_LENGTH = 35;
5584 char *fileName = NULL;
5585 char *newFileName = NULL;
5586 size_t fileNameLength;
5587
5588 if (file_table)
5589 {
5590 unsigned indx = state_machine_regs.file;
5591
5592 if (linfo.li_version < 5)
5593 --indx;
5594 /* PR 20439 */
5595 if (indx >= n_files)
5596 {
5597 warn (_("file index %u >= number of files %u\n"),
5598 indx, n_files);
5599 fileName = _("<corrupt>");
5600 }
5601 else
5602 fileName = (char *) file_table[indx].name;
5603 }
5604 if (!fileName)
5605 fileName = _("<unknown>");
5606
5607 fileNameLength = strlen (fileName);
5608 newFileName = fileName;
5609 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5610 {
5611 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5612 /* Truncate file name */
5613 memcpy (newFileName,
5614 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5615 MAX_FILENAME_LENGTH);
5616 newFileName[MAX_FILENAME_LENGTH] = 0;
5617 }
5618
5619 /* A row with end_seq set to true has a meaningful address, but
5620 the other information in the same row is not significant.
5621 In such a row, print line as "-", and don't print
5622 view/is_stmt. */
5623 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5624 {
5625 if (linfo.li_max_ops_per_insn == 1)
5626 {
5627 if (xop == -DW_LNE_end_sequence)
5628 printf ("%-31s %11s %#18" PRIx64,
5629 newFileName, "-",
5630 state_machine_regs.address);
5631 else
5632 printf ("%-31s %11d %#18" PRIx64,
5633 newFileName, state_machine_regs.line,
5634 state_machine_regs.address);
5635 }
5636 else
5637 {
5638 if (xop == -DW_LNE_end_sequence)
5639 printf ("%-31s %11s %#18" PRIx64 "[%d]",
5640 newFileName, "-",
5641 state_machine_regs.address,
5642 state_machine_regs.op_index);
5643 else
5644 printf ("%-31s %11d %#18" PRIx64 "[%d]",
5645 newFileName, state_machine_regs.line,
5646 state_machine_regs.address,
5647 state_machine_regs.op_index);
5648 }
5649 }
5650 else
5651 {
5652 if (linfo.li_max_ops_per_insn == 1)
5653 {
5654 if (xop == -DW_LNE_end_sequence)
5655 printf ("%s %11s %#18" PRIx64,
5656 newFileName, "-",
5657 state_machine_regs.address);
5658 else
5659 printf ("%s %11d %#18" PRIx64,
5660 newFileName, state_machine_regs.line,
5661 state_machine_regs.address);
5662 }
5663 else
5664 {
5665 if (xop == -DW_LNE_end_sequence)
5666 printf ("%s %11s %#18" PRIx64 "[%d]",
5667 newFileName, "-",
5668 state_machine_regs.address,
5669 state_machine_regs.op_index);
5670 else
5671 printf ("%s %11d %#18" PRIx64 "[%d]",
5672 newFileName, state_machine_regs.line,
5673 state_machine_regs.address,
5674 state_machine_regs.op_index);
5675 }
5676 }
5677
5678 if (xop != -DW_LNE_end_sequence)
5679 {
5680 if (state_machine_regs.view)
5681 printf (" %6u", state_machine_regs.view);
5682 else
5683 printf (" ");
5684
5685 if (state_machine_regs.is_stmt)
5686 printf (" x");
5687 }
5688
5689 putchar ('\n');
5690 state_machine_regs.view++;
5691
5692 if (xop == -DW_LNE_end_sequence)
5693 {
5694 reset_state_machine (linfo.li_default_is_stmt);
5695 putchar ('\n');
5696 }
5697
5698 if (newFileName != fileName)
5699 free (newFileName);
5700 }
5701 }
5702
5703 if (file_table)
5704 {
5705 free (file_table);
5706 file_table = NULL;
5707 n_files = 0;
5708 }
5709
5710 if (directory_table)
5711 {
5712 free (directory_table);
5713 directory_table = NULL;
5714 n_directories = 0;
5715 }
5716
5717 putchar ('\n');
5718 }
5719
5720 return 1;
5721 }
5722
5723 static int
5724 display_debug_lines (struct dwarf_section *section, void *file)
5725 {
5726 unsigned char *data = section->start;
5727 unsigned char *end = data + section->size;
5728 int retValRaw = 1;
5729 int retValDecoded = 1;
5730
5731 if (do_debug_lines == 0)
5732 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5733
5734 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5735 retValRaw = display_debug_lines_raw (section, data, end, file);
5736
5737 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5738 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5739
5740 if (!retValRaw || !retValDecoded)
5741 return 0;
5742
5743 return 1;
5744 }
5745
5746 static debug_info *
5747 find_debug_info_for_offset (uint64_t offset)
5748 {
5749 unsigned int i;
5750
5751 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5752 return NULL;
5753
5754 for (i = 0; i < num_debug_info_entries; i++)
5755 if (debug_information[i].cu_offset == offset)
5756 return debug_information + i;
5757
5758 return NULL;
5759 }
5760
5761 static const char *
5762 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5763 {
5764 /* See gdb/gdb-index.h. */
5765 static const char * const kinds[] =
5766 {
5767 N_ ("no info"),
5768 N_ ("type"),
5769 N_ ("variable"),
5770 N_ ("function"),
5771 N_ ("other"),
5772 N_ ("unused5"),
5773 N_ ("unused6"),
5774 N_ ("unused7")
5775 };
5776
5777 return _ (kinds[kind]);
5778 }
5779
5780 static int
5781 display_debug_pubnames_worker (struct dwarf_section *section,
5782 void *file ATTRIBUTE_UNUSED,
5783 int is_gnu)
5784 {
5785 DWARF2_Internal_PubNames names;
5786 unsigned char *start = section->start;
5787 unsigned char *end = start + section->size;
5788
5789 /* It does not matter if this load fails,
5790 we test for that later on. */
5791 load_debug_info (file);
5792
5793 introduce (section, false);
5794
5795 while (start < end)
5796 {
5797 unsigned char *data;
5798 unsigned long sec_off = start - section->start;
5799 unsigned int offset_size;
5800
5801 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5802 if (names.pn_length == 0xffffffff)
5803 {
5804 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5805 offset_size = 8;
5806 }
5807 else
5808 offset_size = 4;
5809
5810 if (names.pn_length > (size_t) (end - start))
5811 {
5812 warn (_("Debug info is corrupted, "
5813 "%s header at %#lx has length %#" PRIx64 "\n"),
5814 section->name, sec_off, names.pn_length);
5815 break;
5816 }
5817
5818 data = start;
5819 start += names.pn_length;
5820
5821 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
5822 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
5823
5824 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5825 && num_debug_info_entries > 0
5826 && find_debug_info_for_offset (names.pn_offset) == NULL)
5827 warn (_(".debug_info offset of %#" PRIx64
5828 " in %s section does not point to a CU header.\n"),
5829 names.pn_offset, section->name);
5830
5831 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
5832
5833 printf (_(" Length: %" PRId64 "\n"),
5834 names.pn_length);
5835 printf (_(" Version: %d\n"),
5836 names.pn_version);
5837 printf (_(" Offset into .debug_info section: %#" PRIx64 "\n"),
5838 names.pn_offset);
5839 printf (_(" Size of area in .debug_info section: %" PRId64 "\n"),
5840 names.pn_size);
5841
5842 if (names.pn_version != 2 && names.pn_version != 3)
5843 {
5844 static int warned = 0;
5845
5846 if (! warned)
5847 {
5848 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5849 warned = 1;
5850 }
5851
5852 continue;
5853 }
5854
5855 if (is_gnu)
5856 printf (_("\n Offset Kind Name\n"));
5857 else
5858 printf (_("\n Offset\tName\n"));
5859
5860 while (1)
5861 {
5862 size_t maxprint;
5863 uint64_t offset;
5864
5865 SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
5866
5867 if (offset == 0)
5868 break;
5869
5870 if (data >= start)
5871 break;
5872 maxprint = (start - data) - 1;
5873
5874 if (is_gnu)
5875 {
5876 unsigned int kind_data;
5877 gdb_index_symbol_kind kind;
5878 const char *kind_name;
5879 int is_static;
5880
5881 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
5882 maxprint --;
5883 /* GCC computes the kind as the upper byte in the CU index
5884 word, and then right shifts it by the CU index size.
5885 Left shift KIND to where the gdb-index.h accessor macros
5886 can use it. */
5887 kind_data <<= GDB_INDEX_CU_BITSIZE;
5888 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5889 kind_name = get_gdb_index_symbol_kind_name (kind);
5890 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5891 printf (" %-6" PRIx64 " %s,%-10s %.*s\n",
5892 offset, is_static ? _("s") : _("g"),
5893 kind_name, (int) maxprint, data);
5894 }
5895 else
5896 printf (" %-6" PRIx64 "\t%.*s\n",
5897 offset, (int) maxprint, data);
5898
5899 data += strnlen ((char *) data, maxprint);
5900 if (data < start)
5901 data++;
5902 if (data >= start)
5903 break;
5904 }
5905 }
5906
5907 printf ("\n");
5908 return 1;
5909 }
5910
5911 static int
5912 display_debug_pubnames (struct dwarf_section *section, void *file)
5913 {
5914 return display_debug_pubnames_worker (section, file, 0);
5915 }
5916
5917 static int
5918 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5919 {
5920 return display_debug_pubnames_worker (section, file, 1);
5921 }
5922
5923 static int
5924 display_debug_macinfo (struct dwarf_section *section,
5925 void *file ATTRIBUTE_UNUSED)
5926 {
5927 unsigned char *start = section->start;
5928 unsigned char *end = start + section->size;
5929 unsigned char *curr = start;
5930 enum dwarf_macinfo_record_type op;
5931
5932 introduce (section, false);
5933
5934 while (curr < end)
5935 {
5936 unsigned int lineno;
5937 const unsigned char *string;
5938
5939 op = (enum dwarf_macinfo_record_type) *curr;
5940 curr++;
5941
5942 switch (op)
5943 {
5944 case DW_MACINFO_start_file:
5945 {
5946 unsigned int filenum;
5947
5948 READ_ULEB (lineno, curr, end);
5949 READ_ULEB (filenum, curr, end);
5950 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5951 lineno, filenum);
5952 }
5953 break;
5954
5955 case DW_MACINFO_end_file:
5956 printf (_(" DW_MACINFO_end_file\n"));
5957 break;
5958
5959 case DW_MACINFO_define:
5960 READ_ULEB (lineno, curr, end);
5961 string = curr;
5962 curr += strnlen ((char *) string, end - string);
5963 printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
5964 lineno, (int) (curr - string), string);
5965 if (curr < end)
5966 curr++;
5967 break;
5968
5969 case DW_MACINFO_undef:
5970 READ_ULEB (lineno, curr, end);
5971 string = curr;
5972 curr += strnlen ((char *) string, end - string);
5973 printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
5974 lineno, (int) (curr - string), string);
5975 if (curr < end)
5976 curr++;
5977 break;
5978
5979 case DW_MACINFO_vendor_ext:
5980 {
5981 unsigned int constant;
5982
5983 READ_ULEB (constant, curr, end);
5984 string = curr;
5985 curr += strnlen ((char *) string, end - string);
5986 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
5987 constant, (int) (curr - string), string);
5988 if (curr < end)
5989 curr++;
5990 }
5991 break;
5992 }
5993 }
5994
5995 return 1;
5996 }
5997
5998 /* Given LINE_OFFSET into the .debug_line section, attempt to return
5999 filename and dirname corresponding to file name table entry with index
6000 FILEIDX. Return NULL on failure. */
6001
6002 static unsigned char *
6003 get_line_filename_and_dirname (uint64_t line_offset,
6004 uint64_t fileidx,
6005 unsigned char **dir_name)
6006 {
6007 struct dwarf_section *section = &debug_displays [line].section;
6008 unsigned char *hdrptr, *dirtable, *file_name;
6009 unsigned int offset_size;
6010 unsigned int version, opcode_base;
6011 uint64_t length, diridx;
6012 const unsigned char * end;
6013
6014 *dir_name = NULL;
6015 if (section->start == NULL
6016 || line_offset >= section->size
6017 || fileidx == 0)
6018 return NULL;
6019
6020 hdrptr = section->start + line_offset;
6021 end = section->start + section->size;
6022
6023 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
6024 if (length == 0xffffffff)
6025 {
6026 /* This section is 64-bit DWARF 3. */
6027 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
6028 offset_size = 8;
6029 }
6030 else
6031 offset_size = 4;
6032
6033 if (length > (size_t) (end - hdrptr)
6034 || length < 2 + offset_size + 1 + 3 + 1)
6035 return NULL;
6036 end = hdrptr + length;
6037
6038 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
6039 if (version != 2 && version != 3 && version != 4)
6040 return NULL;
6041 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
6042 if (version >= 4)
6043 hdrptr++; /* Skip max_ops_per_insn. */
6044 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
6045
6046 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
6047 if (opcode_base == 0
6048 || opcode_base - 1 >= (size_t) (end - hdrptr))
6049 return NULL;
6050
6051 hdrptr += opcode_base - 1;
6052
6053 dirtable = hdrptr;
6054 /* Skip over dirname table. */
6055 while (*hdrptr != '\0')
6056 {
6057 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6058 if (hdrptr < end)
6059 hdrptr++;
6060 if (hdrptr >= end)
6061 return NULL;
6062 }
6063 hdrptr++; /* Skip the NUL at the end of the table. */
6064
6065 /* Now skip over preceding filename table entries. */
6066 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
6067 {
6068 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6069 if (hdrptr < end)
6070 hdrptr++;
6071 SKIP_ULEB (hdrptr, end);
6072 SKIP_ULEB (hdrptr, end);
6073 SKIP_ULEB (hdrptr, end);
6074 }
6075 if (hdrptr >= end || *hdrptr == '\0')
6076 return NULL;
6077
6078 file_name = hdrptr;
6079 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6080 if (hdrptr < end)
6081 hdrptr++;
6082 if (hdrptr >= end)
6083 return NULL;
6084 READ_ULEB (diridx, hdrptr, end);
6085 if (diridx == 0)
6086 return file_name;
6087 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
6088 {
6089 dirtable += strnlen ((char *) dirtable, end - dirtable);
6090 if (dirtable < end)
6091 dirtable++;
6092 }
6093 if (dirtable >= end || *dirtable == '\0')
6094 return NULL;
6095 *dir_name = dirtable;
6096 return file_name;
6097 }
6098
6099 static int
6100 display_debug_macro (struct dwarf_section *section,
6101 void *file)
6102 {
6103 unsigned char *start = section->start;
6104 unsigned char *end = start + section->size;
6105 unsigned char *curr = start;
6106 unsigned char *extended_op_buf[256];
6107 bool is_dwo = false;
6108 const char *suffix = strrchr (section->name, '.');
6109
6110 if (suffix && strcmp (suffix, ".dwo") == 0)
6111 is_dwo = true;
6112
6113 load_debug_section_with_follow (str, file);
6114 load_debug_section_with_follow (line, file);
6115 load_debug_section_with_follow (str_index, file);
6116
6117 introduce (section, false);
6118
6119 while (curr < end)
6120 {
6121 unsigned int lineno, version, flags;
6122 unsigned int offset_size;
6123 const unsigned char *string;
6124 uint64_t line_offset = 0, sec_offset = curr - start, offset;
6125 unsigned char **extended_ops = NULL;
6126
6127 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6128 if (version != 4 && version != 5)
6129 {
6130 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6131 section->name, version);
6132 return 0;
6133 }
6134
6135 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
6136 offset_size = (flags & 1) ? 8 : 4;
6137 printf (_(" Offset: %#" PRIx64 "\n"), sec_offset);
6138 printf (_(" Version: %d\n"), version);
6139 printf (_(" Offset size: %d\n"), offset_size);
6140 if (flags & 2)
6141 {
6142 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
6143 printf (_(" Offset into .debug_line: %#" PRIx64 "\n"),
6144 line_offset);
6145 }
6146 if (flags & 4)
6147 {
6148 unsigned int i, count, op;
6149 uint64_t nargs, n;
6150
6151 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
6152
6153 memset (extended_op_buf, 0, sizeof (extended_op_buf));
6154 extended_ops = extended_op_buf;
6155 if (count)
6156 {
6157 printf (_(" Extension opcode arguments:\n"));
6158 for (i = 0; i < count; i++)
6159 {
6160 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6161 extended_ops[op] = curr;
6162 READ_ULEB (nargs, curr, end);
6163 if (nargs == 0)
6164 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
6165 else
6166 {
6167 printf (_(" DW_MACRO_%02x arguments: "), op);
6168 for (n = 0; n < nargs; n++)
6169 {
6170 unsigned int form;
6171
6172 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6173 printf ("%s%s", get_FORM_name (form),
6174 n == nargs - 1 ? "\n" : ", ");
6175 switch (form)
6176 {
6177 case DW_FORM_data1:
6178 case DW_FORM_data2:
6179 case DW_FORM_data4:
6180 case DW_FORM_data8:
6181 case DW_FORM_sdata:
6182 case DW_FORM_udata:
6183 case DW_FORM_block:
6184 case DW_FORM_block1:
6185 case DW_FORM_block2:
6186 case DW_FORM_block4:
6187 case DW_FORM_flag:
6188 case DW_FORM_string:
6189 case DW_FORM_strp:
6190 case DW_FORM_sec_offset:
6191 break;
6192 default:
6193 error (_("Invalid extension opcode form %s\n"),
6194 get_FORM_name (form));
6195 return 0;
6196 }
6197 }
6198 }
6199 }
6200 }
6201 }
6202 printf ("\n");
6203
6204 while (1)
6205 {
6206 unsigned int op;
6207
6208 if (curr >= end)
6209 {
6210 error (_(".debug_macro section not zero terminated\n"));
6211 return 0;
6212 }
6213
6214 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6215 if (op == 0)
6216 break;
6217
6218 switch (op)
6219 {
6220 case DW_MACRO_define:
6221 READ_ULEB (lineno, curr, end);
6222 string = curr;
6223 curr += strnlen ((char *) string, end - string);
6224 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6225 lineno, (int) (curr - string), string);
6226 if (curr < end)
6227 curr++;
6228 break;
6229
6230 case DW_MACRO_undef:
6231 READ_ULEB (lineno, curr, end);
6232 string = curr;
6233 curr += strnlen ((char *) string, end - string);
6234 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6235 lineno, (int) (curr - string), string);
6236 if (curr < end)
6237 curr++;
6238 break;
6239
6240 case DW_MACRO_start_file:
6241 {
6242 unsigned int filenum;
6243 unsigned char *file_name = NULL, *dir_name = NULL;
6244
6245 READ_ULEB (lineno, curr, end);
6246 READ_ULEB (filenum, curr, end);
6247
6248 if ((flags & 2) == 0)
6249 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6250 else
6251 file_name
6252 = get_line_filename_and_dirname (line_offset, filenum,
6253 &dir_name);
6254 if (file_name == NULL)
6255 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6256 lineno, filenum);
6257 else
6258 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6259 lineno, filenum,
6260 dir_name != NULL ? (const char *) dir_name : "",
6261 dir_name != NULL ? "/" : "", file_name);
6262 }
6263 break;
6264
6265 case DW_MACRO_end_file:
6266 printf (_(" DW_MACRO_end_file\n"));
6267 break;
6268
6269 case DW_MACRO_define_strp:
6270 READ_ULEB (lineno, curr, end);
6271 if (version == 4 && is_dwo)
6272 READ_ULEB (offset, curr, end);
6273 else
6274 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6275 string = fetch_indirect_string (offset);
6276 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
6277 lineno, string);
6278 break;
6279
6280 case DW_MACRO_undef_strp:
6281 READ_ULEB (lineno, curr, end);
6282 if (version == 4 && is_dwo)
6283 READ_ULEB (offset, curr, end);
6284 else
6285 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6286 string = fetch_indirect_string (offset);
6287 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6288 lineno, string);
6289 break;
6290
6291 case DW_MACRO_import:
6292 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6293 printf (_(" DW_MACRO_import - offset : %#" PRIx64 "\n"),
6294 offset);
6295 break;
6296
6297 case DW_MACRO_define_sup:
6298 READ_ULEB (lineno, curr, end);
6299 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6300 printf (_(" DW_MACRO_define_sup - lineno : %d"
6301 " macro offset : %#" PRIx64 "\n"),
6302 lineno, offset);
6303 break;
6304
6305 case DW_MACRO_undef_sup:
6306 READ_ULEB (lineno, curr, end);
6307 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6308 printf (_(" DW_MACRO_undef_sup - lineno : %d"
6309 " macro offset : %#" PRIx64 "\n"),
6310 lineno, offset);
6311 break;
6312
6313 case DW_MACRO_import_sup:
6314 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6315 printf (_(" DW_MACRO_import_sup - offset : %#" PRIx64 "\n"),
6316 offset);
6317 break;
6318
6319 case DW_MACRO_define_strx:
6320 case DW_MACRO_undef_strx:
6321 READ_ULEB (lineno, curr, end);
6322 READ_ULEB (offset, curr, end);
6323 string = (const unsigned char *)
6324 fetch_indexed_string (offset, NULL, offset_size, false, 0);
6325 if (op == DW_MACRO_define_strx)
6326 printf (" DW_MACRO_define_strx ");
6327 else
6328 printf (" DW_MACRO_undef_strx ");
6329 if (do_wide)
6330 printf (_("(with offset %#" PRIx64 ") "), offset);
6331 printf (_("lineno : %d macro : %s\n"),
6332 lineno, string);
6333 break;
6334
6335 default:
6336 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6337 {
6338 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6339 break;
6340 }
6341
6342 if (extended_ops == NULL || extended_ops[op] == NULL)
6343 {
6344 error (_(" Unknown macro opcode %02x seen\n"), op);
6345 return 0;
6346 }
6347 else
6348 {
6349 /* Skip over unhandled opcodes. */
6350 uint64_t nargs, n;
6351 unsigned char *desc = extended_ops[op];
6352 READ_ULEB (nargs, desc, end);
6353 if (nargs == 0)
6354 {
6355 printf (_(" DW_MACRO_%02x\n"), op);
6356 break;
6357 }
6358 printf (_(" DW_MACRO_%02x -"), op);
6359 for (n = 0; n < nargs; n++)
6360 {
6361 int val;
6362
6363 /* DW_FORM_implicit_const is not expected here. */
6364 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6365 curr
6366 = read_and_display_attr_value (0, val, 0,
6367 start, curr, end, 0, 0,
6368 offset_size, version,
6369 NULL, 0, section,
6370 NULL, ' ', -1);
6371 if (n != nargs - 1)
6372 printf (",");
6373 }
6374 printf ("\n");
6375 }
6376 break;
6377 }
6378 }
6379
6380 printf ("\n");
6381 }
6382
6383 return 1;
6384 }
6385
6386 static int
6387 display_debug_abbrev (struct dwarf_section *section,
6388 void *file ATTRIBUTE_UNUSED)
6389 {
6390 abbrev_entry *entry;
6391 unsigned char *start = section->start;
6392
6393 introduce (section, false);
6394
6395 do
6396 {
6397 uint64_t offset = start - section->start;
6398 abbrev_list *list = find_and_process_abbrev_set (section, 0,
6399 section->size, offset,
6400 NULL);
6401 if (list == NULL)
6402 break;
6403
6404 if (list->first_abbrev)
6405 printf (_(" Number TAG (%#" PRIx64 ")\n"), offset);
6406
6407 for (entry = list->first_abbrev; entry; entry = entry->next)
6408 {
6409 abbrev_attr *attr;
6410
6411 printf (" %ld %s [%s]\n",
6412 entry->number,
6413 get_TAG_name (entry->tag),
6414 entry->children ? _("has children") : _("no children"));
6415
6416 for (attr = entry->first_attr; attr; attr = attr->next)
6417 {
6418 printf (" %-18s %s",
6419 get_AT_name (attr->attribute),
6420 get_FORM_name (attr->form));
6421 if (attr->form == DW_FORM_implicit_const)
6422 printf (": %" PRId64, attr->implicit_const);
6423 putchar ('\n');
6424 }
6425 }
6426 start = list->start_of_next_abbrevs;
6427 free_abbrev_list (list);
6428 }
6429 while (start);
6430
6431 printf ("\n");
6432
6433 return 1;
6434 }
6435
6436 /* Return true when ADDR is the maximum address, when addresses are
6437 POINTER_SIZE bytes long. */
6438
6439 static bool
6440 is_max_address (uint64_t addr, unsigned int pointer_size)
6441 {
6442 uint64_t mask = ~(~(uint64_t) 0 << 1 << (pointer_size * 8 - 1));
6443 return ((addr & mask) == mask);
6444 }
6445
6446 /* Display a view pair list starting at *VSTART_PTR and ending at
6447 VLISTEND within SECTION. */
6448
6449 static void
6450 display_view_pair_list (struct dwarf_section *section,
6451 unsigned char **vstart_ptr,
6452 unsigned int debug_info_entry,
6453 unsigned char *vlistend)
6454 {
6455 unsigned char *vstart = *vstart_ptr;
6456 unsigned char *section_end = section->start + section->size;
6457 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6458
6459 if (vlistend < section_end)
6460 section_end = vlistend;
6461
6462 putchar ('\n');
6463
6464 while (vstart < section_end)
6465 {
6466 uint64_t off = vstart - section->start;
6467 uint64_t vbegin, vend;
6468
6469 READ_ULEB (vbegin, vstart, section_end);
6470 if (vstart == section_end)
6471 break;
6472
6473 READ_ULEB (vend, vstart, section_end);
6474 printf (" %8.8" PRIx64 " ", off);
6475
6476 print_view (vbegin, pointer_size);
6477 print_view (vend, pointer_size);
6478 printf (_("location view pair\n"));
6479 }
6480
6481 putchar ('\n');
6482 *vstart_ptr = vstart;
6483 }
6484
6485 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6486
6487 static void
6488 display_loc_list (struct dwarf_section *section,
6489 unsigned char **start_ptr,
6490 unsigned int debug_info_entry,
6491 uint64_t offset,
6492 uint64_t base_address,
6493 unsigned char **vstart_ptr,
6494 int has_frame_base)
6495 {
6496 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6497 unsigned char *section_end = section->start + section->size;
6498 uint64_t cu_offset;
6499 unsigned int pointer_size;
6500 unsigned int offset_size;
6501 int dwarf_version;
6502 uint64_t begin;
6503 uint64_t end;
6504 unsigned short length;
6505 int need_frame_base;
6506
6507 if (debug_info_entry >= num_debug_info_entries)
6508 {
6509 warn (_("No debug information available for loc lists of entry: %u\n"),
6510 debug_info_entry);
6511 return;
6512 }
6513
6514 cu_offset = debug_information [debug_info_entry].cu_offset;
6515 pointer_size = debug_information [debug_info_entry].pointer_size;
6516 offset_size = debug_information [debug_info_entry].offset_size;
6517 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6518
6519 if (pointer_size < 2 || pointer_size > 8)
6520 {
6521 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6522 pointer_size, debug_info_entry);
6523 return;
6524 }
6525
6526 while (1)
6527 {
6528 uint64_t off = offset + (start - *start_ptr);
6529 uint64_t vbegin = -1, vend = -1;
6530
6531 if (2 * pointer_size > (size_t) (section_end - start))
6532 {
6533 warn (_("Location list starting at offset %#" PRIx64
6534 " is not terminated.\n"), offset);
6535 break;
6536 }
6537
6538 printf (" ");
6539 print_hex (off, 4);
6540
6541 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6542 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6543
6544 if (begin == 0 && end == 0)
6545 {
6546 /* PR 18374: In a object file we can have a location list that
6547 starts with a begin and end of 0 because there are relocations
6548 that need to be applied to the addresses. Actually applying
6549 the relocations now does not help as they will probably resolve
6550 to 0, since the object file has not been fully linked. Real
6551 end of list markers will not have any relocations against them. */
6552 if (! reloc_at (section, off)
6553 && ! reloc_at (section, off + pointer_size))
6554 {
6555 printf (_("<End of list>\n"));
6556 break;
6557 }
6558 }
6559
6560 /* Check base address specifiers. */
6561 if (is_max_address (begin, pointer_size)
6562 && !is_max_address (end, pointer_size))
6563 {
6564 base_address = end;
6565 print_hex (begin, pointer_size);
6566 print_hex (end, pointer_size);
6567 printf (_("(base address)\n"));
6568 continue;
6569 }
6570
6571 if (vstart)
6572 {
6573 off = offset + (vstart - *start_ptr);
6574
6575 READ_ULEB (vbegin, vstart, section_end);
6576 print_view (vbegin, pointer_size);
6577
6578 READ_ULEB (vend, vstart, section_end);
6579 print_view (vend, pointer_size);
6580
6581 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6582 }
6583
6584 if (2 > (size_t) (section_end - start))
6585 {
6586 warn (_("Location list starting at offset %#" PRIx64
6587 " is not terminated.\n"), offset);
6588 break;
6589 }
6590
6591 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6592
6593 if (length > (size_t) (section_end - start))
6594 {
6595 warn (_("Location list starting at offset %#" PRIx64
6596 " is not terminated.\n"), offset);
6597 break;
6598 }
6599
6600 print_hex (begin + base_address, pointer_size);
6601 print_hex (end + base_address, pointer_size);
6602
6603 putchar ('(');
6604 need_frame_base = decode_location_expression (start,
6605 pointer_size,
6606 offset_size,
6607 dwarf_version,
6608 length,
6609 cu_offset, section);
6610 putchar (')');
6611
6612 if (need_frame_base && !has_frame_base)
6613 printf (_(" [without DW_AT_frame_base]"));
6614
6615 if (begin == end && vbegin == vend)
6616 fputs (_(" (start == end)"), stdout);
6617 else if (begin > end || (begin == end && vbegin > vend))
6618 fputs (_(" (start > end)"), stdout);
6619
6620 putchar ('\n');
6621
6622 start += length;
6623 }
6624
6625 *start_ptr = start;
6626 *vstart_ptr = vstart;
6627 }
6628
6629 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6630
6631 static void
6632 display_loclists_list (struct dwarf_section * section,
6633 unsigned char ** start_ptr,
6634 unsigned int debug_info_entry,
6635 uint64_t offset,
6636 uint64_t base_address,
6637 unsigned char ** vstart_ptr,
6638 int has_frame_base)
6639 {
6640 unsigned char *start = *start_ptr;
6641 unsigned char *vstart = *vstart_ptr;
6642 unsigned char *section_end = section->start + section->size;
6643 uint64_t cu_offset;
6644 unsigned int pointer_size;
6645 unsigned int offset_size;
6646 unsigned int dwarf_version;
6647
6648 /* Initialize it due to a false compiler warning. */
6649 uint64_t begin = -1, vbegin = -1;
6650 uint64_t end = -1, vend = -1;
6651 uint64_t length;
6652 int need_frame_base;
6653
6654 if (debug_info_entry >= num_debug_info_entries)
6655 {
6656 warn (_("No debug information available for "
6657 "loclists lists of entry: %u\n"),
6658 debug_info_entry);
6659 return;
6660 }
6661
6662 cu_offset = debug_information [debug_info_entry].cu_offset;
6663 pointer_size = debug_information [debug_info_entry].pointer_size;
6664 offset_size = debug_information [debug_info_entry].offset_size;
6665 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6666
6667 if (pointer_size < 2 || pointer_size > 8)
6668 {
6669 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6670 pointer_size, debug_info_entry);
6671 return;
6672 }
6673
6674 while (1)
6675 {
6676 uint64_t off = offset + (start - *start_ptr);
6677 enum dwarf_location_list_entry_type llet;
6678
6679 if (start + 1 > section_end)
6680 {
6681 warn (_("Location list starting at offset %#" PRIx64
6682 " is not terminated.\n"), offset);
6683 break;
6684 }
6685
6686 printf (" ");
6687 print_hex (off, 4);
6688
6689 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6690
6691 if (vstart && (llet == DW_LLE_offset_pair
6692 || llet == DW_LLE_start_end
6693 || llet == DW_LLE_start_length))
6694 {
6695 off = offset + (vstart - *start_ptr);
6696
6697 READ_ULEB (vbegin, vstart, section_end);
6698 print_view (vbegin, pointer_size);
6699
6700 READ_ULEB (vend, vstart, section_end);
6701 print_view (vend, pointer_size);
6702
6703 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6704 }
6705
6706 switch (llet)
6707 {
6708 case DW_LLE_end_of_list:
6709 printf (_("<End of list>\n"));
6710 break;
6711
6712 case DW_LLE_base_addressx:
6713 READ_ULEB (base_address, start, section_end);
6714 print_hex (base_address, pointer_size);
6715 printf (_("(index into .debug_addr) "));
6716 base_address = fetch_indexed_addr (base_address, pointer_size);
6717 print_hex (base_address, pointer_size);
6718 printf (_("(base address)\n"));
6719 break;
6720
6721 case DW_LLE_startx_endx:
6722 READ_ULEB (begin, start, section_end);
6723 begin = fetch_indexed_addr (begin, pointer_size);
6724 READ_ULEB (end, start, section_end);
6725 end = fetch_indexed_addr (end, pointer_size);
6726 break;
6727
6728 case DW_LLE_startx_length:
6729 READ_ULEB (begin, start, section_end);
6730 begin = fetch_indexed_addr (begin, pointer_size);
6731 READ_ULEB (end, start, section_end);
6732 end += begin;
6733 break;
6734
6735 case DW_LLE_default_location:
6736 begin = end = 0;
6737 break;
6738
6739 case DW_LLE_offset_pair:
6740 READ_ULEB (begin, start, section_end);
6741 begin += base_address;
6742 READ_ULEB (end, start, section_end);
6743 end += base_address;
6744 break;
6745
6746 case DW_LLE_base_address:
6747 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6748 section_end);
6749 print_hex (base_address, pointer_size);
6750 printf (_("(base address)\n"));
6751 break;
6752
6753 case DW_LLE_start_end:
6754 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6755 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6756 break;
6757
6758 case DW_LLE_start_length:
6759 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6760 READ_ULEB (end, start, section_end);
6761 end += begin;
6762 break;
6763
6764 #ifdef DW_LLE_view_pair
6765 case DW_LLE_view_pair:
6766 if (vstart)
6767 printf (_("View pair entry in loclist with locviews attribute\n"));
6768 READ_ULEB (vbegin, start, section_end);
6769 print_view (vbegin, pointer_size);
6770
6771 READ_ULEB (vend, start, section_end);
6772 print_view (vend, pointer_size);
6773
6774 printf (_("views for:\n"));
6775 continue;
6776 #endif
6777
6778 default:
6779 error (_("Invalid location list entry type %d\n"), llet);
6780 return;
6781 }
6782
6783 if (llet == DW_LLE_end_of_list)
6784 break;
6785
6786 if (llet == DW_LLE_base_address
6787 || llet == DW_LLE_base_addressx)
6788 continue;
6789
6790 if (start == section_end)
6791 {
6792 warn (_("Location list starting at offset %#" PRIx64
6793 " is not terminated.\n"), offset);
6794 break;
6795 }
6796 READ_ULEB (length, start, section_end);
6797
6798 if (length > (size_t) (section_end - start))
6799 {
6800 warn (_("Location list starting at offset %#" PRIx64
6801 " is not terminated.\n"), offset);
6802 break;
6803 }
6804
6805 print_hex (begin, pointer_size);
6806 print_hex (end, pointer_size);
6807
6808 putchar ('(');
6809 need_frame_base = decode_location_expression (start,
6810 pointer_size,
6811 offset_size,
6812 dwarf_version,
6813 length,
6814 cu_offset, section);
6815 putchar (')');
6816
6817 if (need_frame_base && !has_frame_base)
6818 printf (_(" [without DW_AT_frame_base]"));
6819
6820 if (begin == end && vbegin == vend)
6821 fputs (_(" (start == end)"), stdout);
6822 else if (begin > end || (begin == end && vbegin > vend))
6823 fputs (_(" (start > end)"), stdout);
6824
6825 putchar ('\n');
6826
6827 start += length;
6828 vbegin = vend = -1;
6829 }
6830
6831 if (vbegin != (uint64_t) -1 || vend != (uint64_t) -1)
6832 printf (_("Trailing view pair not used in a range"));
6833
6834 *start_ptr = start;
6835 *vstart_ptr = vstart;
6836 }
6837
6838 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
6839 right-adjusted in a field of length LEN, and followed by a space. */
6840
6841 static void
6842 print_addr_index (unsigned int idx, unsigned int len)
6843 {
6844 static char buf[15];
6845 snprintf (buf, sizeof (buf), "[%d]", idx);
6846 printf ("%*s ", len, buf);
6847 }
6848
6849 /* Display a location list from a .dwo section. It uses address indexes rather
6850 than embedded addresses. This code closely follows display_loc_list, but the
6851 two are sufficiently different that combining things is very ugly. */
6852
6853 static void
6854 display_loc_list_dwo (struct dwarf_section *section,
6855 unsigned char **start_ptr,
6856 unsigned int debug_info_entry,
6857 uint64_t offset,
6858 unsigned char **vstart_ptr,
6859 int has_frame_base)
6860 {
6861 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6862 unsigned char *section_end = section->start + section->size;
6863 uint64_t cu_offset;
6864 unsigned int pointer_size;
6865 unsigned int offset_size;
6866 int dwarf_version;
6867 int entry_type;
6868 unsigned short length;
6869 int need_frame_base;
6870 unsigned int idx;
6871
6872 if (debug_info_entry >= num_debug_info_entries)
6873 {
6874 warn (_("No debug information for loc lists of entry: %u\n"),
6875 debug_info_entry);
6876 return;
6877 }
6878
6879 cu_offset = debug_information [debug_info_entry].cu_offset;
6880 pointer_size = debug_information [debug_info_entry].pointer_size;
6881 offset_size = debug_information [debug_info_entry].offset_size;
6882 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6883
6884 if (pointer_size < 2 || pointer_size > 8)
6885 {
6886 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6887 pointer_size, debug_info_entry);
6888 return;
6889 }
6890
6891 while (1)
6892 {
6893 printf (" ");
6894 print_hex (offset + (start - *start_ptr), 4);
6895
6896 if (start >= section_end)
6897 {
6898 warn (_("Location list starting at offset %#" PRIx64
6899 " is not terminated.\n"), offset);
6900 break;
6901 }
6902
6903 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
6904
6905 if (vstart)
6906 switch (entry_type)
6907 {
6908 default:
6909 break;
6910
6911 case 2:
6912 case 3:
6913 case 4:
6914 {
6915 uint64_t view;
6916 uint64_t off = offset + (vstart - *start_ptr);
6917
6918 READ_ULEB (view, vstart, section_end);
6919 print_view (view, 8);
6920
6921 READ_ULEB (view, vstart, section_end);
6922 print_view (view, 8);
6923
6924 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6925
6926 }
6927 break;
6928 }
6929
6930 switch (entry_type)
6931 {
6932 case 0: /* A terminating entry. */
6933 *start_ptr = start;
6934 *vstart_ptr = vstart;
6935 printf (_("<End of list>\n"));
6936 return;
6937 case 1: /* A base-address entry. */
6938 READ_ULEB (idx, start, section_end);
6939 print_addr_index (idx, 8);
6940 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
6941 printf (_("(base address selection entry)\n"));
6942 continue;
6943 case 2: /* A start/end entry. */
6944 READ_ULEB (idx, start, section_end);
6945 print_addr_index (idx, 8);
6946 READ_ULEB (idx, start, section_end);
6947 print_addr_index (idx, 8);
6948 break;
6949 case 3: /* A start/length entry. */
6950 READ_ULEB (idx, start, section_end);
6951 print_addr_index (idx, 8);
6952 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6953 printf ("%08x ", idx);
6954 break;
6955 case 4: /* An offset pair entry. */
6956 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6957 printf ("%08x ", idx);
6958 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6959 printf ("%08x ", idx);
6960 break;
6961 default:
6962 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6963 *start_ptr = start;
6964 *vstart_ptr = vstart;
6965 return;
6966 }
6967
6968 if (2 > (size_t) (section_end - start))
6969 {
6970 warn (_("Location list starting at offset %#" PRIx64
6971 " is not terminated.\n"), offset);
6972 break;
6973 }
6974
6975 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6976 if (length > (size_t) (section_end - start))
6977 {
6978 warn (_("Location list starting at offset %#" PRIx64
6979 " is not terminated.\n"), offset);
6980 break;
6981 }
6982
6983 putchar ('(');
6984 need_frame_base = decode_location_expression (start,
6985 pointer_size,
6986 offset_size,
6987 dwarf_version,
6988 length,
6989 cu_offset, section);
6990 putchar (')');
6991
6992 if (need_frame_base && !has_frame_base)
6993 printf (_(" [without DW_AT_frame_base]"));
6994
6995 putchar ('\n');
6996
6997 start += length;
6998 }
6999
7000 *start_ptr = start;
7001 *vstart_ptr = vstart;
7002 }
7003
7004 /* Sort array of indexes in ascending order of loc_offsets[idx] and
7005 loc_views. */
7006
7007 static uint64_t *loc_offsets, *loc_views;
7008
7009 static int
7010 loc_offsets_compar (const void *ap, const void *bp)
7011 {
7012 uint64_t a = loc_offsets[*(const unsigned int *) ap];
7013 uint64_t b = loc_offsets[*(const unsigned int *) bp];
7014
7015 int ret = (a > b) - (b > a);
7016 if (ret)
7017 return ret;
7018
7019 a = loc_views[*(const unsigned int *) ap];
7020 b = loc_views[*(const unsigned int *) bp];
7021
7022 ret = (a > b) - (b > a);
7023
7024 return ret;
7025 }
7026
7027 static int
7028 display_offset_entry_loclists (struct dwarf_section *section)
7029 {
7030 unsigned char * start = section->start;
7031 unsigned char * const end = start + section->size;
7032
7033 introduce (section, false);
7034
7035 do
7036 {
7037 uint64_t length;
7038 unsigned short version;
7039 unsigned char address_size;
7040 unsigned char segment_selector_size;
7041 uint32_t offset_entry_count;
7042 uint32_t i;
7043 bool is_64bit;
7044
7045 printf (_("Table at Offset %#tx\n"), start - section->start);
7046
7047 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
7048 if (length == 0xffffffff)
7049 {
7050 is_64bit = true;
7051 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
7052 }
7053 else
7054 is_64bit = false;
7055
7056 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
7057 SAFE_BYTE_GET_AND_INC (address_size, start, 1, end);
7058 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, end);
7059 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, end);
7060
7061 printf (_(" Length: %#" PRIx64 "\n"), length);
7062 printf (_(" DWARF version: %u\n"), version);
7063 printf (_(" Address size: %u\n"), address_size);
7064 printf (_(" Segment size: %u\n"), segment_selector_size);
7065 printf (_(" Offset entries: %u\n"), offset_entry_count);
7066
7067 if (version < 5)
7068 {
7069 warn (_("The %s section contains a corrupt or "
7070 "unsupported version number: %d.\n"),
7071 section->name, version);
7072 return 0;
7073 }
7074
7075 if (segment_selector_size != 0)
7076 {
7077 warn (_("The %s section contains an "
7078 "unsupported segment selector size: %d.\n"),
7079 section->name, segment_selector_size);
7080 return 0;
7081 }
7082
7083 if (offset_entry_count == 0)
7084 {
7085 warn (_("The %s section contains a table without offset\n"),
7086 section->name);
7087 return 0;
7088 }
7089
7090 printf (_("\n Offset Entries starting at %#tx:\n"),
7091 start - section->start);
7092
7093 for (i = 0; i < offset_entry_count; i++)
7094 {
7095 uint64_t entry;
7096
7097 SAFE_BYTE_GET_AND_INC (entry, start, is_64bit ? 8 : 4, end);
7098 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
7099 }
7100
7101 putchar ('\n');
7102
7103 uint32_t j;
7104
7105 for (j = 1, i = 0; i < offset_entry_count;)
7106 {
7107 unsigned char lle;
7108 uint64_t base_address = 0;
7109 uint64_t begin;
7110 uint64_t finish;
7111 uint64_t off = start - section->start;
7112
7113 if (j != i)
7114 {
7115 printf (_(" Offset Entry %u\n"), i);
7116 j = i;
7117 }
7118
7119 printf (" ");
7120 print_hex (off, 4);
7121
7122 SAFE_BYTE_GET_AND_INC (lle, start, 1, end);
7123
7124 switch (lle)
7125 {
7126 case DW_LLE_end_of_list:
7127 printf (_("<End of list>\n\n"));
7128 i ++;
7129 continue;
7130
7131 case DW_LLE_base_addressx:
7132 READ_ULEB (base_address, start, end);
7133 print_hex (base_address, address_size);
7134 printf (_("(index into .debug_addr) "));
7135 base_address = fetch_indexed_addr (base_address, address_size);
7136 print_hex (base_address, address_size);
7137 printf (_("(base address)\n"));
7138 continue;
7139
7140 case DW_LLE_startx_endx:
7141 READ_ULEB (begin, start, end);
7142 begin = fetch_indexed_addr (begin, address_size);
7143 READ_ULEB (finish, start, end);
7144 finish = fetch_indexed_addr (finish, address_size);
7145 break;
7146
7147 case DW_LLE_startx_length:
7148 READ_ULEB (begin, start, end);
7149 begin = fetch_indexed_addr (begin, address_size);
7150 READ_ULEB (finish, start, end);
7151 finish += begin;
7152 break;
7153
7154 case DW_LLE_offset_pair:
7155 READ_ULEB (begin, start, end);
7156 begin += base_address;
7157 READ_ULEB (finish, start, end);
7158 finish += base_address;
7159 break;
7160
7161 case DW_LLE_default_location:
7162 begin = finish = 0;
7163 break;
7164
7165 case DW_LLE_base_address:
7166 SAFE_BYTE_GET_AND_INC (base_address, start, address_size, end);
7167 print_hex (base_address, address_size);
7168 printf (_("(base address)\n"));
7169 continue;
7170
7171 case DW_LLE_start_end:
7172 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7173 SAFE_BYTE_GET_AND_INC (finish, start, address_size, end);
7174 break;
7175
7176 case DW_LLE_start_length:
7177 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7178 READ_ULEB (finish, start, end);
7179 finish += begin;
7180 break;
7181
7182 default:
7183 error (_("Invalid location list entry type %d\n"), lle);
7184 return 0;
7185 }
7186
7187 if (start == end)
7188 {
7189 warn (_("Location list starting at offset %#" PRIx64
7190 " is not terminated.\n"), off);
7191 break;
7192 }
7193
7194 print_hex (begin, address_size);
7195 print_hex (finish, address_size);
7196
7197 if (begin == finish)
7198 fputs (_("(start == end)"), stdout);
7199 else if (begin > finish)
7200 fputs (_("(start > end)"), stdout);
7201
7202 /* Read the counted location descriptions. */
7203 READ_ULEB (length, start, end);
7204
7205 if (length > (size_t) (end - start))
7206 {
7207 warn (_("Location list starting at offset %#" PRIx64
7208 " is not terminated.\n"), off);
7209 break;
7210 }
7211
7212 (void) decode_location_expression (start, address_size, address_size,
7213 version, length, 0, section);
7214 start += length;
7215 putchar ('\n');
7216 }
7217
7218 putchar ('\n');
7219 }
7220 while (start < end);
7221
7222 return 1;
7223 }
7224
7225 static int
7226 display_debug_loc (struct dwarf_section *section, void *file)
7227 {
7228 unsigned char *start = section->start, *vstart = NULL;
7229 uint64_t bytes;
7230 unsigned char *section_begin = start;
7231 unsigned int num_loc_list = 0;
7232 uint64_t last_offset = 0;
7233 uint64_t last_view = 0;
7234 unsigned int first = 0;
7235 unsigned int i;
7236 unsigned int j;
7237 int seen_first_offset = 0;
7238 int locs_sorted = 1;
7239 unsigned char *next = start, *vnext = vstart;
7240 unsigned int *array = NULL;
7241 const char *suffix = strrchr (section->name, '.');
7242 bool is_dwo = false;
7243 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
7244 uint64_t header_size = 0;
7245
7246 if (suffix && strcmp (suffix, ".dwo") == 0)
7247 is_dwo = true;
7248
7249 bytes = section->size;
7250
7251 if (bytes == 0)
7252 {
7253 printf (_("\nThe %s section is empty.\n"), section->name);
7254 return 0;
7255 }
7256
7257 if (is_loclists)
7258 {
7259 unsigned char *hdrptr = section_begin;
7260 uint64_t ll_length;
7261 unsigned short ll_version;
7262 unsigned char *end = section_begin + section->size;
7263 unsigned char address_size, segment_selector_size;
7264 uint32_t offset_entry_count;
7265
7266 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
7267 if (ll_length == 0xffffffff)
7268 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
7269
7270 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
7271 if (ll_version != 5)
7272 {
7273 warn (_("The %s section contains corrupt or "
7274 "unsupported version number: %d.\n"),
7275 section->name, ll_version);
7276 return 0;
7277 }
7278
7279 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7280
7281 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7282 if (segment_selector_size != 0)
7283 {
7284 warn (_("The %s section contains "
7285 "unsupported segment selector size: %d.\n"),
7286 section->name, segment_selector_size);
7287 return 0;
7288 }
7289
7290 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7291
7292 if (offset_entry_count != 0)
7293 return display_offset_entry_loclists (section);
7294
7295 header_size = hdrptr - section_begin;
7296 }
7297
7298 if (load_debug_info (file) == 0)
7299 {
7300 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7301 section->name);
7302 return 0;
7303 }
7304
7305 /* Check the order of location list in .debug_info section. If
7306 offsets of location lists are in the ascending order, we can
7307 use `debug_information' directly. */
7308 for (i = 0; i < num_debug_info_entries; i++)
7309 {
7310 unsigned int num;
7311
7312 num = debug_information [i].num_loc_offsets;
7313 if (num > num_loc_list)
7314 num_loc_list = num;
7315
7316 /* Check if we can use `debug_information' directly. */
7317 if (locs_sorted && num != 0)
7318 {
7319 if (!seen_first_offset)
7320 {
7321 /* This is the first location list. */
7322 last_offset = debug_information [i].loc_offsets [0];
7323 last_view = debug_information [i].loc_views [0];
7324 first = i;
7325 seen_first_offset = 1;
7326 j = 1;
7327 }
7328 else
7329 j = 0;
7330
7331 for (; j < num; j++)
7332 {
7333 if (last_offset >
7334 debug_information [i].loc_offsets [j]
7335 || (last_offset == debug_information [i].loc_offsets [j]
7336 && last_view > debug_information [i].loc_views [j]))
7337 {
7338 locs_sorted = 0;
7339 break;
7340 }
7341 last_offset = debug_information [i].loc_offsets [j];
7342 last_view = debug_information [i].loc_views [j];
7343 }
7344 }
7345 }
7346
7347 if (!seen_first_offset)
7348 error (_("No location lists in .debug_info section!\n"));
7349
7350 if (debug_information [first].num_loc_offsets > 0
7351 && debug_information [first].loc_offsets [0] != header_size
7352 && debug_information [first].loc_views [0] != header_size)
7353 warn (_("Location lists in %s section start at %#" PRIx64
7354 " rather than %#" PRIx64 "\n"),
7355 section->name, debug_information [first].loc_offsets [0],
7356 header_size);
7357
7358 if (!locs_sorted)
7359 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
7360
7361 introduce (section, false);
7362
7363 if (reloc_at (section, 0))
7364 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
7365
7366 printf (_(" Offset Begin End Expression\n"));
7367
7368 for (i = first; i < num_debug_info_entries; i++)
7369 {
7370 uint64_t offset = 0, voffset = 0;
7371 uint64_t base_address;
7372 unsigned int k;
7373 int has_frame_base;
7374
7375 if (!locs_sorted)
7376 {
7377 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7378 array[k] = k;
7379 loc_offsets = debug_information [i].loc_offsets;
7380 loc_views = debug_information [i].loc_views;
7381 qsort (array, debug_information [i].num_loc_offsets,
7382 sizeof (*array), loc_offsets_compar);
7383 }
7384
7385 /* .debug_loclists has a per-unit header.
7386 Update start if we are detecting it. */
7387 if (debug_information [i].dwarf_version == 5)
7388 {
7389 j = locs_sorted ? 0 : array [0];
7390
7391 if (debug_information [i].num_loc_offsets)
7392 offset = debug_information [i].loc_offsets [j];
7393
7394 if (debug_information [i].num_loc_views)
7395 voffset = debug_information [i].loc_views [j];
7396
7397 /* Assume that the size of the header is constant across CUs. */
7398 if (((start - section_begin) + header_size == offset)
7399 || ((start -section_begin) + header_size == voffset))
7400 start += header_size;
7401 }
7402
7403 int adjacent_view_loclists = 1;
7404 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7405 {
7406 j = locs_sorted ? k : array[k];
7407 if (k
7408 && (debug_information [i].loc_offsets [locs_sorted
7409 ? k - 1 : array [k - 1]]
7410 == debug_information [i].loc_offsets [j])
7411 && (debug_information [i].loc_views [locs_sorted
7412 ? k - 1 : array [k - 1]]
7413 == debug_information [i].loc_views [j]))
7414 continue;
7415 has_frame_base = debug_information [i].have_frame_base [j];
7416 offset = debug_information [i].loc_offsets [j];
7417 next = section_begin + offset;
7418 voffset = debug_information [i].loc_views [j];
7419 if (voffset != (uint64_t) -1)
7420 vnext = section_begin + voffset;
7421 else
7422 vnext = NULL;
7423 base_address = debug_information [i].base_address;
7424
7425 if (vnext && vnext < next)
7426 {
7427 vstart = vnext;
7428 display_view_pair_list (section, &vstart, i, next);
7429 if (start == vnext)
7430 start = vstart;
7431 }
7432
7433 if (start < next)
7434 {
7435 if (vnext && vnext < next)
7436 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7437 " in %s section.\n"),
7438 start - section_begin, voffset, section->name);
7439 else
7440 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7441 " in %s section.\n"),
7442 start - section_begin, offset, section->name);
7443 }
7444 else if (start > next)
7445 warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
7446 " in %s section.\n"),
7447 start - section_begin, offset, section->name);
7448 start = next;
7449 vstart = vnext;
7450
7451 if (offset >= bytes)
7452 {
7453 warn (_("Offset %#" PRIx64 " is bigger than %s section size.\n"),
7454 offset, section->name);
7455 continue;
7456 }
7457
7458 if (vnext && voffset >= bytes)
7459 {
7460 warn (_("View Offset %#" PRIx64 " is bigger than %s section size.\n"),
7461 voffset, section->name);
7462 continue;
7463 }
7464
7465 if (!is_loclists)
7466 {
7467 if (is_dwo)
7468 display_loc_list_dwo (section, &start, i, offset,
7469 &vstart, has_frame_base);
7470 else
7471 display_loc_list (section, &start, i, offset, base_address,
7472 &vstart, has_frame_base);
7473 }
7474 else
7475 {
7476 if (is_dwo)
7477 warn (_("DWO is not yet supported.\n"));
7478 else
7479 display_loclists_list (section, &start, i, offset, base_address,
7480 &vstart, has_frame_base);
7481 }
7482
7483 /* FIXME: this arrangement is quite simplistic. Nothing
7484 requires locview lists to be adjacent to corresponding
7485 loclists, and a single loclist could be augmented by
7486 different locview lists, and vice-versa, unlikely as it
7487 is that it would make sense to do so. Hopefully we'll
7488 have view pair support built into loclists before we ever
7489 need to address all these possibilities. */
7490 if (adjacent_view_loclists && vnext
7491 && vnext != start && vstart != next)
7492 {
7493 adjacent_view_loclists = 0;
7494 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7495 }
7496
7497 if (vnext && vnext == start)
7498 display_view_pair_list (section, &start, i, vstart);
7499 }
7500 }
7501
7502 if (start < section->start + section->size)
7503 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7504 "There are %ld unused bytes at the end of section %s\n",
7505 (long) (section->start + section->size - start)),
7506 (long) (section->start + section->size - start), section->name);
7507 putchar ('\n');
7508 free (array);
7509 return 1;
7510 }
7511
7512 static int
7513 display_debug_str (struct dwarf_section *section,
7514 void *file ATTRIBUTE_UNUSED)
7515 {
7516 unsigned char *start = section->start;
7517 uint64_t bytes = section->size;
7518 uint64_t addr = section->address;
7519
7520 if (bytes == 0)
7521 {
7522 printf (_("\nThe %s section is empty.\n"), section->name);
7523 return 0;
7524 }
7525
7526 introduce (section, false);
7527
7528 while (bytes)
7529 {
7530 int j;
7531 int k;
7532 int lbytes;
7533
7534 lbytes = (bytes > 16 ? 16 : bytes);
7535
7536 printf (" 0x%8.8" PRIx64 " ", addr);
7537
7538 for (j = 0; j < 16; j++)
7539 {
7540 if (j < lbytes)
7541 printf ("%2.2x", start[j]);
7542 else
7543 printf (" ");
7544
7545 if ((j & 3) == 3)
7546 printf (" ");
7547 }
7548
7549 for (j = 0; j < lbytes; j++)
7550 {
7551 k = start[j];
7552 if (k >= ' ' && k < 0x80)
7553 printf ("%c", k);
7554 else
7555 printf (".");
7556 }
7557
7558 putchar ('\n');
7559
7560 start += lbytes;
7561 addr += lbytes;
7562 bytes -= lbytes;
7563 }
7564
7565 putchar ('\n');
7566
7567 return 1;
7568 }
7569
7570 static int
7571 display_debug_info (struct dwarf_section *section, void *file)
7572 {
7573 return process_debug_info (section, file, section->abbrev_sec, false, false);
7574 }
7575
7576 static int
7577 display_debug_types (struct dwarf_section *section, void *file)
7578 {
7579 return process_debug_info (section, file, section->abbrev_sec, false, true);
7580 }
7581
7582 static int
7583 display_trace_info (struct dwarf_section *section, void *file)
7584 {
7585 return process_debug_info (section, file, section->abbrev_sec, false, true);
7586 }
7587
7588 static int
7589 display_debug_aranges (struct dwarf_section *section,
7590 void *file ATTRIBUTE_UNUSED)
7591 {
7592 unsigned char *start = section->start;
7593 unsigned char *end = start + section->size;
7594
7595 introduce (section, false);
7596
7597 /* It does not matter if this load fails,
7598 we test for that later on. */
7599 load_debug_info (file);
7600
7601 while (start < end)
7602 {
7603 unsigned char *hdrptr;
7604 DWARF2_Internal_ARange arange;
7605 unsigned char *addr_ranges;
7606 uint64_t length;
7607 uint64_t address;
7608 uint64_t sec_off;
7609 unsigned char address_size;
7610 unsigned int offset_size;
7611 unsigned char *end_ranges;
7612
7613 hdrptr = start;
7614 sec_off = hdrptr - section->start;
7615
7616 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7617 if (arange.ar_length == 0xffffffff)
7618 {
7619 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7620 offset_size = 8;
7621 }
7622 else
7623 offset_size = 4;
7624
7625 if (arange.ar_length > (size_t) (end - hdrptr))
7626 {
7627 warn (_("Debug info is corrupted, %s header at %#" PRIx64
7628 " has length %#" PRIx64 "\n"),
7629 section->name, sec_off, arange.ar_length);
7630 break;
7631 }
7632 end_ranges = hdrptr + arange.ar_length;
7633
7634 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7635 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7636 end_ranges);
7637
7638 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7639 && num_debug_info_entries > 0
7640 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7641 warn (_(".debug_info offset of %#" PRIx64
7642 " in %s section does not point to a CU header.\n"),
7643 arange.ar_info_offset, section->name);
7644
7645 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7646 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7647
7648 if (arange.ar_version != 2 && arange.ar_version != 3)
7649 {
7650 /* PR 19872: A version number of 0 probably means that there is
7651 padding at the end of the .debug_aranges section. Gold puts
7652 it there when performing an incremental link, for example.
7653 So do not generate a warning in this case. */
7654 if (arange.ar_version)
7655 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7656 break;
7657 }
7658
7659 printf (_(" Length: %" PRId64 "\n"), arange.ar_length);
7660 printf (_(" Version: %d\n"), arange.ar_version);
7661 printf (_(" Offset into .debug_info: %#" PRIx64 "\n"),
7662 arange.ar_info_offset);
7663 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7664 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7665
7666 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7667
7668 /* PR 17512: file: 001-108546-0.001:0.1. */
7669 if (address_size == 0 || address_size > 8)
7670 {
7671 error (_("Invalid address size in %s section!\n"),
7672 section->name);
7673 break;
7674 }
7675
7676 /* The DWARF spec does not require that the address size be a power
7677 of two, but we do. This will have to change if we ever encounter
7678 an uneven architecture. */
7679 if ((address_size & (address_size - 1)) != 0)
7680 {
7681 warn (_("Pointer size + Segment size is not a power of two.\n"));
7682 break;
7683 }
7684
7685 if (address_size > 4)
7686 printf (_("\n Address Length\n"));
7687 else
7688 printf (_("\n Address Length\n"));
7689
7690 addr_ranges = hdrptr;
7691
7692 /* Must pad to an alignment boundary that is twice the address size. */
7693 addr_ranges += (2 * address_size - 1
7694 - (hdrptr - start - 1) % (2 * address_size));
7695
7696 while (2 * address_size <= end_ranges - addr_ranges)
7697 {
7698 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7699 end_ranges);
7700 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7701 end_ranges);
7702 printf (" ");
7703 print_hex (address, address_size);
7704 print_hex_ns (length, address_size);
7705 putchar ('\n');
7706 }
7707
7708 start = end_ranges;
7709 }
7710
7711 printf ("\n");
7712
7713 return 1;
7714 }
7715
7716 /* Comparison function for qsort. */
7717 static int
7718 comp_addr_base (const void * v0, const void * v1)
7719 {
7720 debug_info *info0 = *(debug_info **) v0;
7721 debug_info *info1 = *(debug_info **) v1;
7722 return info0->addr_base - info1->addr_base;
7723 }
7724
7725 /* Display the debug_addr section. */
7726 static int
7727 display_debug_addr (struct dwarf_section *section,
7728 void *file)
7729 {
7730 debug_info **debug_addr_info;
7731 unsigned char *entry;
7732 unsigned char *end;
7733 unsigned int i;
7734 unsigned int count;
7735 unsigned char * header;
7736
7737 if (section->size == 0)
7738 {
7739 printf (_("\nThe %s section is empty.\n"), section->name);
7740 return 0;
7741 }
7742
7743 if (load_debug_info (file) == 0)
7744 {
7745 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7746 section->name);
7747 return 0;
7748 }
7749
7750 introduce (section, false);
7751
7752 /* PR 17531: file: cf38d01b.
7753 We use xcalloc because a corrupt file may not have initialised all of the
7754 fields in the debug_info structure, which means that the sort below might
7755 try to move uninitialised data. */
7756 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7757 sizeof (debug_info *));
7758
7759 count = 0;
7760 for (i = 0; i < num_debug_info_entries; i++)
7761 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7762 {
7763 /* PR 17531: file: cf38d01b. */
7764 if (debug_information[i].addr_base >= section->size)
7765 warn (_("Corrupt address base (%#" PRIx64 ")"
7766 " found in debug section %u\n"),
7767 debug_information[i].addr_base, i);
7768 else
7769 debug_addr_info [count++] = debug_information + i;
7770 }
7771
7772 /* Add a sentinel to make iteration convenient. */
7773 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7774 debug_addr_info [count]->addr_base = section->size;
7775 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7776
7777 header = section->start;
7778 for (i = 0; i < count; i++)
7779 {
7780 unsigned int idx;
7781 unsigned int address_size = debug_addr_info [i]->pointer_size;
7782
7783 printf (_(" For compilation unit at offset %#" PRIx64 ":\n"),
7784 debug_addr_info [i]->cu_offset);
7785
7786 printf (_("\tIndex\tAddress\n"));
7787 entry = section->start + debug_addr_info [i]->addr_base;
7788 if (debug_addr_info [i]->dwarf_version >= 5)
7789 {
7790 size_t header_size = entry - header;
7791 unsigned char *curr_header = header;
7792 uint64_t length;
7793 int version;
7794 int segment_selector_size;
7795
7796 if (header_size != 8 && header_size != 16)
7797 {
7798 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %zd instead\n"),
7799 section->name, header_size);
7800 return 0;
7801 }
7802
7803 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7804 if (length == 0xffffffff)
7805 SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7806 if (length > (size_t) (section->start + section->size - curr_header)
7807 || length < (size_t) (entry - curr_header))
7808 {
7809 warn (_("Corrupt %s section: unit_length field of %#" PRIx64
7810 " is invalid\n"), section->name, length);
7811 return 0;
7812 }
7813 end = curr_header + length;
7814 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7815 if (version != 5)
7816 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7817 section->name, version);
7818
7819 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7820 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7821 address_size += segment_selector_size;
7822 }
7823 else
7824 end = section->start + debug_addr_info [i + 1]->addr_base;
7825
7826 header = end;
7827 idx = 0;
7828
7829 if (address_size < 1 || address_size > sizeof (uint64_t))
7830 {
7831 warn (_("Corrupt %s section: address size (%x) is wrong"),
7832 section->name, address_size);
7833 return 0;
7834 }
7835
7836 while ((size_t) (end - entry) >= address_size)
7837 {
7838 uint64_t base = byte_get (entry, address_size);
7839 printf (_("\t%d:\t"), idx);
7840 print_hex_ns (base, address_size);
7841 printf ("\n");
7842 entry += address_size;
7843 idx++;
7844 }
7845 }
7846 printf ("\n");
7847
7848 free (debug_addr_info);
7849 return 1;
7850 }
7851
7852 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
7853
7854 static int
7855 display_debug_str_offsets (struct dwarf_section *section,
7856 void *file ATTRIBUTE_UNUSED)
7857 {
7858 unsigned long idx;
7859
7860 if (section->size == 0)
7861 {
7862 printf (_("\nThe %s section is empty.\n"), section->name);
7863 return 0;
7864 }
7865
7866 unsigned char *start = section->start;
7867 unsigned char *end = start + section->size;
7868 unsigned char *curr = start;
7869 uint64_t debug_str_offsets_hdr_len;
7870
7871 const char *suffix = strrchr (section->name, '.');
7872 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
7873
7874 if (dwo)
7875 load_debug_section_with_follow (str_dwo, file);
7876 else
7877 load_debug_section_with_follow (str, file);
7878
7879 introduce (section, false);
7880
7881 while (curr < end)
7882 {
7883 uint64_t length;
7884 uint64_t entry_length;
7885
7886 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7887 /* FIXME: We assume that this means 64-bit DWARF is being used. */
7888 if (length == 0xffffffff)
7889 {
7890 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
7891 entry_length = 8;
7892 debug_str_offsets_hdr_len = 16;
7893 }
7894 else
7895 {
7896 entry_length = 4;
7897 debug_str_offsets_hdr_len = 8;
7898 }
7899
7900 unsigned char *entries_end;
7901 if (length == 0)
7902 {
7903 /* This is probably an old style .debug_str_offset section which
7904 just contains offsets and no header (and the first offset is 0). */
7905 length = section->size;
7906 curr = section->start;
7907 entries_end = end;
7908
7909 printf (_(" Length: %#" PRIx64 "\n"), length);
7910 printf (_(" Index Offset [String]\n"));
7911 }
7912 else
7913 {
7914 if (length <= (size_t) (end - curr))
7915 entries_end = curr + length;
7916 else
7917 {
7918 warn (_("Section %s is too small %#" PRIx64 "\n"),
7919 section->name, section->size);
7920 entries_end = end;
7921 }
7922
7923 int version;
7924 SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
7925 if (version != 5)
7926 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7927
7928 int padding;
7929 SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
7930 if (padding != 0)
7931 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7932
7933 printf (_(" Length: %#" PRIx64 "\n"), length);
7934 printf (_(" Version: %#x\n"), version);
7935 printf (_(" Index Offset [String]\n"));
7936 }
7937
7938 for (idx = 0; curr < entries_end; idx++)
7939 {
7940 uint64_t offset;
7941 const unsigned char * string;
7942
7943 if ((size_t) (entries_end - curr) < entry_length)
7944 /* Not enough space to read one entry_length, give up. */
7945 return 0;
7946
7947 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
7948 if (dwo)
7949 string = (const unsigned char *)
7950 fetch_indexed_string (idx, NULL, entry_length, dwo, debug_str_offsets_hdr_len);
7951 else
7952 string = fetch_indirect_string (offset);
7953
7954 printf (" %8lu ", idx);
7955 print_hex (offset, entry_length);
7956 printf (" %s\n", string);
7957 }
7958 }
7959
7960 return 1;
7961 }
7962
7963 /* Each debug_information[x].range_lists[y] gets this representation for
7964 sorting purposes. */
7965
7966 struct range_entry
7967 {
7968 /* The debug_information[x].range_lists[y] value. */
7969 uint64_t ranges_offset;
7970
7971 /* Original debug_information to find parameters of the data. */
7972 debug_info *debug_info_p;
7973 };
7974
7975 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
7976
7977 static int
7978 range_entry_compar (const void *ap, const void *bp)
7979 {
7980 const struct range_entry *a_re = (const struct range_entry *) ap;
7981 const struct range_entry *b_re = (const struct range_entry *) bp;
7982 const uint64_t a = a_re->ranges_offset;
7983 const uint64_t b = b_re->ranges_offset;
7984
7985 return (a > b) - (b > a);
7986 }
7987
7988 static void
7989 display_debug_ranges_list (unsigned char * start,
7990 unsigned char * finish,
7991 unsigned int pointer_size,
7992 uint64_t offset,
7993 uint64_t base_address)
7994 {
7995 while (start < finish)
7996 {
7997 uint64_t begin;
7998 uint64_t end;
7999
8000 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8001 if (start >= finish)
8002 break;
8003 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8004
8005 printf (" ");
8006 print_hex (offset, 4);
8007
8008 if (begin == 0 && end == 0)
8009 {
8010 printf (_("<End of list>\n"));
8011 break;
8012 }
8013
8014 /* Check base address specifiers. */
8015 if (is_max_address (begin, pointer_size)
8016 && !is_max_address (end, pointer_size))
8017 {
8018 base_address = end;
8019 print_hex (begin, pointer_size);
8020 print_hex (end, pointer_size);
8021 printf ("(base address)\n");
8022 continue;
8023 }
8024
8025 print_hex (begin + base_address, pointer_size);
8026 print_hex_ns (end + base_address, pointer_size);
8027
8028 if (begin == end)
8029 fputs (_(" (start == end)"), stdout);
8030 else if (begin > end)
8031 fputs (_(" (start > end)"), stdout);
8032
8033 putchar ('\n');
8034 }
8035 }
8036
8037 static unsigned char *
8038 display_debug_rnglists_list (unsigned char * start,
8039 unsigned char * finish,
8040 unsigned int pointer_size,
8041 uint64_t offset,
8042 uint64_t base_address,
8043 unsigned int offset_size)
8044 {
8045 unsigned char *next = start;
8046 unsigned int debug_addr_section_hdr_len;
8047
8048 if (offset_size == 4)
8049 debug_addr_section_hdr_len = 8;
8050 else
8051 debug_addr_section_hdr_len = 16;
8052
8053 while (1)
8054 {
8055 uint64_t off = offset + (start - next);
8056 enum dwarf_range_list_entry rlet;
8057 /* Initialize it due to a false compiler warning. */
8058 uint64_t begin = -1, length, end = -1;
8059
8060 if (start >= finish)
8061 {
8062 warn (_("Range list starting at offset %#" PRIx64
8063 " is not terminated.\n"), offset);
8064 break;
8065 }
8066
8067 printf (" ");
8068 print_hex (off, 4);
8069
8070 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
8071
8072 switch (rlet)
8073 {
8074 case DW_RLE_end_of_list:
8075 printf (_("<End of list>\n"));
8076 break;
8077 case DW_RLE_base_addressx:
8078 READ_ULEB (base_address, start, finish);
8079 print_hex (base_address, pointer_size);
8080 printf (_("(base address index) "));
8081 base_address = fetch_indexed_addr ((base_address * pointer_size)
8082 + debug_addr_section_hdr_len, pointer_size);
8083 print_hex (base_address, pointer_size);
8084 printf (_("(base address)\n"));
8085 break;
8086 case DW_RLE_startx_endx:
8087 READ_ULEB (begin, start, finish);
8088 READ_ULEB (end, start, finish);
8089 begin = fetch_indexed_addr ((begin * pointer_size)
8090 + debug_addr_section_hdr_len, pointer_size);
8091 end = fetch_indexed_addr ((begin * pointer_size)
8092 + debug_addr_section_hdr_len, pointer_size);
8093 break;
8094 case DW_RLE_startx_length:
8095 READ_ULEB (begin, start, finish);
8096 READ_ULEB (length, start, finish);
8097 begin = fetch_indexed_addr ((begin * pointer_size)
8098 + debug_addr_section_hdr_len, pointer_size);
8099 end = begin + length;
8100 break;
8101 case DW_RLE_offset_pair:
8102 READ_ULEB (begin, start, finish);
8103 READ_ULEB (end, start, finish);
8104 break;
8105 case DW_RLE_base_address:
8106 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
8107 print_hex (base_address, pointer_size);
8108 printf (_("(base address)\n"));
8109 break;
8110 case DW_RLE_start_end:
8111 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8112 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8113 break;
8114 case DW_RLE_start_length:
8115 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8116 READ_ULEB (length, start, finish);
8117 end = begin + length;
8118 break;
8119 default:
8120 error (_("Invalid range list entry type %d\n"), rlet);
8121 rlet = DW_RLE_end_of_list;
8122 break;
8123 }
8124
8125 if (rlet == DW_RLE_end_of_list)
8126 break;
8127 if (rlet == DW_RLE_base_address || rlet == DW_RLE_base_addressx)
8128 continue;
8129
8130 /* Only a DW_RLE_offset_pair needs the base address added. */
8131 if (rlet == DW_RLE_offset_pair)
8132 {
8133 begin += base_address;
8134 end += base_address;
8135 }
8136
8137 print_hex (begin, pointer_size);
8138 print_hex (end, pointer_size);
8139
8140 if (begin == end)
8141 fputs (_(" (start == end)"), stdout);
8142 else if (begin > end)
8143 fputs (_(" (start > end)"), stdout);
8144
8145 putchar ('\n');
8146 }
8147
8148 return start;
8149 }
8150
8151 static int
8152 display_debug_rnglists (struct dwarf_section *section)
8153 {
8154 unsigned char *start = section->start;
8155 unsigned char *finish = start + section->size;
8156
8157 while (start < finish)
8158 {
8159 unsigned char *table_start;
8160 uint64_t offset = start - section->start;
8161 unsigned char *end;
8162 uint64_t initial_length;
8163 unsigned char segment_selector_size;
8164 unsigned int offset_entry_count;
8165 unsigned int i;
8166 unsigned short version;
8167 unsigned char address_size = 0;
8168 unsigned char offset_size;
8169
8170 /* Get and check the length of the block. */
8171 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
8172
8173 if (initial_length == 0xffffffff)
8174 {
8175 /* This section is 64-bit DWARF 3. */
8176 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
8177 offset_size = 8;
8178 }
8179 else
8180 offset_size = 4;
8181
8182 if (initial_length > (size_t) (finish - start))
8183 {
8184 /* If the length field has a relocation against it, then we should
8185 not complain if it is inaccurate (and probably negative).
8186 It is copied from .debug_line handling code. */
8187 if (reloc_at (section, (start - section->start) - offset_size))
8188 initial_length = finish - start;
8189 else
8190 {
8191 warn (_("The length field (%#" PRIx64
8192 ") in the debug_rnglists header is wrong"
8193 " - the section is too small\n"),
8194 initial_length);
8195 return 0;
8196 }
8197 }
8198
8199 end = start + initial_length;
8200
8201 /* Get the other fields in the header. */
8202 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
8203 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
8204 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
8205 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
8206
8207 printf (_(" Table at Offset: %#" PRIx64 ":\n"), offset);
8208 printf (_(" Length: %#" PRIx64 "\n"), initial_length);
8209 printf (_(" DWARF version: %u\n"), version);
8210 printf (_(" Address size: %u\n"), address_size);
8211 printf (_(" Segment size: %u\n"), segment_selector_size);
8212 printf (_(" Offset entries: %u\n"), offset_entry_count);
8213
8214 /* Check the fields. */
8215 if (segment_selector_size != 0)
8216 {
8217 warn (_("The %s section contains "
8218 "unsupported segment selector size: %d.\n"),
8219 section->name, segment_selector_size);
8220 return 0;
8221 }
8222
8223 if (version < 5)
8224 {
8225 warn (_("Only DWARF version 5+ debug_rnglists info "
8226 "is currently supported.\n"));
8227 return 0;
8228 }
8229
8230 table_start = start;
8231
8232 if (offset_entry_count != 0)
8233 {
8234 printf (_("\n Offsets starting at %#tx:\n"),
8235 start - section->start);
8236
8237 for (i = 0; i < offset_entry_count; i++)
8238 {
8239 uint64_t entry;
8240
8241 SAFE_BYTE_GET_AND_INC (entry, start, offset_size, finish);
8242 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
8243 }
8244 }
8245 else
8246 offset_entry_count = 1;
8247
8248 for (i = 0; i < offset_entry_count; i++)
8249 {
8250 uint64_t indx = start - table_start;
8251
8252 offset = start - section->start;
8253 printf (_("\n Offset: %#" PRIx64 ", Index: %#" PRIx64 "\n"),
8254 offset, indx);
8255 printf (_(" Offset Begin End\n"));
8256 start = display_debug_rnglists_list
8257 (start, end, address_size, offset, 0, offset_size);
8258 if (start >= end)
8259 break;
8260 }
8261
8262 start = end;
8263
8264 if (start < finish)
8265 putchar ('\n');
8266 }
8267
8268 putchar ('\n');
8269 return 1;
8270 }
8271
8272 static bool
8273 is_range_list_for_this_section (bool is_rnglists, unsigned int version)
8274 {
8275 if (is_rnglists && version > 4)
8276 return true;
8277
8278 if (! is_rnglists && version < 5)
8279 return true;
8280
8281 return false;
8282 }
8283
8284 static int
8285 display_debug_ranges (struct dwarf_section *section,
8286 void *file ATTRIBUTE_UNUSED)
8287 {
8288 unsigned char *start = section->start;
8289 unsigned char *last_start = start;
8290 uint64_t bytes = section->size;
8291 unsigned char *section_begin = start;
8292 unsigned char *finish = start + bytes;
8293 unsigned int num_range_list, i;
8294 struct range_entry *range_entries;
8295 struct range_entry *range_entry_fill;
8296 bool is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
8297 /* Initialize it due to a false compiler warning. */
8298 unsigned char address_size = 0;
8299 uint64_t last_offset = 0;
8300
8301 if (bytes == 0)
8302 {
8303 printf (_("\nThe %s section is empty.\n"), section->name);
8304 return 0;
8305 }
8306
8307 introduce (section, false);
8308
8309 if (is_rnglists)
8310 return display_debug_rnglists (section);
8311
8312 if (load_debug_info (file) == 0)
8313 {
8314 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
8315 section->name);
8316 return 0;
8317 }
8318
8319 num_range_list = 0;
8320 for (i = 0; i < num_debug_info_entries; i++)
8321 num_range_list += debug_information [i].num_range_lists;
8322
8323 if (num_range_list == 0)
8324 {
8325 /* This can happen when the file was compiled with -gsplit-debug
8326 which removes references to range lists from the primary .o file. */
8327 printf (_("No range lists referenced by .debug_info section.\n"));
8328 return 1;
8329 }
8330
8331 range_entries = (struct range_entry *)
8332 xmalloc (sizeof (*range_entries) * num_range_list);
8333 range_entry_fill = range_entries;
8334
8335 for (i = 0; i < num_debug_info_entries; i++)
8336 {
8337 debug_info *debug_info_p = &debug_information[i];
8338 unsigned int j;
8339
8340 for (j = 0; j < debug_info_p->num_range_lists; j++)
8341 {
8342 if (is_range_list_for_this_section (is_rnglists, debug_info_p->range_versions[j]))
8343 {
8344 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
8345 range_entry_fill->debug_info_p = debug_info_p;
8346 range_entry_fill++;
8347 }
8348 }
8349 }
8350
8351 num_range_list = range_entry_fill - range_entries;
8352 qsort (range_entries, num_range_list, sizeof (*range_entries),
8353 range_entry_compar);
8354
8355 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
8356 warn (_("Range lists in %s section start at %#" PRIx64 "\n"),
8357 section->name, range_entries[0].ranges_offset);
8358
8359 putchar ('\n');
8360 printf (_(" Offset Begin End\n"));
8361
8362 for (i = 0; i < num_range_list; i++)
8363 {
8364 struct range_entry *range_entry = &range_entries[i];
8365 debug_info *debug_info_p = range_entry->debug_info_p;
8366 unsigned int pointer_size;
8367 uint64_t offset;
8368 unsigned char *next;
8369 uint64_t base_address;
8370
8371 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
8372 offset = range_entry->ranges_offset;
8373 base_address = debug_info_p->base_address;
8374
8375 /* PR 17512: file: 001-101485-0.001:0.1. */
8376 if (pointer_size < 2 || pointer_size > 8)
8377 {
8378 warn (_("Corrupt pointer size (%d) in debug entry at offset %#" PRIx64 "\n"),
8379 pointer_size, offset);
8380 continue;
8381 }
8382
8383 if (offset > (size_t) (finish - section_begin))
8384 {
8385 warn (_("Corrupt offset (%#" PRIx64 ") in range entry %u\n"),
8386 offset, i);
8387 continue;
8388 }
8389
8390 next = section_begin + offset + debug_info_p->rnglists_base;
8391
8392 /* If multiple DWARF entities reference the same range then we will
8393 have multiple entries in the `range_entries' list for the same
8394 offset. Thanks to the sort above these will all be consecutive in
8395 the `range_entries' list, so we can easily ignore duplicates
8396 here. */
8397 if (i > 0 && last_offset == offset)
8398 continue;
8399 last_offset = offset;
8400
8401 if (dwarf_check != 0 && i > 0)
8402 {
8403 if (start < next)
8404 warn (_("There is a hole [%#tx - %#tx] in %s section.\n"),
8405 start - section_begin, next - section_begin, section->name);
8406 else if (start > next)
8407 {
8408 if (next == last_start)
8409 continue;
8410 warn (_("There is an overlap [%#tx - %#tx] in %s section.\n"),
8411 start - section_begin, next - section_begin, section->name);
8412 }
8413 }
8414
8415 start = next;
8416 last_start = next;
8417
8418 display_debug_ranges_list
8419 (start, finish, pointer_size, offset, base_address);
8420 }
8421 putchar ('\n');
8422
8423 free (range_entries);
8424
8425 return 1;
8426 }
8427
8428 typedef struct Frame_Chunk
8429 {
8430 struct Frame_Chunk *next;
8431 unsigned char *chunk_start;
8432 unsigned int ncols;
8433 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8434 short int *col_type;
8435 int64_t *col_offset;
8436 char *augmentation;
8437 unsigned int code_factor;
8438 int data_factor;
8439 uint64_t pc_begin;
8440 uint64_t pc_range;
8441 unsigned int cfa_reg;
8442 uint64_t cfa_offset;
8443 unsigned int ra;
8444 unsigned char fde_encoding;
8445 unsigned char cfa_exp;
8446 unsigned char ptr_size;
8447 unsigned char segment_size;
8448 }
8449 Frame_Chunk;
8450
8451 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8452 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
8453 static const char *const *dwarf_regnames;
8454 static unsigned int dwarf_regnames_count;
8455 static bool is_aarch64;
8456
8457 /* A marker for a col_type that means this column was never referenced
8458 in the frame info. */
8459 #define DW_CFA_unreferenced (-1)
8460
8461 /* Return 0 if no more space is needed, 1 if more space is needed,
8462 -1 for invalid reg. */
8463
8464 static int
8465 frame_need_space (Frame_Chunk *fc, unsigned int reg)
8466 {
8467 unsigned int prev = fc->ncols;
8468
8469 if (reg < (unsigned int) fc->ncols)
8470 return 0;
8471
8472 if (dwarf_regnames_count > 0
8473 && reg > dwarf_regnames_count)
8474 return -1;
8475
8476 fc->ncols = reg + 1;
8477 /* PR 17512: file: 10450-2643-0.004.
8478 If reg == -1 then this can happen... */
8479 if (fc->ncols == 0)
8480 return -1;
8481
8482 /* PR 17512: file: 2844a11d. */
8483 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
8484 {
8485 error (_("Unfeasibly large register number: %u\n"), reg);
8486 fc->ncols = 0;
8487 /* FIXME: 1024 is an arbitrary limit. Increase it if
8488 we ever encounter a valid binary that exceeds it. */
8489 return -1;
8490 }
8491
8492 fc->col_type = xcrealloc (fc->col_type, fc->ncols,
8493 sizeof (*fc->col_type));
8494 fc->col_offset = xcrealloc (fc->col_offset, fc->ncols,
8495 sizeof (*fc->col_offset));
8496 /* PR 17512: file:002-10025-0.005. */
8497 if (fc->col_type == NULL || fc->col_offset == NULL)
8498 {
8499 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8500 fc->ncols);
8501 fc->ncols = 0;
8502 return -1;
8503 }
8504
8505 while (prev < fc->ncols)
8506 {
8507 fc->col_type[prev] = DW_CFA_unreferenced;
8508 fc->col_offset[prev] = 0;
8509 prev++;
8510 }
8511 return 1;
8512 }
8513
8514 static const char *const dwarf_regnames_i386[] =
8515 {
8516 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8517 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8518 "eip", "eflags", NULL, /* 8 - 10 */
8519 "st0", "st1", "st2", "st3", /* 11 - 14 */
8520 "st4", "st5", "st6", "st7", /* 15 - 18 */
8521 NULL, NULL, /* 19 - 20 */
8522 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8523 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8524 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8525 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8526 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8527 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8528 "tr", "ldtr", /* 48 - 49 */
8529 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8530 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8531 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8532 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8533 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8534 NULL, NULL, NULL, /* 90 - 92 */
8535 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
8536 };
8537
8538 static const char *const dwarf_regnames_iamcu[] =
8539 {
8540 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8541 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8542 "eip", "eflags", NULL, /* 8 - 10 */
8543 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8544 NULL, NULL, /* 19 - 20 */
8545 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8546 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8547 NULL, NULL, NULL, /* 37 - 39 */
8548 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8549 "tr", "ldtr", /* 48 - 49 */
8550 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8551 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8552 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8553 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8554 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8555 NULL, NULL, NULL, /* 90 - 92 */
8556 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8557 };
8558
8559 static void
8560 init_dwarf_regnames_i386 (void)
8561 {
8562 dwarf_regnames = dwarf_regnames_i386;
8563 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8564 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8565 }
8566
8567 static void
8568 init_dwarf_regnames_iamcu (void)
8569 {
8570 dwarf_regnames = dwarf_regnames_iamcu;
8571 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8572 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8573 }
8574
8575 static const char *const DW_CFA_GNU_window_save_name[] =
8576 {
8577 "DW_CFA_GNU_window_save",
8578 "DW_CFA_AARCH64_negate_ra_state"
8579 };
8580
8581 static const char *const dwarf_regnames_x86_64[] =
8582 {
8583 "rax", "rdx", "rcx", "rbx",
8584 "rsi", "rdi", "rbp", "rsp",
8585 "r8", "r9", "r10", "r11",
8586 "r12", "r13", "r14", "r15",
8587 "rip",
8588 "xmm0", "xmm1", "xmm2", "xmm3",
8589 "xmm4", "xmm5", "xmm6", "xmm7",
8590 "xmm8", "xmm9", "xmm10", "xmm11",
8591 "xmm12", "xmm13", "xmm14", "xmm15",
8592 "st0", "st1", "st2", "st3",
8593 "st4", "st5", "st6", "st7",
8594 "mm0", "mm1", "mm2", "mm3",
8595 "mm4", "mm5", "mm6", "mm7",
8596 "rflags",
8597 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8598 "fs.base", "gs.base", NULL, NULL,
8599 "tr", "ldtr",
8600 "mxcsr", "fcw", "fsw",
8601 "xmm16", "xmm17", "xmm18", "xmm19",
8602 "xmm20", "xmm21", "xmm22", "xmm23",
8603 "xmm24", "xmm25", "xmm26", "xmm27",
8604 "xmm28", "xmm29", "xmm30", "xmm31",
8605 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8606 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8607 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8608 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8609 NULL, NULL, NULL, /* 115 - 117 */
8610 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8611 };
8612
8613 static void
8614 init_dwarf_regnames_x86_64 (void)
8615 {
8616 dwarf_regnames = dwarf_regnames_x86_64;
8617 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8618 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8619 }
8620
8621 static const char *const dwarf_regnames_aarch64[] =
8622 {
8623 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8624 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8625 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8626 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8627 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
8628 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8629 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8630 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8631 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8632 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8633 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8634 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8635 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8636 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8637 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8638 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8639 };
8640
8641 static void
8642 init_dwarf_regnames_aarch64 (void)
8643 {
8644 dwarf_regnames = dwarf_regnames_aarch64;
8645 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8646 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8647 is_aarch64 = true;
8648 }
8649
8650 static const char *const dwarf_regnames_s390[] =
8651 {
8652 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8653 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8654 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8655 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8656 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8657 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8658 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8659 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8660 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8661 "pswm", "pswa",
8662 NULL, NULL,
8663 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8664 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8665 };
8666
8667 static void
8668 init_dwarf_regnames_s390 (void)
8669 {
8670 dwarf_regnames = dwarf_regnames_s390;
8671 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8672 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8673 }
8674
8675 static const char *const dwarf_regnames_riscv[] =
8676 {
8677 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8678 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8679 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8680 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8681 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8682 "fs0", "fs1", /* 40 - 41 */
8683 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8684 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8685 "fs10", "fs11", /* 58 - 59 */
8686 "ft8", "ft9", "ft10", "ft11", /* 60 - 63 */
8687 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 64 - 71 */
8688 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 72 - 79 */
8689 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 80 - 87 */
8690 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 88 - 95 */
8691 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", /* 96 - 103 */
8692 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", /* 104 - 111 */
8693 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", /* 112 - 119 */
8694 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", /* 120 - 127 */
8695 };
8696
8697 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8698 the large number of CSRs. */
8699
8700 static const char *
8701 regname_internal_riscv (unsigned int regno)
8702 {
8703 const char *name = NULL;
8704
8705 /* Lookup in the table first, this covers GPR and FPR. */
8706 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8707 name = dwarf_regnames_riscv [regno];
8708 else if (regno >= 4096 && regno <= 8191)
8709 {
8710 /* This might be a CSR, these live in a sparse number space from 4096
8711 to 8191 These numbers are defined in the RISC-V ELF ABI
8712 document. */
8713 switch (regno)
8714 {
8715 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8716 case VALUE + 4096: name = #NAME; break;
8717 #include "opcode/riscv-opc.h"
8718 #undef DECLARE_CSR
8719
8720 default:
8721 {
8722 static char csr_name[10];
8723 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8724 name = csr_name;
8725 }
8726 break;
8727 }
8728 }
8729
8730 return name;
8731 }
8732
8733 static void
8734 init_dwarf_regnames_riscv (void)
8735 {
8736 dwarf_regnames = NULL;
8737 dwarf_regnames_count = 8192;
8738 dwarf_regnames_lookup_func = regname_internal_riscv;
8739 }
8740
8741 void
8742 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8743 {
8744 dwarf_regnames_lookup_func = NULL;
8745 is_aarch64 = false;
8746
8747 switch (e_machine)
8748 {
8749 case EM_386:
8750 init_dwarf_regnames_i386 ();
8751 break;
8752
8753 case EM_IAMCU:
8754 init_dwarf_regnames_iamcu ();
8755 break;
8756
8757 case EM_X86_64:
8758 case EM_L1OM:
8759 case EM_K1OM:
8760 init_dwarf_regnames_x86_64 ();
8761 break;
8762
8763 case EM_AARCH64:
8764 init_dwarf_regnames_aarch64 ();
8765 break;
8766
8767 case EM_S390:
8768 init_dwarf_regnames_s390 ();
8769 break;
8770
8771 case EM_RISCV:
8772 init_dwarf_regnames_riscv ();
8773 break;
8774
8775 default:
8776 break;
8777 }
8778 }
8779
8780 /* Initialize the DWARF register name lookup state based on the
8781 architecture and specific machine type of a BFD. */
8782
8783 void
8784 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8785 unsigned long mach)
8786 {
8787 dwarf_regnames_lookup_func = NULL;
8788 is_aarch64 = false;
8789
8790 switch (arch)
8791 {
8792 case bfd_arch_i386:
8793 switch (mach)
8794 {
8795 case bfd_mach_x86_64:
8796 case bfd_mach_x86_64_intel_syntax:
8797 case bfd_mach_x64_32:
8798 case bfd_mach_x64_32_intel_syntax:
8799 init_dwarf_regnames_x86_64 ();
8800 break;
8801
8802 default:
8803 init_dwarf_regnames_i386 ();
8804 break;
8805 }
8806 break;
8807
8808 case bfd_arch_iamcu:
8809 init_dwarf_regnames_iamcu ();
8810 break;
8811
8812 case bfd_arch_aarch64:
8813 init_dwarf_regnames_aarch64();
8814 break;
8815
8816 case bfd_arch_s390:
8817 init_dwarf_regnames_s390 ();
8818 break;
8819
8820 case bfd_arch_riscv:
8821 init_dwarf_regnames_riscv ();
8822 break;
8823
8824 default:
8825 break;
8826 }
8827 }
8828
8829 static const char *
8830 regname_internal_by_table_only (unsigned int regno)
8831 {
8832 if (dwarf_regnames != NULL
8833 && regno < dwarf_regnames_count
8834 && dwarf_regnames [regno] != NULL)
8835 return dwarf_regnames [regno];
8836
8837 return NULL;
8838 }
8839
8840 static const char *
8841 regname (unsigned int regno, int name_only_p)
8842 {
8843 static char reg[64];
8844
8845 const char *name = NULL;
8846
8847 if (dwarf_regnames_lookup_func != NULL)
8848 name = dwarf_regnames_lookup_func (regno);
8849
8850 if (name != NULL)
8851 {
8852 if (name_only_p)
8853 return name;
8854 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
8855 }
8856 else
8857 snprintf (reg, sizeof (reg), "r%d", regno);
8858 return reg;
8859 }
8860
8861 static void
8862 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
8863 {
8864 unsigned int r;
8865 char tmp[100];
8866
8867 if (*max_regs != fc->ncols)
8868 *max_regs = fc->ncols;
8869
8870 if (*need_col_headers)
8871 {
8872 *need_col_headers = 0;
8873
8874 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
8875
8876 for (r = 0; r < *max_regs; r++)
8877 if (fc->col_type[r] != DW_CFA_unreferenced)
8878 {
8879 if (r == fc->ra)
8880 printf ("ra ");
8881 else
8882 printf ("%-5s ", regname (r, 1));
8883 }
8884
8885 printf ("\n");
8886 }
8887
8888 print_hex (fc->pc_begin, eh_addr_size);
8889 if (fc->cfa_exp)
8890 strcpy (tmp, "exp");
8891 else
8892 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
8893 printf ("%-8s ", tmp);
8894
8895 for (r = 0; r < fc->ncols; r++)
8896 {
8897 if (fc->col_type[r] != DW_CFA_unreferenced)
8898 {
8899 switch (fc->col_type[r])
8900 {
8901 case DW_CFA_undefined:
8902 strcpy (tmp, "u");
8903 break;
8904 case DW_CFA_same_value:
8905 strcpy (tmp, "s");
8906 break;
8907 case DW_CFA_offset:
8908 sprintf (tmp, "c%+" PRId64, fc->col_offset[r]);
8909 break;
8910 case DW_CFA_val_offset:
8911 sprintf (tmp, "v%+" PRId64, fc->col_offset[r]);
8912 break;
8913 case DW_CFA_register:
8914 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
8915 break;
8916 case DW_CFA_expression:
8917 strcpy (tmp, "exp");
8918 break;
8919 case DW_CFA_val_expression:
8920 strcpy (tmp, "vexp");
8921 break;
8922 default:
8923 strcpy (tmp, "n/a");
8924 break;
8925 }
8926 printf ("%-5s ", tmp);
8927 }
8928 }
8929 printf ("\n");
8930 }
8931
8932 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
8933
8934 static unsigned char *
8935 read_cie (unsigned char *start, unsigned char *end,
8936 Frame_Chunk **p_cie, int *p_version,
8937 uint64_t *p_aug_len, unsigned char **p_aug)
8938 {
8939 int version;
8940 Frame_Chunk *fc;
8941 unsigned char *augmentation_data = NULL;
8942 uint64_t augmentation_data_len = 0;
8943
8944 * p_cie = NULL;
8945 /* PR 17512: file: 001-228113-0.004. */
8946 if (start >= end)
8947 return end;
8948
8949 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8950 memset (fc, 0, sizeof (Frame_Chunk));
8951
8952 fc->col_type = xmalloc (sizeof (*fc->col_type));
8953 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
8954
8955 version = *start++;
8956
8957 fc->augmentation = (char *) start;
8958 /* PR 17512: file: 001-228113-0.004.
8959 Skip past augmentation name, but avoid running off the end of the data. */
8960 while (start < end)
8961 if (* start ++ == '\0')
8962 break;
8963 if (start == end)
8964 {
8965 warn (_("No terminator for augmentation name\n"));
8966 goto fail;
8967 }
8968
8969 if (strcmp (fc->augmentation, "eh") == 0)
8970 {
8971 if (eh_addr_size > (size_t) (end - start))
8972 goto fail;
8973 start += eh_addr_size;
8974 }
8975
8976 if (version >= 4)
8977 {
8978 if (2 > (size_t) (end - start))
8979 goto fail;
8980 GET (fc->ptr_size, 1);
8981 if (fc->ptr_size < 1 || fc->ptr_size > 8)
8982 {
8983 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
8984 goto fail;
8985 }
8986
8987 GET (fc->segment_size, 1);
8988 /* PR 17512: file: e99d2804. */
8989 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
8990 {
8991 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
8992 goto fail;
8993 }
8994
8995 eh_addr_size = fc->ptr_size;
8996 }
8997 else
8998 {
8999 fc->ptr_size = eh_addr_size;
9000 fc->segment_size = 0;
9001 }
9002
9003 READ_ULEB (fc->code_factor, start, end);
9004 READ_SLEB (fc->data_factor, start, end);
9005
9006 if (start >= end)
9007 goto fail;
9008
9009 if (version == 1)
9010 {
9011 GET (fc->ra, 1);
9012 }
9013 else
9014 {
9015 READ_ULEB (fc->ra, start, end);
9016 }
9017
9018 if (fc->augmentation[0] == 'z')
9019 {
9020 if (start >= end)
9021 goto fail;
9022 READ_ULEB (augmentation_data_len, start, end);
9023 augmentation_data = start;
9024 /* PR 17512: file: 11042-2589-0.004. */
9025 if (augmentation_data_len > (size_t) (end - start))
9026 {
9027 warn (_("Augmentation data too long: %#" PRIx64
9028 ", expected at most %#tx\n"),
9029 augmentation_data_len, end - start);
9030 goto fail;
9031 }
9032 start += augmentation_data_len;
9033 }
9034
9035 if (augmentation_data_len)
9036 {
9037 unsigned char *p;
9038 unsigned char *q;
9039 unsigned char *qend;
9040
9041 p = (unsigned char *) fc->augmentation + 1;
9042 q = augmentation_data;
9043 qend = q + augmentation_data_len;
9044
9045 while (p < end && q < qend)
9046 {
9047 if (*p == 'L')
9048 q++;
9049 else if (*p == 'P')
9050 q += 1 + size_of_encoded_value (*q);
9051 else if (*p == 'R')
9052 fc->fde_encoding = *q++;
9053 else if (*p == 'S')
9054 ;
9055 else if (*p == 'B')
9056 ;
9057 else
9058 break;
9059 p++;
9060 }
9061 /* Note - it is OK if this loop terminates with q < qend.
9062 Padding may have been inserted to align the end of the CIE. */
9063 }
9064
9065 *p_cie = fc;
9066 if (p_version)
9067 *p_version = version;
9068 if (p_aug_len)
9069 {
9070 *p_aug_len = augmentation_data_len;
9071 *p_aug = augmentation_data;
9072 }
9073 return start;
9074
9075 fail:
9076 free (fc->col_offset);
9077 free (fc->col_type);
9078 free (fc);
9079 return end;
9080 }
9081
9082 /* Prints out the contents on the DATA array formatted as unsigned bytes.
9083 If do_wide is not enabled, then formats the output to fit into 80 columns.
9084 PRINTED contains the number of characters already written to the current
9085 output line. */
9086
9087 static void
9088 display_data (size_t printed, const unsigned char *data, size_t len)
9089 {
9090 if (do_wide || len < ((80 - printed) / 3))
9091 for (printed = 0; printed < len; ++printed)
9092 printf (" %02x", data[printed]);
9093 else
9094 {
9095 for (printed = 0; printed < len; ++printed)
9096 {
9097 if (printed % (80 / 3) == 0)
9098 putchar ('\n');
9099 printf (" %02x", data[printed]);
9100 }
9101 }
9102 }
9103
9104 /* Prints out the contents on the augmentation data array.
9105 If do_wide is not enabled, then formats the output to fit into 80 columns. */
9106
9107 static void
9108 display_augmentation_data (const unsigned char * data, uint64_t len)
9109 {
9110 size_t i;
9111
9112 i = printf (_(" Augmentation data: "));
9113 display_data (i, data, len);
9114 }
9115
9116 static int
9117 display_debug_frames (struct dwarf_section *section,
9118 void *file ATTRIBUTE_UNUSED)
9119 {
9120 unsigned char *start = section->start;
9121 unsigned char *end = start + section->size;
9122 unsigned char *section_start = start;
9123 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
9124 Frame_Chunk *remembered_state = NULL;
9125 Frame_Chunk *rs;
9126 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
9127 unsigned int max_regs = 0;
9128 const char *bad_reg = _("bad register: ");
9129 unsigned int saved_eh_addr_size = eh_addr_size;
9130
9131 introduce (section, false);
9132
9133 while (start < end)
9134 {
9135 unsigned char *saved_start;
9136 unsigned char *block_end;
9137 uint64_t length;
9138 uint64_t cie_id;
9139 Frame_Chunk *fc;
9140 Frame_Chunk *cie;
9141 int need_col_headers = 1;
9142 unsigned char *augmentation_data = NULL;
9143 uint64_t augmentation_data_len = 0;
9144 unsigned int encoded_ptr_size = saved_eh_addr_size;
9145 unsigned int offset_size;
9146 bool all_nops;
9147 static Frame_Chunk fde_fc;
9148
9149 saved_start = start;
9150
9151 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
9152
9153 if (length == 0)
9154 {
9155 printf ("\n%08tx ZERO terminator\n\n",
9156 saved_start - section_start);
9157 /* Skip any zero terminators that directly follow.
9158 A corrupt section size could have loaded a whole
9159 slew of zero filled memory bytes. eg
9160 PR 17512: file: 070-19381-0.004. */
9161 while (start < end && * start == 0)
9162 ++ start;
9163 continue;
9164 }
9165
9166 if (length == 0xffffffff)
9167 {
9168 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
9169 offset_size = 8;
9170 }
9171 else
9172 offset_size = 4;
9173
9174 if (length > (size_t) (end - start))
9175 {
9176 warn ("Invalid length %#" PRIx64 " in FDE at %#tx\n",
9177 length, saved_start - section_start);
9178 block_end = end;
9179 }
9180 else
9181 block_end = start + length;
9182
9183 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
9184
9185 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
9186 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
9187 {
9188 int version;
9189 unsigned int mreg;
9190
9191 start = read_cie (start, block_end, &cie, &version,
9192 &augmentation_data_len, &augmentation_data);
9193 /* PR 17512: file: 027-135133-0.005. */
9194 if (cie == NULL)
9195 break;
9196
9197 fc = cie;
9198 fc->next = chunks;
9199 chunks = fc;
9200 fc->chunk_start = saved_start;
9201 mreg = max_regs > 0 ? max_regs - 1 : 0;
9202 if (mreg < fc->ra)
9203 mreg = fc->ra;
9204 if (frame_need_space (fc, mreg) < 0)
9205 break;
9206 if (fc->fde_encoding)
9207 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9208
9209 printf ("\n%08tx ", saved_start - section_start);
9210 print_hex (length, fc->ptr_size);
9211 print_hex (cie_id, offset_size);
9212
9213 if (do_debug_frames_interp)
9214 {
9215 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
9216 fc->code_factor, fc->data_factor, fc->ra);
9217 }
9218 else
9219 {
9220 printf ("CIE\n");
9221 printf (" Version: %d\n", version);
9222 printf (" Augmentation: \"%s\"\n", fc->augmentation);
9223 if (version >= 4)
9224 {
9225 printf (" Pointer Size: %u\n", fc->ptr_size);
9226 printf (" Segment Size: %u\n", fc->segment_size);
9227 }
9228 printf (" Code alignment factor: %u\n", fc->code_factor);
9229 printf (" Data alignment factor: %d\n", fc->data_factor);
9230 printf (" Return address column: %d\n", fc->ra);
9231
9232 if (augmentation_data_len)
9233 display_augmentation_data (augmentation_data, augmentation_data_len);
9234
9235 putchar ('\n');
9236 }
9237 }
9238 else
9239 {
9240 unsigned char *look_for;
9241 unsigned long segment_selector;
9242 uint64_t cie_off;
9243
9244 cie_off = cie_id;
9245 if (is_eh)
9246 {
9247 uint64_t sign = (uint64_t) 1 << (offset_size * 8 - 1);
9248 cie_off = (cie_off ^ sign) - sign;
9249 cie_off = start - 4 - section_start - cie_off;
9250 }
9251
9252 look_for = section_start + cie_off;
9253 if (cie_off <= (size_t) (saved_start - section_start))
9254 {
9255 for (cie = chunks; cie ; cie = cie->next)
9256 if (cie->chunk_start == look_for)
9257 break;
9258 }
9259 else if (cie_off >= section->size)
9260 cie = NULL;
9261 else
9262 {
9263 for (cie = forward_refs; cie ; cie = cie->next)
9264 if (cie->chunk_start == look_for)
9265 break;
9266 if (!cie)
9267 {
9268 unsigned int off_size;
9269 unsigned char *cie_scan;
9270
9271 cie_scan = look_for;
9272 off_size = 4;
9273 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
9274 if (length == 0xffffffff)
9275 {
9276 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
9277 off_size = 8;
9278 }
9279 if (length != 0 && length <= (size_t) (end - cie_scan))
9280 {
9281 uint64_t c_id;
9282 unsigned char *cie_end = cie_scan + length;
9283
9284 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
9285 cie_end);
9286 if (is_eh
9287 ? c_id == 0
9288 : ((off_size == 4 && c_id == DW_CIE_ID)
9289 || (off_size == 8 && c_id == DW64_CIE_ID)))
9290 {
9291 int version;
9292 unsigned int mreg;
9293
9294 read_cie (cie_scan, cie_end, &cie, &version,
9295 &augmentation_data_len, &augmentation_data);
9296 /* PR 17512: file: 3450-2098-0.004. */
9297 if (cie == NULL)
9298 {
9299 warn (_("Failed to read CIE information\n"));
9300 break;
9301 }
9302 cie->next = forward_refs;
9303 forward_refs = cie;
9304 cie->chunk_start = look_for;
9305 mreg = max_regs > 0 ? max_regs - 1 : 0;
9306 if (mreg < cie->ra)
9307 mreg = cie->ra;
9308 if (frame_need_space (cie, mreg) < 0)
9309 {
9310 warn (_("Invalid max register\n"));
9311 break;
9312 }
9313 if (cie->fde_encoding)
9314 encoded_ptr_size
9315 = size_of_encoded_value (cie->fde_encoding);
9316 }
9317 }
9318 }
9319 }
9320
9321 fc = &fde_fc;
9322 memset (fc, 0, sizeof (Frame_Chunk));
9323
9324 if (!cie)
9325 {
9326 fc->ncols = 0;
9327 fc->col_type = xmalloc (sizeof (*fc->col_type));
9328 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
9329 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
9330 {
9331 warn (_("Invalid max register\n"));
9332 break;
9333 }
9334 cie = fc;
9335 fc->augmentation = "";
9336 fc->fde_encoding = 0;
9337 fc->ptr_size = eh_addr_size;
9338 fc->segment_size = 0;
9339 }
9340 else
9341 {
9342 fc->ncols = cie->ncols;
9343 fc->col_type = xcmalloc (fc->ncols, sizeof (*fc->col_type));
9344 fc->col_offset = xcmalloc (fc->ncols, sizeof (*fc->col_offset));
9345 memcpy (fc->col_type, cie->col_type,
9346 fc->ncols * sizeof (*fc->col_type));
9347 memcpy (fc->col_offset, cie->col_offset,
9348 fc->ncols * sizeof (*fc->col_offset));
9349 fc->augmentation = cie->augmentation;
9350 fc->ptr_size = cie->ptr_size;
9351 eh_addr_size = cie->ptr_size;
9352 fc->segment_size = cie->segment_size;
9353 fc->code_factor = cie->code_factor;
9354 fc->data_factor = cie->data_factor;
9355 fc->cfa_reg = cie->cfa_reg;
9356 fc->cfa_offset = cie->cfa_offset;
9357 fc->ra = cie->ra;
9358 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
9359 {
9360 warn (_("Invalid max register\n"));
9361 break;
9362 }
9363 fc->fde_encoding = cie->fde_encoding;
9364 }
9365
9366 if (fc->fde_encoding)
9367 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9368
9369 segment_selector = 0;
9370 if (fc->segment_size)
9371 {
9372 if (fc->segment_size > sizeof (segment_selector))
9373 {
9374 /* PR 17512: file: 9e196b3e. */
9375 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
9376 fc->segment_size = 4;
9377 }
9378 SAFE_BYTE_GET_AND_INC (segment_selector, start,
9379 fc->segment_size, block_end);
9380 }
9381
9382 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
9383 block_end);
9384
9385 /* FIXME: It appears that sometimes the final pc_range value is
9386 encoded in less than encoded_ptr_size bytes. See the x86_64
9387 run of the "objcopy on compressed debug sections" test for an
9388 example of this. */
9389 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
9390 block_end);
9391
9392 if (cie->augmentation[0] == 'z')
9393 {
9394 READ_ULEB (augmentation_data_len, start, block_end);
9395 augmentation_data = start;
9396 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
9397 if (augmentation_data_len > (size_t) (block_end - start))
9398 {
9399 warn (_("Augmentation data too long: %#" PRIx64 ", "
9400 "expected at most %#tx\n"),
9401 augmentation_data_len, block_end - start);
9402 start = block_end;
9403 augmentation_data = NULL;
9404 augmentation_data_len = 0;
9405 }
9406 start += augmentation_data_len;
9407 }
9408
9409 printf ("\n%08tx ", saved_start - section_start);
9410 print_hex (length, fc->ptr_size);
9411 print_hex (cie_id, offset_size);
9412 printf ("FDE ");
9413
9414 if (cie->chunk_start)
9415 printf ("cie=%08tx", cie->chunk_start - section_start);
9416 else
9417 /* Ideally translate "invalid " to 8 chars, trailing space
9418 is optional. */
9419 printf (_("cie=invalid "));
9420
9421 printf (" pc=");
9422 if (fc->segment_size)
9423 printf ("%04lx:", segment_selector);
9424
9425 print_hex_ns (fc->pc_begin, fc->ptr_size);
9426 printf ("..");
9427 print_hex_ns (fc->pc_begin + fc->pc_range, fc->ptr_size);
9428 printf ("\n");
9429
9430 if (! do_debug_frames_interp && augmentation_data_len)
9431 {
9432 display_augmentation_data (augmentation_data, augmentation_data_len);
9433 putchar ('\n');
9434 }
9435 }
9436
9437 /* At this point, fc is the current chunk, cie (if any) is set, and
9438 we're about to interpret instructions for the chunk. */
9439 /* ??? At present we need to do this always, since this sizes the
9440 fc->col_type and fc->col_offset arrays, which we write into always.
9441 We should probably split the interpreted and non-interpreted bits
9442 into two different routines, since there's so much that doesn't
9443 really overlap between them. */
9444 if (1 || do_debug_frames_interp)
9445 {
9446 /* Start by making a pass over the chunk, allocating storage
9447 and taking note of what registers are used. */
9448 unsigned char *tmp = start;
9449
9450 while (start < block_end)
9451 {
9452 unsigned int reg, op, opa;
9453 unsigned long temp;
9454
9455 op = *start++;
9456 opa = op & 0x3f;
9457 if (op & 0xc0)
9458 op &= 0xc0;
9459
9460 /* Warning: if you add any more cases to this switch, be
9461 sure to add them to the corresponding switch below. */
9462 reg = -1u;
9463 switch (op)
9464 {
9465 case DW_CFA_advance_loc:
9466 break;
9467 case DW_CFA_offset:
9468 SKIP_ULEB (start, block_end);
9469 reg = opa;
9470 break;
9471 case DW_CFA_restore:
9472 reg = opa;
9473 break;
9474 case DW_CFA_set_loc:
9475 if ((size_t) (block_end - start) < encoded_ptr_size)
9476 start = block_end;
9477 else
9478 start += encoded_ptr_size;
9479 break;
9480 case DW_CFA_advance_loc1:
9481 if ((size_t) (block_end - start) < 1)
9482 start = block_end;
9483 else
9484 start += 1;
9485 break;
9486 case DW_CFA_advance_loc2:
9487 if ((size_t) (block_end - start) < 2)
9488 start = block_end;
9489 else
9490 start += 2;
9491 break;
9492 case DW_CFA_advance_loc4:
9493 if ((size_t) (block_end - start) < 4)
9494 start = block_end;
9495 else
9496 start += 4;
9497 break;
9498 case DW_CFA_offset_extended:
9499 case DW_CFA_val_offset:
9500 READ_ULEB (reg, start, block_end);
9501 SKIP_ULEB (start, block_end);
9502 break;
9503 case DW_CFA_restore_extended:
9504 READ_ULEB (reg, start, block_end);
9505 break;
9506 case DW_CFA_undefined:
9507 READ_ULEB (reg, start, block_end);
9508 break;
9509 case DW_CFA_same_value:
9510 READ_ULEB (reg, start, block_end);
9511 break;
9512 case DW_CFA_register:
9513 READ_ULEB (reg, start, block_end);
9514 SKIP_ULEB (start, block_end);
9515 break;
9516 case DW_CFA_def_cfa:
9517 SKIP_ULEB (start, block_end);
9518 SKIP_ULEB (start, block_end);
9519 break;
9520 case DW_CFA_def_cfa_register:
9521 SKIP_ULEB (start, block_end);
9522 break;
9523 case DW_CFA_def_cfa_offset:
9524 SKIP_ULEB (start, block_end);
9525 break;
9526 case DW_CFA_def_cfa_expression:
9527 READ_ULEB (temp, start, block_end);
9528 if ((size_t) (block_end - start) < temp)
9529 start = block_end;
9530 else
9531 start += temp;
9532 break;
9533 case DW_CFA_expression:
9534 case DW_CFA_val_expression:
9535 READ_ULEB (reg, start, block_end);
9536 READ_ULEB (temp, start, block_end);
9537 if ((size_t) (block_end - start) < temp)
9538 start = block_end;
9539 else
9540 start += temp;
9541 break;
9542 case DW_CFA_offset_extended_sf:
9543 case DW_CFA_val_offset_sf:
9544 READ_ULEB (reg, start, block_end);
9545 SKIP_SLEB (start, block_end);
9546 break;
9547 case DW_CFA_def_cfa_sf:
9548 SKIP_ULEB (start, block_end);
9549 SKIP_SLEB (start, block_end);
9550 break;
9551 case DW_CFA_def_cfa_offset_sf:
9552 SKIP_SLEB (start, block_end);
9553 break;
9554 case DW_CFA_MIPS_advance_loc8:
9555 if ((size_t) (block_end - start) < 8)
9556 start = block_end;
9557 else
9558 start += 8;
9559 break;
9560 case DW_CFA_GNU_args_size:
9561 SKIP_ULEB (start, block_end);
9562 break;
9563 case DW_CFA_GNU_negative_offset_extended:
9564 READ_ULEB (reg, start, block_end);
9565 SKIP_ULEB (start, block_end);
9566 break;
9567 default:
9568 break;
9569 }
9570 if (reg != -1u && frame_need_space (fc, reg) >= 0)
9571 {
9572 /* Don't leave any reg as DW_CFA_unreferenced so
9573 that frame_display_row prints name of regs in
9574 header, and all referenced regs in each line. */
9575 if (reg >= cie->ncols
9576 || cie->col_type[reg] == DW_CFA_unreferenced)
9577 fc->col_type[reg] = DW_CFA_undefined;
9578 else
9579 fc->col_type[reg] = cie->col_type[reg];
9580 }
9581 }
9582 start = tmp;
9583 }
9584
9585 all_nops = true;
9586
9587 /* Now we know what registers are used, make a second pass over
9588 the chunk, this time actually printing out the info. */
9589
9590 while (start < block_end)
9591 {
9592 unsigned op, opa;
9593 /* Note: It is tempting to use an unsigned long for 'reg' but there
9594 are various functions, notably frame_space_needed() that assume that
9595 reg is an unsigned int. */
9596 unsigned int reg;
9597 int64_t sofs;
9598 uint64_t ofs;
9599 const char *reg_prefix = "";
9600
9601 op = *start++;
9602 opa = op & 0x3f;
9603 if (op & 0xc0)
9604 op &= 0xc0;
9605
9606 /* Make a note if something other than DW_CFA_nop happens. */
9607 if (op != DW_CFA_nop)
9608 all_nops = false;
9609
9610 /* Warning: if you add any more cases to this switch, be
9611 sure to add them to the corresponding switch above. */
9612 switch (op)
9613 {
9614 case DW_CFA_advance_loc:
9615 opa *= fc->code_factor;
9616 if (do_debug_frames_interp)
9617 frame_display_row (fc, &need_col_headers, &max_regs);
9618 else
9619 {
9620 printf (" DW_CFA_advance_loc: %d to ", opa);
9621 print_hex_ns (fc->pc_begin + opa, fc->ptr_size);
9622 printf ("\n");
9623 }
9624 fc->pc_begin += opa;
9625 break;
9626
9627 case DW_CFA_offset:
9628 READ_ULEB (ofs, start, block_end);
9629 ofs *= fc->data_factor;
9630 if (opa >= fc->ncols)
9631 reg_prefix = bad_reg;
9632 if (! do_debug_frames_interp || *reg_prefix != '\0')
9633 printf (" DW_CFA_offset: %s%s at cfa%+" PRId64 "\n",
9634 reg_prefix, regname (opa, 0), ofs);
9635 if (*reg_prefix == '\0')
9636 {
9637 fc->col_type[opa] = DW_CFA_offset;
9638 fc->col_offset[opa] = ofs;
9639 }
9640 break;
9641
9642 case DW_CFA_restore:
9643 if (opa >= fc->ncols)
9644 reg_prefix = bad_reg;
9645 if (! do_debug_frames_interp || *reg_prefix != '\0')
9646 printf (" DW_CFA_restore: %s%s\n",
9647 reg_prefix, regname (opa, 0));
9648 if (*reg_prefix != '\0')
9649 break;
9650
9651 if (opa >= cie->ncols
9652 || cie->col_type[opa] == DW_CFA_unreferenced)
9653 {
9654 fc->col_type[opa] = DW_CFA_undefined;
9655 fc->col_offset[opa] = 0;
9656 }
9657 else
9658 {
9659 fc->col_type[opa] = cie->col_type[opa];
9660 fc->col_offset[opa] = cie->col_offset[opa];
9661 }
9662 break;
9663
9664 case DW_CFA_set_loc:
9665 ofs = get_encoded_value (&start, fc->fde_encoding, section,
9666 block_end);
9667 if (do_debug_frames_interp)
9668 frame_display_row (fc, &need_col_headers, &max_regs);
9669 else
9670 {
9671 printf (" DW_CFA_set_loc: ");
9672 print_hex_ns (ofs, fc->ptr_size);
9673 printf ("\n");
9674 }
9675 fc->pc_begin = ofs;
9676 break;
9677
9678 case DW_CFA_advance_loc1:
9679 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9680 ofs *= fc->code_factor;
9681 if (do_debug_frames_interp)
9682 frame_display_row (fc, &need_col_headers, &max_regs);
9683 else
9684 {
9685 printf (" DW_CFA_advance_loc1: %" PRId64 " to ", ofs);
9686 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9687 printf ("\n");
9688 }
9689 fc->pc_begin += ofs;
9690 break;
9691
9692 case DW_CFA_advance_loc2:
9693 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9694 ofs *= fc->code_factor;
9695 if (do_debug_frames_interp)
9696 frame_display_row (fc, &need_col_headers, &max_regs);
9697 else
9698 {
9699 printf (" DW_CFA_advance_loc2: %" PRId64 " to ", ofs);
9700 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9701 printf ("\n");
9702 }
9703 fc->pc_begin += ofs;
9704 break;
9705
9706 case DW_CFA_advance_loc4:
9707 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9708 ofs *= fc->code_factor;
9709 if (do_debug_frames_interp)
9710 frame_display_row (fc, &need_col_headers, &max_regs);
9711 else
9712 {
9713 printf (" DW_CFA_advance_loc4: %" PRId64 " to ", ofs);
9714 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9715 printf ("\n");
9716 }
9717 fc->pc_begin += ofs;
9718 break;
9719
9720 case DW_CFA_offset_extended:
9721 READ_ULEB (reg, start, block_end);
9722 READ_ULEB (ofs, start, block_end);
9723 ofs *= fc->data_factor;
9724 if (reg >= fc->ncols)
9725 reg_prefix = bad_reg;
9726 if (! do_debug_frames_interp || *reg_prefix != '\0')
9727 printf (" DW_CFA_offset_extended: %s%s at cfa%+" PRId64 "\n",
9728 reg_prefix, regname (reg, 0), ofs);
9729 if (*reg_prefix == '\0')
9730 {
9731 fc->col_type[reg] = DW_CFA_offset;
9732 fc->col_offset[reg] = ofs;
9733 }
9734 break;
9735
9736 case DW_CFA_val_offset:
9737 READ_ULEB (reg, start, block_end);
9738 READ_ULEB (ofs, start, block_end);
9739 ofs *= fc->data_factor;
9740 if (reg >= fc->ncols)
9741 reg_prefix = bad_reg;
9742 if (! do_debug_frames_interp || *reg_prefix != '\0')
9743 printf (" DW_CFA_val_offset: %s%s is cfa%+" PRId64 "\n",
9744 reg_prefix, regname (reg, 0), ofs);
9745 if (*reg_prefix == '\0')
9746 {
9747 fc->col_type[reg] = DW_CFA_val_offset;
9748 fc->col_offset[reg] = ofs;
9749 }
9750 break;
9751
9752 case DW_CFA_restore_extended:
9753 READ_ULEB (reg, start, block_end);
9754 if (reg >= fc->ncols)
9755 reg_prefix = bad_reg;
9756 if (! do_debug_frames_interp || *reg_prefix != '\0')
9757 printf (" DW_CFA_restore_extended: %s%s\n",
9758 reg_prefix, regname (reg, 0));
9759 if (*reg_prefix != '\0')
9760 break;
9761
9762 if (reg >= cie->ncols
9763 || cie->col_type[reg] == DW_CFA_unreferenced)
9764 {
9765 fc->col_type[reg] = DW_CFA_undefined;
9766 fc->col_offset[reg] = 0;
9767 }
9768 else
9769 {
9770 fc->col_type[reg] = cie->col_type[reg];
9771 fc->col_offset[reg] = cie->col_offset[reg];
9772 }
9773 break;
9774
9775 case DW_CFA_undefined:
9776 READ_ULEB (reg, start, block_end);
9777 if (reg >= fc->ncols)
9778 reg_prefix = bad_reg;
9779 if (! do_debug_frames_interp || *reg_prefix != '\0')
9780 printf (" DW_CFA_undefined: %s%s\n",
9781 reg_prefix, regname (reg, 0));
9782 if (*reg_prefix == '\0')
9783 {
9784 fc->col_type[reg] = DW_CFA_undefined;
9785 fc->col_offset[reg] = 0;
9786 }
9787 break;
9788
9789 case DW_CFA_same_value:
9790 READ_ULEB (reg, start, block_end);
9791 if (reg >= fc->ncols)
9792 reg_prefix = bad_reg;
9793 if (! do_debug_frames_interp || *reg_prefix != '\0')
9794 printf (" DW_CFA_same_value: %s%s\n",
9795 reg_prefix, regname (reg, 0));
9796 if (*reg_prefix == '\0')
9797 {
9798 fc->col_type[reg] = DW_CFA_same_value;
9799 fc->col_offset[reg] = 0;
9800 }
9801 break;
9802
9803 case DW_CFA_register:
9804 READ_ULEB (reg, start, block_end);
9805 READ_ULEB (ofs, start, block_end);
9806 if (reg >= fc->ncols)
9807 reg_prefix = bad_reg;
9808 if (! do_debug_frames_interp || *reg_prefix != '\0')
9809 {
9810 printf (" DW_CFA_register: %s%s in ",
9811 reg_prefix, regname (reg, 0));
9812 puts (regname (ofs, 0));
9813 }
9814 if (*reg_prefix == '\0')
9815 {
9816 fc->col_type[reg] = DW_CFA_register;
9817 fc->col_offset[reg] = ofs;
9818 }
9819 break;
9820
9821 case DW_CFA_remember_state:
9822 if (! do_debug_frames_interp)
9823 printf (" DW_CFA_remember_state\n");
9824 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9825 rs->cfa_offset = fc->cfa_offset;
9826 rs->cfa_reg = fc->cfa_reg;
9827 rs->ra = fc->ra;
9828 rs->cfa_exp = fc->cfa_exp;
9829 rs->ncols = fc->ncols;
9830 rs->col_type = xcmalloc (rs->ncols, sizeof (*rs->col_type));
9831 rs->col_offset = xcmalloc (rs->ncols, sizeof (*rs->col_offset));
9832 memcpy (rs->col_type, fc->col_type,
9833 rs->ncols * sizeof (*fc->col_type));
9834 memcpy (rs->col_offset, fc->col_offset,
9835 rs->ncols * sizeof (*fc->col_offset));
9836 rs->next = remembered_state;
9837 remembered_state = rs;
9838 break;
9839
9840 case DW_CFA_restore_state:
9841 if (! do_debug_frames_interp)
9842 printf (" DW_CFA_restore_state\n");
9843 rs = remembered_state;
9844 if (rs)
9845 {
9846 remembered_state = rs->next;
9847 fc->cfa_offset = rs->cfa_offset;
9848 fc->cfa_reg = rs->cfa_reg;
9849 fc->ra = rs->ra;
9850 fc->cfa_exp = rs->cfa_exp;
9851 if (frame_need_space (fc, rs->ncols - 1) < 0)
9852 {
9853 warn (_("Invalid column number in saved frame state\n"));
9854 fc->ncols = 0;
9855 }
9856 else
9857 {
9858 memcpy (fc->col_type, rs->col_type,
9859 rs->ncols * sizeof (*rs->col_type));
9860 memcpy (fc->col_offset, rs->col_offset,
9861 rs->ncols * sizeof (*rs->col_offset));
9862 }
9863 free (rs->col_type);
9864 free (rs->col_offset);
9865 free (rs);
9866 }
9867 else if (do_debug_frames_interp)
9868 printf ("Mismatched DW_CFA_restore_state\n");
9869 break;
9870
9871 case DW_CFA_def_cfa:
9872 READ_ULEB (fc->cfa_reg, start, block_end);
9873 READ_ULEB (fc->cfa_offset, start, block_end);
9874 fc->cfa_exp = 0;
9875 if (! do_debug_frames_interp)
9876 printf (" DW_CFA_def_cfa: %s ofs %d\n",
9877 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
9878 break;
9879
9880 case DW_CFA_def_cfa_register:
9881 READ_ULEB (fc->cfa_reg, start, block_end);
9882 fc->cfa_exp = 0;
9883 if (! do_debug_frames_interp)
9884 printf (" DW_CFA_def_cfa_register: %s\n",
9885 regname (fc->cfa_reg, 0));
9886 break;
9887
9888 case DW_CFA_def_cfa_offset:
9889 READ_ULEB (fc->cfa_offset, start, block_end);
9890 if (! do_debug_frames_interp)
9891 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
9892 break;
9893
9894 case DW_CFA_nop:
9895 if (! do_debug_frames_interp)
9896 printf (" DW_CFA_nop\n");
9897 break;
9898
9899 case DW_CFA_def_cfa_expression:
9900 READ_ULEB (ofs, start, block_end);
9901 if (ofs > (size_t) (block_end - start))
9902 {
9903 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
9904 "DW_CFA_def_cfa_expression", ofs);
9905 break;
9906 }
9907 if (! do_debug_frames_interp)
9908 {
9909 printf (" DW_CFA_def_cfa_expression (");
9910 decode_location_expression (start, eh_addr_size, 0, -1,
9911 ofs, 0, section);
9912 printf (")\n");
9913 }
9914 fc->cfa_exp = 1;
9915 start += ofs;
9916 break;
9917
9918 case DW_CFA_expression:
9919 READ_ULEB (reg, start, block_end);
9920 READ_ULEB (ofs, start, block_end);
9921 if (reg >= fc->ncols)
9922 reg_prefix = bad_reg;
9923 /* PR 17512: file: 069-133014-0.006. */
9924 /* PR 17512: file: 98c02eb4. */
9925 if (ofs > (size_t) (block_end - start))
9926 {
9927 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
9928 "DW_CFA_expression", ofs);
9929 break;
9930 }
9931 if (! do_debug_frames_interp || *reg_prefix != '\0')
9932 {
9933 printf (" DW_CFA_expression: %s%s (",
9934 reg_prefix, regname (reg, 0));
9935 decode_location_expression (start, eh_addr_size, 0, -1,
9936 ofs, 0, section);
9937 printf (")\n");
9938 }
9939 if (*reg_prefix == '\0')
9940 fc->col_type[reg] = DW_CFA_expression;
9941 start += ofs;
9942 break;
9943
9944 case DW_CFA_val_expression:
9945 READ_ULEB (reg, start, block_end);
9946 READ_ULEB (ofs, start, block_end);
9947 if (reg >= fc->ncols)
9948 reg_prefix = bad_reg;
9949 if (ofs > (size_t) (block_end - start))
9950 {
9951 printf (" %s: <corrupt len %" PRIu64 ">\n",
9952 "DW_CFA_val_expression", ofs);
9953 break;
9954 }
9955 if (! do_debug_frames_interp || *reg_prefix != '\0')
9956 {
9957 printf (" DW_CFA_val_expression: %s%s (",
9958 reg_prefix, regname (reg, 0));
9959 decode_location_expression (start, eh_addr_size, 0, -1,
9960 ofs, 0, section);
9961 printf (")\n");
9962 }
9963 if (*reg_prefix == '\0')
9964 fc->col_type[reg] = DW_CFA_val_expression;
9965 start += ofs;
9966 break;
9967
9968 case DW_CFA_offset_extended_sf:
9969 READ_ULEB (reg, start, block_end);
9970 READ_SLEB (sofs, start, block_end);
9971 /* data_factor multiplicaton done here as unsigned to
9972 avoid integer overflow warnings from asan on fuzzed
9973 objects. */
9974 ofs = sofs;
9975 ofs *= fc->data_factor;
9976 if (reg >= fc->ncols)
9977 reg_prefix = bad_reg;
9978 if (! do_debug_frames_interp || *reg_prefix != '\0')
9979 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+" PRId64 "\n",
9980 reg_prefix, regname (reg, 0), ofs);
9981 if (*reg_prefix == '\0')
9982 {
9983 fc->col_type[reg] = DW_CFA_offset;
9984 fc->col_offset[reg] = ofs;
9985 }
9986 break;
9987
9988 case DW_CFA_val_offset_sf:
9989 READ_ULEB (reg, start, block_end);
9990 READ_SLEB (sofs, start, block_end);
9991 ofs = sofs;
9992 ofs *= fc->data_factor;
9993 if (reg >= fc->ncols)
9994 reg_prefix = bad_reg;
9995 if (! do_debug_frames_interp || *reg_prefix != '\0')
9996 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+" PRId64 "\n",
9997 reg_prefix, regname (reg, 0), ofs);
9998 if (*reg_prefix == '\0')
9999 {
10000 fc->col_type[reg] = DW_CFA_val_offset;
10001 fc->col_offset[reg] = ofs;
10002 }
10003 break;
10004
10005 case DW_CFA_def_cfa_sf:
10006 READ_ULEB (fc->cfa_reg, start, block_end);
10007 READ_SLEB (sofs, start, block_end);
10008 ofs = sofs;
10009 ofs *= fc->data_factor;
10010 fc->cfa_offset = ofs;
10011 fc->cfa_exp = 0;
10012 if (! do_debug_frames_interp)
10013 printf (" DW_CFA_def_cfa_sf: %s ofs %" PRId64 "\n",
10014 regname (fc->cfa_reg, 0), ofs);
10015 break;
10016
10017 case DW_CFA_def_cfa_offset_sf:
10018 READ_SLEB (sofs, start, block_end);
10019 ofs = sofs;
10020 ofs *= fc->data_factor;
10021 fc->cfa_offset = ofs;
10022 if (! do_debug_frames_interp)
10023 printf (" DW_CFA_def_cfa_offset_sf: %" PRId64 "\n", ofs);
10024 break;
10025
10026 case DW_CFA_MIPS_advance_loc8:
10027 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
10028 ofs *= fc->code_factor;
10029 if (do_debug_frames_interp)
10030 frame_display_row (fc, &need_col_headers, &max_regs);
10031 else
10032 {
10033 printf (" DW_CFA_MIPS_advance_loc8: %" PRId64 " to ", ofs);
10034 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
10035 printf ("\n");
10036 }
10037 fc->pc_begin += ofs;
10038 break;
10039
10040 case DW_CFA_GNU_window_save:
10041 if (! do_debug_frames_interp)
10042 printf (" %s\n", DW_CFA_GNU_window_save_name[is_aarch64]);
10043 break;
10044
10045 case DW_CFA_GNU_args_size:
10046 READ_ULEB (ofs, start, block_end);
10047 if (! do_debug_frames_interp)
10048 printf (" DW_CFA_GNU_args_size: %" PRIu64 "\n", ofs);
10049 break;
10050
10051 case DW_CFA_GNU_negative_offset_extended:
10052 READ_ULEB (reg, start, block_end);
10053 READ_SLEB (sofs, start, block_end);
10054 ofs = sofs;
10055 ofs = -ofs * fc->data_factor;
10056 if (reg >= fc->ncols)
10057 reg_prefix = bad_reg;
10058 if (! do_debug_frames_interp || *reg_prefix != '\0')
10059 printf (" DW_CFA_GNU_negative_offset_extended: %s%s "
10060 "at cfa%+" PRId64 "\n",
10061 reg_prefix, regname (reg, 0), ofs);
10062 if (*reg_prefix == '\0')
10063 {
10064 fc->col_type[reg] = DW_CFA_offset;
10065 fc->col_offset[reg] = ofs;
10066 }
10067 break;
10068
10069 default:
10070 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
10071 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
10072 else
10073 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
10074 start = block_end;
10075 }
10076 }
10077
10078 /* Interpret the CFA - as long as it is not completely full of NOPs. */
10079 if (do_debug_frames_interp && ! all_nops)
10080 frame_display_row (fc, &need_col_headers, &max_regs);
10081
10082 if (fde_fc.col_type != NULL)
10083 {
10084 free (fde_fc.col_type);
10085 fde_fc.col_type = NULL;
10086 }
10087 if (fde_fc.col_offset != NULL)
10088 {
10089 free (fde_fc.col_offset);
10090 fde_fc.col_offset = NULL;
10091 }
10092
10093 start = block_end;
10094 eh_addr_size = saved_eh_addr_size;
10095 }
10096
10097 printf ("\n");
10098
10099 while (remembered_state != NULL)
10100 {
10101 rs = remembered_state;
10102 remembered_state = rs->next;
10103 free (rs->col_type);
10104 free (rs->col_offset);
10105 rs->next = NULL; /* Paranoia. */
10106 free (rs);
10107 }
10108
10109 while (chunks != NULL)
10110 {
10111 rs = chunks;
10112 chunks = rs->next;
10113 free (rs->col_type);
10114 free (rs->col_offset);
10115 rs->next = NULL; /* Paranoia. */
10116 free (rs);
10117 }
10118
10119 while (forward_refs != NULL)
10120 {
10121 rs = forward_refs;
10122 forward_refs = rs->next;
10123 free (rs->col_type);
10124 free (rs->col_offset);
10125 rs->next = NULL; /* Paranoia. */
10126 free (rs);
10127 }
10128
10129 return 1;
10130 }
10131
10132 #undef GET
10133
10134 static int
10135 display_debug_names (struct dwarf_section *section, void *file)
10136 {
10137 unsigned char *hdrptr = section->start;
10138 uint64_t unit_length;
10139 unsigned char *unit_start;
10140 const unsigned char *const section_end = section->start + section->size;
10141 unsigned char *unit_end;
10142
10143 introduce (section, false);
10144
10145 load_debug_section_with_follow (str, file);
10146
10147 for (; hdrptr < section_end; hdrptr = unit_end)
10148 {
10149 unsigned int offset_size;
10150 uint16_t dwarf_version, padding;
10151 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
10152 uint64_t bucket_count, name_count, abbrev_table_size;
10153 uint32_t augmentation_string_size;
10154 unsigned int i;
10155 bool augmentation_printable;
10156 const char *augmentation_string;
10157 size_t total;
10158
10159 unit_start = hdrptr;
10160
10161 /* Get and check the length of the block. */
10162 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
10163
10164 if (unit_length == 0xffffffff)
10165 {
10166 /* This section is 64-bit DWARF. */
10167 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
10168 offset_size = 8;
10169 }
10170 else
10171 offset_size = 4;
10172
10173 if (unit_length > (size_t) (section_end - hdrptr)
10174 || unit_length < 2 + 2 + 4 * 7)
10175 {
10176 too_short:
10177 warn (_("Debug info is corrupted, %s header at %#tx"
10178 " has length %#" PRIx64 "\n"),
10179 section->name, unit_start - section->start, unit_length);
10180 return 0;
10181 }
10182 unit_end = hdrptr + unit_length;
10183
10184 /* Get and check the version number. */
10185 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
10186 printf (_("Version %d\n"), (int) dwarf_version);
10187
10188 /* Prior versions did not exist, and future versions may not be
10189 backwards compatible. */
10190 if (dwarf_version != 5)
10191 {
10192 warn (_("Only DWARF version 5 .debug_names "
10193 "is currently supported.\n"));
10194 return 0;
10195 }
10196
10197 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
10198 if (padding != 0)
10199 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
10200 padding);
10201
10202 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
10203 if (comp_unit_count == 0)
10204 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
10205
10206 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
10207 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
10208 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
10209 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
10210 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
10211
10212 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
10213 if (augmentation_string_size % 4 != 0)
10214 {
10215 warn (_("Augmentation string length %u must be rounded up "
10216 "to a multiple of 4 in .debug_names.\n"),
10217 augmentation_string_size);
10218 augmentation_string_size += (-augmentation_string_size) & 3;
10219 }
10220 if (augmentation_string_size > (size_t) (unit_end - hdrptr))
10221 goto too_short;
10222
10223 printf (_("Augmentation string:"));
10224
10225 augmentation_printable = true;
10226 augmentation_string = (const char *) hdrptr;
10227
10228 for (i = 0; i < augmentation_string_size; i++)
10229 {
10230 unsigned char uc;
10231
10232 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
10233 printf (" %02x", uc);
10234
10235 if (uc != 0 && !ISPRINT (uc))
10236 augmentation_printable = false;
10237 }
10238
10239 if (augmentation_printable)
10240 {
10241 printf (" (\"");
10242 for (i = 0;
10243 i < augmentation_string_size && augmentation_string[i];
10244 ++i)
10245 putchar (augmentation_string[i]);
10246 printf ("\")");
10247 }
10248 putchar ('\n');
10249
10250 printf (_("CU table:\n"));
10251 if (_mul_overflow (comp_unit_count, offset_size, &total)
10252 || total > (size_t) (unit_end - hdrptr))
10253 goto too_short;
10254 for (i = 0; i < comp_unit_count; i++)
10255 {
10256 uint64_t cu_offset;
10257
10258 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
10259 printf ("[%3u] %#" PRIx64 "\n", i, cu_offset);
10260 }
10261 putchar ('\n');
10262
10263 printf (_("TU table:\n"));
10264 if (_mul_overflow (local_type_unit_count, offset_size, &total)
10265 || total > (size_t) (unit_end - hdrptr))
10266 goto too_short;
10267 for (i = 0; i < local_type_unit_count; i++)
10268 {
10269 uint64_t tu_offset;
10270
10271 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
10272 printf ("[%3u] %#" PRIx64 "\n", i, tu_offset);
10273 }
10274 putchar ('\n');
10275
10276 printf (_("Foreign TU table:\n"));
10277 if (_mul_overflow (foreign_type_unit_count, 8, &total)
10278 || total > (size_t) (unit_end - hdrptr))
10279 goto too_short;
10280 for (i = 0; i < foreign_type_unit_count; i++)
10281 {
10282 uint64_t signature;
10283
10284 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
10285 printf (_("[%3u] "), i);
10286 print_hex_ns (signature, 8);
10287 putchar ('\n');
10288 }
10289 putchar ('\n');
10290
10291 uint64_t xtra = (bucket_count * sizeof (uint32_t)
10292 + name_count * (sizeof (uint32_t) + 2 * offset_size)
10293 + abbrev_table_size);
10294 if (xtra > (size_t) (unit_end - hdrptr))
10295 {
10296 warn (_("Entry pool offset (%#" PRIx64 ") exceeds unit size %#tx "
10297 "for unit %#tx in the debug_names\n"),
10298 xtra, unit_end - unit_start, unit_start - section->start);
10299 return 0;
10300 }
10301 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
10302 hdrptr += bucket_count * sizeof (uint32_t);
10303 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
10304 hdrptr += name_count * sizeof (uint32_t);
10305 unsigned char *const name_table_string_offsets = hdrptr;
10306 hdrptr += name_count * offset_size;
10307 unsigned char *const name_table_entry_offsets = hdrptr;
10308 hdrptr += name_count * offset_size;
10309 unsigned char *const abbrev_table = hdrptr;
10310 hdrptr += abbrev_table_size;
10311 const unsigned char *const abbrev_table_end = hdrptr;
10312 unsigned char *const entry_pool = hdrptr;
10313
10314 size_t buckets_filled = 0;
10315 size_t bucketi;
10316 for (bucketi = 0; bucketi < bucket_count; bucketi++)
10317 {
10318 const uint32_t bucket = hash_table_buckets[bucketi];
10319
10320 if (bucket != 0)
10321 ++buckets_filled;
10322 }
10323 printf (ngettext ("Used %zu of %lu bucket.\n",
10324 "Used %zu of %lu buckets.\n",
10325 (unsigned long) bucket_count),
10326 buckets_filled, (unsigned long) bucket_count);
10327
10328 if (bucket_count != 0)
10329 {
10330 uint32_t hash_prev = 0;
10331 size_t hash_clash_count = 0;
10332 size_t longest_clash = 0;
10333 size_t this_length = 0;
10334 size_t hashi;
10335 for (hashi = 0; hashi < name_count; hashi++)
10336 {
10337 const uint32_t hash_this = hash_table_hashes[hashi];
10338
10339 if (hashi > 0)
10340 {
10341 if (hash_prev % bucket_count == hash_this % bucket_count)
10342 {
10343 ++hash_clash_count;
10344 ++this_length;
10345 longest_clash = MAX (longest_clash, this_length);
10346 }
10347 else
10348 this_length = 0;
10349 }
10350 hash_prev = hash_this;
10351 }
10352 printf (_("Out of %" PRIu64 " items there are %zu bucket clashes"
10353 " (longest of %zu entries).\n"),
10354 name_count, hash_clash_count, longest_clash);
10355
10356 if (name_count != buckets_filled + hash_clash_count)
10357 warn (_("The name_count (%" PRIu64 ")"
10358 " is not the same as the used bucket_count"
10359 " (%zu) + the hash clash count (%zu)"),
10360 name_count, buckets_filled, hash_clash_count);
10361 }
10362
10363 struct abbrev_lookup_entry
10364 {
10365 uint64_t abbrev_tag;
10366 unsigned char *abbrev_lookup_ptr;
10367 };
10368 struct abbrev_lookup_entry *abbrev_lookup = NULL;
10369 size_t abbrev_lookup_used = 0;
10370 size_t abbrev_lookup_allocated = 0;
10371
10372 unsigned char *abbrevptr = abbrev_table;
10373 for (;;)
10374 {
10375 uint64_t abbrev_tag;
10376
10377 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
10378 if (abbrev_tag == 0)
10379 break;
10380 if (abbrev_lookup_used == abbrev_lookup_allocated)
10381 {
10382 abbrev_lookup_allocated = MAX (0x100,
10383 abbrev_lookup_allocated * 2);
10384 abbrev_lookup = xrealloc (abbrev_lookup,
10385 (abbrev_lookup_allocated
10386 * sizeof (*abbrev_lookup)));
10387 }
10388 assert (abbrev_lookup_used < abbrev_lookup_allocated);
10389 struct abbrev_lookup_entry *entry;
10390 for (entry = abbrev_lookup;
10391 entry < abbrev_lookup + abbrev_lookup_used;
10392 entry++)
10393 if (entry->abbrev_tag == abbrev_tag)
10394 {
10395 warn (_("Duplicate abbreviation tag %" PRIu64
10396 " in unit %#tx in the debug_names section\n"),
10397 abbrev_tag, unit_start - section->start);
10398 break;
10399 }
10400 entry = &abbrev_lookup[abbrev_lookup_used++];
10401 entry->abbrev_tag = abbrev_tag;
10402 entry->abbrev_lookup_ptr = abbrevptr;
10403
10404 /* Skip DWARF tag. */
10405 SKIP_ULEB (abbrevptr, abbrev_table_end);
10406 for (;;)
10407 {
10408 uint64_t xindex, form;
10409
10410 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10411 READ_ULEB (form, abbrevptr, abbrev_table_end);
10412 if (xindex == 0 && form == 0)
10413 break;
10414 }
10415 }
10416
10417 printf (_("\nSymbol table:\n"));
10418 uint32_t namei;
10419 for (namei = 0; namei < name_count; ++namei)
10420 {
10421 uint64_t string_offset, entry_offset;
10422 unsigned char *p;
10423
10424 p = name_table_string_offsets + namei * offset_size;
10425 SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
10426 p = name_table_entry_offsets + namei * offset_size;
10427 SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
10428
10429 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
10430 fetch_indirect_string (string_offset));
10431
10432 unsigned char *entryptr = entry_pool + entry_offset;
10433
10434 /* We need to scan first whether there is a single or multiple
10435 entries. TAGNO is -2 for the first entry, it is -1 for the
10436 initial tag read of the second entry, then it becomes 0 for the
10437 first entry for real printing etc. */
10438 int tagno = -2;
10439 /* Initialize it due to a false compiler warning. */
10440 uint64_t second_abbrev_tag = -1;
10441 for (;;)
10442 {
10443 uint64_t abbrev_tag;
10444 uint64_t dwarf_tag;
10445 const struct abbrev_lookup_entry *entry;
10446
10447 READ_ULEB (abbrev_tag, entryptr, unit_end);
10448 if (tagno == -1)
10449 {
10450 second_abbrev_tag = abbrev_tag;
10451 tagno = 0;
10452 entryptr = entry_pool + entry_offset;
10453 continue;
10454 }
10455 if (abbrev_tag == 0)
10456 break;
10457 if (tagno >= 0)
10458 printf ("%s<%" PRIu64 ">",
10459 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
10460 abbrev_tag);
10461
10462 for (entry = abbrev_lookup;
10463 entry < abbrev_lookup + abbrev_lookup_used;
10464 entry++)
10465 if (entry->abbrev_tag == abbrev_tag)
10466 break;
10467 if (entry >= abbrev_lookup + abbrev_lookup_used)
10468 {
10469 warn (_("Undefined abbreviation tag %" PRId64
10470 " in unit %#tx in the debug_names section\n"),
10471 abbrev_tag,
10472 unit_start - section->start);
10473 break;
10474 }
10475 abbrevptr = entry->abbrev_lookup_ptr;
10476 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
10477 if (tagno >= 0)
10478 printf (" %s", get_TAG_name (dwarf_tag));
10479 for (;;)
10480 {
10481 uint64_t xindex, form;
10482
10483 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10484 READ_ULEB (form, abbrevptr, abbrev_table_end);
10485 if (xindex == 0 && form == 0)
10486 break;
10487
10488 if (tagno >= 0)
10489 printf (" %s", get_IDX_name (xindex));
10490 entryptr = read_and_display_attr_value (0, form, 0,
10491 unit_start, entryptr, unit_end,
10492 0, 0, offset_size,
10493 dwarf_version, NULL,
10494 (tagno < 0), section,
10495 NULL, '=', -1);
10496 }
10497 ++tagno;
10498 }
10499 if (tagno <= 0)
10500 printf (_(" <no entries>"));
10501 putchar ('\n');
10502 }
10503
10504 free (abbrev_lookup);
10505 }
10506
10507 return 1;
10508 }
10509
10510 static int
10511 display_debug_links (struct dwarf_section * section,
10512 void * file ATTRIBUTE_UNUSED)
10513 {
10514 const unsigned char * filename;
10515 unsigned int filelen;
10516
10517 introduce (section, false);
10518
10519 /* The .gnu_debuglink section is formatted as:
10520 (c-string) Filename.
10521 (padding) If needed to reach a 4 byte boundary.
10522 (uint32_t) CRC32 value.
10523
10524 The .gun_debugaltlink section is formatted as:
10525 (c-string) Filename.
10526 (binary) Build-ID. */
10527
10528 filename = section->start;
10529 filelen = strnlen ((const char *) filename, section->size);
10530 if (filelen == section->size)
10531 {
10532 warn (_("The debuglink filename is corrupt/missing\n"));
10533 return 0;
10534 }
10535
10536 printf (_(" Separate debug info file: %s\n"), filename);
10537
10538 if (startswith (section->name, ".gnu_debuglink"))
10539 {
10540 unsigned int crc32;
10541 unsigned int crc_offset;
10542
10543 crc_offset = filelen + 1;
10544 crc_offset = (crc_offset + 3) & ~3;
10545 if (crc_offset + 4 > section->size)
10546 {
10547 warn (_("CRC offset missing/truncated\n"));
10548 return 0;
10549 }
10550
10551 crc32 = byte_get (filename + crc_offset, 4);
10552
10553 printf (_(" CRC value: %#x\n"), crc32);
10554
10555 if (crc_offset + 4 < section->size)
10556 {
10557 warn (_("There are %#" PRIx64
10558 " extraneous bytes at the end of the section\n"),
10559 section->size - (crc_offset + 4));
10560 return 0;
10561 }
10562 }
10563 else /* startswith (section->name, ".gnu_debugaltlink") */
10564 {
10565 const unsigned char *build_id = section->start + filelen + 1;
10566 size_t build_id_len = section->size - (filelen + 1);
10567 size_t printed;
10568
10569 /* FIXME: Should we support smaller build-id notes ? */
10570 if (build_id_len < 0x14)
10571 {
10572 warn (_("Build-ID is too short (%#zx bytes)\n"), build_id_len);
10573 return 0;
10574 }
10575
10576 printed = printf (_(" Build-ID (%#zx bytes):"), build_id_len);
10577 display_data (printed, build_id, build_id_len);
10578 putchar ('\n');
10579 }
10580
10581 putchar ('\n');
10582 return 1;
10583 }
10584
10585 static int
10586 display_gdb_index (struct dwarf_section *section,
10587 void *file ATTRIBUTE_UNUSED)
10588 {
10589 unsigned char *start = section->start;
10590 uint32_t version;
10591 uint32_t cu_list_offset, tu_list_offset;
10592 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10593 unsigned int cu_list_elements, tu_list_elements;
10594 unsigned int address_table_elements, symbol_table_slots;
10595 unsigned char *cu_list, *tu_list;
10596 unsigned char *address_table, *symbol_table, *constant_pool;
10597 unsigned int i;
10598
10599 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10600
10601 introduce (section, false);
10602
10603 if (section->size < 6 * sizeof (uint32_t))
10604 {
10605 warn (_("Truncated header in the %s section.\n"), section->name);
10606 return 0;
10607 }
10608
10609 version = byte_get_little_endian (start, 4);
10610 printf (_("Version %lu\n"), (unsigned long) version);
10611
10612 /* Prior versions are obsolete, and future versions may not be
10613 backwards compatible. */
10614 if (version < 3 || version > 8)
10615 {
10616 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10617 return 0;
10618 }
10619 if (version < 4)
10620 warn (_("The address table data in version 3 may be wrong.\n"));
10621 if (version < 5)
10622 warn (_("Version 4 does not support case insensitive lookups.\n"));
10623 if (version < 6)
10624 warn (_("Version 5 does not include inlined functions.\n"));
10625 if (version < 7)
10626 warn (_("Version 6 does not include symbol attributes.\n"));
10627 /* Version 7 indices generated by Gold have bad type unit references,
10628 PR binutils/15021. But we don't know if the index was generated by
10629 Gold or not, so to avoid worrying users with gdb-generated indices
10630 we say nothing for version 7 here. */
10631
10632 cu_list_offset = byte_get_little_endian (start + 4, 4);
10633 tu_list_offset = byte_get_little_endian (start + 8, 4);
10634 address_table_offset = byte_get_little_endian (start + 12, 4);
10635 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10636 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10637
10638 if (cu_list_offset > section->size
10639 || tu_list_offset > section->size
10640 || address_table_offset > section->size
10641 || symbol_table_offset > section->size
10642 || constant_pool_offset > section->size
10643 || tu_list_offset < cu_list_offset
10644 || address_table_offset < tu_list_offset
10645 || symbol_table_offset < address_table_offset
10646 || constant_pool_offset < symbol_table_offset)
10647 {
10648 warn (_("Corrupt header in the %s section.\n"), section->name);
10649 return 0;
10650 }
10651
10652 cu_list_elements = (tu_list_offset - cu_list_offset) / 16;
10653 tu_list_elements = (address_table_offset - tu_list_offset) / 24;
10654 address_table_elements = (symbol_table_offset - address_table_offset) / 20;
10655 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10656
10657 cu_list = start + cu_list_offset;
10658 tu_list = start + tu_list_offset;
10659 address_table = start + address_table_offset;
10660 symbol_table = start + symbol_table_offset;
10661 constant_pool = start + constant_pool_offset;
10662
10663 printf (_("\nCU table:\n"));
10664 for (i = 0; i < cu_list_elements; i++)
10665 {
10666 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 16, 8);
10667 uint64_t cu_length = byte_get_little_endian (cu_list + i * 16 + 8, 8);
10668
10669 printf ("[%3u] %#" PRIx64 " - %#" PRIx64 "\n",
10670 i, cu_offset, cu_offset + cu_length - 1);
10671 }
10672
10673 printf (_("\nTU table:\n"));
10674 for (i = 0; i < tu_list_elements; i++)
10675 {
10676 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 24, 8);
10677 uint64_t type_offset = byte_get_little_endian (tu_list + i * 24 + 8, 8);
10678 uint64_t signature = byte_get_little_endian (tu_list + i * 24 + 16, 8);
10679
10680 printf ("[%3u] %#" PRIx64 " %#" PRIx64 " ",
10681 i, tu_offset, type_offset);
10682 print_hex_ns (signature, 8);
10683 printf ("\n");
10684 }
10685
10686 printf (_("\nAddress table:\n"));
10687 for (i = 0; i < address_table_elements; i++)
10688 {
10689 uint64_t low = byte_get_little_endian (address_table + i * 20, 8);
10690 uint64_t high = byte_get_little_endian (address_table + i * 20 + 8, 8);
10691 uint32_t cu_index = byte_get_little_endian (address_table + i * 20 + 16, 4);
10692
10693 print_hex (low, 8);
10694 print_hex (high, 8);
10695 printf ("%" PRIu32 "\n", cu_index);
10696 }
10697
10698 printf (_("\nSymbol table:\n"));
10699 for (i = 0; i < symbol_table_slots; ++i)
10700 {
10701 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10702 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10703 uint32_t num_cus, cu;
10704
10705 if (name_offset != 0
10706 || cu_vector_offset != 0)
10707 {
10708 unsigned int j;
10709
10710 /* PR 17531: file: 5b7b07ad. */
10711 if (name_offset >= section->size - constant_pool_offset)
10712 {
10713 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10714 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10715 name_offset, i);
10716 }
10717 else
10718 printf ("[%3u] %.*s:", i,
10719 (int) (section->size - (constant_pool_offset + name_offset)),
10720 constant_pool + name_offset);
10721
10722 if (section->size - constant_pool_offset < 4
10723 || cu_vector_offset > section->size - constant_pool_offset - 4)
10724 {
10725 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10726 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10727 cu_vector_offset, i);
10728 continue;
10729 }
10730
10731 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10732
10733 if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10734 + cu_vector_offset + 4))
10735 {
10736 printf ("<invalid number of CUs: %d>\n", num_cus);
10737 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10738 num_cus, i);
10739 continue;
10740 }
10741
10742 if (num_cus > 1)
10743 printf ("\n");
10744
10745 for (j = 0; j < num_cus; ++j)
10746 {
10747 int is_static;
10748 gdb_index_symbol_kind kind;
10749
10750 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10751 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10752 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10753 cu = GDB_INDEX_CU_VALUE (cu);
10754 /* Convert to TU number if it's for a type unit. */
10755 if (cu >= cu_list_elements)
10756 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10757 (unsigned long) cu - cu_list_elements);
10758 else
10759 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10760
10761 printf (" [%s, %s]",
10762 is_static ? _("static") : _("global"),
10763 get_gdb_index_symbol_kind_name (kind));
10764 if (num_cus > 1)
10765 printf ("\n");
10766 }
10767 if (num_cus <= 1)
10768 printf ("\n");
10769 }
10770 }
10771
10772 return 1;
10773 }
10774
10775 /* Pre-allocate enough space for the CU/TU sets needed. */
10776
10777 static void
10778 prealloc_cu_tu_list (unsigned int nshndx)
10779 {
10780 if (nshndx == 0)
10781 /* Always allocate at least one entry for the end-marker. */
10782 nshndx = 1;
10783
10784 if (shndx_pool == NULL)
10785 {
10786 shndx_pool_size = nshndx;
10787 shndx_pool_used = 0;
10788 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10789 sizeof (unsigned int));
10790 }
10791 else
10792 {
10793 shndx_pool_size = shndx_pool_used + nshndx;
10794 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10795 sizeof (unsigned int));
10796 }
10797 }
10798
10799 static void
10800 add_shndx_to_cu_tu_entry (unsigned int shndx)
10801 {
10802 shndx_pool [shndx_pool_used++] = shndx;
10803 }
10804
10805 static void
10806 end_cu_tu_entry (void)
10807 {
10808 shndx_pool [shndx_pool_used++] = 0;
10809 }
10810
10811 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10812
10813 static const char *
10814 get_DW_SECT_short_name (unsigned int dw_sect)
10815 {
10816 static char buf[16];
10817
10818 switch (dw_sect)
10819 {
10820 case DW_SECT_INFO:
10821 return "info";
10822 case DW_SECT_TYPES:
10823 return "types";
10824 case DW_SECT_ABBREV:
10825 return "abbrev";
10826 case DW_SECT_LINE:
10827 return "line";
10828 case DW_SECT_LOC:
10829 return "loc";
10830 case DW_SECT_STR_OFFSETS:
10831 return "str_off";
10832 case DW_SECT_MACINFO:
10833 return "macinfo";
10834 case DW_SECT_MACRO:
10835 return "macro";
10836 default:
10837 break;
10838 }
10839
10840 snprintf (buf, sizeof (buf), "%d", dw_sect);
10841 return buf;
10842 }
10843
10844 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10845 These sections are extensions for Fission.
10846 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
10847
10848 static bool
10849 process_cu_tu_index (struct dwarf_section *section, int do_display)
10850 {
10851 unsigned char *phdr = section->start;
10852 unsigned char *limit = phdr + section->size;
10853 unsigned char *phash;
10854 unsigned char *pindex;
10855 unsigned char *ppool;
10856 unsigned int version;
10857 unsigned int ncols = 0;
10858 unsigned int nused;
10859 unsigned int nslots;
10860 unsigned int i;
10861 unsigned int j;
10862 uint64_t signature;
10863 size_t total;
10864
10865 /* PR 17512: file: 002-168123-0.004. */
10866 if (phdr == NULL)
10867 {
10868 warn (_("Section %s is empty\n"), section->name);
10869 return false;
10870 }
10871 /* PR 17512: file: 002-376-0.004. */
10872 if (section->size < 24)
10873 {
10874 warn (_("Section %s is too small to contain a CU/TU header\n"),
10875 section->name);
10876 return false;
10877 }
10878
10879 phash = phdr;
10880 SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
10881 if (version >= 2)
10882 SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
10883 SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
10884 SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
10885
10886 pindex = phash + (size_t) nslots * 8;
10887 ppool = pindex + (size_t) nslots * 4;
10888
10889 if (do_display)
10890 {
10891 introduce (section, false);
10892
10893 printf (_(" Version: %u\n"), version);
10894 if (version >= 2)
10895 printf (_(" Number of columns: %u\n"), ncols);
10896 printf (_(" Number of used entries: %u\n"), nused);
10897 printf (_(" Number of slots: %u\n\n"), nslots);
10898 }
10899
10900 /* PR 17531: file: 45d69832. */
10901 if (_mul_overflow ((size_t) nslots, 12, &total)
10902 || total > (size_t) (limit - phash))
10903 {
10904 warn (ngettext ("Section %s is too small for %u slot\n",
10905 "Section %s is too small for %u slots\n",
10906 nslots),
10907 section->name, nslots);
10908 return false;
10909 }
10910
10911 if (version == 1)
10912 {
10913 unsigned char *shndx_list;
10914 unsigned int shndx;
10915
10916 if (!do_display)
10917 {
10918 prealloc_cu_tu_list ((limit - ppool) / 4);
10919 for (shndx_list = ppool + 4; shndx_list <= limit - 4; shndx_list += 4)
10920 {
10921 shndx = byte_get (shndx_list, 4);
10922 add_shndx_to_cu_tu_entry (shndx);
10923 }
10924 end_cu_tu_entry ();
10925 }
10926 else
10927 for (i = 0; i < nslots; i++)
10928 {
10929 SAFE_BYTE_GET (signature, phash, 8, limit);
10930 if (signature != 0)
10931 {
10932 SAFE_BYTE_GET (j, pindex, 4, limit);
10933 shndx_list = ppool + j * 4;
10934 /* PR 17531: file: 705e010d. */
10935 if (shndx_list < ppool)
10936 {
10937 warn (_("Section index pool located before start of section\n"));
10938 return false;
10939 }
10940
10941 printf (_(" [%3d] Signature: %#" PRIx64 " Sections: "),
10942 i, signature);
10943 for (;;)
10944 {
10945 if (shndx_list >= limit)
10946 {
10947 warn (_("Section %s too small for shndx pool\n"),
10948 section->name);
10949 return false;
10950 }
10951 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
10952 if (shndx == 0)
10953 break;
10954 printf (" %d", shndx);
10955 shndx_list += 4;
10956 }
10957 printf ("\n");
10958 }
10959 phash += 8;
10960 pindex += 4;
10961 }
10962 }
10963 else if (version == 2)
10964 {
10965 unsigned int val;
10966 unsigned int dw_sect;
10967 unsigned char *ph = phash;
10968 unsigned char *pi = pindex;
10969 unsigned char *poffsets = ppool + (size_t) ncols * 4;
10970 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
10971 bool is_tu_index;
10972 struct cu_tu_set *this_set = NULL;
10973 unsigned int row;
10974 unsigned char *prow;
10975 size_t temp;
10976
10977 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
10978
10979 /* PR 17531: file: 0dd159bf.
10980 Check for integer overflow (can occur when size_t is 32-bit)
10981 with overlarge ncols or nused values. */
10982 if (nused == -1u
10983 || _mul_overflow ((size_t) ncols, 4, &temp)
10984 || _mul_overflow ((size_t) nused + 1, temp, &total)
10985 || total > (size_t) (limit - ppool)
10986 /* PR 30227: ncols could be 0. */
10987 || _mul_overflow ((size_t) nused + 1, 4, &total)
10988 || total > (size_t) (limit - ppool))
10989 {
10990 warn (_("Section %s too small for offset and size tables\n"),
10991 section->name);
10992 return false;
10993 }
10994
10995 if (do_display)
10996 {
10997 printf (_(" Offset table\n"));
10998 printf (" slot %-16s ",
10999 is_tu_index ? _("signature") : _("dwo_id"));
11000 }
11001 else
11002 {
11003 if (is_tu_index)
11004 {
11005 tu_count = nused;
11006 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
11007 this_set = tu_sets;
11008 }
11009 else
11010 {
11011 cu_count = nused;
11012 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
11013 this_set = cu_sets;
11014 }
11015 }
11016
11017 if (do_display)
11018 {
11019 for (j = 0; j < ncols; j++)
11020 {
11021 unsigned char *p = ppool + j * 4;
11022 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11023 printf (" %8s", get_DW_SECT_short_name (dw_sect));
11024 }
11025 printf ("\n");
11026 }
11027
11028 for (i = 0; i < nslots; i++)
11029 {
11030 SAFE_BYTE_GET (signature, ph, 8, limit);
11031
11032 SAFE_BYTE_GET (row, pi, 4, limit);
11033 if (row != 0)
11034 {
11035 /* PR 17531: file: a05f6ab3. */
11036 if (row > nused)
11037 {
11038 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
11039 row, nused);
11040 return false;
11041 }
11042
11043 if (!do_display)
11044 {
11045 size_t num_copy = sizeof (uint64_t);
11046
11047 memcpy (&this_set[row - 1].signature, ph, num_copy);
11048 }
11049
11050 prow = poffsets + (row - 1) * ncols * 4;
11051 if (do_display)
11052 printf (" [%3d] %#" PRIx64, i, signature);
11053 for (j = 0; j < ncols; j++)
11054 {
11055 unsigned char *p = prow + j * 4;
11056 SAFE_BYTE_GET (val, p, 4, limit);
11057 if (do_display)
11058 printf (" %8d", val);
11059 else
11060 {
11061 p = ppool + j * 4;
11062 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11063
11064 /* PR 17531: file: 10796eb3. */
11065 if (dw_sect >= DW_SECT_MAX)
11066 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11067 else
11068 this_set [row - 1].section_offsets [dw_sect] = val;
11069 }
11070 }
11071
11072 if (do_display)
11073 printf ("\n");
11074 }
11075 ph += 8;
11076 pi += 4;
11077 }
11078
11079 ph = phash;
11080 pi = pindex;
11081 if (do_display)
11082 {
11083 printf ("\n");
11084 printf (_(" Size table\n"));
11085 printf (" slot %-16s ",
11086 is_tu_index ? _("signature") : _("dwo_id"));
11087 }
11088
11089 for (j = 0; j < ncols; j++)
11090 {
11091 unsigned char *p = ppool + j * 4;
11092 SAFE_BYTE_GET (val, p, 4, limit);
11093 if (do_display)
11094 printf (" %8s", get_DW_SECT_short_name (val));
11095 }
11096
11097 if (do_display)
11098 printf ("\n");
11099
11100 for (i = 0; i < nslots; i++)
11101 {
11102 SAFE_BYTE_GET (signature, ph, 8, limit);
11103
11104 SAFE_BYTE_GET (row, pi, 4, limit);
11105 if (row != 0)
11106 {
11107 prow = psizes + (row - 1) * ncols * 4;
11108
11109 if (do_display)
11110 printf (" [%3d] %#" PRIx64, i, signature);
11111
11112 for (j = 0; j < ncols; j++)
11113 {
11114 unsigned char *p = prow + j * 4;
11115
11116 /* PR 28645: Check for overflow. Since we do not know how
11117 many populated rows there will be, we cannot just
11118 perform a single check at the start of this function. */
11119 if (p > (limit - 4))
11120 {
11121 if (do_display)
11122 printf ("\n");
11123 warn (_("Too many rows/columns in DWARF index section %s\n"),
11124 section->name);
11125 return false;
11126 }
11127
11128 SAFE_BYTE_GET (val, p, 4, limit);
11129
11130 if (do_display)
11131 printf (" %8d", val);
11132 else
11133 {
11134 p = ppool + j * 4;
11135 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11136 if (dw_sect >= DW_SECT_MAX)
11137 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11138 else
11139 this_set [row - 1].section_sizes [dw_sect] = val;
11140 }
11141 }
11142
11143 if (do_display)
11144 printf ("\n");
11145 }
11146
11147 ph += 8;
11148 pi += 4;
11149 }
11150 }
11151 else if (do_display)
11152 printf (_(" Unsupported version (%d)\n"), version);
11153
11154 if (do_display)
11155 printf ("\n");
11156
11157 return true;
11158 }
11159
11160 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
11161
11162 /* Load the CU and TU indexes if present. This will build a list of
11163 section sets that we can use to associate a .debug_info.dwo section
11164 with its associated .debug_abbrev.dwo section in a .dwp file. */
11165
11166 static bool
11167 load_cu_tu_indexes (void *file)
11168 {
11169 /* If we have already loaded (or tried to load) the CU and TU indexes
11170 then do not bother to repeat the task. */
11171 if (cu_tu_indexes_read == -1)
11172 {
11173 cu_tu_indexes_read = true;
11174
11175 if (load_debug_section_with_follow (dwp_cu_index, file))
11176 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
11177 cu_tu_indexes_read = false;
11178
11179 if (load_debug_section_with_follow (dwp_tu_index, file))
11180 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
11181 cu_tu_indexes_read = false;
11182 }
11183
11184 return (bool) cu_tu_indexes_read;
11185 }
11186
11187 /* Find the set of sections that includes section SHNDX. */
11188
11189 unsigned int *
11190 find_cu_tu_set (void *file, unsigned int shndx)
11191 {
11192 unsigned int i;
11193
11194 if (! load_cu_tu_indexes (file))
11195 return NULL;
11196
11197 /* Find SHNDX in the shndx pool. */
11198 for (i = 0; i < shndx_pool_used; i++)
11199 if (shndx_pool [i] == shndx)
11200 break;
11201
11202 if (i >= shndx_pool_used)
11203 return NULL;
11204
11205 /* Now backup to find the first entry in the set. */
11206 while (i > 0 && shndx_pool [i - 1] != 0)
11207 i--;
11208
11209 return shndx_pool + i;
11210 }
11211
11212 /* Display a .debug_cu_index or .debug_tu_index section. */
11213
11214 static int
11215 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
11216 {
11217 return process_cu_tu_index (section, 1);
11218 }
11219
11220 static int
11221 display_debug_not_supported (struct dwarf_section *section,
11222 void *file ATTRIBUTE_UNUSED)
11223 {
11224 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
11225 section->name);
11226
11227 return 1;
11228 }
11229
11230 /* Like malloc, but takes two parameters like calloc.
11231 Verifies that the first parameter is not too large.
11232 Note: does *not* initialise the allocated memory to zero. */
11233
11234 void *
11235 cmalloc (uint64_t nmemb, size_t size)
11236 {
11237 /* Check for overflow. */
11238 if (nmemb >= ~(size_t) 0 / size)
11239 return NULL;
11240
11241 return xmalloc (nmemb * size);
11242 }
11243
11244 /* Like xmalloc, but takes two parameters like calloc.
11245 Verifies that the first parameter is not too large.
11246 Note: does *not* initialise the allocated memory to zero. */
11247
11248 void *
11249 xcmalloc (uint64_t nmemb, size_t size)
11250 {
11251 /* Check for overflow. */
11252 if (nmemb >= ~(size_t) 0 / size)
11253 {
11254 fprintf (stderr,
11255 _("Attempt to allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11256 nmemb);
11257 xexit (1);
11258 }
11259
11260 return xmalloc (nmemb * size);
11261 }
11262
11263 /* Like xrealloc, but takes three parameters.
11264 Verifies that the second parameter is not too large.
11265 Note: does *not* initialise any new memory to zero. */
11266
11267 void *
11268 xcrealloc (void *ptr, uint64_t nmemb, size_t size)
11269 {
11270 /* Check for overflow. */
11271 if (nmemb >= ~(size_t) 0 / size)
11272 {
11273 error (_("Attempt to re-allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11274 nmemb);
11275 xexit (1);
11276 }
11277
11278 return xrealloc (ptr, nmemb * size);
11279 }
11280
11281 /* Like xcalloc, but verifies that the first parameter is not too large. */
11282
11283 void *
11284 xcalloc2 (uint64_t nmemb, size_t size)
11285 {
11286 /* Check for overflow. */
11287 if (nmemb >= ~(size_t) 0 / size)
11288 {
11289 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#" PRIx64 "\n"),
11290 nmemb);
11291 xexit (1);
11292 }
11293
11294 return xcalloc (nmemb, size);
11295 }
11296
11297 static unsigned long
11298 calc_gnu_debuglink_crc32 (unsigned long crc,
11299 const unsigned char *buf,
11300 size_t len)
11301 {
11302 static const unsigned long crc32_table[256] =
11303 {
11304 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
11305 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
11306 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
11307 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
11308 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
11309 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
11310 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
11311 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
11312 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
11313 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
11314 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
11315 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
11316 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
11317 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
11318 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
11319 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
11320 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
11321 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
11322 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
11323 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
11324 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
11325 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
11326 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
11327 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
11328 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
11329 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
11330 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
11331 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
11332 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
11333 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
11334 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
11335 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
11336 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
11337 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
11338 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
11339 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
11340 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
11341 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
11342 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
11343 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
11344 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
11345 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
11346 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
11347 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
11348 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
11349 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
11350 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
11351 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
11352 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
11353 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
11354 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
11355 0x2d02ef8d
11356 };
11357 const unsigned char *end;
11358
11359 crc = ~crc & 0xffffffff;
11360 for (end = buf + len; buf < end; ++ buf)
11361 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
11362 return ~crc & 0xffffffff;
11363 }
11364
11365 typedef bool (*check_func_type) (const char *, void *);
11366 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
11367
11368 static bool
11369 check_gnu_debuglink (const char * pathname, void * crc_pointer)
11370 {
11371 static unsigned char buffer[8 * 1024];
11372 FILE *f;
11373 size_t count;
11374 unsigned long crc = 0;
11375 void *sep_data;
11376
11377 sep_data = open_debug_file (pathname);
11378 if (sep_data == NULL)
11379 return false;
11380
11381 /* Yes - we are opening the file twice... */
11382 f = fopen (pathname, "rb");
11383 if (f == NULL)
11384 {
11385 /* Paranoia: This should never happen. */
11386 close_debug_file (sep_data);
11387 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
11388 return false;
11389 }
11390
11391 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
11392 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
11393
11394 fclose (f);
11395
11396 if (crc != * (unsigned long *) crc_pointer)
11397 {
11398 close_debug_file (sep_data);
11399 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
11400 pathname);
11401 return false;
11402 }
11403
11404 return true;
11405 }
11406
11407 static const char *
11408 parse_gnu_debuglink (struct dwarf_section * section, void * data)
11409 {
11410 const char * name;
11411 unsigned int crc_offset;
11412 unsigned long * crc32 = (unsigned long *) data;
11413
11414 /* The name is first.
11415 The CRC value is stored after the filename, aligned up to 4 bytes. */
11416 name = (const char *) section->start;
11417
11418 crc_offset = strnlen (name, section->size) + 1;
11419 if (crc_offset == 1)
11420 return NULL;
11421 crc_offset = (crc_offset + 3) & ~3;
11422 if (crc_offset + 4 > section->size)
11423 return NULL;
11424
11425 * crc32 = byte_get (section->start + crc_offset, 4);
11426 return name;
11427 }
11428
11429 static bool
11430 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
11431 {
11432 void * sep_data = open_debug_file (filename);
11433
11434 if (sep_data == NULL)
11435 return false;
11436
11437 /* FIXME: We should now extract the build-id in the separate file
11438 and check it... */
11439
11440 return true;
11441 }
11442
11443 typedef struct build_id_data
11444 {
11445 size_t len;
11446 const unsigned char *data;
11447 } Build_id_data;
11448
11449 static const char *
11450 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11451 {
11452 const char *name;
11453 size_t namelen;
11454 size_t id_len;
11455 Build_id_data *build_id_data;
11456
11457 /* The name is first.
11458 The build-id follows immediately, with no padding, up to the section's end. */
11459
11460 name = (const char *) section->start;
11461 namelen = strnlen (name, section->size) + 1;
11462 if (namelen == 1)
11463 return NULL;
11464 if (namelen >= section->size)
11465 return NULL;
11466
11467 id_len = section->size - namelen;
11468 if (id_len < 0x14)
11469 return NULL;
11470
11471 build_id_data = (Build_id_data *) data;
11472 build_id_data->len = id_len;
11473 build_id_data->data = section->start + namelen;
11474
11475 return name;
11476 }
11477
11478 static void
11479 add_separate_debug_file (const char * filename, void * handle)
11480 {
11481 separate_info * i = xmalloc (sizeof * i);
11482
11483 i->filename = filename;
11484 i->handle = handle;
11485 i->next = first_separate_info;
11486 first_separate_info = i;
11487 }
11488
11489 #if HAVE_LIBDEBUGINFOD
11490 /* Query debuginfod servers for the target debuglink or debugaltlink
11491 file. If successful, store the path of the file in filename and
11492 return TRUE, otherwise return FALSE. */
11493
11494 static bool
11495 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11496 char ** filename,
11497 void * file)
11498 {
11499 size_t build_id_len;
11500 unsigned char * build_id;
11501
11502 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11503 {
11504 /* Get the build-id of file. */
11505 build_id = get_build_id (file);
11506 build_id_len = 0;
11507 }
11508 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11509 {
11510 /* Get the build-id of the debugaltlink file. */
11511 unsigned int filelen;
11512
11513 filelen = strnlen ((const char *)section->start, section->size);
11514 if (filelen == section->size)
11515 /* Corrupt debugaltlink. */
11516 return false;
11517
11518 build_id = section->start + filelen + 1;
11519 build_id_len = section->size - (filelen + 1);
11520
11521 if (build_id_len == 0)
11522 return false;
11523 }
11524 else
11525 return false;
11526
11527 if (build_id)
11528 {
11529 int fd;
11530 debuginfod_client * client;
11531
11532 client = debuginfod_begin ();
11533 if (client == NULL)
11534 return false;
11535
11536 /* Query debuginfod servers for the target file. If found its path
11537 will be stored in filename. */
11538 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11539 debuginfod_end (client);
11540
11541 /* Only free build_id if we allocated space for a hex string
11542 in get_build_id (). */
11543 if (build_id_len == 0)
11544 free (build_id);
11545
11546 if (fd >= 0)
11547 {
11548 /* File successfully retrieved. Close fd since we want to
11549 use open_debug_file () on filename instead. */
11550 close (fd);
11551 return true;
11552 }
11553 }
11554
11555 return false;
11556 }
11557 #endif /* HAVE_LIBDEBUGINFOD */
11558
11559 static void *
11560 load_separate_debug_info (const char * main_filename,
11561 struct dwarf_section * xlink,
11562 parse_func_type parse_func,
11563 check_func_type check_func,
11564 void * func_data,
11565 void * file ATTRIBUTE_UNUSED)
11566 {
11567 const char * separate_filename;
11568 char * debug_filename;
11569 char * canon_dir;
11570 size_t canon_dirlen;
11571 size_t dirlen;
11572 char * canon_filename;
11573 char * canon_debug_filename;
11574 bool self;
11575
11576 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11577 {
11578 warn (_("Corrupt debuglink section: %s\n"),
11579 xlink->name ? xlink->name : xlink->uncompressed_name);
11580 return NULL;
11581 }
11582
11583 /* Attempt to locate the separate file.
11584 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11585
11586 canon_filename = lrealpath (main_filename);
11587 canon_dir = xstrdup (canon_filename);
11588
11589 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11590 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11591 break;
11592 canon_dir[canon_dirlen] = '\0';
11593
11594 #ifndef DEBUGDIR
11595 #define DEBUGDIR "/lib/debug"
11596 #endif
11597 #ifndef EXTRA_DEBUG_ROOT1
11598 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11599 #endif
11600 #ifndef EXTRA_DEBUG_ROOT2
11601 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11602 #endif
11603
11604 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11605 + canon_dirlen
11606 + strlen (".debug/")
11607 #ifdef EXTRA_DEBUG_ROOT1
11608 + strlen (EXTRA_DEBUG_ROOT1)
11609 #endif
11610 #ifdef EXTRA_DEBUG_ROOT2
11611 + strlen (EXTRA_DEBUG_ROOT2)
11612 #endif
11613 + strlen (separate_filename)
11614 + 1);
11615 if (debug_filename == NULL)
11616 {
11617 warn (_("Out of memory"));
11618 free (canon_dir);
11619 free (canon_filename);
11620 return NULL;
11621 }
11622
11623 /* First try in the current directory. */
11624 sprintf (debug_filename, "%s", separate_filename);
11625 if (check_func (debug_filename, func_data))
11626 goto found;
11627
11628 /* Then try in a subdirectory called .debug. */
11629 sprintf (debug_filename, ".debug/%s", separate_filename);
11630 if (check_func (debug_filename, func_data))
11631 goto found;
11632
11633 /* Then try in the same directory as the original file. */
11634 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11635 if (check_func (debug_filename, func_data))
11636 goto found;
11637
11638 /* And the .debug subdirectory of that directory. */
11639 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11640 if (check_func (debug_filename, func_data))
11641 goto found;
11642
11643 #ifdef EXTRA_DEBUG_ROOT1
11644 /* Try the first extra debug file root. */
11645 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11646 if (check_func (debug_filename, func_data))
11647 goto found;
11648
11649 /* Try the first extra debug file root. */
11650 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11651 if (check_func (debug_filename, func_data))
11652 goto found;
11653 #endif
11654
11655 #ifdef EXTRA_DEBUG_ROOT2
11656 /* Try the second extra debug file root. */
11657 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11658 if (check_func (debug_filename, func_data))
11659 goto found;
11660 #endif
11661
11662 /* Then try in the global debug_filename directory. */
11663 strcpy (debug_filename, DEBUGDIR);
11664 dirlen = strlen (DEBUGDIR) - 1;
11665 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11666 strcat (debug_filename, "/");
11667 strcat (debug_filename, (const char *) separate_filename);
11668
11669 if (check_func (debug_filename, func_data))
11670 goto found;
11671
11672 #if HAVE_LIBDEBUGINFOD
11673 {
11674 char * tmp_filename;
11675
11676 if (use_debuginfod
11677 && debuginfod_fetch_separate_debug_info (xlink,
11678 & tmp_filename,
11679 file))
11680 {
11681 /* File successfully downloaded from server, replace
11682 debug_filename with the file's path. */
11683 free (debug_filename);
11684 debug_filename = tmp_filename;
11685 goto found;
11686 }
11687 }
11688 #endif
11689
11690 if (do_debug_links)
11691 {
11692 /* Failed to find the file. */
11693 warn (_("could not find separate debug file '%s'\n"),
11694 separate_filename);
11695 warn (_("tried: %s\n"), debug_filename);
11696
11697 #ifdef EXTRA_DEBUG_ROOT2
11698 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11699 separate_filename);
11700 warn (_("tried: %s\n"), debug_filename);
11701 #endif
11702
11703 #ifdef EXTRA_DEBUG_ROOT1
11704 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11705 canon_dir, separate_filename);
11706 warn (_("tried: %s\n"), debug_filename);
11707
11708 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11709 separate_filename);
11710 warn (_("tried: %s\n"), debug_filename);
11711 #endif
11712
11713 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11714 separate_filename);
11715 warn (_("tried: %s\n"), debug_filename);
11716
11717 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11718 warn (_("tried: %s\n"), debug_filename);
11719
11720 sprintf (debug_filename, ".debug/%s", separate_filename);
11721 warn (_("tried: %s\n"), debug_filename);
11722
11723 sprintf (debug_filename, "%s", separate_filename);
11724 warn (_("tried: %s\n"), debug_filename);
11725
11726 #if HAVE_LIBDEBUGINFOD
11727 if (use_debuginfod)
11728 {
11729 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11730
11731 if (urls == NULL)
11732 urls = "";
11733
11734 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11735 }
11736 #endif
11737 }
11738
11739 free (canon_dir);
11740 free (debug_filename);
11741 free (canon_filename);
11742 return NULL;
11743
11744 found:
11745 free (canon_dir);
11746
11747 canon_debug_filename = lrealpath (debug_filename);
11748 self = strcmp (canon_debug_filename, canon_filename) == 0;
11749 free (canon_filename);
11750 free (canon_debug_filename);
11751 if (self)
11752 {
11753 free (debug_filename);
11754 return NULL;
11755 }
11756
11757 void * debug_handle;
11758
11759 /* Now open the file.... */
11760 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11761 {
11762 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11763 free (debug_filename);
11764 return NULL;
11765 }
11766
11767 /* FIXME: We do not check to see if there are any other separate debug info
11768 files that would also match. */
11769
11770 if (do_debug_links)
11771 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11772 add_separate_debug_file (debug_filename, debug_handle);
11773
11774 /* Do not free debug_filename - it might be referenced inside
11775 the structure returned by open_debug_file(). */
11776 return debug_handle;
11777 }
11778
11779 /* Attempt to load a separate dwarf object file. */
11780
11781 static void *
11782 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11783 {
11784 char * separate_filename;
11785 void * separate_handle;
11786
11787 if (IS_ABSOLUTE_PATH (name))
11788 separate_filename = strdup (name);
11789 else
11790 /* FIXME: Skip adding / if dwo_dir ends in /. */
11791 separate_filename = concat (dir, "/", name, NULL);
11792 if (separate_filename == NULL)
11793 {
11794 warn (_("Out of memory allocating dwo filename\n"));
11795 return NULL;
11796 }
11797
11798 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11799 {
11800 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11801 free (separate_filename);
11802 return NULL;
11803 }
11804
11805 /* FIXME: We should check the dwo_id. */
11806
11807 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11808
11809 add_separate_debug_file (separate_filename, separate_handle);
11810 /* Note - separate_filename will be freed in free_debug_memory(). */
11811 return separate_handle;
11812 }
11813
11814 static void *
11815 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11816 {
11817 char * f = filename;
11818
11819 f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11820 id_len --;
11821 while (id_len --)
11822 f += sprintf (f, "%02x", (unsigned) *data++);
11823 strcpy (f, ".debug");
11824
11825 return open_debug_file (filename);
11826 }
11827
11828 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section. */
11829
11830 static void
11831 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11832 {
11833 if (! load_debug_section (note_gnu_build_id, main_file))
11834 return; /* No .note.gnu.build-id section. */
11835
11836 struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11837 if (section == NULL)
11838 {
11839 warn (_("Unable to load the .note.gnu.build-id section\n"));
11840 return;
11841 }
11842
11843 if (section->start == NULL || section->size < 0x18)
11844 {
11845 warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11846 return;
11847 }
11848
11849 /* In theory we should extract the contents of the section into
11850 a note structure and then check the fields. For now though
11851 just use hard coded offsets instead:
11852
11853 Field Bytes Contents
11854 NSize 0...3 4
11855 DSize 4...7 8+
11856 Type 8..11 3 (NT_GNU_BUILD_ID)
11857 Name 12.15 GNU\0
11858 Data 16.... */
11859
11860 /* FIXME: Check the name size, name and type fields. */
11861
11862 unsigned long build_id_size;
11863 build_id_size = byte_get (section->start + 4, 4);
11864 if (build_id_size < 8)
11865 {
11866 warn (_(".note.gnu.build-id data size is too small\n"));
11867 return;
11868 }
11869
11870 if (build_id_size > (section->size - 16))
11871 {
11872 warn (_(".note.gnu.build-id data size is too big\n"));
11873 return;
11874 }
11875
11876 char * filename;
11877 filename = xmalloc (strlen (".build-id/")
11878 + build_id_size * 2 + 2
11879 + strlen (".debug")
11880 /* The next string should be the same as the longest
11881 name found in the prefixes[] array below. */
11882 + strlen ("/usrlib64/debug/usr")
11883 + 1);
11884 void * handle;
11885
11886 static const char * prefixes[] =
11887 {
11888 "",
11889 ".debug/",
11890 "/usr/lib/debug/",
11891 "/usr/lib/debug/usr/",
11892 "/usr/lib64/debug/",
11893 "/usr/lib64/debug/usr"
11894 };
11895 long unsigned int i;
11896
11897 for (i = 0; i < ARRAY_SIZE (prefixes); i++)
11898 {
11899 handle = try_build_id_prefix (prefixes[i], filename,
11900 section->start + 16, build_id_size);
11901 if (handle != NULL)
11902 break;
11903 }
11904 /* FIXME: TYhe BFD library also tries a global debugfile directory prefix. */
11905 if (handle == NULL)
11906 {
11907 /* Failed to find a debug file associated with the build-id.
11908 This is not an error however, rather it just means that
11909 the debug info has probably not been loaded on the system,
11910 or that another method is being used to link to the debug
11911 info. */
11912 free (filename);
11913 return;
11914 }
11915
11916 add_separate_debug_file (filename, handle);
11917 }
11918
11919 /* Try to load a debug file pointed to by the .debug_sup section. */
11920
11921 static void
11922 load_debug_sup_file (const char * main_filename, void * file)
11923 {
11924 if (! load_debug_section (debug_sup, file))
11925 return; /* No .debug_sup section. */
11926
11927 struct dwarf_section * section;
11928 section = & debug_displays [debug_sup].section;
11929 assert (section != NULL);
11930
11931 if (section->start == NULL || section->size < 5)
11932 {
11933 warn (_(".debug_sup section is corrupt/empty\n"));
11934 return;
11935 }
11936
11937 if (section->start[2] != 0)
11938 return; /* This is a supplementary file. */
11939
11940 const char * filename = (const char *) section->start + 3;
11941 if (strnlen (filename, section->size - 3) == section->size - 3)
11942 {
11943 warn (_("filename in .debug_sup section is corrupt\n"));
11944 return;
11945 }
11946
11947 if (filename[0] != '/' && strchr (main_filename, '/'))
11948 {
11949 char * new_name;
11950 int new_len;
11951
11952 new_len = asprintf (& new_name, "%.*s/%s",
11953 (int) (strrchr (main_filename, '/') - main_filename),
11954 main_filename,
11955 filename);
11956 if (new_len < 3)
11957 {
11958 warn (_("unable to construct path for supplementary debug file"));
11959 if (new_len > -1)
11960 free (new_name);
11961 return;
11962 }
11963 filename = new_name;
11964 }
11965 else
11966 {
11967 /* PR 27796: Make sure that we pass a filename that can be free'd to
11968 add_separate_debug_file(). */
11969 filename = strdup (filename);
11970 if (filename == NULL)
11971 {
11972 warn (_("out of memory constructing filename for .debug_sup link\n"));
11973 return;
11974 }
11975 }
11976
11977 void * handle = open_debug_file (filename);
11978 if (handle == NULL)
11979 {
11980 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
11981 free ((void *) filename);
11982 return;
11983 }
11984
11985 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
11986
11987 /* FIXME: Compare the checksums, if present. */
11988 add_separate_debug_file (filename, handle);
11989 }
11990
11991 /* Load a debuglink section and/or a debugaltlink section, if either are present.
11992 Recursively check the loaded files for more of these sections.
11993 Also follow any links in .debug_sup sections.
11994 FIXME: Should also check for DWO_* entries in the newly loaded files. */
11995
11996 static void
11997 check_for_and_load_links (void * file, const char * filename)
11998 {
11999 void * handle = NULL;
12000
12001 if (load_debug_section (gnu_debugaltlink, file))
12002 {
12003 Build_id_data build_id_data;
12004
12005 handle = load_separate_debug_info (filename,
12006 & debug_displays[gnu_debugaltlink].section,
12007 parse_gnu_debugaltlink,
12008 check_gnu_debugaltlink,
12009 & build_id_data,
12010 file);
12011 if (handle)
12012 {
12013 assert (handle == first_separate_info->handle);
12014 check_for_and_load_links (first_separate_info->handle,
12015 first_separate_info->filename);
12016 }
12017 }
12018
12019 if (load_debug_section (gnu_debuglink, file))
12020 {
12021 unsigned long crc32;
12022
12023 handle = load_separate_debug_info (filename,
12024 & debug_displays[gnu_debuglink].section,
12025 parse_gnu_debuglink,
12026 check_gnu_debuglink,
12027 & crc32,
12028 file);
12029 if (handle)
12030 {
12031 assert (handle == first_separate_info->handle);
12032 check_for_and_load_links (first_separate_info->handle,
12033 first_separate_info->filename);
12034 }
12035 }
12036
12037 load_debug_sup_file (filename, file);
12038
12039 load_build_id_debug_file (filename, file);
12040 }
12041
12042 /* Load the separate debug info file(s) attached to FILE, if any exist.
12043 Returns TRUE if any were found, FALSE otherwise.
12044 If TRUE is returned then the linked list starting at first_separate_info
12045 will be populated with open file handles. */
12046
12047 bool
12048 load_separate_debug_files (void * file, const char * filename)
12049 {
12050 /* Skip this operation if we are not interested in debug links. */
12051 if (! do_follow_links && ! do_debug_links)
12052 return false;
12053
12054 /* See if there are any dwo links. */
12055 if (load_debug_section (str, file)
12056 && load_debug_section (abbrev, file)
12057 && load_debug_section (info, file))
12058 {
12059 /* Load the .debug_addr section, if it exists. */
12060 load_debug_section (debug_addr, file);
12061 /* Load the .debug_str_offsets section, if it exists. */
12062 load_debug_section (str_index, file);
12063 /* Load the .debug_loclists section, if it exists. */
12064 load_debug_section (loclists, file);
12065 /* Load the .debug_rnglists section, if it exists. */
12066 load_debug_section (rnglists, file);
12067
12068 free_dwo_info ();
12069
12070 if (process_debug_info (& debug_displays[info].section, file, abbrev,
12071 true, false))
12072 {
12073 bool introduced = false;
12074 dwo_info *dwinfo;
12075 const char *dir = NULL;
12076 const char *id = NULL;
12077 const char *name = NULL;
12078
12079 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
12080 {
12081 /* Accumulate NAME, DIR and ID fields. */
12082 switch (dwinfo->type)
12083 {
12084 case DWO_NAME:
12085 if (name != NULL)
12086 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
12087 name = dwinfo->value;
12088 break;
12089
12090 case DWO_DIR:
12091 /* There can be multiple DW_AT_comp_dir entries in a CU,
12092 so do not complain. */
12093 dir = dwinfo->value;
12094 break;
12095
12096 case DWO_ID:
12097 if (id != NULL)
12098 warn (_("multiple DWO_IDs encountered for the same CU\n"));
12099 id = dwinfo->value;
12100 break;
12101
12102 default:
12103 error (_("Unexpected DWO INFO type"));
12104 break;
12105 }
12106
12107 /* If we have reached the end of our list, or we are changing
12108 CUs, then display the information that we have accumulated
12109 so far. */
12110 if (name != NULL
12111 && (dwinfo->next == NULL
12112 || dwinfo->next->cu_offset != dwinfo->cu_offset))
12113 {
12114 if (do_debug_links)
12115 {
12116 if (! introduced)
12117 {
12118 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
12119 debug_displays [info].section.uncompressed_name);
12120 introduced = true;
12121 }
12122
12123 printf (_(" Name: %s\n"), name);
12124 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
12125 if (id != NULL)
12126 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
12127 else if (debug_information[0].dwarf_version != 5)
12128 printf (_(" ID: <not specified>\n"));
12129 printf ("\n\n");
12130 }
12131
12132 if (do_follow_links)
12133 load_dwo_file (filename, name, dir, id);
12134
12135 name = dir = id = NULL;
12136 }
12137 }
12138 }
12139 }
12140
12141 if (! do_follow_links)
12142 /* The other debug links will be displayed by display_debug_links()
12143 so we do not need to do any further processing here. */
12144 return false;
12145
12146 /* FIXME: We do not check for the presence of both link sections in the same file. */
12147 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
12148 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
12149
12150 check_for_and_load_links (file, filename);
12151 if (first_separate_info != NULL)
12152 return true;
12153
12154 do_follow_links = 0;
12155 return false;
12156 }
12157
12158 void
12159 free_debug_memory (void)
12160 {
12161 unsigned int i;
12162
12163 free_all_abbrevs ();
12164
12165 free (shndx_pool);
12166 shndx_pool = NULL;
12167 shndx_pool_size = 0;
12168 shndx_pool_used = 0;
12169 free (cu_sets);
12170 cu_sets = NULL;
12171 cu_count = 0;
12172 free (tu_sets);
12173 tu_sets = NULL;
12174 tu_count = 0;
12175
12176 memset (level_type_signed, 0, sizeof level_type_signed);
12177 cu_tu_indexes_read = -1;
12178
12179 for (i = 0; i < max; i++)
12180 free_debug_section ((enum dwarf_section_display_enum) i);
12181
12182 if (debug_information != NULL)
12183 {
12184 for (i = 0; i < alloc_num_debug_info_entries; i++)
12185 free_debug_information (&debug_information[i]);
12186 free (debug_information);
12187 debug_information = NULL;
12188 alloc_num_debug_info_entries = num_debug_info_entries = 0;
12189 }
12190
12191 separate_info * d;
12192 separate_info * next;
12193
12194 for (d = first_separate_info; d != NULL; d = next)
12195 {
12196 close_debug_file (d->handle);
12197 free ((void *) d->filename);
12198 next = d->next;
12199 free ((void *) d);
12200 }
12201 first_separate_info = NULL;
12202
12203 free_dwo_info ();
12204 }
12205
12206 typedef struct
12207 {
12208 const char letter;
12209 const char *option;
12210 int *variable;
12211 int val;
12212 } debug_dump_long_opts;
12213
12214 static const debug_dump_long_opts debug_option_table[] =
12215 {
12216 { 'A', "addr", &do_debug_addr, 1 },
12217 { 'a', "abbrev", &do_debug_abbrevs, 1 },
12218 { 'c', "cu_index", &do_debug_cu_index, 1 },
12219 #ifdef HAVE_LIBDEBUGINFOD
12220 { 'D', "use-debuginfod", &use_debuginfod, 1 },
12221 { 'E', "do-not-use-debuginfod", &use_debuginfod, 0 },
12222 #endif
12223 { 'F', "frames-interp", &do_debug_frames_interp, 1 },
12224 { 'f', "frames", &do_debug_frames, 1 },
12225 { 'g', "gdb_index", &do_gdb_index, 1 },
12226 { 'i', "info", &do_debug_info, 1 },
12227 { 'K', "follow-links", &do_follow_links, 1 },
12228 { 'k', "links", &do_debug_links, 1 },
12229 { 'L', "decodedline", &do_debug_lines, FLAG_DEBUG_LINES_DECODED },
12230 { 'l', "rawline", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12231 /* For compatibility with earlier versions of readelf. */
12232 { 'l', "line", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12233 { 'm', "macro", &do_debug_macinfo, 1 },
12234 { 'N', "no-follow-links", &do_follow_links, 0 },
12235 { 'O', "str-offsets", &do_debug_str_offsets, 1 },
12236 { 'o', "loc", &do_debug_loc, 1 },
12237 { 'p', "pubnames", &do_debug_pubnames, 1 },
12238 { 'R', "Ranges", &do_debug_ranges, 1 },
12239 { 'r', "aranges", &do_debug_aranges, 1 },
12240 /* For compatibility with earlier versions of readelf. */
12241 { 'r', "ranges", &do_debug_aranges, 1 },
12242 { 's', "str", &do_debug_str, 1 },
12243 { 'T', "trace_aranges", &do_trace_aranges, 1 },
12244 { 't', "pubtypes", &do_debug_pubtypes, 1 },
12245 { 'U', "trace_info", &do_trace_info, 1 },
12246 { 'u', "trace_abbrev", &do_trace_abbrevs, 1 },
12247 { 0, NULL, NULL, 0 }
12248 };
12249
12250 /* Enable display of specific DWARF sections as determined by the comma
12251 separated strings in NAMES. Returns non-zero if any displaying was
12252 enabled. */
12253
12254 int
12255 dwarf_select_sections_by_names (const char *names)
12256 {
12257 const char *p;
12258 int result = 0;
12259
12260 p = names;
12261 while (*p)
12262 {
12263 const debug_dump_long_opts *entry;
12264
12265 for (entry = debug_option_table; entry->option; entry++)
12266 {
12267 size_t len = strlen (entry->option);
12268
12269 if (strncmp (p, entry->option, len) == 0
12270 && (p[len] == ',' || p[len] == '\0'))
12271 {
12272 if (entry->val == 0)
12273 * entry->variable = 0;
12274 else
12275 * entry->variable = entry->val;
12276 result |= entry->val;
12277
12278 p += len;
12279 break;
12280 }
12281 }
12282
12283 if (entry->option == NULL)
12284 {
12285 warn (_("Unrecognized debug option '%s'\n"), p);
12286 p = strchr (p, ',');
12287 if (p == NULL)
12288 break;
12289 }
12290
12291 if (*p == ',')
12292 p++;
12293 }
12294
12295 /* The --debug-dump=frames-interp option also enables the
12296 --debug-dump=frames option. */
12297 if (do_debug_frames_interp)
12298 do_debug_frames = 1;
12299
12300 return result;
12301 }
12302
12303 /* Enable display of specific DWARF sections as determined by the characters
12304 in LETTERS. Returns non-zero if any displaying was enabled. */
12305
12306 int
12307 dwarf_select_sections_by_letters (const char *letters)
12308 {
12309 int result = 0;
12310
12311 while (* letters)
12312 {
12313 const debug_dump_long_opts *entry;
12314
12315 for (entry = debug_option_table; entry->letter; entry++)
12316 {
12317 if (entry->letter == * letters)
12318 {
12319 if (entry->val == 0)
12320 * entry->variable = 0;
12321 else
12322 * entry->variable |= entry->val;
12323 result |= entry->val;
12324 break;
12325 }
12326 }
12327
12328 if (entry->letter == 0)
12329 warn (_("Unrecognized debug letter option '%c'\n"), * letters);
12330
12331 letters ++;
12332 }
12333
12334 /* The --debug-dump=frames-interp option also enables the
12335 --debug-dump=frames option. */
12336 if (do_debug_frames_interp)
12337 do_debug_frames = 1;
12338
12339 return result;
12340 }
12341
12342 void
12343 dwarf_select_sections_all (void)
12344 {
12345 do_debug_info = 1;
12346 do_debug_abbrevs = 1;
12347 do_debug_lines = FLAG_DEBUG_LINES_RAW;
12348 do_debug_pubnames = 1;
12349 do_debug_pubtypes = 1;
12350 do_debug_aranges = 1;
12351 do_debug_ranges = 1;
12352 do_debug_frames = 1;
12353 do_debug_macinfo = 1;
12354 do_debug_str = 1;
12355 do_debug_loc = 1;
12356 do_gdb_index = 1;
12357 do_trace_info = 1;
12358 do_trace_abbrevs = 1;
12359 do_trace_aranges = 1;
12360 do_debug_addr = 1;
12361 do_debug_cu_index = 1;
12362 do_follow_links = 1;
12363 do_debug_links = 1;
12364 do_debug_str_offsets = 1;
12365 }
12366
12367 #define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
12368 #define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
12369
12370 /* N.B. The order here must match the order in section_display_enum. */
12371
12372 struct dwarf_section_display debug_displays[] =
12373 {
12374 { { ".debug_abbrev", ".zdebug_abbrev", ".dwabrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12375 { { ".debug_aranges", ".zdebug_aranges", ".dwarnge", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
12376 { { ".debug_frame", ".zdebug_frame", ".dwframe", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12377 { { ".debug_info", ".zdebug_info", ".dwinfo", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
12378 { { ".debug_line", ".zdebug_line", ".dwline", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12379 { { ".debug_pubnames", ".zdebug_pubnames", ".dwpbnms", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
12380 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
12381 { { ".eh_frame", "", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12382 { { ".debug_macinfo", ".zdebug_macinfo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12383 { { ".debug_macro", ".zdebug_macro", ".dwmac", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12384 { { ".debug_str", ".zdebug_str", ".dwstr", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12385 { { ".debug_line_str", ".zdebug_line_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12386 { { ".debug_loc", ".zdebug_loc", ".dwloc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12387 { { ".debug_loclists", ".zdebug_loclists", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12388 { { ".debug_loclists.dwo", ".zdebug_loclists.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12389 { { ".debug_pubtypes", ".zdebug_pubtypes", ".dwpbtyp", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
12390 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
12391 { { ".debug_ranges", ".zdebug_ranges", ".dwrnges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12392 { { ".debug_rnglists", ".zdebug_rnglists", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12393 { { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12394 { { ".debug_static_func", ".zdebug_static_func", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12395 { { ".debug_static_vars", ".zdebug_static_vars", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12396 { { ".debug_types", ".zdebug_types", "", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
12397 { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12398 { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
12399 { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
12400 { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
12401 { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
12402 { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
12403 { { ".debug_info.dwo", ".zdebug_info.dwo", "", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
12404 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", "", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12405 { { ".debug_types.dwo", ".zdebug_types.dwo", "", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
12406 { { ".debug_line.dwo", ".zdebug_line.dwo", "", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12407 { { ".debug_loc.dwo", ".zdebug_loc.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12408 { { ".debug_macro.dwo", ".zdebug_macro.dwo", "", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12409 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12410 { { ".debug_str.dwo", ".zdebug_str.dwo", "", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
12411 { { ".debug_str_offsets", ".zdebug_str_offsets", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12412 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12413 { { ".debug_addr", ".zdebug_addr", "", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
12414 { { ".debug_cu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12415 { { ".debug_tu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12416 { { ".gnu_debuglink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12417 { { ".gnu_debugaltlink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12418 { { ".debug_sup", "", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
12419 /* Separate debug info files can containt their own .debug_str section,
12420 and this might be in *addition* to a .debug_str section already present
12421 in the main file. Hence we need to have two entries for .debug_str. */
12422 { { ".debug_str", ".zdebug_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12423 { { ".note.gnu.build-id", "", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12424 };
12425
12426 /* A static assertion. */
12427 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];