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