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