Fix an attempt to allocate an overlarge amount of memory when decoding a corrupt...
[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 index_offset = idx * offset_size;
663
664 if (this_set != NULL)
665 index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
666
667 index_offset += str_offsets_base;
668
669 if (index_offset + offset_size > index_section->size)
670 {
671 warn (_("string index of %" PRIu64 " converts to an offset of %#" PRIx64
672 " which is too big for section %s"),
673 idx, index_offset, str_section->name);
674
675 return _("<string index too big>");
676 }
677
678 /* FIXME: If we are being paranoid then we should also check to see if
679 IDX references an entry beyond the end of the string table pointed to
680 by STR_OFFSETS_BASE. (Since there can be more than one string table
681 in a DWARF string section). */
682
683 str_offset = byte_get (index_section->start + index_offset, offset_size);
684
685 str_offset -= str_section->address;
686 if (str_offset >= str_section->size)
687 {
688 warn (_("indirect offset too big: %#" PRIx64 "\n"), str_offset);
689 return _("<indirect index offset is too big>");
690 }
691
692 ret = (const char *) str_section->start + str_offset;
693
694 /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
695 Since our caller is expecting to receive a well formed C string we test
696 for the lack of a terminating byte here. */
697 if (strnlen (ret, str_section->size - str_offset)
698 == str_section->size - str_offset)
699 return _("<no NUL byte at end of section>");
700
701 return ret;
702 }
703
704 static uint64_t
705 fetch_indexed_addr (uint64_t offset, uint32_t num_bytes)
706 {
707 struct dwarf_section *section = &debug_displays [debug_addr].section;
708
709 if (section->start == NULL)
710 {
711 warn (_("Cannot fetch indexed address: the .debug_addr section is missing\n"));
712 return 0;
713 }
714
715 if (offset + num_bytes > section->size)
716 {
717 warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
718 section->name, offset);
719 return 0;
720 }
721
722 return byte_get (section->start + offset, num_bytes);
723 }
724
725 /* Fetch a value from a debug section that has been indexed by
726 something in another section (eg DW_FORM_loclistx or DW_FORM_rnglistx).
727 Returns -1 if the value could not be found. */
728
729 static uint64_t
730 fetch_indexed_value (uint64_t idx,
731 enum dwarf_section_display_enum sec_enum,
732 uint64_t base_address)
733 {
734 struct dwarf_section *section = &debug_displays [sec_enum].section;
735
736 if (section->start == NULL)
737 {
738 warn (_("Unable to locate %s section\n"), section->uncompressed_name);
739 return -1;
740 }
741
742 if (section->size < 4)
743 {
744 warn (_("Section %s is too small to contain an value indexed from another section!\n"),
745 section->name);
746 return -1;
747 }
748
749 uint32_t pointer_size, bias;
750
751 if (byte_get (section->start, 4) == 0xffffffff)
752 {
753 pointer_size = 8;
754 bias = 20;
755 }
756 else
757 {
758 pointer_size = 4;
759 bias = 12;
760 }
761
762 uint64_t offset = idx * pointer_size;
763
764 /* Offsets are biased by the size of the section header
765 or base address. */
766 if (base_address)
767 offset += base_address;
768 else
769 offset += bias;
770
771 if (offset + pointer_size > section->size)
772 {
773 warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
774 section->name, offset);
775 return -1;
776 }
777
778 return byte_get (section->start + offset, pointer_size);
779 }
780
781 /* FIXME: There are better and more efficient ways to handle
782 these structures. For now though, I just want something that
783 is simple to implement. */
784 /* Records a single attribute in an abbrev. */
785 typedef struct abbrev_attr
786 {
787 unsigned long attribute;
788 unsigned long form;
789 int64_t implicit_const;
790 struct abbrev_attr *next;
791 }
792 abbrev_attr;
793
794 /* Records a single abbrev. */
795 typedef struct abbrev_entry
796 {
797 unsigned long number;
798 unsigned long tag;
799 int children;
800 struct abbrev_attr * first_attr;
801 struct abbrev_attr * last_attr;
802 struct abbrev_entry * next;
803 }
804 abbrev_entry;
805
806 /* Records a set of abbreviations. */
807 typedef struct abbrev_list
808 {
809 abbrev_entry * first_abbrev;
810 abbrev_entry * last_abbrev;
811 unsigned char * raw;
812 struct abbrev_list * next;
813 unsigned char * start_of_next_abbrevs;
814 }
815 abbrev_list;
816
817 /* Records all the abbrevs found so far. */
818 static struct abbrev_list * abbrev_lists = NULL;
819
820 typedef struct abbrev_map
821 {
822 uint64_t start;
823 uint64_t end;
824 abbrev_list *list;
825 } abbrev_map;
826
827 /* Maps between CU offsets and abbrev sets. */
828 static abbrev_map * cu_abbrev_map = NULL;
829 static unsigned long num_abbrev_map_entries = 0;
830 static unsigned long next_free_abbrev_map_entry = 0;
831
832 #define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
833 #define ABBREV_MAP_ENTRIES_INCREMENT 8
834
835 static void
836 record_abbrev_list_for_cu (uint64_t start, uint64_t end,
837 abbrev_list *list, abbrev_list *free_list)
838 {
839 if (free_list != NULL)
840 {
841 list->next = abbrev_lists;
842 abbrev_lists = list;
843 }
844
845 if (cu_abbrev_map == NULL)
846 {
847 num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
848 cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
849 }
850 else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
851 {
852 num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
853 cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
854 }
855
856 cu_abbrev_map[next_free_abbrev_map_entry].start = start;
857 cu_abbrev_map[next_free_abbrev_map_entry].end = end;
858 cu_abbrev_map[next_free_abbrev_map_entry].list = list;
859 next_free_abbrev_map_entry ++;
860 }
861
862 static abbrev_list *
863 free_abbrev_list (abbrev_list *list)
864 {
865 abbrev_entry *abbrv = list->first_abbrev;
866
867 while (abbrv)
868 {
869 abbrev_attr *attr = abbrv->first_attr;
870
871 while (attr)
872 {
873 abbrev_attr *next_attr = attr->next;
874 free (attr);
875 attr = next_attr;
876 }
877
878 abbrev_entry *next_abbrev = abbrv->next;
879 free (abbrv);
880 abbrv = next_abbrev;
881 }
882
883 abbrev_list *next = list->next;
884 free (list);
885 return next;
886 }
887
888 static void
889 free_all_abbrevs (void)
890 {
891 while (abbrev_lists)
892 abbrev_lists = free_abbrev_list (abbrev_lists);
893
894 free (cu_abbrev_map);
895 cu_abbrev_map = NULL;
896 next_free_abbrev_map_entry = 0;
897 }
898
899 static abbrev_list *
900 find_abbrev_list_by_raw_abbrev (unsigned char *raw)
901 {
902 abbrev_list * list;
903
904 for (list = abbrev_lists; list != NULL; list = list->next)
905 if (list->raw == raw)
906 return list;
907
908 return NULL;
909 }
910
911 /* Find the abbreviation map for the CU that includes OFFSET.
912 OFFSET is an absolute offset from the start of the .debug_info section. */
913 /* FIXME: This function is going to slow down readelf & objdump.
914 Not caching abbrevs is likely the answer. */
915
916 static abbrev_map *
917 find_abbrev_map_by_offset (uint64_t offset)
918 {
919 unsigned long i;
920
921 for (i = 0; i < next_free_abbrev_map_entry; i++)
922 if (cu_abbrev_map[i].start <= offset
923 && cu_abbrev_map[i].end > offset)
924 return cu_abbrev_map + i;
925
926 return NULL;
927 }
928
929 static void
930 add_abbrev (unsigned long number,
931 unsigned long tag,
932 int children,
933 abbrev_list * list)
934 {
935 abbrev_entry * entry;
936
937 entry = (abbrev_entry *) xmalloc (sizeof (*entry));
938
939 entry->number = number;
940 entry->tag = tag;
941 entry->children = children;
942 entry->first_attr = NULL;
943 entry->last_attr = NULL;
944 entry->next = NULL;
945
946 assert (list != NULL);
947
948 if (list->first_abbrev == NULL)
949 list->first_abbrev = entry;
950 else
951 list->last_abbrev->next = entry;
952
953 list->last_abbrev = entry;
954 }
955
956 static void
957 add_abbrev_attr (unsigned long attribute,
958 unsigned long form,
959 int64_t implicit_const,
960 abbrev_list *list)
961 {
962 abbrev_attr *attr;
963
964 attr = (abbrev_attr *) xmalloc (sizeof (*attr));
965
966 attr->attribute = attribute;
967 attr->form = form;
968 attr->implicit_const = implicit_const;
969 attr->next = NULL;
970
971 assert (list != NULL && list->last_abbrev != NULL);
972
973 if (list->last_abbrev->first_attr == NULL)
974 list->last_abbrev->first_attr = attr;
975 else
976 list->last_abbrev->last_attr->next = attr;
977
978 list->last_abbrev->last_attr = attr;
979 }
980
981 /* Return processed (partial) contents of a .debug_abbrev section.
982 Returns NULL on errors. */
983
984 static abbrev_list *
985 process_abbrev_set (struct dwarf_section *section,
986 unsigned char *start,
987 unsigned char *end)
988 {
989 abbrev_list *list = xmalloc (sizeof (*list));
990 list->first_abbrev = NULL;
991 list->last_abbrev = NULL;
992 list->raw = start;
993 list->next = NULL;
994
995 while (start < end)
996 {
997 unsigned long entry;
998 unsigned long tag;
999 unsigned long attribute;
1000 int children;
1001
1002 READ_ULEB (entry, start, end);
1003
1004 /* A single zero is supposed to end the set according
1005 to the standard. If there's more, then signal that to
1006 the caller. */
1007 if (start == end || entry == 0)
1008 {
1009 list->start_of_next_abbrevs = start != end ? start : NULL;
1010 return list;
1011 }
1012
1013 READ_ULEB (tag, start, end);
1014 if (start == end)
1015 return free_abbrev_list (list);
1016
1017 children = *start++;
1018
1019 add_abbrev (entry, tag, children, list);
1020
1021 do
1022 {
1023 unsigned long form;
1024 /* Initialize it due to a false compiler warning. */
1025 int64_t implicit_const = -1;
1026
1027 READ_ULEB (attribute, start, end);
1028 if (start == end)
1029 break;
1030
1031 READ_ULEB (form, start, end);
1032 if (start == end)
1033 break;
1034
1035 if (form == DW_FORM_implicit_const)
1036 {
1037 READ_SLEB (implicit_const, start, end);
1038 if (start == end)
1039 break;
1040 }
1041
1042 add_abbrev_attr (attribute, form, implicit_const, list);
1043 }
1044 while (attribute != 0);
1045 }
1046
1047 /* Report the missing single zero which ends the section. */
1048 error (_("%s section not zero terminated\n"), section->name);
1049
1050 return free_abbrev_list (list);
1051 }
1052
1053 /* Return a sequence of abbrevs in SECTION starting at ABBREV_BASE
1054 plus ABBREV_OFFSET and finishing at ABBREV_BASE + ABBREV_SIZE.
1055 If FREE_LIST is non-NULL search the already decoded abbrevs on
1056 abbrev_lists first and if found set *FREE_LIST to NULL. If
1057 searching doesn't find a matching abbrev, set *FREE_LIST to the
1058 newly allocated list. If FREE_LIST is NULL, no search is done and
1059 the returned abbrev_list is always newly allocated. */
1060
1061 static abbrev_list *
1062 find_and_process_abbrev_set (struct dwarf_section *section,
1063 uint64_t abbrev_base,
1064 uint64_t abbrev_size,
1065 uint64_t abbrev_offset,
1066 abbrev_list **free_list)
1067 {
1068 if (free_list)
1069 *free_list = NULL;
1070
1071 if (abbrev_base >= section->size
1072 || abbrev_size > section->size - abbrev_base)
1073 {
1074 /* PR 17531: file:4bcd9ce9. */
1075 warn (_("Debug info is corrupted, abbrev size (%#" PRIx64 ")"
1076 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1077 abbrev_base + abbrev_size, section->size);
1078 return NULL;
1079 }
1080 if (abbrev_offset >= abbrev_size)
1081 {
1082 warn (_("Debug info is corrupted, abbrev offset (%#" PRIx64 ")"
1083 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1084 abbrev_offset, abbrev_size);
1085 return NULL;
1086 }
1087
1088 unsigned char *start = section->start + abbrev_base + abbrev_offset;
1089 unsigned char *end = section->start + abbrev_base + abbrev_size;
1090 abbrev_list *list = NULL;
1091 if (free_list)
1092 list = find_abbrev_list_by_raw_abbrev (start);
1093 if (list == NULL)
1094 {
1095 list = process_abbrev_set (section, start, end);
1096 if (free_list)
1097 *free_list = list;
1098 }
1099 return list;
1100 }
1101
1102 static const char *
1103 get_TAG_name (uint64_t tag)
1104 {
1105 const char *name = NULL;
1106
1107 if ((unsigned int) tag == tag)
1108 name = get_DW_TAG_name ((unsigned int) tag);
1109 if (name == NULL)
1110 {
1111 static char buffer[100];
1112
1113 if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1114 snprintf (buffer, sizeof (buffer),
1115 _("User TAG value: %#" PRIx64), tag);
1116 else
1117 snprintf (buffer, sizeof (buffer),
1118 _("Unknown TAG value: %#" PRIx64), tag);
1119 return buffer;
1120 }
1121
1122 return name;
1123 }
1124
1125 static const char *
1126 get_FORM_name (unsigned long form)
1127 {
1128 const char *name = NULL;
1129
1130 if (form == 0)
1131 return "DW_FORM value: 0";
1132
1133 if ((unsigned int) form == form)
1134 name = get_DW_FORM_name ((unsigned int) form);
1135 if (name == NULL)
1136 {
1137 static char buffer[100];
1138
1139 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1140 return buffer;
1141 }
1142
1143 return name;
1144 }
1145
1146 static const char *
1147 get_IDX_name (unsigned long idx)
1148 {
1149 const char *name = NULL;
1150
1151 if ((unsigned int) idx == idx)
1152 name = get_DW_IDX_name ((unsigned int) idx);
1153 if (name == NULL)
1154 {
1155 static char buffer[100];
1156
1157 snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1158 return buffer;
1159 }
1160
1161 return name;
1162 }
1163
1164 static unsigned char *
1165 display_block (unsigned char *data,
1166 uint64_t length,
1167 const unsigned char * const end, char delimiter)
1168 {
1169 size_t maxlen;
1170
1171 printf (_("%c%" PRIu64 " byte block: "), delimiter, length);
1172 if (data > end)
1173 return (unsigned char *) end;
1174
1175 maxlen = end - data;
1176 length = length > maxlen ? maxlen : length;
1177
1178 while (length --)
1179 printf ("%" PRIx64 " ", byte_get (data++, 1));
1180
1181 return data;
1182 }
1183
1184 static int
1185 decode_location_expression (unsigned char * data,
1186 unsigned int pointer_size,
1187 unsigned int offset_size,
1188 int dwarf_version,
1189 uint64_t length,
1190 uint64_t cu_offset,
1191 struct dwarf_section * section)
1192 {
1193 unsigned op;
1194 uint64_t uvalue;
1195 int64_t svalue;
1196 unsigned char *end = data + length;
1197 int need_frame_base = 0;
1198
1199 while (data < end)
1200 {
1201 op = *data++;
1202
1203 switch (op)
1204 {
1205 case DW_OP_addr:
1206 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1207 printf ("DW_OP_addr: %" PRIx64, uvalue);
1208 break;
1209 case DW_OP_deref:
1210 printf ("DW_OP_deref");
1211 break;
1212 case DW_OP_const1u:
1213 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1214 printf ("DW_OP_const1u: %" PRIu64, uvalue);
1215 break;
1216 case DW_OP_const1s:
1217 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1218 printf ("DW_OP_const1s: %" PRId64, svalue);
1219 break;
1220 case DW_OP_const2u:
1221 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1222 printf ("DW_OP_const2u: %" PRIu64, uvalue);
1223 break;
1224 case DW_OP_const2s:
1225 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1226 printf ("DW_OP_const2s: %" PRId64, svalue);
1227 break;
1228 case DW_OP_const4u:
1229 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1230 printf ("DW_OP_const4u: %" PRIu64, uvalue);
1231 break;
1232 case DW_OP_const4s:
1233 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1234 printf ("DW_OP_const4s: %" PRId64, svalue);
1235 break;
1236 case DW_OP_const8u:
1237 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1238 printf ("DW_OP_const8u: %" PRIu64, uvalue);
1239 break;
1240 case DW_OP_const8s:
1241 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 8, end);
1242 printf ("DW_OP_const8s: %" PRId64, svalue);
1243 break;
1244 case DW_OP_constu:
1245 READ_ULEB (uvalue, data, end);
1246 printf ("DW_OP_constu: %" PRIu64, uvalue);
1247 break;
1248 case DW_OP_consts:
1249 READ_SLEB (svalue, data, end);
1250 printf ("DW_OP_consts: %" PRId64, svalue);
1251 break;
1252 case DW_OP_dup:
1253 printf ("DW_OP_dup");
1254 break;
1255 case DW_OP_drop:
1256 printf ("DW_OP_drop");
1257 break;
1258 case DW_OP_over:
1259 printf ("DW_OP_over");
1260 break;
1261 case DW_OP_pick:
1262 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1263 printf ("DW_OP_pick: %" PRIu64, uvalue);
1264 break;
1265 case DW_OP_swap:
1266 printf ("DW_OP_swap");
1267 break;
1268 case DW_OP_rot:
1269 printf ("DW_OP_rot");
1270 break;
1271 case DW_OP_xderef:
1272 printf ("DW_OP_xderef");
1273 break;
1274 case DW_OP_abs:
1275 printf ("DW_OP_abs");
1276 break;
1277 case DW_OP_and:
1278 printf ("DW_OP_and");
1279 break;
1280 case DW_OP_div:
1281 printf ("DW_OP_div");
1282 break;
1283 case DW_OP_minus:
1284 printf ("DW_OP_minus");
1285 break;
1286 case DW_OP_mod:
1287 printf ("DW_OP_mod");
1288 break;
1289 case DW_OP_mul:
1290 printf ("DW_OP_mul");
1291 break;
1292 case DW_OP_neg:
1293 printf ("DW_OP_neg");
1294 break;
1295 case DW_OP_not:
1296 printf ("DW_OP_not");
1297 break;
1298 case DW_OP_or:
1299 printf ("DW_OP_or");
1300 break;
1301 case DW_OP_plus:
1302 printf ("DW_OP_plus");
1303 break;
1304 case DW_OP_plus_uconst:
1305 READ_ULEB (uvalue, data, end);
1306 printf ("DW_OP_plus_uconst: %" PRIu64, uvalue);
1307 break;
1308 case DW_OP_shl:
1309 printf ("DW_OP_shl");
1310 break;
1311 case DW_OP_shr:
1312 printf ("DW_OP_shr");
1313 break;
1314 case DW_OP_shra:
1315 printf ("DW_OP_shra");
1316 break;
1317 case DW_OP_xor:
1318 printf ("DW_OP_xor");
1319 break;
1320 case DW_OP_bra:
1321 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1322 printf ("DW_OP_bra: %" PRId64, svalue);
1323 break;
1324 case DW_OP_eq:
1325 printf ("DW_OP_eq");
1326 break;
1327 case DW_OP_ge:
1328 printf ("DW_OP_ge");
1329 break;
1330 case DW_OP_gt:
1331 printf ("DW_OP_gt");
1332 break;
1333 case DW_OP_le:
1334 printf ("DW_OP_le");
1335 break;
1336 case DW_OP_lt:
1337 printf ("DW_OP_lt");
1338 break;
1339 case DW_OP_ne:
1340 printf ("DW_OP_ne");
1341 break;
1342 case DW_OP_skip:
1343 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1344 printf ("DW_OP_skip: %" PRId64, svalue);
1345 break;
1346
1347 case DW_OP_lit0:
1348 case DW_OP_lit1:
1349 case DW_OP_lit2:
1350 case DW_OP_lit3:
1351 case DW_OP_lit4:
1352 case DW_OP_lit5:
1353 case DW_OP_lit6:
1354 case DW_OP_lit7:
1355 case DW_OP_lit8:
1356 case DW_OP_lit9:
1357 case DW_OP_lit10:
1358 case DW_OP_lit11:
1359 case DW_OP_lit12:
1360 case DW_OP_lit13:
1361 case DW_OP_lit14:
1362 case DW_OP_lit15:
1363 case DW_OP_lit16:
1364 case DW_OP_lit17:
1365 case DW_OP_lit18:
1366 case DW_OP_lit19:
1367 case DW_OP_lit20:
1368 case DW_OP_lit21:
1369 case DW_OP_lit22:
1370 case DW_OP_lit23:
1371 case DW_OP_lit24:
1372 case DW_OP_lit25:
1373 case DW_OP_lit26:
1374 case DW_OP_lit27:
1375 case DW_OP_lit28:
1376 case DW_OP_lit29:
1377 case DW_OP_lit30:
1378 case DW_OP_lit31:
1379 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1380 break;
1381
1382 case DW_OP_reg0:
1383 case DW_OP_reg1:
1384 case DW_OP_reg2:
1385 case DW_OP_reg3:
1386 case DW_OP_reg4:
1387 case DW_OP_reg5:
1388 case DW_OP_reg6:
1389 case DW_OP_reg7:
1390 case DW_OP_reg8:
1391 case DW_OP_reg9:
1392 case DW_OP_reg10:
1393 case DW_OP_reg11:
1394 case DW_OP_reg12:
1395 case DW_OP_reg13:
1396 case DW_OP_reg14:
1397 case DW_OP_reg15:
1398 case DW_OP_reg16:
1399 case DW_OP_reg17:
1400 case DW_OP_reg18:
1401 case DW_OP_reg19:
1402 case DW_OP_reg20:
1403 case DW_OP_reg21:
1404 case DW_OP_reg22:
1405 case DW_OP_reg23:
1406 case DW_OP_reg24:
1407 case DW_OP_reg25:
1408 case DW_OP_reg26:
1409 case DW_OP_reg27:
1410 case DW_OP_reg28:
1411 case DW_OP_reg29:
1412 case DW_OP_reg30:
1413 case DW_OP_reg31:
1414 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1415 regname (op - DW_OP_reg0, 1));
1416 break;
1417
1418 case DW_OP_breg0:
1419 case DW_OP_breg1:
1420 case DW_OP_breg2:
1421 case DW_OP_breg3:
1422 case DW_OP_breg4:
1423 case DW_OP_breg5:
1424 case DW_OP_breg6:
1425 case DW_OP_breg7:
1426 case DW_OP_breg8:
1427 case DW_OP_breg9:
1428 case DW_OP_breg10:
1429 case DW_OP_breg11:
1430 case DW_OP_breg12:
1431 case DW_OP_breg13:
1432 case DW_OP_breg14:
1433 case DW_OP_breg15:
1434 case DW_OP_breg16:
1435 case DW_OP_breg17:
1436 case DW_OP_breg18:
1437 case DW_OP_breg19:
1438 case DW_OP_breg20:
1439 case DW_OP_breg21:
1440 case DW_OP_breg22:
1441 case DW_OP_breg23:
1442 case DW_OP_breg24:
1443 case DW_OP_breg25:
1444 case DW_OP_breg26:
1445 case DW_OP_breg27:
1446 case DW_OP_breg28:
1447 case DW_OP_breg29:
1448 case DW_OP_breg30:
1449 case DW_OP_breg31:
1450 READ_SLEB (svalue, data, end);
1451 printf ("DW_OP_breg%d (%s): %" PRId64,
1452 op - DW_OP_breg0, regname (op - DW_OP_breg0, 1), svalue);
1453 break;
1454
1455 case DW_OP_regx:
1456 READ_ULEB (uvalue, data, end);
1457 printf ("DW_OP_regx: %" PRIu64 " (%s)",
1458 uvalue, regname (uvalue, 1));
1459 break;
1460 case DW_OP_fbreg:
1461 need_frame_base = 1;
1462 READ_SLEB (svalue, data, end);
1463 printf ("DW_OP_fbreg: %" PRId64, svalue);
1464 break;
1465 case DW_OP_bregx:
1466 READ_ULEB (uvalue, data, end);
1467 READ_SLEB (svalue, data, end);
1468 printf ("DW_OP_bregx: %" PRIu64 " (%s) %" PRId64,
1469 uvalue, regname (uvalue, 1), svalue);
1470 break;
1471 case DW_OP_piece:
1472 READ_ULEB (uvalue, data, end);
1473 printf ("DW_OP_piece: %" PRIu64, uvalue);
1474 break;
1475 case DW_OP_deref_size:
1476 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1477 printf ("DW_OP_deref_size: %" PRIu64, uvalue);
1478 break;
1479 case DW_OP_xderef_size:
1480 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1481 printf ("DW_OP_xderef_size: %" PRIu64, uvalue);
1482 break;
1483 case DW_OP_nop:
1484 printf ("DW_OP_nop");
1485 break;
1486
1487 /* DWARF 3 extensions. */
1488 case DW_OP_push_object_address:
1489 printf ("DW_OP_push_object_address");
1490 break;
1491 case DW_OP_call2:
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, 2, end);
1495 printf ("DW_OP_call2: <%#" PRIx64 ">", svalue + cu_offset);
1496 break;
1497 case DW_OP_call4:
1498 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1499 this ought to be an 8-byte wide computation. */
1500 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1501 printf ("DW_OP_call4: <%#" PRIx64 ">", svalue + cu_offset);
1502 break;
1503 case DW_OP_call_ref:
1504 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1505 this ought to be an 8-byte wide computation. */
1506 if (dwarf_version == -1)
1507 {
1508 printf (_("(DW_OP_call_ref in frame info)"));
1509 /* No way to tell where the next op is, so just bail. */
1510 return need_frame_base;
1511 }
1512 if (dwarf_version == 2)
1513 {
1514 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1515 }
1516 else
1517 {
1518 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1519 }
1520 printf ("DW_OP_call_ref: <%#" PRIx64 ">", uvalue);
1521 break;
1522 case DW_OP_form_tls_address:
1523 printf ("DW_OP_form_tls_address");
1524 break;
1525 case DW_OP_call_frame_cfa:
1526 printf ("DW_OP_call_frame_cfa");
1527 break;
1528 case DW_OP_bit_piece:
1529 printf ("DW_OP_bit_piece: ");
1530 READ_ULEB (uvalue, data, end);
1531 printf (_("size: %" PRIu64 " "), uvalue);
1532 READ_ULEB (uvalue, data, end);
1533 printf (_("offset: %" PRIu64 " "), uvalue);
1534 break;
1535
1536 /* DWARF 4 extensions. */
1537 case DW_OP_stack_value:
1538 printf ("DW_OP_stack_value");
1539 break;
1540
1541 case DW_OP_implicit_value:
1542 printf ("DW_OP_implicit_value");
1543 READ_ULEB (uvalue, data, end);
1544 data = display_block (data, uvalue, end, ' ');
1545 break;
1546
1547 /* GNU extensions. */
1548 case DW_OP_GNU_push_tls_address:
1549 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1550 break;
1551 case DW_OP_GNU_uninit:
1552 printf ("DW_OP_GNU_uninit");
1553 /* FIXME: Is there data associated with this OP ? */
1554 break;
1555 case DW_OP_GNU_encoded_addr:
1556 {
1557 int encoding = 0;
1558 uint64_t addr;
1559
1560 if (data < end)
1561 encoding = *data++;
1562 addr = get_encoded_value (&data, encoding, section, end);
1563
1564 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1565 print_hex_ns (addr, pointer_size);
1566 }
1567 break;
1568 case DW_OP_implicit_pointer:
1569 case DW_OP_GNU_implicit_pointer:
1570 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1571 this ought to be an 8-byte wide computation. */
1572 if (dwarf_version == -1)
1573 {
1574 printf (_("(%s in frame info)"),
1575 (op == DW_OP_implicit_pointer
1576 ? "DW_OP_implicit_pointer"
1577 : "DW_OP_GNU_implicit_pointer"));
1578 /* No way to tell where the next op is, so just bail. */
1579 return need_frame_base;
1580 }
1581 if (dwarf_version == 2)
1582 {
1583 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1584 }
1585 else
1586 {
1587 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1588 }
1589 READ_SLEB (svalue, data, end);
1590 printf ("%s: <%#" PRIx64 "> %" PRId64,
1591 (op == DW_OP_implicit_pointer
1592 ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1593 uvalue, svalue);
1594 break;
1595 case DW_OP_entry_value:
1596 case DW_OP_GNU_entry_value:
1597 READ_ULEB (uvalue, data, end);
1598 /* PR 17531: file: 0cc9cd00. */
1599 if (uvalue > (size_t) (end - data))
1600 uvalue = end - data;
1601 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1602 : "DW_OP_GNU_entry_value"));
1603 if (decode_location_expression (data, pointer_size, offset_size,
1604 dwarf_version, uvalue,
1605 cu_offset, section))
1606 need_frame_base = 1;
1607 putchar (')');
1608 data += uvalue;
1609 break;
1610 case DW_OP_const_type:
1611 case DW_OP_GNU_const_type:
1612 READ_ULEB (uvalue, data, end);
1613 printf ("%s: <%#" PRIx64 "> ",
1614 (op == DW_OP_const_type ? "DW_OP_const_type"
1615 : "DW_OP_GNU_const_type"),
1616 cu_offset + uvalue);
1617 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1618 data = display_block (data, uvalue, end, ' ');
1619 break;
1620 case DW_OP_regval_type:
1621 case DW_OP_GNU_regval_type:
1622 READ_ULEB (uvalue, data, end);
1623 printf ("%s: %" PRIu64 " (%s)",
1624 (op == DW_OP_regval_type ? "DW_OP_regval_type"
1625 : "DW_OP_GNU_regval_type"),
1626 uvalue, regname (uvalue, 1));
1627 READ_ULEB (uvalue, data, end);
1628 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1629 break;
1630 case DW_OP_deref_type:
1631 case DW_OP_GNU_deref_type:
1632 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1633 printf ("%s: %" PRId64,
1634 (op == DW_OP_deref_type ? "DW_OP_deref_type"
1635 : "DW_OP_GNU_deref_type"),
1636 uvalue);
1637 READ_ULEB (uvalue, data, end);
1638 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1639 break;
1640 case DW_OP_convert:
1641 case DW_OP_GNU_convert:
1642 READ_ULEB (uvalue, data, end);
1643 printf ("%s <%#" PRIx64 ">",
1644 (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1645 uvalue ? cu_offset + uvalue : uvalue);
1646 break;
1647 case DW_OP_reinterpret:
1648 case DW_OP_GNU_reinterpret:
1649 READ_ULEB (uvalue, data, end);
1650 printf ("%s <%#" PRIx64 ">",
1651 (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1652 : "DW_OP_GNU_reinterpret"),
1653 uvalue ? cu_offset + uvalue : uvalue);
1654 break;
1655 case DW_OP_GNU_parameter_ref:
1656 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1657 printf ("DW_OP_GNU_parameter_ref: <%#" PRIx64 ">",
1658 cu_offset + uvalue);
1659 break;
1660 case DW_OP_addrx:
1661 READ_ULEB (uvalue, data, end);
1662 printf ("DW_OP_addrx <%#" PRIx64 ">", uvalue);
1663 break;
1664 case DW_OP_GNU_addr_index:
1665 READ_ULEB (uvalue, data, end);
1666 printf ("DW_OP_GNU_addr_index <%#" PRIx64 ">", uvalue);
1667 break;
1668 case DW_OP_GNU_const_index:
1669 READ_ULEB (uvalue, data, end);
1670 printf ("DW_OP_GNU_const_index <%#" PRIx64 ">", uvalue);
1671 break;
1672 case DW_OP_GNU_variable_value:
1673 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1674 this ought to be an 8-byte wide computation. */
1675 if (dwarf_version == -1)
1676 {
1677 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1678 /* No way to tell where the next op is, so just bail. */
1679 return need_frame_base;
1680 }
1681 if (dwarf_version == 2)
1682 {
1683 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1684 }
1685 else
1686 {
1687 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1688 }
1689 printf ("DW_OP_GNU_variable_value: <%#" PRIx64 ">", uvalue);
1690 break;
1691
1692 /* HP extensions. */
1693 case DW_OP_HP_is_value:
1694 printf ("DW_OP_HP_is_value");
1695 /* FIXME: Is there data associated with this OP ? */
1696 break;
1697 case DW_OP_HP_fltconst4:
1698 printf ("DW_OP_HP_fltconst4");
1699 /* FIXME: Is there data associated with this OP ? */
1700 break;
1701 case DW_OP_HP_fltconst8:
1702 printf ("DW_OP_HP_fltconst8");
1703 /* FIXME: Is there data associated with this OP ? */
1704 break;
1705 case DW_OP_HP_mod_range:
1706 printf ("DW_OP_HP_mod_range");
1707 /* FIXME: Is there data associated with this OP ? */
1708 break;
1709 case DW_OP_HP_unmod_range:
1710 printf ("DW_OP_HP_unmod_range");
1711 /* FIXME: Is there data associated with this OP ? */
1712 break;
1713 case DW_OP_HP_tls:
1714 printf ("DW_OP_HP_tls");
1715 /* FIXME: Is there data associated with this OP ? */
1716 break;
1717
1718 /* PGI (STMicroelectronics) extensions. */
1719 case DW_OP_PGI_omp_thread_num:
1720 /* Pushes the thread number for the current thread as it would be
1721 returned by the standard OpenMP library function:
1722 omp_get_thread_num(). The "current thread" is the thread for
1723 which the expression is being evaluated. */
1724 printf ("DW_OP_PGI_omp_thread_num");
1725 break;
1726
1727 default:
1728 if (op >= DW_OP_lo_user
1729 && op <= DW_OP_hi_user)
1730 printf (_("(User defined location op %#x)"), op);
1731 else
1732 printf (_("(Unknown location op %#x)"), op);
1733 /* No way to tell where the next op is, so just bail. */
1734 return need_frame_base;
1735 }
1736
1737 /* Separate the ops. */
1738 if (data < end)
1739 printf ("; ");
1740 }
1741
1742 return need_frame_base;
1743 }
1744
1745 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1746 This is used for DWARF package files. */
1747
1748 static struct cu_tu_set *
1749 find_cu_tu_set_v2 (uint64_t cu_offset, int do_types)
1750 {
1751 struct cu_tu_set *p;
1752 unsigned int nsets;
1753 unsigned int dw_sect;
1754
1755 if (do_types)
1756 {
1757 p = tu_sets;
1758 nsets = tu_count;
1759 dw_sect = DW_SECT_TYPES;
1760 }
1761 else
1762 {
1763 p = cu_sets;
1764 nsets = cu_count;
1765 dw_sect = DW_SECT_INFO;
1766 }
1767 while (nsets > 0)
1768 {
1769 if (p->section_offsets [dw_sect] == cu_offset)
1770 return p;
1771 p++;
1772 nsets--;
1773 }
1774 return NULL;
1775 }
1776
1777 static const char *
1778 fetch_alt_indirect_string (uint64_t offset)
1779 {
1780 separate_info * i;
1781
1782 if (! do_follow_links)
1783 return "";
1784
1785 if (first_separate_info == NULL)
1786 return _("<no links available>");
1787
1788 for (i = first_separate_info; i != NULL; i = i->next)
1789 {
1790 struct dwarf_section * section;
1791 const char * ret;
1792
1793 if (! load_debug_section (separate_debug_str, i->handle))
1794 continue;
1795
1796 section = &debug_displays [separate_debug_str].section;
1797
1798 if (section->start == NULL)
1799 continue;
1800
1801 if (offset >= section->size)
1802 continue;
1803
1804 ret = (const char *) (section->start + offset);
1805 /* Unfortunately we cannot rely upon the .debug_str section ending with a
1806 NUL byte. Since our caller is expecting to receive a well formed C
1807 string we test for the lack of a terminating byte here. */
1808 if (strnlen ((const char *) ret, section->size - offset)
1809 == section->size - offset)
1810 return _("<no NUL byte at end of alt .debug_str section>");
1811
1812 return ret;
1813 }
1814
1815 warn (_("DW_FORM_GNU_strp_alt offset (%#" PRIx64 ")"
1816 " too big or no string sections available\n"), offset);
1817 return _("<offset is too big>");
1818 }
1819
1820 static const char *
1821 get_AT_name (unsigned long attribute)
1822 {
1823 const char *name;
1824
1825 if (attribute == 0)
1826 return "DW_AT value: 0";
1827
1828 /* One value is shared by the MIPS and HP extensions: */
1829 if (attribute == DW_AT_MIPS_fde)
1830 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1831
1832 name = get_DW_AT_name (attribute);
1833
1834 if (name == NULL)
1835 {
1836 static char buffer[100];
1837
1838 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1839 attribute);
1840 return buffer;
1841 }
1842
1843 return name;
1844 }
1845
1846 static void
1847 add_dwo_info (const char * value, uint64_t cu_offset, dwo_type type)
1848 {
1849 dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1850
1851 dwinfo->type = type;
1852 dwinfo->value = value;
1853 dwinfo->cu_offset = cu_offset;
1854 dwinfo->next = first_dwo_info;
1855 first_dwo_info = dwinfo;
1856 }
1857
1858 static void
1859 add_dwo_name (const char * name, uint64_t cu_offset)
1860 {
1861 add_dwo_info (name, cu_offset, DWO_NAME);
1862 }
1863
1864 static void
1865 add_dwo_dir (const char * dir, uint64_t cu_offset)
1866 {
1867 add_dwo_info (dir, cu_offset, DWO_DIR);
1868 }
1869
1870 static void
1871 add_dwo_id (const char * id, uint64_t cu_offset)
1872 {
1873 add_dwo_info (id, cu_offset, DWO_ID);
1874 }
1875
1876 static void
1877 free_dwo_info (void)
1878 {
1879 dwo_info * dwinfo;
1880 dwo_info * next;
1881
1882 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1883 {
1884 next = dwinfo->next;
1885 free (dwinfo);
1886 }
1887 first_dwo_info = NULL;
1888 }
1889
1890 /* Ensure that START + UVALUE is less than END.
1891 Return an adjusted UVALUE if necessary to ensure this relationship. */
1892
1893 static inline uint64_t
1894 check_uvalue (const unsigned char *start,
1895 uint64_t uvalue,
1896 const unsigned char *end)
1897 {
1898 uint64_t max_uvalue = end - start;
1899
1900 /* See PR 17512: file: 008-103549-0.001:0.1.
1901 and PR 24829 for examples of where these tests are triggered. */
1902 if (uvalue > max_uvalue)
1903 {
1904 warn (_("Corrupt attribute block length: %#" PRIx64 "\n"), uvalue);
1905 uvalue = max_uvalue;
1906 }
1907
1908 return uvalue;
1909 }
1910
1911 static unsigned char *
1912 skip_attr_bytes (unsigned long form,
1913 unsigned char *data,
1914 unsigned char *end,
1915 uint64_t pointer_size,
1916 uint64_t offset_size,
1917 int dwarf_version,
1918 uint64_t *value_return)
1919 {
1920 int64_t svalue;
1921 uint64_t uvalue = 0;
1922 uint64_t inc = 0;
1923
1924 * value_return = 0;
1925
1926 switch (form)
1927 {
1928 case DW_FORM_ref_addr:
1929 if (dwarf_version == 2)
1930 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1931 else if (dwarf_version > 2)
1932 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1933 else
1934 return NULL;
1935 break;
1936
1937 case DW_FORM_addr:
1938 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1939 break;
1940
1941 case DW_FORM_strp:
1942 case DW_FORM_line_strp:
1943 case DW_FORM_sec_offset:
1944 case DW_FORM_GNU_ref_alt:
1945 case DW_FORM_GNU_strp_alt:
1946 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1947 break;
1948
1949 case DW_FORM_flag_present:
1950 uvalue = 1;
1951 break;
1952
1953 case DW_FORM_ref1:
1954 case DW_FORM_flag:
1955 case DW_FORM_data1:
1956 case DW_FORM_strx1:
1957 case DW_FORM_addrx1:
1958 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1959 break;
1960
1961 case DW_FORM_strx3:
1962 case DW_FORM_addrx3:
1963 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
1964 break;
1965
1966 case DW_FORM_ref2:
1967 case DW_FORM_data2:
1968 case DW_FORM_strx2:
1969 case DW_FORM_addrx2:
1970 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1971 break;
1972
1973 case DW_FORM_ref4:
1974 case DW_FORM_data4:
1975 case DW_FORM_strx4:
1976 case DW_FORM_addrx4:
1977 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1978 break;
1979
1980 case DW_FORM_sdata:
1981 READ_SLEB (svalue, data, end);
1982 uvalue = svalue;
1983 break;
1984
1985 case DW_FORM_ref_udata:
1986 case DW_FORM_udata:
1987 case DW_FORM_GNU_str_index:
1988 case DW_FORM_strx:
1989 case DW_FORM_GNU_addr_index:
1990 case DW_FORM_addrx:
1991 case DW_FORM_loclistx:
1992 case DW_FORM_rnglistx:
1993 READ_ULEB (uvalue, data, end);
1994 break;
1995
1996 case DW_FORM_ref8:
1997 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1998 break;
1999
2000 case DW_FORM_data8:
2001 case DW_FORM_ref_sig8:
2002 inc = 8;
2003 break;
2004
2005 case DW_FORM_data16:
2006 inc = 16;
2007 break;
2008
2009 case DW_FORM_string:
2010 inc = strnlen ((char *) data, end - data) + 1;
2011 break;
2012
2013 case DW_FORM_block:
2014 case DW_FORM_exprloc:
2015 READ_ULEB (uvalue, data, end);
2016 inc = uvalue;
2017 break;
2018
2019 case DW_FORM_block1:
2020 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2021 inc = uvalue;
2022 break;
2023
2024 case DW_FORM_block2:
2025 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2026 inc = uvalue;
2027 break;
2028
2029 case DW_FORM_block4:
2030 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2031 inc = uvalue;
2032 break;
2033
2034 case DW_FORM_indirect:
2035 READ_ULEB (form, data, end);
2036 if (form == DW_FORM_implicit_const)
2037 SKIP_ULEB (data, end);
2038 return skip_attr_bytes (form, data, end, pointer_size, offset_size,
2039 dwarf_version, value_return);
2040
2041 default:
2042 return NULL;
2043 }
2044
2045 * value_return = uvalue;
2046 if (inc <= (size_t) (end - data))
2047 data += inc;
2048 else
2049 data = end;
2050 return data;
2051 }
2052
2053 /* Given form FORM with value UVALUE, locate and return the abbreviation
2054 associated with it. */
2055
2056 static abbrev_entry *
2057 get_type_abbrev_from_form (unsigned long form,
2058 unsigned long uvalue,
2059 uint64_t cu_offset,
2060 unsigned char *cu_end,
2061 const struct dwarf_section *section,
2062 unsigned long *abbrev_num_return,
2063 unsigned char **data_return,
2064 abbrev_map **map_return)
2065 {
2066 unsigned long abbrev_number;
2067 abbrev_map * map;
2068 abbrev_entry * entry;
2069 unsigned char * data;
2070
2071 if (abbrev_num_return != NULL)
2072 * abbrev_num_return = 0;
2073 if (data_return != NULL)
2074 * data_return = NULL;
2075
2076 switch (form)
2077 {
2078 case DW_FORM_GNU_ref_alt:
2079 case DW_FORM_ref_sig8:
2080 /* FIXME: We are unable to handle this form at the moment. */
2081 return NULL;
2082
2083 case DW_FORM_ref_addr:
2084 if (uvalue >= section->size)
2085 {
2086 warn (_("Unable to resolve ref_addr form: uvalue %lx "
2087 "> section size %" PRIx64 " (%s)\n"),
2088 uvalue, section->size, section->name);
2089 return NULL;
2090 }
2091 break;
2092
2093 case DW_FORM_ref_sup4:
2094 case DW_FORM_ref_sup8:
2095 break;
2096
2097 case DW_FORM_ref1:
2098 case DW_FORM_ref2:
2099 case DW_FORM_ref4:
2100 case DW_FORM_ref8:
2101 case DW_FORM_ref_udata:
2102 if (uvalue + cu_offset < uvalue
2103 || uvalue + cu_offset > (size_t) (cu_end - section->start))
2104 {
2105 warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %" PRIx64
2106 " > CU size %tx\n"),
2107 uvalue, cu_offset, cu_end - section->start);
2108 return NULL;
2109 }
2110 uvalue += cu_offset;
2111 break;
2112
2113 /* FIXME: Are there other DW_FORMs that can be used by types ? */
2114
2115 default:
2116 warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2117 return NULL;
2118 }
2119
2120 data = (unsigned char *) section->start + uvalue;
2121 map = find_abbrev_map_by_offset (uvalue);
2122
2123 if (map == NULL)
2124 {
2125 warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2126 return NULL;
2127 }
2128 if (map->list == NULL)
2129 {
2130 warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2131 return NULL;
2132 }
2133
2134 if (map_return != NULL)
2135 {
2136 if (form == DW_FORM_ref_addr)
2137 *map_return = map;
2138 else
2139 *map_return = NULL;
2140 }
2141
2142 READ_ULEB (abbrev_number, data, section->start + section->size);
2143
2144 for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2145 if (entry->number == abbrev_number)
2146 break;
2147
2148 if (abbrev_num_return != NULL)
2149 * abbrev_num_return = abbrev_number;
2150
2151 if (data_return != NULL)
2152 * data_return = data;
2153
2154 if (entry == NULL)
2155 warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2156
2157 return entry;
2158 }
2159
2160 /* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2161 can be determined to be a signed type. The data for ENTRY can be
2162 found starting at DATA. */
2163
2164 static void
2165 get_type_signedness (abbrev_entry *entry,
2166 const struct dwarf_section *section,
2167 unsigned char *data,
2168 unsigned char *end,
2169 uint64_t cu_offset,
2170 uint64_t pointer_size,
2171 uint64_t offset_size,
2172 int dwarf_version,
2173 bool *is_signed,
2174 unsigned int nesting)
2175 {
2176 abbrev_attr * attr;
2177
2178 * is_signed = false;
2179
2180 #define MAX_NESTING 20
2181 if (nesting > MAX_NESTING)
2182 {
2183 /* FIXME: Warn - or is this expected ?
2184 NB/ We need to avoid infinite recursion. */
2185 return;
2186 }
2187
2188 for (attr = entry->first_attr;
2189 attr != NULL && attr->attribute;
2190 attr = attr->next)
2191 {
2192 unsigned char * orig_data = data;
2193 uint64_t uvalue = 0;
2194
2195 data = skip_attr_bytes (attr->form, data, end, pointer_size,
2196 offset_size, dwarf_version, & uvalue);
2197 if (data == NULL)
2198 return;
2199
2200 switch (attr->attribute)
2201 {
2202 case DW_AT_linkage_name:
2203 case DW_AT_name:
2204 if (do_wide)
2205 {
2206 if (attr->form == DW_FORM_strp)
2207 printf (", %s", fetch_indirect_string (uvalue));
2208 else if (attr->form == DW_FORM_string)
2209 printf (", %.*s", (int) (end - orig_data), orig_data);
2210 }
2211 break;
2212
2213 case DW_AT_type:
2214 /* Recurse. */
2215 {
2216 abbrev_entry *type_abbrev;
2217 unsigned char *type_data;
2218 abbrev_map *map;
2219
2220 type_abbrev = get_type_abbrev_from_form (attr->form,
2221 uvalue,
2222 cu_offset,
2223 end,
2224 section,
2225 NULL /* abbrev num return */,
2226 &type_data,
2227 &map);
2228 if (type_abbrev == NULL)
2229 break;
2230
2231 get_type_signedness (type_abbrev, section, type_data,
2232 map ? section->start + map->end : end,
2233 map ? map->start : cu_offset,
2234 pointer_size, offset_size, dwarf_version,
2235 is_signed, nesting + 1);
2236 }
2237 break;
2238
2239 case DW_AT_encoding:
2240 /* Determine signness. */
2241 switch (uvalue)
2242 {
2243 case DW_ATE_address:
2244 /* FIXME - some architectures have signed addresses. */
2245 case DW_ATE_boolean:
2246 case DW_ATE_unsigned:
2247 case DW_ATE_unsigned_char:
2248 case DW_ATE_unsigned_fixed:
2249 * is_signed = false;
2250 break;
2251
2252 default:
2253 case DW_ATE_complex_float:
2254 case DW_ATE_float:
2255 case DW_ATE_signed:
2256 case DW_ATE_signed_char:
2257 case DW_ATE_imaginary_float:
2258 case DW_ATE_decimal_float:
2259 case DW_ATE_signed_fixed:
2260 * is_signed = true;
2261 break;
2262 }
2263 break;
2264 }
2265 }
2266 }
2267
2268 static void
2269 read_and_print_leb128 (unsigned char *data,
2270 unsigned int *bytes_read,
2271 unsigned const char *end,
2272 bool is_signed)
2273 {
2274 int status;
2275 uint64_t val = read_leb128 (data, end, is_signed, bytes_read, &status);
2276 if (status != 0)
2277 report_leb_status (status);
2278 else if (is_signed)
2279 printf ("%" PRId64, val);
2280 else
2281 printf ("%" PRIu64, val);
2282 }
2283
2284 static void
2285 display_discr_list (unsigned long form,
2286 uint64_t uvalue,
2287 unsigned char *data,
2288 int level)
2289 {
2290 unsigned char *end = data;
2291
2292 if (uvalue == 0)
2293 {
2294 printf ("[default]");
2295 return;
2296 }
2297
2298 switch (form)
2299 {
2300 case DW_FORM_block:
2301 case DW_FORM_block1:
2302 case DW_FORM_block2:
2303 case DW_FORM_block4:
2304 /* Move data pointer back to the start of the byte array. */
2305 data -= uvalue;
2306 break;
2307 default:
2308 printf ("<corrupt>\n");
2309 warn (_("corrupt discr_list - not using a block form\n"));
2310 return;
2311 }
2312
2313 if (uvalue < 2)
2314 {
2315 printf ("<corrupt>\n");
2316 warn (_("corrupt discr_list - block not long enough\n"));
2317 return;
2318 }
2319
2320 bool is_signed = (level > 0 && level <= MAX_CU_NESTING
2321 ? level_type_signed [level - 1] : false);
2322
2323 printf ("(");
2324 while (data < end)
2325 {
2326 unsigned char discriminant;
2327 unsigned int bytes_read;
2328
2329 SAFE_BYTE_GET_AND_INC (discriminant, data, 1, end);
2330
2331 switch (discriminant)
2332 {
2333 case DW_DSC_label:
2334 printf ("label ");
2335 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2336 data += bytes_read;
2337 break;
2338
2339 case DW_DSC_range:
2340 printf ("range ");
2341 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2342 data += bytes_read;
2343
2344 printf ("..");
2345 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2346 data += bytes_read;
2347 break;
2348
2349 default:
2350 printf ("<corrupt>\n");
2351 warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
2352 discriminant);
2353 return;
2354 }
2355
2356 if (data < end)
2357 printf (", ");
2358 }
2359
2360 if (is_signed)
2361 printf (")(signed)");
2362 else
2363 printf (")(unsigned)");
2364 }
2365
2366 static unsigned char *
2367 read_and_display_attr_value (unsigned long attribute,
2368 unsigned long form,
2369 int64_t implicit_const,
2370 unsigned char *start,
2371 unsigned char *data,
2372 unsigned char *end,
2373 uint64_t cu_offset,
2374 uint64_t pointer_size,
2375 uint64_t offset_size,
2376 int dwarf_version,
2377 debug_info *debug_info_p,
2378 int do_loc,
2379 struct dwarf_section *section,
2380 struct cu_tu_set *this_set,
2381 char delimiter,
2382 int level)
2383 {
2384 int64_t svalue;
2385 uint64_t uvalue = 0;
2386 uint64_t uvalue_hi = 0;
2387 unsigned char *block_start = NULL;
2388 unsigned char *orig_data = data;
2389
2390 if (data > end || (data == end && form != DW_FORM_flag_present))
2391 {
2392 warn (_("Corrupt attribute\n"));
2393 return data;
2394 }
2395
2396 if (do_wide && ! do_loc)
2397 {
2398 /* PR 26847: Display the name of the form. */
2399 const char * name = get_FORM_name (form);
2400
2401 /* For convenience we skip the DW_FORM_ prefix to the name. */
2402 if (name[0] == 'D')
2403 name += 8; /* strlen ("DW_FORM_") */
2404 printf ("%c(%s)", delimiter, name);
2405 }
2406
2407 switch (form)
2408 {
2409 case DW_FORM_ref_addr:
2410 if (dwarf_version == 2)
2411 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2412 else if (dwarf_version > 2)
2413 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2414 else
2415 error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
2416 break;
2417
2418 case DW_FORM_addr:
2419 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2420 break;
2421
2422 case DW_FORM_strp_sup:
2423 case DW_FORM_strp:
2424 case DW_FORM_line_strp:
2425 case DW_FORM_sec_offset:
2426 case DW_FORM_GNU_ref_alt:
2427 case DW_FORM_GNU_strp_alt:
2428 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2429 break;
2430
2431 case DW_FORM_flag_present:
2432 uvalue = 1;
2433 break;
2434
2435 case DW_FORM_ref1:
2436 case DW_FORM_flag:
2437 case DW_FORM_data1:
2438 case DW_FORM_strx1:
2439 case DW_FORM_addrx1:
2440 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2441 break;
2442
2443 case DW_FORM_ref2:
2444 case DW_FORM_data2:
2445 case DW_FORM_strx2:
2446 case DW_FORM_addrx2:
2447 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2448 break;
2449
2450 case DW_FORM_strx3:
2451 case DW_FORM_addrx3:
2452 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2453 break;
2454
2455 case DW_FORM_ref_sup4:
2456 case DW_FORM_ref4:
2457 case DW_FORM_data4:
2458 case DW_FORM_strx4:
2459 case DW_FORM_addrx4:
2460 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2461 break;
2462
2463 case DW_FORM_ref_sup8:
2464 case DW_FORM_ref8:
2465 case DW_FORM_data8:
2466 case DW_FORM_ref_sig8:
2467 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2468 break;
2469
2470 case DW_FORM_data16:
2471 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2472 SAFE_BYTE_GET_AND_INC (uvalue_hi, data, 8, end);
2473 if (byte_get != byte_get_little_endian)
2474 {
2475 uint64_t utmp = uvalue;
2476 uvalue = uvalue_hi;
2477 uvalue_hi = utmp;
2478 }
2479 break;
2480
2481 case DW_FORM_sdata:
2482 READ_SLEB (svalue, data, end);
2483 uvalue = svalue;
2484 break;
2485
2486 case DW_FORM_GNU_str_index:
2487 case DW_FORM_strx:
2488 case DW_FORM_ref_udata:
2489 case DW_FORM_udata:
2490 case DW_FORM_GNU_addr_index:
2491 case DW_FORM_addrx:
2492 case DW_FORM_loclistx:
2493 case DW_FORM_rnglistx:
2494 READ_ULEB (uvalue, data, end);
2495 break;
2496
2497 case DW_FORM_indirect:
2498 READ_ULEB (form, data, end);
2499 if (!do_loc)
2500 printf ("%c%s", delimiter, get_FORM_name (form));
2501 if (form == DW_FORM_implicit_const)
2502 READ_SLEB (implicit_const, data, end);
2503 return read_and_display_attr_value (attribute, form, implicit_const,
2504 start, data, end,
2505 cu_offset, pointer_size,
2506 offset_size, dwarf_version,
2507 debug_info_p, do_loc,
2508 section, this_set, delimiter, level);
2509
2510 case DW_FORM_implicit_const:
2511 uvalue = implicit_const;
2512 break;
2513
2514 default:
2515 break;
2516 }
2517
2518 switch (form)
2519 {
2520 case DW_FORM_ref_addr:
2521 if (!do_loc)
2522 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2523 break;
2524
2525 case DW_FORM_GNU_ref_alt:
2526 if (!do_loc)
2527 {
2528 if (do_wide)
2529 /* We have already printed the form name. */
2530 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2531 else
2532 printf ("%c<alt %#" PRIx64 ">", delimiter, uvalue);
2533 }
2534 /* FIXME: Follow the reference... */
2535 break;
2536
2537 case DW_FORM_ref1:
2538 case DW_FORM_ref2:
2539 case DW_FORM_ref4:
2540 case DW_FORM_ref_sup4:
2541 case DW_FORM_ref_udata:
2542 if (!do_loc)
2543 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2544 break;
2545
2546 case DW_FORM_data4:
2547 case DW_FORM_addr:
2548 case DW_FORM_sec_offset:
2549 if (!do_loc)
2550 printf ("%c%#" PRIx64, delimiter, uvalue);
2551 break;
2552
2553 case DW_FORM_flag_present:
2554 case DW_FORM_flag:
2555 case DW_FORM_data1:
2556 case DW_FORM_data2:
2557 case DW_FORM_sdata:
2558 if (!do_loc)
2559 printf ("%c%" PRId64, delimiter, uvalue);
2560 break;
2561
2562 case DW_FORM_udata:
2563 if (!do_loc)
2564 printf ("%c%" PRIu64, delimiter, uvalue);
2565 break;
2566
2567 case DW_FORM_implicit_const:
2568 if (!do_loc)
2569 printf ("%c%" PRId64, delimiter, implicit_const);
2570 break;
2571
2572 case DW_FORM_ref_sup8:
2573 case DW_FORM_ref8:
2574 case DW_FORM_data8:
2575 if (!do_loc)
2576 {
2577 uint64_t utmp = uvalue;
2578 if (form == DW_FORM_ref8)
2579 utmp += cu_offset;
2580 printf ("%c%#" PRIx64, delimiter, utmp);
2581 }
2582 break;
2583
2584 case DW_FORM_data16:
2585 if (!do_loc)
2586 {
2587 if (uvalue_hi == 0)
2588 printf (" %#" PRIx64, uvalue);
2589 else
2590 printf (" %#" PRIx64 "%016" PRIx64, uvalue_hi, uvalue);
2591 }
2592 break;
2593
2594 case DW_FORM_string:
2595 if (!do_loc)
2596 printf ("%c%.*s", delimiter, (int) (end - data), data);
2597 data += strnlen ((char *) data, end - data);
2598 if (data < end)
2599 data++;
2600 break;
2601
2602 case DW_FORM_block:
2603 case DW_FORM_exprloc:
2604 READ_ULEB (uvalue, data, end);
2605 do_block:
2606 block_start = data;
2607 if (block_start >= end)
2608 {
2609 warn (_("Block ends prematurely\n"));
2610 uvalue = 0;
2611 block_start = end;
2612 }
2613
2614 uvalue = check_uvalue (block_start, uvalue, end);
2615
2616 data = block_start + uvalue;
2617 if (!do_loc)
2618 {
2619 unsigned char op;
2620
2621 SAFE_BYTE_GET (op, block_start, sizeof (op), end);
2622 if (op != DW_OP_addrx)
2623 data = display_block (block_start, uvalue, end, delimiter);
2624 }
2625 break;
2626
2627 case DW_FORM_block1:
2628 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2629 goto do_block;
2630
2631 case DW_FORM_block2:
2632 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2633 goto do_block;
2634
2635 case DW_FORM_block4:
2636 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2637 goto do_block;
2638
2639 case DW_FORM_strp:
2640 if (!do_loc)
2641 {
2642 if (do_wide)
2643 /* We have already displayed the form name. */
2644 printf (_("%c(offset: %#" PRIx64 "): %s"),
2645 delimiter, uvalue, fetch_indirect_string (uvalue));
2646 else
2647 printf (_("%c(indirect string, offset: %#" PRIx64 "): %s"),
2648 delimiter, uvalue, fetch_indirect_string (uvalue));
2649 }
2650 break;
2651
2652 case DW_FORM_line_strp:
2653 if (!do_loc)
2654 {
2655 if (do_wide)
2656 /* We have already displayed the form name. */
2657 printf (_("%c(offset: %#" PRIx64 "): %s"),
2658 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2659 else
2660 printf (_("%c(indirect line string, offset: %#" PRIx64 "): %s"),
2661 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2662 }
2663 break;
2664
2665 case DW_FORM_GNU_str_index:
2666 case DW_FORM_strx:
2667 case DW_FORM_strx1:
2668 case DW_FORM_strx2:
2669 case DW_FORM_strx3:
2670 case DW_FORM_strx4:
2671 if (!do_loc)
2672 {
2673 const char *suffix = section ? strrchr (section->name, '.') : NULL;
2674 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2675 const char *strng;
2676
2677 strng = fetch_indexed_string (uvalue, this_set, offset_size, dwo,
2678 debug_info_p ? debug_info_p->str_offsets_base : 0);
2679 if (do_wide)
2680 /* We have already displayed the form name. */
2681 printf (_("%c(offset: %#" PRIx64 "): %s"),
2682 delimiter, uvalue, strng);
2683 else
2684 printf (_("%c(indexed string: %#" PRIx64 "): %s"),
2685 delimiter, uvalue, strng);
2686 }
2687 break;
2688
2689 case DW_FORM_GNU_strp_alt:
2690 if (!do_loc)
2691 {
2692 if (do_wide)
2693 /* We have already displayed the form name. */
2694 printf (_("%c(offset: %#" PRIx64 ") %s"),
2695 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2696 else
2697 printf (_("%c(alt indirect string, offset: %#" PRIx64 ") %s"),
2698 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2699 }
2700 break;
2701
2702 case DW_FORM_indirect:
2703 /* Handled above. */
2704 break;
2705
2706 case DW_FORM_ref_sig8:
2707 if (!do_loc)
2708 printf ("%c%s: %#" PRIx64, delimiter, do_wide ? "" : "signature",
2709 uvalue);
2710 break;
2711
2712 case DW_FORM_GNU_addr_index:
2713 case DW_FORM_addrx:
2714 case DW_FORM_addrx1:
2715 case DW_FORM_addrx2:
2716 case DW_FORM_addrx3:
2717 case DW_FORM_addrx4:
2718 case DW_FORM_loclistx:
2719 case DW_FORM_rnglistx:
2720 if (!do_loc)
2721 {
2722 uint64_t base, idx;
2723 const char *suffix = strrchr (section->name, '.');
2724 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2725
2726 if (form == DW_FORM_loclistx)
2727 {
2728 if (dwo)
2729 {
2730 idx = fetch_indexed_value (uvalue, loclists_dwo, 0);
2731 if (idx != (uint64_t) -1)
2732 idx += (offset_size == 8) ? 20 : 12;
2733 }
2734 else if (debug_info_p == NULL)
2735 {
2736 idx = fetch_indexed_value (uvalue, loclists, 0);
2737 }
2738 else
2739 {
2740 /* We want to compute:
2741 idx = fetch_indexed_value (uvalue, loclists, debug_info_p->loclists_base);
2742 idx += debug_info_p->loclists_base;
2743 Fortunately we already have that sum cached in the
2744 loc_offsets array. */
2745 if (uvalue < debug_info_p->num_loc_offsets)
2746 idx = debug_info_p->loc_offsets [uvalue];
2747 else
2748 {
2749 warn (_("loc_offset %" PRIu64 " too big\n"), uvalue);
2750 idx = -1;
2751 }
2752 }
2753 }
2754 else if (form == DW_FORM_rnglistx)
2755 {
2756 if (dwo)
2757 {
2758 idx = fetch_indexed_value (uvalue, rnglists_dwo, 0);
2759 if (idx != (uint64_t) -1)
2760 idx += (offset_size == 8) ? 20 : 12;
2761 }
2762 else
2763 {
2764 if (debug_info_p == NULL)
2765 base = 0;
2766 else
2767 base = debug_info_p->rnglists_base;
2768 /* We do not have a cached value this time, so we perform the
2769 computation manually. */
2770 idx = fetch_indexed_value (uvalue, rnglists, base);
2771 if (idx != (uint64_t) -1)
2772 idx += base;
2773 }
2774 }
2775 else
2776 {
2777 if (debug_info_p == NULL)
2778 base = 0;
2779 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2780 base = 0;
2781 else
2782 base = debug_info_p->addr_base;
2783
2784 base += uvalue * pointer_size;
2785 idx = fetch_indexed_addr (base, pointer_size);
2786 }
2787
2788 /* We have already displayed the form name. */
2789 if (idx != (uint64_t) -1)
2790 printf (_("%c(index: %#" PRIx64 "): %#" PRIx64),
2791 delimiter, uvalue, idx);
2792 }
2793 break;
2794
2795 case DW_FORM_strp_sup:
2796 if (!do_loc)
2797 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2798 break;
2799
2800 default:
2801 warn (_("Unrecognized form: %#lx"), form);
2802 /* What to do? Consume a byte maybe? */
2803 ++data;
2804 break;
2805 }
2806
2807 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
2808 && num_debug_info_entries == 0
2809 && debug_info_p != NULL)
2810 {
2811 switch (attribute)
2812 {
2813 case DW_AT_loclists_base:
2814 if (debug_info_p->loclists_base)
2815 warn (_("CU @ %#" PRIx64 " has multiple loclists_base values "
2816 "(%#" PRIx64 " and %#" PRIx64 ")"),
2817 debug_info_p->cu_offset,
2818 debug_info_p->loclists_base, uvalue);
2819 svalue = uvalue;
2820 if (svalue < 0)
2821 {
2822 warn (_("CU @ %#" PRIx64 " has has a negative loclists_base "
2823 "value of %#" PRIx64 " - treating as zero"),
2824 debug_info_p->cu_offset, svalue);
2825 uvalue = 0;
2826 }
2827 debug_info_p->loclists_base = uvalue;
2828 break;
2829
2830 case DW_AT_rnglists_base:
2831 if (debug_info_p->rnglists_base)
2832 warn (_("CU @ %#" PRIx64 " has multiple rnglists_base values "
2833 "(%#" PRIx64 " and %#" PRIx64 ")"),
2834 debug_info_p->cu_offset,
2835 debug_info_p->rnglists_base, uvalue);
2836 svalue = uvalue;
2837 if (svalue < 0)
2838 {
2839 warn (_("CU @ %#" PRIx64 " has has a negative rnglists_base "
2840 "value of %#" PRIx64 " - treating as zero"),
2841 debug_info_p->cu_offset, svalue);
2842 uvalue = 0;
2843 }
2844 debug_info_p->rnglists_base = uvalue;
2845 break;
2846
2847 case DW_AT_str_offsets_base:
2848 if (debug_info_p->str_offsets_base)
2849 warn (_("CU @ %#" PRIx64 " has multiple str_offsets_base values "
2850 "%#" PRIx64 " and %#" PRIx64 ")"),
2851 debug_info_p->cu_offset,
2852 debug_info_p->str_offsets_base, uvalue);
2853 svalue = uvalue;
2854 if (svalue < 0)
2855 {
2856 warn (_("CU @ %#" PRIx64 " has has a negative stroffsets_base "
2857 "value of %#" PRIx64 " - treating as zero"),
2858 debug_info_p->cu_offset, svalue);
2859 uvalue = 0;
2860 }
2861 debug_info_p->str_offsets_base = uvalue;
2862 break;
2863
2864 case DW_AT_frame_base:
2865 have_frame_base = 1;
2866 /* Fall through. */
2867 case DW_AT_location:
2868 case DW_AT_GNU_locviews:
2869 case DW_AT_string_length:
2870 case DW_AT_return_addr:
2871 case DW_AT_data_member_location:
2872 case DW_AT_vtable_elem_location:
2873 case DW_AT_segment:
2874 case DW_AT_static_link:
2875 case DW_AT_use_location:
2876 case DW_AT_call_value:
2877 case DW_AT_GNU_call_site_value:
2878 case DW_AT_call_data_value:
2879 case DW_AT_GNU_call_site_data_value:
2880 case DW_AT_call_target:
2881 case DW_AT_GNU_call_site_target:
2882 case DW_AT_call_target_clobbered:
2883 case DW_AT_GNU_call_site_target_clobbered:
2884 if ((dwarf_version < 4
2885 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2886 || form == DW_FORM_sec_offset
2887 || form == DW_FORM_loclistx)
2888 {
2889 /* Process location list. */
2890 unsigned int lmax = debug_info_p->max_loc_offsets;
2891 unsigned int num = debug_info_p->num_loc_offsets;
2892
2893 if (lmax == 0 || num >= lmax)
2894 {
2895 lmax += 1024;
2896 debug_info_p->loc_offsets = (uint64_t *)
2897 xcrealloc (debug_info_p->loc_offsets,
2898 lmax, sizeof (*debug_info_p->loc_offsets));
2899 debug_info_p->loc_views = (uint64_t *)
2900 xcrealloc (debug_info_p->loc_views,
2901 lmax, sizeof (*debug_info_p->loc_views));
2902 debug_info_p->have_frame_base = (int *)
2903 xcrealloc (debug_info_p->have_frame_base,
2904 lmax, sizeof (*debug_info_p->have_frame_base));
2905 debug_info_p->max_loc_offsets = lmax;
2906 }
2907 if (form == DW_FORM_loclistx)
2908 uvalue = fetch_indexed_value (num, loclists, debug_info_p->loclists_base);
2909 else if (this_set != NULL)
2910 uvalue += this_set->section_offsets [DW_SECT_LOC];
2911
2912 debug_info_p->have_frame_base [num] = have_frame_base;
2913 if (attribute != DW_AT_GNU_locviews)
2914 {
2915 uvalue += debug_info_p->loclists_base;
2916
2917 /* Corrupt DWARF info can produce more offsets than views.
2918 See PR 23062 for an example. */
2919 if (debug_info_p->num_loc_offsets
2920 > debug_info_p->num_loc_views)
2921 warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2922 else
2923 {
2924 debug_info_p->loc_offsets [num] = uvalue;
2925 debug_info_p->num_loc_offsets++;
2926 }
2927 }
2928 else
2929 {
2930 assert (debug_info_p->num_loc_views <= num);
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 int64_t 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 %" PRId64 " 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 %" PRId64 " 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 %" PRId64 "\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
5001 directory_table = (char **)
5002 xcalloc (n_directories, sizeof (unsigned char *));
5003
5004 for (entryi = 0; entryi < n_directories; entryi++)
5005 {
5006 char **pathp = &directory_table[entryi];
5007
5008 format = format_start;
5009 for (formati = 0; formati < format_count; formati++)
5010 {
5011 uint64_t content_type, form;
5012 uint64_t uvalue;
5013
5014 READ_ULEB (content_type, format, end);
5015 READ_ULEB (form, format, end);
5016 if (data >= end)
5017 {
5018 warn (_("Corrupt directories list\n"));
5019 break;
5020 }
5021 switch (content_type)
5022 {
5023 case DW_LNCT_path:
5024 switch (form)
5025 {
5026 case DW_FORM_string:
5027 *pathp = (char *) data;
5028 break;
5029 case DW_FORM_line_strp:
5030 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5031 end);
5032 /* Remove const by the cast. */
5033 *pathp = (char *)
5034 fetch_indirect_line_string (uvalue);
5035 break;
5036 }
5037 break;
5038 }
5039 data = read_and_display_attr_value (0, form, 0, start,
5040 data, end, 0, 0,
5041 linfo.li_offset_size,
5042 linfo.li_version,
5043 NULL, 1, section,
5044 NULL, '\t', -1);
5045 }
5046 if (data >= end)
5047 {
5048 warn (_("Corrupt directories list\n"));
5049 break;
5050 }
5051 }
5052
5053 /* Skip files format. */
5054 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5055 if (do_checks && format_count > 5)
5056 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5057 format_count);
5058 format_start = data;
5059 for (formati = 0; formati < format_count; formati++)
5060 {
5061 SKIP_ULEB (data, end);
5062 SKIP_ULEB (data, end);
5063 }
5064
5065 READ_ULEB (n_files, data, end);
5066 if (data >= end && n_files > 0)
5067 {
5068 warn (_("Corrupt file name list\n"));
5069 break;
5070 }
5071
5072 if (n_files == 0)
5073 file_table = NULL;
5074 else
5075 file_table = (File_Entry *) xcalloc (n_files,
5076 sizeof (File_Entry));
5077
5078 for (entryi = 0; entryi < n_files; entryi++)
5079 {
5080 File_Entry *file = &file_table[entryi];
5081
5082 format = format_start;
5083 for (formati = 0; formati < format_count; formati++)
5084 {
5085 uint64_t content_type, form;
5086 uint64_t uvalue;
5087 unsigned char *tmp;
5088
5089 READ_ULEB (content_type, format, end);
5090 READ_ULEB (form, format, end);
5091 if (data >= end)
5092 {
5093 warn (_("Corrupt file name list\n"));
5094 break;
5095 }
5096 switch (content_type)
5097 {
5098 case DW_LNCT_path:
5099 switch (form)
5100 {
5101 case DW_FORM_string:
5102 file->name = (char *) data;
5103 break;
5104 case DW_FORM_line_strp:
5105 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5106 end);
5107 /* Remove const by the cast. */
5108 file->name = (char *)
5109 fetch_indirect_line_string (uvalue);
5110 break;
5111 }
5112 break;
5113 case DW_LNCT_directory_index:
5114 switch (form)
5115 {
5116 case DW_FORM_data1:
5117 SAFE_BYTE_GET (file->directory_index, data, 1,
5118 end);
5119 break;
5120 case DW_FORM_data2:
5121 SAFE_BYTE_GET (file->directory_index, data, 2,
5122 end);
5123 break;
5124 case DW_FORM_udata:
5125 tmp = data;
5126 READ_ULEB (file->directory_index, tmp, end);
5127 break;
5128 }
5129 break;
5130 }
5131 data = read_and_display_attr_value (0, form, 0, start,
5132 data, end, 0, 0,
5133 linfo.li_offset_size,
5134 linfo.li_version,
5135 NULL, 1, section,
5136 NULL, '\t', -1);
5137 }
5138 if (data >= end)
5139 {
5140 warn (_("Corrupt file name list\n"));
5141 break;
5142 }
5143 }
5144 }
5145 else
5146 {
5147 if (*data != 0)
5148 {
5149 char *ptr_directory_table = (char *) data;
5150
5151 while (data < end && *data != 0)
5152 {
5153 data += strnlen ((char *) data, end - data);
5154 if (data < end)
5155 data++;
5156 n_directories++;
5157 }
5158
5159 /* PR 20440 */
5160 if (data >= end)
5161 {
5162 warn (_("directory table ends unexpectedly\n"));
5163 n_directories = 0;
5164 break;
5165 }
5166
5167 /* Go through the directory table again to save the directories. */
5168 directory_table = (char **)
5169 xmalloc (n_directories * sizeof (unsigned char *));
5170
5171 i = 0;
5172 while (*ptr_directory_table != 0)
5173 {
5174 directory_table[i] = ptr_directory_table;
5175 ptr_directory_table += strlen (ptr_directory_table) + 1;
5176 i++;
5177 }
5178 }
5179 /* Skip the NUL at the end of the table. */
5180 data++;
5181
5182 /* Traverse the File Name table just to count the entries. */
5183 if (data < end && *data != 0)
5184 {
5185 unsigned char *ptr_file_name_table = data;
5186
5187 while (data < end && *data != 0)
5188 {
5189 /* Skip Name, directory index, last modification
5190 time and length of file. */
5191 data += strnlen ((char *) data, end - data);
5192 if (data < end)
5193 data++;
5194 SKIP_ULEB (data, end);
5195 SKIP_ULEB (data, end);
5196 SKIP_ULEB (data, end);
5197 n_files++;
5198 }
5199
5200 if (data >= end)
5201 {
5202 warn (_("file table ends unexpectedly\n"));
5203 n_files = 0;
5204 break;
5205 }
5206
5207 /* Go through the file table again to save the strings. */
5208 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5209
5210 i = 0;
5211 while (*ptr_file_name_table != 0)
5212 {
5213 file_table[i].name = (char *) ptr_file_name_table;
5214 ptr_file_name_table
5215 += strlen ((char *) ptr_file_name_table) + 1;
5216
5217 /* We are not interested in directory, time or size. */
5218 READ_ULEB (file_table[i].directory_index,
5219 ptr_file_name_table, end);
5220 READ_ULEB (file_table[i].modification_date,
5221 ptr_file_name_table, end);
5222 READ_ULEB (file_table[i].length,
5223 ptr_file_name_table, end);
5224 i++;
5225 }
5226 i = 0;
5227 }
5228
5229 /* Skip the NUL at the end of the table. */
5230 data++;
5231 }
5232
5233 /* Print the Compilation Unit's name and a header. */
5234 if (file_table == NULL)
5235 printf (_("CU: No directory table\n"));
5236 else if (directory_table == NULL)
5237 printf (_("CU: %s:\n"), null_name (file_table[0].name));
5238 else
5239 {
5240 unsigned int ix = file_table[0].directory_index;
5241 const char *directory;
5242
5243 if (ix == 0 && linfo.li_version < 5)
5244 directory = ".";
5245 /* PR 20439 */
5246 else if (n_directories == 0)
5247 directory = _("<unknown>");
5248 else
5249 {
5250 if (linfo.li_version < 5)
5251 --ix;
5252 if (ix >= n_directories)
5253 {
5254 warn (_("directory index %u "
5255 ">= number of directories %u\n"),
5256 ix, n_directories);
5257 directory = _("<corrupt>");
5258 }
5259 else
5260 directory = directory_table[ix];
5261 }
5262 if (do_wide)
5263 printf (_("CU: %s/%s:\n"),
5264 null_name (directory),
5265 null_name (file_table[0].name));
5266 else
5267 printf ("%s:\n", null_name (file_table[0].name));
5268 }
5269
5270 if (n_files > 0)
5271 {
5272 if (do_wide)
5273 printf (_("File name Line number Starting address View Stmt\n"));
5274 else
5275 printf (_("File name Line number Starting address View Stmt\n"));
5276 }
5277 else
5278 printf (_("CU: Empty file name table\n"));
5279 saved_linfo = linfo;
5280 }
5281
5282 /* This loop iterates through the Dwarf Line Number Program. */
5283 while (data < end_of_sequence)
5284 {
5285 unsigned char op_code;
5286 int xop;
5287 int adv;
5288 unsigned long int uladv;
5289 int is_special_opcode = 0;
5290
5291 op_code = *data++;
5292 xop = op_code;
5293
5294 if (op_code >= linfo.li_opcode_base)
5295 {
5296 op_code -= linfo.li_opcode_base;
5297 uladv = (op_code / linfo.li_line_range);
5298 if (linfo.li_max_ops_per_insn == 1)
5299 {
5300 uladv *= linfo.li_min_insn_length;
5301 state_machine_regs.address += uladv;
5302 if (uladv)
5303 state_machine_regs.view = 0;
5304 }
5305 else
5306 {
5307 unsigned addrdelta
5308 = ((state_machine_regs.op_index + uladv)
5309 / linfo.li_max_ops_per_insn)
5310 * linfo.li_min_insn_length;
5311 state_machine_regs.address
5312 += addrdelta;
5313 state_machine_regs.op_index
5314 = (state_machine_regs.op_index + uladv)
5315 % linfo.li_max_ops_per_insn;
5316 if (addrdelta)
5317 state_machine_regs.view = 0;
5318 }
5319
5320 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5321 state_machine_regs.line += adv;
5322 is_special_opcode = 1;
5323 /* Increment view after printing this row. */
5324 }
5325 else
5326 switch (op_code)
5327 {
5328 case DW_LNS_extended_op:
5329 {
5330 unsigned int ext_op_code_len;
5331 unsigned char ext_op_code;
5332 unsigned char *op_code_end;
5333 unsigned char *op_code_data = data;
5334
5335 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5336 op_code_end = op_code_data + ext_op_code_len;
5337 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5338 {
5339 warn (_("Badly formed extended line op encountered!\n"));
5340 break;
5341 }
5342 ext_op_code = *op_code_data++;
5343 xop = ext_op_code;
5344 xop = -xop;
5345
5346 switch (ext_op_code)
5347 {
5348 case DW_LNE_end_sequence:
5349 /* Reset stuff after printing this row. */
5350 break;
5351 case DW_LNE_set_address:
5352 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5353 op_code_data,
5354 op_code_end - op_code_data,
5355 op_code_end);
5356 state_machine_regs.op_index = 0;
5357 state_machine_regs.view = 0;
5358 break;
5359 case DW_LNE_define_file:
5360 file_table = (File_Entry *) xrealloc
5361 (file_table, (n_files + 1) * sizeof (File_Entry));
5362
5363 ++state_machine_regs.last_file_entry;
5364 /* Source file name. */
5365 file_table[n_files].name = (char *) op_code_data;
5366 op_code_data += strlen ((char *) op_code_data) + 1;
5367 /* Directory index. */
5368 READ_ULEB (file_table[n_files].directory_index,
5369 op_code_data, op_code_end);
5370 /* Last modification time. */
5371 READ_ULEB (file_table[n_files].modification_date,
5372 op_code_data, op_code_end);
5373 /* File length. */
5374 READ_ULEB (file_table[n_files].length,
5375 op_code_data, op_code_end);
5376 n_files++;
5377 break;
5378
5379 case DW_LNE_set_discriminator:
5380 case DW_LNE_HP_set_sequence:
5381 /* Simply ignored. */
5382 break;
5383
5384 default:
5385 printf (_("UNKNOWN (%u): length %ld\n"),
5386 ext_op_code, (long int) (op_code_data - data));
5387 break;
5388 }
5389 data = op_code_end;
5390 break;
5391 }
5392 case DW_LNS_copy:
5393 /* Increment view after printing this row. */
5394 break;
5395
5396 case DW_LNS_advance_pc:
5397 READ_ULEB (uladv, data, end);
5398 if (linfo.li_max_ops_per_insn == 1)
5399 {
5400 uladv *= linfo.li_min_insn_length;
5401 state_machine_regs.address += uladv;
5402 if (uladv)
5403 state_machine_regs.view = 0;
5404 }
5405 else
5406 {
5407 unsigned addrdelta
5408 = ((state_machine_regs.op_index + uladv)
5409 / linfo.li_max_ops_per_insn)
5410 * linfo.li_min_insn_length;
5411 state_machine_regs.address
5412 += addrdelta;
5413 state_machine_regs.op_index
5414 = (state_machine_regs.op_index + uladv)
5415 % linfo.li_max_ops_per_insn;
5416 if (addrdelta)
5417 state_machine_regs.view = 0;
5418 }
5419 break;
5420
5421 case DW_LNS_advance_line:
5422 READ_SLEB (adv, data, end);
5423 state_machine_regs.line += adv;
5424 break;
5425
5426 case DW_LNS_set_file:
5427 READ_ULEB (uladv, data, end);
5428 state_machine_regs.file = uladv;
5429
5430 unsigned file = state_machine_regs.file;
5431 if (linfo.li_version < 5)
5432 --file;
5433
5434 if (file_table == NULL || n_files == 0)
5435 printf (_("\n [Use file table entry %d]\n"), file);
5436 /* PR 20439 */
5437 else if (file >= n_files)
5438 {
5439 warn (_("file index %u >= number of files %u\n"),
5440 file, n_files);
5441 printf (_("\n <over large file table index %u>"), file);
5442 }
5443 else
5444 {
5445 unsigned dir = file_table[file].directory_index;
5446 if (dir == 0 && linfo.li_version < 5)
5447 /* If directory index is 0, that means compilation
5448 current directory. bfd/dwarf2.c shows
5449 DW_AT_comp_dir here but in keeping with the
5450 readelf practice of minimal interpretation of
5451 file data, we show "./". */
5452 printf ("\n./%s:[++]\n",
5453 null_name (file_table[file].name));
5454 else if (directory_table == NULL || n_directories == 0)
5455 printf (_("\n [Use file %s "
5456 "in directory table entry %d]\n"),
5457 null_name (file_table[file].name), dir);
5458 else
5459 {
5460 if (linfo.li_version < 5)
5461 --dir;
5462 /* PR 20439 */
5463 if (dir >= n_directories)
5464 {
5465 warn (_("directory index %u "
5466 ">= number of directories %u\n"),
5467 dir, n_directories);
5468 printf (_("\n <over large directory table entry "
5469 "%u>\n"), dir);
5470 }
5471 else
5472 printf ("\n%s/%s:\n",
5473 null_name (directory_table[dir]),
5474 null_name (file_table[file].name));
5475 }
5476 }
5477 break;
5478
5479 case DW_LNS_set_column:
5480 READ_ULEB (uladv, data, end);
5481 state_machine_regs.column = uladv;
5482 break;
5483
5484 case DW_LNS_negate_stmt:
5485 adv = state_machine_regs.is_stmt;
5486 adv = ! adv;
5487 state_machine_regs.is_stmt = adv;
5488 break;
5489
5490 case DW_LNS_set_basic_block:
5491 state_machine_regs.basic_block = 1;
5492 break;
5493
5494 case DW_LNS_const_add_pc:
5495 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5496 if (linfo.li_max_ops_per_insn == 1)
5497 {
5498 uladv *= linfo.li_min_insn_length;
5499 state_machine_regs.address += uladv;
5500 if (uladv)
5501 state_machine_regs.view = 0;
5502 }
5503 else
5504 {
5505 unsigned addrdelta
5506 = ((state_machine_regs.op_index + uladv)
5507 / linfo.li_max_ops_per_insn)
5508 * linfo.li_min_insn_length;
5509 state_machine_regs.address
5510 += addrdelta;
5511 state_machine_regs.op_index
5512 = (state_machine_regs.op_index + uladv)
5513 % linfo.li_max_ops_per_insn;
5514 if (addrdelta)
5515 state_machine_regs.view = 0;
5516 }
5517 break;
5518
5519 case DW_LNS_fixed_advance_pc:
5520 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5521 state_machine_regs.address += uladv;
5522 state_machine_regs.op_index = 0;
5523 /* Do NOT reset view. */
5524 break;
5525
5526 case DW_LNS_set_prologue_end:
5527 break;
5528
5529 case DW_LNS_set_epilogue_begin:
5530 break;
5531
5532 case DW_LNS_set_isa:
5533 READ_ULEB (uladv, data, end);
5534 printf (_(" Set ISA to %lu\n"), uladv);
5535 break;
5536
5537 default:
5538 printf (_(" Unknown opcode %d with operands: "), op_code);
5539
5540 if (standard_opcodes != NULL)
5541 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5542 {
5543 uint64_t val;
5544
5545 READ_ULEB (val, data, end);
5546 printf ("%#" PRIx64 "%s", val, i == 1 ? "" : ", ");
5547 }
5548 putchar ('\n');
5549 break;
5550 }
5551
5552 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5553 to the DWARF address/line matrix. */
5554 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5555 || (xop == DW_LNS_copy))
5556 {
5557 const unsigned int MAX_FILENAME_LENGTH = 35;
5558 char *fileName = NULL;
5559 char *newFileName = NULL;
5560 size_t fileNameLength;
5561
5562 if (file_table)
5563 {
5564 unsigned indx = state_machine_regs.file;
5565
5566 if (linfo.li_version < 5)
5567 --indx;
5568 /* PR 20439 */
5569 if (indx >= n_files)
5570 {
5571 warn (_("file index %u >= number of files %u\n"),
5572 indx, n_files);
5573 fileName = _("<corrupt>");
5574 }
5575 else
5576 fileName = (char *) file_table[indx].name;
5577 }
5578 if (!fileName)
5579 fileName = _("<unknown>");
5580
5581 fileNameLength = strlen (fileName);
5582 newFileName = fileName;
5583 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5584 {
5585 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5586 /* Truncate file name */
5587 memcpy (newFileName,
5588 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5589 MAX_FILENAME_LENGTH);
5590 newFileName[MAX_FILENAME_LENGTH] = 0;
5591 }
5592
5593 /* A row with end_seq set to true has a meaningful address, but
5594 the other information in the same row is not significant.
5595 In such a row, print line as "-", and don't print
5596 view/is_stmt. */
5597 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5598 {
5599 if (linfo.li_max_ops_per_insn == 1)
5600 {
5601 if (xop == -DW_LNE_end_sequence)
5602 printf ("%-31s %11s %#18" PRIx64,
5603 newFileName, "-",
5604 state_machine_regs.address);
5605 else
5606 printf ("%-31s %11d %#18" PRIx64,
5607 newFileName, state_machine_regs.line,
5608 state_machine_regs.address);
5609 }
5610 else
5611 {
5612 if (xop == -DW_LNE_end_sequence)
5613 printf ("%-31s %11s %#18" PRIx64 "[%d]",
5614 newFileName, "-",
5615 state_machine_regs.address,
5616 state_machine_regs.op_index);
5617 else
5618 printf ("%-31s %11d %#18" PRIx64 "[%d]",
5619 newFileName, state_machine_regs.line,
5620 state_machine_regs.address,
5621 state_machine_regs.op_index);
5622 }
5623 }
5624 else
5625 {
5626 if (linfo.li_max_ops_per_insn == 1)
5627 {
5628 if (xop == -DW_LNE_end_sequence)
5629 printf ("%s %11s %#18" PRIx64,
5630 newFileName, "-",
5631 state_machine_regs.address);
5632 else
5633 printf ("%s %11d %#18" PRIx64,
5634 newFileName, state_machine_regs.line,
5635 state_machine_regs.address);
5636 }
5637 else
5638 {
5639 if (xop == -DW_LNE_end_sequence)
5640 printf ("%s %11s %#18" PRIx64 "[%d]",
5641 newFileName, "-",
5642 state_machine_regs.address,
5643 state_machine_regs.op_index);
5644 else
5645 printf ("%s %11d %#18" PRIx64 "[%d]",
5646 newFileName, state_machine_regs.line,
5647 state_machine_regs.address,
5648 state_machine_regs.op_index);
5649 }
5650 }
5651
5652 if (xop != -DW_LNE_end_sequence)
5653 {
5654 if (state_machine_regs.view)
5655 printf (" %6u", state_machine_regs.view);
5656 else
5657 printf (" ");
5658
5659 if (state_machine_regs.is_stmt)
5660 printf (" x");
5661 }
5662
5663 putchar ('\n');
5664 state_machine_regs.view++;
5665
5666 if (xop == -DW_LNE_end_sequence)
5667 {
5668 reset_state_machine (linfo.li_default_is_stmt);
5669 putchar ('\n');
5670 }
5671
5672 if (newFileName != fileName)
5673 free (newFileName);
5674 }
5675 }
5676
5677 if (file_table)
5678 {
5679 free (file_table);
5680 file_table = NULL;
5681 n_files = 0;
5682 }
5683
5684 if (directory_table)
5685 {
5686 free (directory_table);
5687 directory_table = NULL;
5688 n_directories = 0;
5689 }
5690
5691 putchar ('\n');
5692 }
5693
5694 return 1;
5695 }
5696
5697 static int
5698 display_debug_lines (struct dwarf_section *section, void *file)
5699 {
5700 unsigned char *data = section->start;
5701 unsigned char *end = data + section->size;
5702 int retValRaw = 1;
5703 int retValDecoded = 1;
5704
5705 if (do_debug_lines == 0)
5706 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5707
5708 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5709 retValRaw = display_debug_lines_raw (section, data, end, file);
5710
5711 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5712 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5713
5714 if (!retValRaw || !retValDecoded)
5715 return 0;
5716
5717 return 1;
5718 }
5719
5720 static debug_info *
5721 find_debug_info_for_offset (uint64_t offset)
5722 {
5723 unsigned int i;
5724
5725 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5726 return NULL;
5727
5728 for (i = 0; i < num_debug_info_entries; i++)
5729 if (debug_information[i].cu_offset == offset)
5730 return debug_information + i;
5731
5732 return NULL;
5733 }
5734
5735 static const char *
5736 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5737 {
5738 /* See gdb/gdb-index.h. */
5739 static const char * const kinds[] =
5740 {
5741 N_ ("no info"),
5742 N_ ("type"),
5743 N_ ("variable"),
5744 N_ ("function"),
5745 N_ ("other"),
5746 N_ ("unused5"),
5747 N_ ("unused6"),
5748 N_ ("unused7")
5749 };
5750
5751 return _ (kinds[kind]);
5752 }
5753
5754 static int
5755 display_debug_pubnames_worker (struct dwarf_section *section,
5756 void *file ATTRIBUTE_UNUSED,
5757 int is_gnu)
5758 {
5759 DWARF2_Internal_PubNames names;
5760 unsigned char *start = section->start;
5761 unsigned char *end = start + section->size;
5762
5763 /* It does not matter if this load fails,
5764 we test for that later on. */
5765 load_debug_info (file);
5766
5767 introduce (section, false);
5768
5769 while (start < end)
5770 {
5771 unsigned char *data;
5772 unsigned long sec_off = start - section->start;
5773 unsigned int offset_size;
5774
5775 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5776 if (names.pn_length == 0xffffffff)
5777 {
5778 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5779 offset_size = 8;
5780 }
5781 else
5782 offset_size = 4;
5783
5784 if (names.pn_length > (size_t) (end - start))
5785 {
5786 warn (_("Debug info is corrupted, "
5787 "%s header at %#lx has length %#" PRIx64 "\n"),
5788 section->name, sec_off, names.pn_length);
5789 break;
5790 }
5791
5792 data = start;
5793 start += names.pn_length;
5794
5795 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
5796 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
5797
5798 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5799 && num_debug_info_entries > 0
5800 && find_debug_info_for_offset (names.pn_offset) == NULL)
5801 warn (_(".debug_info offset of %#" PRIx64
5802 " in %s section does not point to a CU header.\n"),
5803 names.pn_offset, section->name);
5804
5805 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
5806
5807 printf (_(" Length: %" PRId64 "\n"),
5808 names.pn_length);
5809 printf (_(" Version: %d\n"),
5810 names.pn_version);
5811 printf (_(" Offset into .debug_info section: %#" PRIx64 "\n"),
5812 names.pn_offset);
5813 printf (_(" Size of area in .debug_info section: %" PRId64 "\n"),
5814 names.pn_size);
5815
5816 if (names.pn_version != 2 && names.pn_version != 3)
5817 {
5818 static int warned = 0;
5819
5820 if (! warned)
5821 {
5822 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5823 warned = 1;
5824 }
5825
5826 continue;
5827 }
5828
5829 if (is_gnu)
5830 printf (_("\n Offset Kind Name\n"));
5831 else
5832 printf (_("\n Offset\tName\n"));
5833
5834 while (1)
5835 {
5836 size_t maxprint;
5837 uint64_t offset;
5838
5839 SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
5840
5841 if (offset == 0)
5842 break;
5843
5844 if (data >= start)
5845 break;
5846 maxprint = (start - data) - 1;
5847
5848 if (is_gnu)
5849 {
5850 unsigned int kind_data;
5851 gdb_index_symbol_kind kind;
5852 const char *kind_name;
5853 int is_static;
5854
5855 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
5856 maxprint --;
5857 /* GCC computes the kind as the upper byte in the CU index
5858 word, and then right shifts it by the CU index size.
5859 Left shift KIND to where the gdb-index.h accessor macros
5860 can use it. */
5861 kind_data <<= GDB_INDEX_CU_BITSIZE;
5862 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5863 kind_name = get_gdb_index_symbol_kind_name (kind);
5864 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5865 printf (" %-6" PRIx64 " %s,%-10s %.*s\n",
5866 offset, is_static ? _("s") : _("g"),
5867 kind_name, (int) maxprint, data);
5868 }
5869 else
5870 printf (" %-6" PRIx64 "\t%.*s\n",
5871 offset, (int) maxprint, data);
5872
5873 data += strnlen ((char *) data, maxprint);
5874 if (data < start)
5875 data++;
5876 if (data >= start)
5877 break;
5878 }
5879 }
5880
5881 printf ("\n");
5882 return 1;
5883 }
5884
5885 static int
5886 display_debug_pubnames (struct dwarf_section *section, void *file)
5887 {
5888 return display_debug_pubnames_worker (section, file, 0);
5889 }
5890
5891 static int
5892 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5893 {
5894 return display_debug_pubnames_worker (section, file, 1);
5895 }
5896
5897 static int
5898 display_debug_macinfo (struct dwarf_section *section,
5899 void *file ATTRIBUTE_UNUSED)
5900 {
5901 unsigned char *start = section->start;
5902 unsigned char *end = start + section->size;
5903 unsigned char *curr = start;
5904 enum dwarf_macinfo_record_type op;
5905
5906 introduce (section, false);
5907
5908 while (curr < end)
5909 {
5910 unsigned int lineno;
5911 const unsigned char *string;
5912
5913 op = (enum dwarf_macinfo_record_type) *curr;
5914 curr++;
5915
5916 switch (op)
5917 {
5918 case DW_MACINFO_start_file:
5919 {
5920 unsigned int filenum;
5921
5922 READ_ULEB (lineno, curr, end);
5923 READ_ULEB (filenum, curr, end);
5924 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5925 lineno, filenum);
5926 }
5927 break;
5928
5929 case DW_MACINFO_end_file:
5930 printf (_(" DW_MACINFO_end_file\n"));
5931 break;
5932
5933 case DW_MACINFO_define:
5934 READ_ULEB (lineno, curr, end);
5935 string = curr;
5936 curr += strnlen ((char *) string, end - string);
5937 printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
5938 lineno, (int) (curr - string), string);
5939 if (curr < end)
5940 curr++;
5941 break;
5942
5943 case DW_MACINFO_undef:
5944 READ_ULEB (lineno, curr, end);
5945 string = curr;
5946 curr += strnlen ((char *) string, end - string);
5947 printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
5948 lineno, (int) (curr - string), string);
5949 if (curr < end)
5950 curr++;
5951 break;
5952
5953 case DW_MACINFO_vendor_ext:
5954 {
5955 unsigned int constant;
5956
5957 READ_ULEB (constant, curr, end);
5958 string = curr;
5959 curr += strnlen ((char *) string, end - string);
5960 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
5961 constant, (int) (curr - string), string);
5962 if (curr < end)
5963 curr++;
5964 }
5965 break;
5966 }
5967 }
5968
5969 return 1;
5970 }
5971
5972 /* Given LINE_OFFSET into the .debug_line section, attempt to return
5973 filename and dirname corresponding to file name table entry with index
5974 FILEIDX. Return NULL on failure. */
5975
5976 static unsigned char *
5977 get_line_filename_and_dirname (uint64_t line_offset,
5978 uint64_t fileidx,
5979 unsigned char **dir_name)
5980 {
5981 struct dwarf_section *section = &debug_displays [line].section;
5982 unsigned char *hdrptr, *dirtable, *file_name;
5983 unsigned int offset_size;
5984 unsigned int version, opcode_base;
5985 uint64_t length, diridx;
5986 const unsigned char * end;
5987
5988 *dir_name = NULL;
5989 if (section->start == NULL
5990 || line_offset >= section->size
5991 || fileidx == 0)
5992 return NULL;
5993
5994 hdrptr = section->start + line_offset;
5995 end = section->start + section->size;
5996
5997 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
5998 if (length == 0xffffffff)
5999 {
6000 /* This section is 64-bit DWARF 3. */
6001 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
6002 offset_size = 8;
6003 }
6004 else
6005 offset_size = 4;
6006
6007 if (length > (size_t) (end - hdrptr)
6008 || length < 2 + offset_size + 1 + 3 + 1)
6009 return NULL;
6010 end = hdrptr + length;
6011
6012 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
6013 if (version != 2 && version != 3 && version != 4)
6014 return NULL;
6015 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
6016 if (version >= 4)
6017 hdrptr++; /* Skip max_ops_per_insn. */
6018 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
6019
6020 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
6021 if (opcode_base == 0
6022 || opcode_base - 1 >= (size_t) (end - hdrptr))
6023 return NULL;
6024
6025 hdrptr += opcode_base - 1;
6026
6027 dirtable = hdrptr;
6028 /* Skip over dirname table. */
6029 while (*hdrptr != '\0')
6030 {
6031 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6032 if (hdrptr < end)
6033 hdrptr++;
6034 if (hdrptr >= end)
6035 return NULL;
6036 }
6037 hdrptr++; /* Skip the NUL at the end of the table. */
6038
6039 /* Now skip over preceding filename table entries. */
6040 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
6041 {
6042 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6043 if (hdrptr < end)
6044 hdrptr++;
6045 SKIP_ULEB (hdrptr, end);
6046 SKIP_ULEB (hdrptr, end);
6047 SKIP_ULEB (hdrptr, end);
6048 }
6049 if (hdrptr >= end || *hdrptr == '\0')
6050 return NULL;
6051
6052 file_name = hdrptr;
6053 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6054 if (hdrptr < end)
6055 hdrptr++;
6056 if (hdrptr >= end)
6057 return NULL;
6058 READ_ULEB (diridx, hdrptr, end);
6059 if (diridx == 0)
6060 return file_name;
6061 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
6062 {
6063 dirtable += strnlen ((char *) dirtable, end - dirtable);
6064 if (dirtable < end)
6065 dirtable++;
6066 }
6067 if (dirtable >= end || *dirtable == '\0')
6068 return NULL;
6069 *dir_name = dirtable;
6070 return file_name;
6071 }
6072
6073 static int
6074 display_debug_macro (struct dwarf_section *section,
6075 void *file)
6076 {
6077 unsigned char *start = section->start;
6078 unsigned char *end = start + section->size;
6079 unsigned char *curr = start;
6080 unsigned char *extended_op_buf[256];
6081 bool is_dwo = false;
6082 const char *suffix = strrchr (section->name, '.');
6083
6084 if (suffix && strcmp (suffix, ".dwo") == 0)
6085 is_dwo = true;
6086
6087 load_debug_section_with_follow (str, file);
6088 load_debug_section_with_follow (line, file);
6089 load_debug_section_with_follow (str_index, file);
6090
6091 introduce (section, false);
6092
6093 while (curr < end)
6094 {
6095 unsigned int lineno, version, flags;
6096 unsigned int offset_size;
6097 const unsigned char *string;
6098 uint64_t line_offset = 0, sec_offset = curr - start, offset;
6099 unsigned char **extended_ops = NULL;
6100
6101 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6102 if (version != 4 && version != 5)
6103 {
6104 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6105 section->name, version);
6106 return 0;
6107 }
6108
6109 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
6110 offset_size = (flags & 1) ? 8 : 4;
6111 printf (_(" Offset: %#" PRIx64 "\n"), sec_offset);
6112 printf (_(" Version: %d\n"), version);
6113 printf (_(" Offset size: %d\n"), offset_size);
6114 if (flags & 2)
6115 {
6116 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
6117 printf (_(" Offset into .debug_line: %#" PRIx64 "\n"),
6118 line_offset);
6119 }
6120 if (flags & 4)
6121 {
6122 unsigned int i, count, op;
6123 uint64_t nargs, n;
6124
6125 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
6126
6127 memset (extended_op_buf, 0, sizeof (extended_op_buf));
6128 extended_ops = extended_op_buf;
6129 if (count)
6130 {
6131 printf (_(" Extension opcode arguments:\n"));
6132 for (i = 0; i < count; i++)
6133 {
6134 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6135 extended_ops[op] = curr;
6136 READ_ULEB (nargs, curr, end);
6137 if (nargs == 0)
6138 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
6139 else
6140 {
6141 printf (_(" DW_MACRO_%02x arguments: "), op);
6142 for (n = 0; n < nargs; n++)
6143 {
6144 unsigned int form;
6145
6146 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6147 printf ("%s%s", get_FORM_name (form),
6148 n == nargs - 1 ? "\n" : ", ");
6149 switch (form)
6150 {
6151 case DW_FORM_data1:
6152 case DW_FORM_data2:
6153 case DW_FORM_data4:
6154 case DW_FORM_data8:
6155 case DW_FORM_sdata:
6156 case DW_FORM_udata:
6157 case DW_FORM_block:
6158 case DW_FORM_block1:
6159 case DW_FORM_block2:
6160 case DW_FORM_block4:
6161 case DW_FORM_flag:
6162 case DW_FORM_string:
6163 case DW_FORM_strp:
6164 case DW_FORM_sec_offset:
6165 break;
6166 default:
6167 error (_("Invalid extension opcode form %s\n"),
6168 get_FORM_name (form));
6169 return 0;
6170 }
6171 }
6172 }
6173 }
6174 }
6175 }
6176 printf ("\n");
6177
6178 while (1)
6179 {
6180 unsigned int op;
6181
6182 if (curr >= end)
6183 {
6184 error (_(".debug_macro section not zero terminated\n"));
6185 return 0;
6186 }
6187
6188 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6189 if (op == 0)
6190 break;
6191
6192 switch (op)
6193 {
6194 case DW_MACRO_define:
6195 READ_ULEB (lineno, curr, end);
6196 string = curr;
6197 curr += strnlen ((char *) string, end - string);
6198 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6199 lineno, (int) (curr - string), string);
6200 if (curr < end)
6201 curr++;
6202 break;
6203
6204 case DW_MACRO_undef:
6205 READ_ULEB (lineno, curr, end);
6206 string = curr;
6207 curr += strnlen ((char *) string, end - string);
6208 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6209 lineno, (int) (curr - string), string);
6210 if (curr < end)
6211 curr++;
6212 break;
6213
6214 case DW_MACRO_start_file:
6215 {
6216 unsigned int filenum;
6217 unsigned char *file_name = NULL, *dir_name = NULL;
6218
6219 READ_ULEB (lineno, curr, end);
6220 READ_ULEB (filenum, curr, end);
6221
6222 if ((flags & 2) == 0)
6223 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6224 else
6225 file_name
6226 = get_line_filename_and_dirname (line_offset, filenum,
6227 &dir_name);
6228 if (file_name == NULL)
6229 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6230 lineno, filenum);
6231 else
6232 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6233 lineno, filenum,
6234 dir_name != NULL ? (const char *) dir_name : "",
6235 dir_name != NULL ? "/" : "", file_name);
6236 }
6237 break;
6238
6239 case DW_MACRO_end_file:
6240 printf (_(" DW_MACRO_end_file\n"));
6241 break;
6242
6243 case DW_MACRO_define_strp:
6244 READ_ULEB (lineno, curr, end);
6245 if (version == 4 && is_dwo)
6246 READ_ULEB (offset, curr, end);
6247 else
6248 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6249 string = fetch_indirect_string (offset);
6250 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
6251 lineno, string);
6252 break;
6253
6254 case DW_MACRO_undef_strp:
6255 READ_ULEB (lineno, curr, end);
6256 if (version == 4 && is_dwo)
6257 READ_ULEB (offset, curr, end);
6258 else
6259 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6260 string = fetch_indirect_string (offset);
6261 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6262 lineno, string);
6263 break;
6264
6265 case DW_MACRO_import:
6266 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6267 printf (_(" DW_MACRO_import - offset : %#" PRIx64 "\n"),
6268 offset);
6269 break;
6270
6271 case DW_MACRO_define_sup:
6272 READ_ULEB (lineno, curr, end);
6273 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6274 printf (_(" DW_MACRO_define_sup - lineno : %d"
6275 " macro offset : %#" PRIx64 "\n"),
6276 lineno, offset);
6277 break;
6278
6279 case DW_MACRO_undef_sup:
6280 READ_ULEB (lineno, curr, end);
6281 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6282 printf (_(" DW_MACRO_undef_sup - lineno : %d"
6283 " macro offset : %#" PRIx64 "\n"),
6284 lineno, offset);
6285 break;
6286
6287 case DW_MACRO_import_sup:
6288 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6289 printf (_(" DW_MACRO_import_sup - offset : %#" PRIx64 "\n"),
6290 offset);
6291 break;
6292
6293 case DW_MACRO_define_strx:
6294 case DW_MACRO_undef_strx:
6295 READ_ULEB (lineno, curr, end);
6296 READ_ULEB (offset, curr, end);
6297 string = (const unsigned char *)
6298 fetch_indexed_string (offset, NULL, offset_size, false, 0);
6299 if (op == DW_MACRO_define_strx)
6300 printf (" DW_MACRO_define_strx ");
6301 else
6302 printf (" DW_MACRO_undef_strx ");
6303 if (do_wide)
6304 printf (_("(with offset %#" PRIx64 ") "), offset);
6305 printf (_("lineno : %d macro : %s\n"),
6306 lineno, string);
6307 break;
6308
6309 default:
6310 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6311 {
6312 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6313 break;
6314 }
6315
6316 if (extended_ops == NULL || extended_ops[op] == NULL)
6317 {
6318 error (_(" Unknown macro opcode %02x seen\n"), op);
6319 return 0;
6320 }
6321 else
6322 {
6323 /* Skip over unhandled opcodes. */
6324 uint64_t nargs, n;
6325 unsigned char *desc = extended_ops[op];
6326 READ_ULEB (nargs, desc, end);
6327 if (nargs == 0)
6328 {
6329 printf (_(" DW_MACRO_%02x\n"), op);
6330 break;
6331 }
6332 printf (_(" DW_MACRO_%02x -"), op);
6333 for (n = 0; n < nargs; n++)
6334 {
6335 int val;
6336
6337 /* DW_FORM_implicit_const is not expected here. */
6338 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6339 curr
6340 = read_and_display_attr_value (0, val, 0,
6341 start, curr, end, 0, 0,
6342 offset_size, version,
6343 NULL, 0, section,
6344 NULL, ' ', -1);
6345 if (n != nargs - 1)
6346 printf (",");
6347 }
6348 printf ("\n");
6349 }
6350 break;
6351 }
6352 }
6353
6354 printf ("\n");
6355 }
6356
6357 return 1;
6358 }
6359
6360 static int
6361 display_debug_abbrev (struct dwarf_section *section,
6362 void *file ATTRIBUTE_UNUSED)
6363 {
6364 abbrev_entry *entry;
6365 unsigned char *start = section->start;
6366
6367 introduce (section, false);
6368
6369 do
6370 {
6371 uint64_t offset = start - section->start;
6372 abbrev_list *list = find_and_process_abbrev_set (section, 0,
6373 section->size, offset,
6374 NULL);
6375 if (list == NULL)
6376 break;
6377
6378 if (list->first_abbrev)
6379 printf (_(" Number TAG (%#" PRIx64 ")\n"), offset);
6380
6381 for (entry = list->first_abbrev; entry; entry = entry->next)
6382 {
6383 abbrev_attr *attr;
6384
6385 printf (" %ld %s [%s]\n",
6386 entry->number,
6387 get_TAG_name (entry->tag),
6388 entry->children ? _("has children") : _("no children"));
6389
6390 for (attr = entry->first_attr; attr; attr = attr->next)
6391 {
6392 printf (" %-18s %s",
6393 get_AT_name (attr->attribute),
6394 get_FORM_name (attr->form));
6395 if (attr->form == DW_FORM_implicit_const)
6396 printf (": %" PRId64, attr->implicit_const);
6397 putchar ('\n');
6398 }
6399 }
6400 start = list->start_of_next_abbrevs;
6401 free_abbrev_list (list);
6402 }
6403 while (start);
6404
6405 printf ("\n");
6406
6407 return 1;
6408 }
6409
6410 /* Return true when ADDR is the maximum address, when addresses are
6411 POINTER_SIZE bytes long. */
6412
6413 static bool
6414 is_max_address (uint64_t addr, unsigned int pointer_size)
6415 {
6416 uint64_t mask = ~(~(uint64_t) 0 << 1 << (pointer_size * 8 - 1));
6417 return ((addr & mask) == mask);
6418 }
6419
6420 /* Display a view pair list starting at *VSTART_PTR and ending at
6421 VLISTEND within SECTION. */
6422
6423 static void
6424 display_view_pair_list (struct dwarf_section *section,
6425 unsigned char **vstart_ptr,
6426 unsigned int debug_info_entry,
6427 unsigned char *vlistend)
6428 {
6429 unsigned char *vstart = *vstart_ptr;
6430 unsigned char *section_end = section->start + section->size;
6431 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6432
6433 if (vlistend < section_end)
6434 section_end = vlistend;
6435
6436 putchar ('\n');
6437
6438 while (vstart < section_end)
6439 {
6440 uint64_t off = vstart - section->start;
6441 uint64_t vbegin, vend;
6442
6443 READ_ULEB (vbegin, vstart, section_end);
6444 if (vstart == section_end)
6445 break;
6446
6447 READ_ULEB (vend, vstart, section_end);
6448 printf (" %8.8" PRIx64 " ", off);
6449
6450 print_view (vbegin, pointer_size);
6451 print_view (vend, pointer_size);
6452 printf (_("location view pair\n"));
6453 }
6454
6455 putchar ('\n');
6456 *vstart_ptr = vstart;
6457 }
6458
6459 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6460
6461 static void
6462 display_loc_list (struct dwarf_section *section,
6463 unsigned char **start_ptr,
6464 unsigned int debug_info_entry,
6465 uint64_t offset,
6466 uint64_t base_address,
6467 unsigned char **vstart_ptr,
6468 int has_frame_base)
6469 {
6470 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6471 unsigned char *section_end = section->start + section->size;
6472 uint64_t cu_offset;
6473 unsigned int pointer_size;
6474 unsigned int offset_size;
6475 int dwarf_version;
6476 uint64_t begin;
6477 uint64_t end;
6478 unsigned short length;
6479 int need_frame_base;
6480
6481 if (debug_info_entry >= num_debug_info_entries)
6482 {
6483 warn (_("No debug information available for loc lists of entry: %u\n"),
6484 debug_info_entry);
6485 return;
6486 }
6487
6488 cu_offset = debug_information [debug_info_entry].cu_offset;
6489 pointer_size = debug_information [debug_info_entry].pointer_size;
6490 offset_size = debug_information [debug_info_entry].offset_size;
6491 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6492
6493 if (pointer_size < 2 || pointer_size > 8)
6494 {
6495 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6496 pointer_size, debug_info_entry);
6497 return;
6498 }
6499
6500 while (1)
6501 {
6502 uint64_t off = offset + (start - *start_ptr);
6503 uint64_t vbegin = -1, vend = -1;
6504
6505 if (2 * pointer_size > (size_t) (section_end - start))
6506 {
6507 warn (_("Location list starting at offset %#" PRIx64
6508 " is not terminated.\n"), offset);
6509 break;
6510 }
6511
6512 printf (" ");
6513 print_hex (off, 4);
6514
6515 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6516 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6517
6518 if (begin == 0 && end == 0)
6519 {
6520 /* PR 18374: In a object file we can have a location list that
6521 starts with a begin and end of 0 because there are relocations
6522 that need to be applied to the addresses. Actually applying
6523 the relocations now does not help as they will probably resolve
6524 to 0, since the object file has not been fully linked. Real
6525 end of list markers will not have any relocations against them. */
6526 if (! reloc_at (section, off)
6527 && ! reloc_at (section, off + pointer_size))
6528 {
6529 printf (_("<End of list>\n"));
6530 break;
6531 }
6532 }
6533
6534 /* Check base address specifiers. */
6535 if (is_max_address (begin, pointer_size)
6536 && !is_max_address (end, pointer_size))
6537 {
6538 base_address = end;
6539 print_hex (begin, pointer_size);
6540 print_hex (end, pointer_size);
6541 printf (_("(base address)\n"));
6542 continue;
6543 }
6544
6545 if (vstart)
6546 {
6547 off = offset + (vstart - *start_ptr);
6548
6549 READ_ULEB (vbegin, vstart, section_end);
6550 print_view (vbegin, pointer_size);
6551
6552 READ_ULEB (vend, vstart, section_end);
6553 print_view (vend, pointer_size);
6554
6555 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6556 }
6557
6558 if (2 > (size_t) (section_end - start))
6559 {
6560 warn (_("Location list starting at offset %#" PRIx64
6561 " is not terminated.\n"), offset);
6562 break;
6563 }
6564
6565 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6566
6567 if (length > (size_t) (section_end - start))
6568 {
6569 warn (_("Location list starting at offset %#" PRIx64
6570 " is not terminated.\n"), offset);
6571 break;
6572 }
6573
6574 print_hex (begin + base_address, pointer_size);
6575 print_hex (end + base_address, pointer_size);
6576
6577 putchar ('(');
6578 need_frame_base = decode_location_expression (start,
6579 pointer_size,
6580 offset_size,
6581 dwarf_version,
6582 length,
6583 cu_offset, section);
6584 putchar (')');
6585
6586 if (need_frame_base && !has_frame_base)
6587 printf (_(" [without DW_AT_frame_base]"));
6588
6589 if (begin == end && vbegin == vend)
6590 fputs (_(" (start == end)"), stdout);
6591 else if (begin > end || (begin == end && vbegin > vend))
6592 fputs (_(" (start > end)"), stdout);
6593
6594 putchar ('\n');
6595
6596 start += length;
6597 }
6598
6599 *start_ptr = start;
6600 *vstart_ptr = vstart;
6601 }
6602
6603 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6604
6605 static void
6606 display_loclists_list (struct dwarf_section * section,
6607 unsigned char ** start_ptr,
6608 unsigned int debug_info_entry,
6609 uint64_t offset,
6610 uint64_t base_address,
6611 unsigned char ** vstart_ptr,
6612 int has_frame_base)
6613 {
6614 unsigned char *start = *start_ptr;
6615 unsigned char *vstart = *vstart_ptr;
6616 unsigned char *section_end = section->start + section->size;
6617 uint64_t cu_offset;
6618 unsigned int pointer_size;
6619 unsigned int offset_size;
6620 unsigned int dwarf_version;
6621
6622 /* Initialize it due to a false compiler warning. */
6623 uint64_t begin = -1, vbegin = -1;
6624 uint64_t end = -1, vend = -1;
6625 uint64_t length;
6626 int need_frame_base;
6627
6628 if (debug_info_entry >= num_debug_info_entries)
6629 {
6630 warn (_("No debug information available for "
6631 "loclists lists of entry: %u\n"),
6632 debug_info_entry);
6633 return;
6634 }
6635
6636 cu_offset = debug_information [debug_info_entry].cu_offset;
6637 pointer_size = debug_information [debug_info_entry].pointer_size;
6638 offset_size = debug_information [debug_info_entry].offset_size;
6639 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6640
6641 if (pointer_size < 2 || pointer_size > 8)
6642 {
6643 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6644 pointer_size, debug_info_entry);
6645 return;
6646 }
6647
6648 while (1)
6649 {
6650 uint64_t off = offset + (start - *start_ptr);
6651 enum dwarf_location_list_entry_type llet;
6652
6653 if (start + 1 > section_end)
6654 {
6655 warn (_("Location list starting at offset %#" PRIx64
6656 " is not terminated.\n"), offset);
6657 break;
6658 }
6659
6660 printf (" ");
6661 print_hex (off, 4);
6662
6663 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6664
6665 if (vstart && (llet == DW_LLE_offset_pair
6666 || llet == DW_LLE_start_end
6667 || llet == DW_LLE_start_length))
6668 {
6669 off = offset + (vstart - *start_ptr);
6670
6671 READ_ULEB (vbegin, vstart, section_end);
6672 print_view (vbegin, pointer_size);
6673
6674 READ_ULEB (vend, vstart, section_end);
6675 print_view (vend, pointer_size);
6676
6677 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6678 }
6679
6680 switch (llet)
6681 {
6682 case DW_LLE_end_of_list:
6683 printf (_("<End of list>\n"));
6684 break;
6685
6686 case DW_LLE_base_addressx:
6687 READ_ULEB (base_address, start, section_end);
6688 print_hex (base_address, pointer_size);
6689 printf (_("(index into .debug_addr) "));
6690 base_address = fetch_indexed_addr (base_address, pointer_size);
6691 print_hex (base_address, pointer_size);
6692 printf (_("(base address)\n"));
6693 break;
6694
6695 case DW_LLE_startx_endx:
6696 READ_ULEB (begin, start, section_end);
6697 begin = fetch_indexed_addr (begin, pointer_size);
6698 READ_ULEB (end, start, section_end);
6699 end = fetch_indexed_addr (end, pointer_size);
6700 break;
6701
6702 case DW_LLE_startx_length:
6703 READ_ULEB (begin, start, section_end);
6704 begin = fetch_indexed_addr (begin, pointer_size);
6705 READ_ULEB (end, start, section_end);
6706 end += begin;
6707 break;
6708
6709 case DW_LLE_default_location:
6710 begin = end = 0;
6711 break;
6712
6713 case DW_LLE_offset_pair:
6714 READ_ULEB (begin, start, section_end);
6715 begin += base_address;
6716 READ_ULEB (end, start, section_end);
6717 end += base_address;
6718 break;
6719
6720 case DW_LLE_base_address:
6721 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6722 section_end);
6723 print_hex (base_address, pointer_size);
6724 printf (_("(base address)\n"));
6725 break;
6726
6727 case DW_LLE_start_end:
6728 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6729 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6730 break;
6731
6732 case DW_LLE_start_length:
6733 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6734 READ_ULEB (end, start, section_end);
6735 end += begin;
6736 break;
6737
6738 #ifdef DW_LLE_view_pair
6739 case DW_LLE_view_pair:
6740 if (vstart)
6741 printf (_("View pair entry in loclist with locviews attribute\n"));
6742 READ_ULEB (vbegin, start, section_end);
6743 print_view (vbegin, pointer_size);
6744
6745 READ_ULEB (vend, start, section_end);
6746 print_view (vend, pointer_size);
6747
6748 printf (_("views for:\n"));
6749 continue;
6750 #endif
6751
6752 default:
6753 error (_("Invalid location list entry type %d\n"), llet);
6754 return;
6755 }
6756
6757 if (llet == DW_LLE_end_of_list)
6758 break;
6759
6760 if (llet == DW_LLE_base_address
6761 || llet == DW_LLE_base_addressx)
6762 continue;
6763
6764 if (start == section_end)
6765 {
6766 warn (_("Location list starting at offset %#" PRIx64
6767 " is not terminated.\n"), offset);
6768 break;
6769 }
6770 READ_ULEB (length, start, section_end);
6771
6772 if (length > (size_t) (section_end - start))
6773 {
6774 warn (_("Location list starting at offset %#" PRIx64
6775 " is not terminated.\n"), offset);
6776 break;
6777 }
6778
6779 print_hex (begin, pointer_size);
6780 print_hex (end, pointer_size);
6781
6782 putchar ('(');
6783 need_frame_base = decode_location_expression (start,
6784 pointer_size,
6785 offset_size,
6786 dwarf_version,
6787 length,
6788 cu_offset, section);
6789 putchar (')');
6790
6791 if (need_frame_base && !has_frame_base)
6792 printf (_(" [without DW_AT_frame_base]"));
6793
6794 if (begin == end && vbegin == vend)
6795 fputs (_(" (start == end)"), stdout);
6796 else if (begin > end || (begin == end && vbegin > vend))
6797 fputs (_(" (start > end)"), stdout);
6798
6799 putchar ('\n');
6800
6801 start += length;
6802 vbegin = vend = -1;
6803 }
6804
6805 if (vbegin != (uint64_t) -1 || vend != (uint64_t) -1)
6806 printf (_("Trailing view pair not used in a range"));
6807
6808 *start_ptr = start;
6809 *vstart_ptr = vstart;
6810 }
6811
6812 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
6813 right-adjusted in a field of length LEN, and followed by a space. */
6814
6815 static void
6816 print_addr_index (unsigned int idx, unsigned int len)
6817 {
6818 static char buf[15];
6819 snprintf (buf, sizeof (buf), "[%d]", idx);
6820 printf ("%*s ", len, buf);
6821 }
6822
6823 /* Display a location list from a .dwo section. It uses address indexes rather
6824 than embedded addresses. This code closely follows display_loc_list, but the
6825 two are sufficiently different that combining things is very ugly. */
6826
6827 static void
6828 display_loc_list_dwo (struct dwarf_section *section,
6829 unsigned char **start_ptr,
6830 unsigned int debug_info_entry,
6831 uint64_t offset,
6832 unsigned char **vstart_ptr,
6833 int has_frame_base)
6834 {
6835 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6836 unsigned char *section_end = section->start + section->size;
6837 uint64_t cu_offset;
6838 unsigned int pointer_size;
6839 unsigned int offset_size;
6840 int dwarf_version;
6841 int entry_type;
6842 unsigned short length;
6843 int need_frame_base;
6844 unsigned int idx;
6845
6846 if (debug_info_entry >= num_debug_info_entries)
6847 {
6848 warn (_("No debug information for loc lists of entry: %u\n"),
6849 debug_info_entry);
6850 return;
6851 }
6852
6853 cu_offset = debug_information [debug_info_entry].cu_offset;
6854 pointer_size = debug_information [debug_info_entry].pointer_size;
6855 offset_size = debug_information [debug_info_entry].offset_size;
6856 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6857
6858 if (pointer_size < 2 || pointer_size > 8)
6859 {
6860 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6861 pointer_size, debug_info_entry);
6862 return;
6863 }
6864
6865 while (1)
6866 {
6867 printf (" ");
6868 print_hex (offset + (start - *start_ptr), 4);
6869
6870 if (start >= section_end)
6871 {
6872 warn (_("Location list starting at offset %#" PRIx64
6873 " is not terminated.\n"), offset);
6874 break;
6875 }
6876
6877 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
6878
6879 if (vstart)
6880 switch (entry_type)
6881 {
6882 default:
6883 break;
6884
6885 case 2:
6886 case 3:
6887 case 4:
6888 {
6889 uint64_t view;
6890 uint64_t off = offset + (vstart - *start_ptr);
6891
6892 READ_ULEB (view, vstart, section_end);
6893 print_view (view, 8);
6894
6895 READ_ULEB (view, vstart, section_end);
6896 print_view (view, 8);
6897
6898 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6899
6900 }
6901 break;
6902 }
6903
6904 switch (entry_type)
6905 {
6906 case 0: /* A terminating entry. */
6907 *start_ptr = start;
6908 *vstart_ptr = vstart;
6909 printf (_("<End of list>\n"));
6910 return;
6911 case 1: /* A base-address entry. */
6912 READ_ULEB (idx, start, section_end);
6913 print_addr_index (idx, 8);
6914 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
6915 printf (_("(base address selection entry)\n"));
6916 continue;
6917 case 2: /* A start/end entry. */
6918 READ_ULEB (idx, start, section_end);
6919 print_addr_index (idx, 8);
6920 READ_ULEB (idx, start, section_end);
6921 print_addr_index (idx, 8);
6922 break;
6923 case 3: /* A start/length entry. */
6924 READ_ULEB (idx, start, section_end);
6925 print_addr_index (idx, 8);
6926 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6927 printf ("%08x ", idx);
6928 break;
6929 case 4: /* An offset pair entry. */
6930 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6931 printf ("%08x ", idx);
6932 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6933 printf ("%08x ", idx);
6934 break;
6935 default:
6936 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6937 *start_ptr = start;
6938 *vstart_ptr = vstart;
6939 return;
6940 }
6941
6942 if (2 > (size_t) (section_end - start))
6943 {
6944 warn (_("Location list starting at offset %#" PRIx64
6945 " is not terminated.\n"), offset);
6946 break;
6947 }
6948
6949 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6950 if (length > (size_t) (section_end - start))
6951 {
6952 warn (_("Location list starting at offset %#" PRIx64
6953 " is not terminated.\n"), offset);
6954 break;
6955 }
6956
6957 putchar ('(');
6958 need_frame_base = decode_location_expression (start,
6959 pointer_size,
6960 offset_size,
6961 dwarf_version,
6962 length,
6963 cu_offset, section);
6964 putchar (')');
6965
6966 if (need_frame_base && !has_frame_base)
6967 printf (_(" [without DW_AT_frame_base]"));
6968
6969 putchar ('\n');
6970
6971 start += length;
6972 }
6973
6974 *start_ptr = start;
6975 *vstart_ptr = vstart;
6976 }
6977
6978 /* Sort array of indexes in ascending order of loc_offsets[idx] and
6979 loc_views. */
6980
6981 static uint64_t *loc_offsets, *loc_views;
6982
6983 static int
6984 loc_offsets_compar (const void *ap, const void *bp)
6985 {
6986 uint64_t a = loc_offsets[*(const unsigned int *) ap];
6987 uint64_t b = loc_offsets[*(const unsigned int *) bp];
6988
6989 int ret = (a > b) - (b > a);
6990 if (ret)
6991 return ret;
6992
6993 a = loc_views[*(const unsigned int *) ap];
6994 b = loc_views[*(const unsigned int *) bp];
6995
6996 ret = (a > b) - (b > a);
6997
6998 return ret;
6999 }
7000
7001 static int
7002 display_offset_entry_loclists (struct dwarf_section *section)
7003 {
7004 unsigned char * start = section->start;
7005 unsigned char * const end = start + section->size;
7006
7007 introduce (section, false);
7008
7009 do
7010 {
7011 uint64_t length;
7012 unsigned short version;
7013 unsigned char address_size;
7014 unsigned char segment_selector_size;
7015 uint32_t offset_entry_count;
7016 uint32_t i;
7017 bool is_64bit;
7018
7019 printf (_("Table at Offset %#tx\n"), start - section->start);
7020
7021 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
7022 if (length == 0xffffffff)
7023 {
7024 is_64bit = true;
7025 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
7026 }
7027 else
7028 is_64bit = false;
7029
7030 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
7031 SAFE_BYTE_GET_AND_INC (address_size, start, 1, end);
7032 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, end);
7033 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, end);
7034
7035 printf (_(" Length: %#" PRIx64 "\n"), length);
7036 printf (_(" DWARF version: %u\n"), version);
7037 printf (_(" Address size: %u\n"), address_size);
7038 printf (_(" Segment size: %u\n"), segment_selector_size);
7039 printf (_(" Offset entries: %u\n"), offset_entry_count);
7040
7041 if (version < 5)
7042 {
7043 warn (_("The %s section contains a corrupt or "
7044 "unsupported version number: %d.\n"),
7045 section->name, version);
7046 return 0;
7047 }
7048
7049 if (segment_selector_size != 0)
7050 {
7051 warn (_("The %s section contains an "
7052 "unsupported segment selector size: %d.\n"),
7053 section->name, segment_selector_size);
7054 return 0;
7055 }
7056
7057 if (offset_entry_count == 0)
7058 {
7059 warn (_("The %s section contains a table without offset\n"),
7060 section->name);
7061 return 0;
7062 }
7063
7064 printf (_("\n Offset Entries starting at %#tx:\n"),
7065 start - section->start);
7066
7067 for (i = 0; i < offset_entry_count; i++)
7068 {
7069 uint64_t entry;
7070
7071 SAFE_BYTE_GET_AND_INC (entry, start, is_64bit ? 8 : 4, end);
7072 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
7073 }
7074
7075 putchar ('\n');
7076
7077 uint32_t j;
7078
7079 for (j = 1, i = 0; i < offset_entry_count;)
7080 {
7081 unsigned char lle;
7082 uint64_t base_address = 0;
7083 uint64_t begin;
7084 uint64_t finish;
7085 uint64_t off = start - section->start;
7086
7087 if (j != i)
7088 {
7089 printf (_(" Offset Entry %u\n"), i);
7090 j = i;
7091 }
7092
7093 printf (" ");
7094 print_hex (off, 4);
7095
7096 SAFE_BYTE_GET_AND_INC (lle, start, 1, end);
7097
7098 switch (lle)
7099 {
7100 case DW_LLE_end_of_list:
7101 printf (_("<End of list>\n\n"));
7102 i ++;
7103 continue;
7104
7105 case DW_LLE_base_addressx:
7106 READ_ULEB (base_address, start, end);
7107 print_hex (base_address, address_size);
7108 printf (_("(index into .debug_addr) "));
7109 base_address = fetch_indexed_addr (base_address, address_size);
7110 print_hex (base_address, address_size);
7111 printf (_("(base address)\n"));
7112 continue;
7113
7114 case DW_LLE_startx_endx:
7115 READ_ULEB (begin, start, end);
7116 begin = fetch_indexed_addr (begin, address_size);
7117 READ_ULEB (finish, start, end);
7118 finish = fetch_indexed_addr (finish, address_size);
7119 break;
7120
7121 case DW_LLE_startx_length:
7122 READ_ULEB (begin, start, end);
7123 begin = fetch_indexed_addr (begin, address_size);
7124 READ_ULEB (finish, start, end);
7125 finish += begin;
7126 break;
7127
7128 case DW_LLE_offset_pair:
7129 READ_ULEB (begin, start, end);
7130 begin += base_address;
7131 READ_ULEB (finish, start, end);
7132 finish += base_address;
7133 break;
7134
7135 case DW_LLE_default_location:
7136 begin = finish = 0;
7137 break;
7138
7139 case DW_LLE_base_address:
7140 SAFE_BYTE_GET_AND_INC (base_address, start, address_size, end);
7141 print_hex (base_address, address_size);
7142 printf (_("(base address)\n"));
7143 continue;
7144
7145 case DW_LLE_start_end:
7146 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7147 SAFE_BYTE_GET_AND_INC (finish, start, address_size, end);
7148 break;
7149
7150 case DW_LLE_start_length:
7151 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7152 READ_ULEB (finish, start, end);
7153 finish += begin;
7154 break;
7155
7156 default:
7157 error (_("Invalid location list entry type %d\n"), lle);
7158 return 0;
7159 }
7160
7161 if (start == end)
7162 {
7163 warn (_("Location list starting at offset %#" PRIx64
7164 " is not terminated.\n"), off);
7165 break;
7166 }
7167
7168 print_hex (begin, address_size);
7169 print_hex (finish, address_size);
7170
7171 if (begin == finish)
7172 fputs (_("(start == end)"), stdout);
7173 else if (begin > finish)
7174 fputs (_("(start > end)"), stdout);
7175
7176 /* Read the counted location descriptions. */
7177 READ_ULEB (length, start, end);
7178
7179 if (length > (size_t) (end - start))
7180 {
7181 warn (_("Location list starting at offset %#" PRIx64
7182 " is not terminated.\n"), off);
7183 break;
7184 }
7185
7186 (void) decode_location_expression (start, address_size, address_size,
7187 version, length, 0, section);
7188 start += length;
7189 putchar ('\n');
7190 }
7191
7192 putchar ('\n');
7193 }
7194 while (start < end);
7195
7196 return 1;
7197 }
7198
7199 static int
7200 display_debug_loc (struct dwarf_section *section, void *file)
7201 {
7202 unsigned char *start = section->start, *vstart = NULL;
7203 uint64_t bytes;
7204 unsigned char *section_begin = start;
7205 unsigned int num_loc_list = 0;
7206 uint64_t last_offset = 0;
7207 uint64_t last_view = 0;
7208 unsigned int first = 0;
7209 unsigned int i;
7210 unsigned int j;
7211 int seen_first_offset = 0;
7212 int locs_sorted = 1;
7213 unsigned char *next = start, *vnext = vstart;
7214 unsigned int *array = NULL;
7215 const char *suffix = strrchr (section->name, '.');
7216 bool is_dwo = false;
7217 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
7218 uint64_t header_size = 0;
7219
7220 if (suffix && strcmp (suffix, ".dwo") == 0)
7221 is_dwo = true;
7222
7223 bytes = section->size;
7224
7225 if (bytes == 0)
7226 {
7227 printf (_("\nThe %s section is empty.\n"), section->name);
7228 return 0;
7229 }
7230
7231 if (is_loclists)
7232 {
7233 unsigned char *hdrptr = section_begin;
7234 uint64_t ll_length;
7235 unsigned short ll_version;
7236 unsigned char *end = section_begin + section->size;
7237 unsigned char address_size, segment_selector_size;
7238 uint32_t offset_entry_count;
7239
7240 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
7241 if (ll_length == 0xffffffff)
7242 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
7243
7244 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
7245 if (ll_version != 5)
7246 {
7247 warn (_("The %s section contains corrupt or "
7248 "unsupported version number: %d.\n"),
7249 section->name, ll_version);
7250 return 0;
7251 }
7252
7253 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7254
7255 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7256 if (segment_selector_size != 0)
7257 {
7258 warn (_("The %s section contains "
7259 "unsupported segment selector size: %d.\n"),
7260 section->name, segment_selector_size);
7261 return 0;
7262 }
7263
7264 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7265
7266 if (offset_entry_count != 0)
7267 return display_offset_entry_loclists (section);
7268
7269 header_size = hdrptr - section_begin;
7270 }
7271
7272 if (load_debug_info (file) == 0)
7273 {
7274 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7275 section->name);
7276 return 0;
7277 }
7278
7279 /* Check the order of location list in .debug_info section. If
7280 offsets of location lists are in the ascending order, we can
7281 use `debug_information' directly. */
7282 for (i = 0; i < num_debug_info_entries; i++)
7283 {
7284 unsigned int num;
7285
7286 num = debug_information [i].num_loc_offsets;
7287 if (num > num_loc_list)
7288 num_loc_list = num;
7289
7290 /* Check if we can use `debug_information' directly. */
7291 if (locs_sorted && num != 0)
7292 {
7293 if (!seen_first_offset)
7294 {
7295 /* This is the first location list. */
7296 last_offset = debug_information [i].loc_offsets [0];
7297 last_view = debug_information [i].loc_views [0];
7298 first = i;
7299 seen_first_offset = 1;
7300 j = 1;
7301 }
7302 else
7303 j = 0;
7304
7305 for (; j < num; j++)
7306 {
7307 if (last_offset >
7308 debug_information [i].loc_offsets [j]
7309 || (last_offset == debug_information [i].loc_offsets [j]
7310 && last_view > debug_information [i].loc_views [j]))
7311 {
7312 locs_sorted = 0;
7313 break;
7314 }
7315 last_offset = debug_information [i].loc_offsets [j];
7316 last_view = debug_information [i].loc_views [j];
7317 }
7318 }
7319 }
7320
7321 if (!seen_first_offset)
7322 error (_("No location lists in .debug_info section!\n"));
7323
7324 if (debug_information [first].num_loc_offsets > 0
7325 && debug_information [first].loc_offsets [0] != header_size
7326 && debug_information [first].loc_views [0] != header_size)
7327 warn (_("Location lists in %s section start at %#" PRIx64
7328 " rather than %#" PRIx64 "\n"),
7329 section->name, debug_information [first].loc_offsets [0],
7330 header_size);
7331
7332 if (!locs_sorted)
7333 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
7334
7335 introduce (section, false);
7336
7337 if (reloc_at (section, 0))
7338 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
7339
7340 printf (_(" Offset Begin End Expression\n"));
7341
7342 for (i = first; i < num_debug_info_entries; i++)
7343 {
7344 uint64_t offset = 0, voffset = 0;
7345 uint64_t base_address;
7346 unsigned int k;
7347 int has_frame_base;
7348
7349 if (!locs_sorted)
7350 {
7351 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7352 array[k] = k;
7353 loc_offsets = debug_information [i].loc_offsets;
7354 loc_views = debug_information [i].loc_views;
7355 qsort (array, debug_information [i].num_loc_offsets,
7356 sizeof (*array), loc_offsets_compar);
7357 }
7358
7359 /* .debug_loclists has a per-unit header.
7360 Update start if we are detecting it. */
7361 if (debug_information [i].dwarf_version == 5)
7362 {
7363 j = locs_sorted ? 0 : array [0];
7364
7365 if (debug_information [i].num_loc_offsets)
7366 offset = debug_information [i].loc_offsets [j];
7367
7368 if (debug_information [i].num_loc_views)
7369 voffset = debug_information [i].loc_views [j];
7370
7371 /* Assume that the size of the header is constant across CUs. */
7372 if (((start - section_begin) + header_size == offset)
7373 || ((start -section_begin) + header_size == voffset))
7374 start += header_size;
7375 }
7376
7377 int adjacent_view_loclists = 1;
7378 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7379 {
7380 j = locs_sorted ? k : array[k];
7381 if (k
7382 && (debug_information [i].loc_offsets [locs_sorted
7383 ? k - 1 : array [k - 1]]
7384 == debug_information [i].loc_offsets [j])
7385 && (debug_information [i].loc_views [locs_sorted
7386 ? k - 1 : array [k - 1]]
7387 == debug_information [i].loc_views [j]))
7388 continue;
7389 has_frame_base = debug_information [i].have_frame_base [j];
7390 offset = debug_information [i].loc_offsets [j];
7391 next = section_begin + offset;
7392 voffset = debug_information [i].loc_views [j];
7393 if (voffset != (uint64_t) -1)
7394 vnext = section_begin + voffset;
7395 else
7396 vnext = NULL;
7397 base_address = debug_information [i].base_address;
7398
7399 if (vnext && vnext < next)
7400 {
7401 vstart = vnext;
7402 display_view_pair_list (section, &vstart, i, next);
7403 if (start == vnext)
7404 start = vstart;
7405 }
7406
7407 if (start < next)
7408 {
7409 if (vnext && vnext < next)
7410 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7411 " in %s section.\n"),
7412 start - section_begin, voffset, section->name);
7413 else
7414 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7415 " in %s section.\n"),
7416 start - section_begin, offset, section->name);
7417 }
7418 else if (start > next)
7419 warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
7420 " in %s section.\n"),
7421 start - section_begin, offset, section->name);
7422 start = next;
7423 vstart = vnext;
7424
7425 if (offset >= bytes)
7426 {
7427 warn (_("Offset %#" PRIx64 " is bigger than %s section size.\n"),
7428 offset, section->name);
7429 continue;
7430 }
7431
7432 if (vnext && voffset >= bytes)
7433 {
7434 warn (_("View Offset %#" PRIx64 " is bigger than %s section size.\n"),
7435 voffset, section->name);
7436 continue;
7437 }
7438
7439 if (!is_loclists)
7440 {
7441 if (is_dwo)
7442 display_loc_list_dwo (section, &start, i, offset,
7443 &vstart, has_frame_base);
7444 else
7445 display_loc_list (section, &start, i, offset, base_address,
7446 &vstart, has_frame_base);
7447 }
7448 else
7449 {
7450 if (is_dwo)
7451 warn (_("DWO is not yet supported.\n"));
7452 else
7453 display_loclists_list (section, &start, i, offset, base_address,
7454 &vstart, has_frame_base);
7455 }
7456
7457 /* FIXME: this arrangement is quite simplistic. Nothing
7458 requires locview lists to be adjacent to corresponding
7459 loclists, and a single loclist could be augmented by
7460 different locview lists, and vice-versa, unlikely as it
7461 is that it would make sense to do so. Hopefully we'll
7462 have view pair support built into loclists before we ever
7463 need to address all these possibilities. */
7464 if (adjacent_view_loclists && vnext
7465 && vnext != start && vstart != next)
7466 {
7467 adjacent_view_loclists = 0;
7468 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7469 }
7470
7471 if (vnext && vnext == start)
7472 display_view_pair_list (section, &start, i, vstart);
7473 }
7474 }
7475
7476 if (start < section->start + section->size)
7477 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7478 "There are %ld unused bytes at the end of section %s\n",
7479 (long) (section->start + section->size - start)),
7480 (long) (section->start + section->size - start), section->name);
7481 putchar ('\n');
7482 free (array);
7483 return 1;
7484 }
7485
7486 static int
7487 display_debug_str (struct dwarf_section *section,
7488 void *file ATTRIBUTE_UNUSED)
7489 {
7490 unsigned char *start = section->start;
7491 uint64_t bytes = section->size;
7492 uint64_t addr = section->address;
7493
7494 if (bytes == 0)
7495 {
7496 printf (_("\nThe %s section is empty.\n"), section->name);
7497 return 0;
7498 }
7499
7500 introduce (section, false);
7501
7502 while (bytes)
7503 {
7504 int j;
7505 int k;
7506 int lbytes;
7507
7508 lbytes = (bytes > 16 ? 16 : bytes);
7509
7510 printf (" 0x%8.8" PRIx64 " ", addr);
7511
7512 for (j = 0; j < 16; j++)
7513 {
7514 if (j < lbytes)
7515 printf ("%2.2x", start[j]);
7516 else
7517 printf (" ");
7518
7519 if ((j & 3) == 3)
7520 printf (" ");
7521 }
7522
7523 for (j = 0; j < lbytes; j++)
7524 {
7525 k = start[j];
7526 if (k >= ' ' && k < 0x80)
7527 printf ("%c", k);
7528 else
7529 printf (".");
7530 }
7531
7532 putchar ('\n');
7533
7534 start += lbytes;
7535 addr += lbytes;
7536 bytes -= lbytes;
7537 }
7538
7539 putchar ('\n');
7540
7541 return 1;
7542 }
7543
7544 static int
7545 display_debug_info (struct dwarf_section *section, void *file)
7546 {
7547 return process_debug_info (section, file, section->abbrev_sec, false, false);
7548 }
7549
7550 static int
7551 display_debug_types (struct dwarf_section *section, void *file)
7552 {
7553 return process_debug_info (section, file, section->abbrev_sec, false, true);
7554 }
7555
7556 static int
7557 display_trace_info (struct dwarf_section *section, void *file)
7558 {
7559 return process_debug_info (section, file, section->abbrev_sec, false, true);
7560 }
7561
7562 static int
7563 display_debug_aranges (struct dwarf_section *section,
7564 void *file ATTRIBUTE_UNUSED)
7565 {
7566 unsigned char *start = section->start;
7567 unsigned char *end = start + section->size;
7568
7569 introduce (section, false);
7570
7571 /* It does not matter if this load fails,
7572 we test for that later on. */
7573 load_debug_info (file);
7574
7575 while (start < end)
7576 {
7577 unsigned char *hdrptr;
7578 DWARF2_Internal_ARange arange;
7579 unsigned char *addr_ranges;
7580 uint64_t length;
7581 uint64_t address;
7582 uint64_t sec_off;
7583 unsigned char address_size;
7584 unsigned int offset_size;
7585 unsigned char *end_ranges;
7586
7587 hdrptr = start;
7588 sec_off = hdrptr - section->start;
7589
7590 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7591 if (arange.ar_length == 0xffffffff)
7592 {
7593 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7594 offset_size = 8;
7595 }
7596 else
7597 offset_size = 4;
7598
7599 if (arange.ar_length > (size_t) (end - hdrptr))
7600 {
7601 warn (_("Debug info is corrupted, %s header at %#" PRIx64
7602 " has length %#" PRIx64 "\n"),
7603 section->name, sec_off, arange.ar_length);
7604 break;
7605 }
7606 end_ranges = hdrptr + arange.ar_length;
7607
7608 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7609 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7610 end_ranges);
7611
7612 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7613 && num_debug_info_entries > 0
7614 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7615 warn (_(".debug_info offset of %#" PRIx64
7616 " in %s section does not point to a CU header.\n"),
7617 arange.ar_info_offset, section->name);
7618
7619 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7620 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7621
7622 if (arange.ar_version != 2 && arange.ar_version != 3)
7623 {
7624 /* PR 19872: A version number of 0 probably means that there is
7625 padding at the end of the .debug_aranges section. Gold puts
7626 it there when performing an incremental link, for example.
7627 So do not generate a warning in this case. */
7628 if (arange.ar_version)
7629 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7630 break;
7631 }
7632
7633 printf (_(" Length: %" PRId64 "\n"), arange.ar_length);
7634 printf (_(" Version: %d\n"), arange.ar_version);
7635 printf (_(" Offset into .debug_info: %#" PRIx64 "\n"),
7636 arange.ar_info_offset);
7637 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7638 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7639
7640 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7641
7642 /* PR 17512: file: 001-108546-0.001:0.1. */
7643 if (address_size == 0 || address_size > 8)
7644 {
7645 error (_("Invalid address size in %s section!\n"),
7646 section->name);
7647 break;
7648 }
7649
7650 /* The DWARF spec does not require that the address size be a power
7651 of two, but we do. This will have to change if we ever encounter
7652 an uneven architecture. */
7653 if ((address_size & (address_size - 1)) != 0)
7654 {
7655 warn (_("Pointer size + Segment size is not a power of two.\n"));
7656 break;
7657 }
7658
7659 if (address_size > 4)
7660 printf (_("\n Address Length\n"));
7661 else
7662 printf (_("\n Address Length\n"));
7663
7664 addr_ranges = hdrptr;
7665
7666 /* Must pad to an alignment boundary that is twice the address size. */
7667 addr_ranges += (2 * address_size - 1
7668 - (hdrptr - start - 1) % (2 * address_size));
7669
7670 while (2 * address_size <= end_ranges - addr_ranges)
7671 {
7672 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7673 end_ranges);
7674 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7675 end_ranges);
7676 printf (" ");
7677 print_hex (address, address_size);
7678 print_hex_ns (length, address_size);
7679 putchar ('\n');
7680 }
7681
7682 start = end_ranges;
7683 }
7684
7685 printf ("\n");
7686
7687 return 1;
7688 }
7689
7690 /* Comparison function for qsort. */
7691 static int
7692 comp_addr_base (const void * v0, const void * v1)
7693 {
7694 debug_info *info0 = *(debug_info **) v0;
7695 debug_info *info1 = *(debug_info **) v1;
7696 return info0->addr_base - info1->addr_base;
7697 }
7698
7699 /* Display the debug_addr section. */
7700 static int
7701 display_debug_addr (struct dwarf_section *section,
7702 void *file)
7703 {
7704 debug_info **debug_addr_info;
7705 unsigned char *entry;
7706 unsigned char *end;
7707 unsigned int i;
7708 unsigned int count;
7709 unsigned char * header;
7710
7711 if (section->size == 0)
7712 {
7713 printf (_("\nThe %s section is empty.\n"), section->name);
7714 return 0;
7715 }
7716
7717 if (load_debug_info (file) == 0)
7718 {
7719 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7720 section->name);
7721 return 0;
7722 }
7723
7724 introduce (section, false);
7725
7726 /* PR 17531: file: cf38d01b.
7727 We use xcalloc because a corrupt file may not have initialised all of the
7728 fields in the debug_info structure, which means that the sort below might
7729 try to move uninitialised data. */
7730 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7731 sizeof (debug_info *));
7732
7733 count = 0;
7734 for (i = 0; i < num_debug_info_entries; i++)
7735 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7736 {
7737 /* PR 17531: file: cf38d01b. */
7738 if (debug_information[i].addr_base >= section->size)
7739 warn (_("Corrupt address base (%#" PRIx64 ")"
7740 " found in debug section %u\n"),
7741 debug_information[i].addr_base, i);
7742 else
7743 debug_addr_info [count++] = debug_information + i;
7744 }
7745
7746 /* Add a sentinel to make iteration convenient. */
7747 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7748 debug_addr_info [count]->addr_base = section->size;
7749 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7750
7751 header = section->start;
7752 for (i = 0; i < count; i++)
7753 {
7754 unsigned int idx;
7755 unsigned int address_size = debug_addr_info [i]->pointer_size;
7756
7757 printf (_(" For compilation unit at offset %#" PRIx64 ":\n"),
7758 debug_addr_info [i]->cu_offset);
7759
7760 printf (_("\tIndex\tAddress\n"));
7761 entry = section->start + debug_addr_info [i]->addr_base;
7762 if (debug_addr_info [i]->dwarf_version >= 5)
7763 {
7764 size_t header_size = entry - header;
7765 unsigned char *curr_header = header;
7766 uint64_t length;
7767 int version;
7768 int segment_selector_size;
7769
7770 if (header_size != 8 && header_size != 16)
7771 {
7772 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %zd instead\n"),
7773 section->name, header_size);
7774 return 0;
7775 }
7776
7777 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7778 if (length == 0xffffffff)
7779 SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7780 if (length > (size_t) (section->start + section->size - curr_header)
7781 || length < (size_t) (entry - curr_header))
7782 {
7783 warn (_("Corrupt %s section: unit_length field of %#" PRIx64
7784 " is invalid\n"), section->name, length);
7785 return 0;
7786 }
7787 end = curr_header + length;
7788 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7789 if (version != 5)
7790 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7791 section->name, version);
7792
7793 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7794 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7795 address_size += segment_selector_size;
7796 }
7797 else
7798 end = section->start + debug_addr_info [i + 1]->addr_base;
7799
7800 header = end;
7801 idx = 0;
7802
7803 if (address_size < 1 || address_size > sizeof (uint64_t))
7804 {
7805 warn (_("Corrupt %s section: address size (%x) is wrong"),
7806 section->name, address_size);
7807 return 0;
7808 }
7809
7810 while ((size_t) (end - entry) >= address_size)
7811 {
7812 uint64_t base = byte_get (entry, address_size);
7813 printf (_("\t%d:\t"), idx);
7814 print_hex_ns (base, address_size);
7815 printf ("\n");
7816 entry += address_size;
7817 idx++;
7818 }
7819 }
7820 printf ("\n");
7821
7822 free (debug_addr_info);
7823 return 1;
7824 }
7825
7826 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
7827
7828 static int
7829 display_debug_str_offsets (struct dwarf_section *section,
7830 void *file ATTRIBUTE_UNUSED)
7831 {
7832 unsigned long idx;
7833
7834 if (section->size == 0)
7835 {
7836 printf (_("\nThe %s section is empty.\n"), section->name);
7837 return 0;
7838 }
7839
7840 unsigned char *start = section->start;
7841 unsigned char *end = start + section->size;
7842 unsigned char *curr = start;
7843 uint64_t debug_str_offsets_hdr_len;
7844
7845 const char *suffix = strrchr (section->name, '.');
7846 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
7847
7848 if (dwo)
7849 load_debug_section_with_follow (str_dwo, file);
7850 else
7851 load_debug_section_with_follow (str, file);
7852
7853 introduce (section, false);
7854
7855 while (curr < end)
7856 {
7857 uint64_t length;
7858 uint64_t entry_length;
7859
7860 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7861 /* FIXME: We assume that this means 64-bit DWARF is being used. */
7862 if (length == 0xffffffff)
7863 {
7864 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
7865 entry_length = 8;
7866 debug_str_offsets_hdr_len = 16;
7867 }
7868 else
7869 {
7870 entry_length = 4;
7871 debug_str_offsets_hdr_len = 8;
7872 }
7873
7874 unsigned char *entries_end;
7875 if (length == 0)
7876 {
7877 /* This is probably an old style .debug_str_offset section which
7878 just contains offsets and no header (and the first offset is 0). */
7879 length = section->size;
7880 curr = section->start;
7881 entries_end = end;
7882
7883 printf (_(" Length: %#" PRIx64 "\n"), length);
7884 printf (_(" Index Offset [String]\n"));
7885 }
7886 else
7887 {
7888 if (length <= (size_t) (end - curr))
7889 entries_end = curr + length;
7890 else
7891 {
7892 warn (_("Section %s is too small %#" PRIx64 "\n"),
7893 section->name, section->size);
7894 entries_end = end;
7895 }
7896
7897 int version;
7898 SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
7899 if (version != 5)
7900 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7901
7902 int padding;
7903 SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
7904 if (padding != 0)
7905 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7906
7907 printf (_(" Length: %#" PRIx64 "\n"), length);
7908 printf (_(" Version: %#x\n"), version);
7909 printf (_(" Index Offset [String]\n"));
7910 }
7911
7912 for (idx = 0; curr < entries_end; idx++)
7913 {
7914 uint64_t offset;
7915 const unsigned char * string;
7916
7917 if ((size_t) (entries_end - curr) < entry_length)
7918 /* Not enough space to read one entry_length, give up. */
7919 return 0;
7920
7921 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
7922 if (dwo)
7923 string = (const unsigned char *)
7924 fetch_indexed_string (idx, NULL, entry_length, dwo, debug_str_offsets_hdr_len);
7925 else
7926 string = fetch_indirect_string (offset);
7927
7928 printf (" %8lu ", idx);
7929 print_hex (offset, entry_length);
7930 printf (" %s\n", string);
7931 }
7932 }
7933
7934 return 1;
7935 }
7936
7937 /* Each debug_information[x].range_lists[y] gets this representation for
7938 sorting purposes. */
7939
7940 struct range_entry
7941 {
7942 /* The debug_information[x].range_lists[y] value. */
7943 uint64_t ranges_offset;
7944
7945 /* Original debug_information to find parameters of the data. */
7946 debug_info *debug_info_p;
7947 };
7948
7949 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
7950
7951 static int
7952 range_entry_compar (const void *ap, const void *bp)
7953 {
7954 const struct range_entry *a_re = (const struct range_entry *) ap;
7955 const struct range_entry *b_re = (const struct range_entry *) bp;
7956 const uint64_t a = a_re->ranges_offset;
7957 const uint64_t b = b_re->ranges_offset;
7958
7959 return (a > b) - (b > a);
7960 }
7961
7962 static void
7963 display_debug_ranges_list (unsigned char * start,
7964 unsigned char * finish,
7965 unsigned int pointer_size,
7966 uint64_t offset,
7967 uint64_t base_address)
7968 {
7969 while (start < finish)
7970 {
7971 uint64_t begin;
7972 uint64_t end;
7973
7974 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7975 if (start >= finish)
7976 break;
7977 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7978
7979 printf (" ");
7980 print_hex (offset, 4);
7981
7982 if (begin == 0 && end == 0)
7983 {
7984 printf (_("<End of list>\n"));
7985 break;
7986 }
7987
7988 /* Check base address specifiers. */
7989 if (is_max_address (begin, pointer_size)
7990 && !is_max_address (end, pointer_size))
7991 {
7992 base_address = end;
7993 print_hex (begin, pointer_size);
7994 print_hex (end, pointer_size);
7995 printf ("(base address)\n");
7996 continue;
7997 }
7998
7999 print_hex (begin + base_address, pointer_size);
8000 print_hex_ns (end + base_address, pointer_size);
8001
8002 if (begin == end)
8003 fputs (_(" (start == end)"), stdout);
8004 else if (begin > end)
8005 fputs (_(" (start > end)"), stdout);
8006
8007 putchar ('\n');
8008 }
8009 }
8010
8011 static unsigned char *
8012 display_debug_rnglists_list (unsigned char * start,
8013 unsigned char * finish,
8014 unsigned int pointer_size,
8015 uint64_t offset,
8016 uint64_t base_address,
8017 unsigned int offset_size)
8018 {
8019 unsigned char *next = start;
8020 unsigned int debug_addr_section_hdr_len;
8021
8022 if (offset_size == 4)
8023 debug_addr_section_hdr_len = 8;
8024 else
8025 debug_addr_section_hdr_len = 16;
8026
8027 while (1)
8028 {
8029 uint64_t off = offset + (start - next);
8030 enum dwarf_range_list_entry rlet;
8031 /* Initialize it due to a false compiler warning. */
8032 uint64_t begin = -1, length, end = -1;
8033
8034 if (start >= finish)
8035 {
8036 warn (_("Range list starting at offset %#" PRIx64
8037 " is not terminated.\n"), offset);
8038 break;
8039 }
8040
8041 printf (" ");
8042 print_hex (off, 4);
8043
8044 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
8045
8046 switch (rlet)
8047 {
8048 case DW_RLE_end_of_list:
8049 printf (_("<End of list>\n"));
8050 break;
8051 case DW_RLE_base_addressx:
8052 READ_ULEB (base_address, start, finish);
8053 print_hex (base_address, pointer_size);
8054 printf (_("(base address index) "));
8055 base_address = fetch_indexed_addr ((base_address * pointer_size)
8056 + debug_addr_section_hdr_len, pointer_size);
8057 print_hex (base_address, pointer_size);
8058 printf (_("(base address)\n"));
8059 break;
8060 case DW_RLE_startx_endx:
8061 READ_ULEB (begin, start, finish);
8062 READ_ULEB (end, start, finish);
8063 begin = fetch_indexed_addr ((begin * pointer_size)
8064 + debug_addr_section_hdr_len, pointer_size);
8065 end = fetch_indexed_addr ((begin * pointer_size)
8066 + debug_addr_section_hdr_len, pointer_size);
8067 break;
8068 case DW_RLE_startx_length:
8069 READ_ULEB (begin, start, finish);
8070 READ_ULEB (length, start, finish);
8071 begin = fetch_indexed_addr ((begin * pointer_size)
8072 + debug_addr_section_hdr_len, pointer_size);
8073 end = begin + length;
8074 break;
8075 case DW_RLE_offset_pair:
8076 READ_ULEB (begin, start, finish);
8077 READ_ULEB (end, start, finish);
8078 break;
8079 case DW_RLE_base_address:
8080 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
8081 print_hex (base_address, pointer_size);
8082 printf (_("(base address)\n"));
8083 break;
8084 case DW_RLE_start_end:
8085 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8086 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8087 break;
8088 case DW_RLE_start_length:
8089 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8090 READ_ULEB (length, start, finish);
8091 end = begin + length;
8092 break;
8093 default:
8094 error (_("Invalid range list entry type %d\n"), rlet);
8095 rlet = DW_RLE_end_of_list;
8096 break;
8097 }
8098
8099 if (rlet == DW_RLE_end_of_list)
8100 break;
8101 if (rlet == DW_RLE_base_address || rlet == DW_RLE_base_addressx)
8102 continue;
8103
8104 /* Only a DW_RLE_offset_pair needs the base address added. */
8105 if (rlet == DW_RLE_offset_pair)
8106 {
8107 begin += base_address;
8108 end += base_address;
8109 }
8110
8111 print_hex (begin, pointer_size);
8112 print_hex (end, pointer_size);
8113
8114 if (begin == end)
8115 fputs (_(" (start == end)"), stdout);
8116 else if (begin > end)
8117 fputs (_(" (start > end)"), stdout);
8118
8119 putchar ('\n');
8120 }
8121
8122 return start;
8123 }
8124
8125 static int
8126 display_debug_rnglists (struct dwarf_section *section)
8127 {
8128 unsigned char *start = section->start;
8129 unsigned char *finish = start + section->size;
8130
8131 while (start < finish)
8132 {
8133 unsigned char *table_start;
8134 uint64_t offset = start - section->start;
8135 unsigned char *end;
8136 uint64_t initial_length;
8137 unsigned char segment_selector_size;
8138 unsigned int offset_entry_count;
8139 unsigned int i;
8140 unsigned short version;
8141 unsigned char address_size = 0;
8142 unsigned char offset_size;
8143
8144 /* Get and check the length of the block. */
8145 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
8146
8147 if (initial_length == 0xffffffff)
8148 {
8149 /* This section is 64-bit DWARF 3. */
8150 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
8151 offset_size = 8;
8152 }
8153 else
8154 offset_size = 4;
8155
8156 if (initial_length > (size_t) (finish - start))
8157 {
8158 /* If the length field has a relocation against it, then we should
8159 not complain if it is inaccurate (and probably negative).
8160 It is copied from .debug_line handling code. */
8161 if (reloc_at (section, (start - section->start) - offset_size))
8162 initial_length = finish - start;
8163 else
8164 {
8165 warn (_("The length field (%#" PRIx64
8166 ") in the debug_rnglists header is wrong"
8167 " - the section is too small\n"),
8168 initial_length);
8169 return 0;
8170 }
8171 }
8172
8173 end = start + initial_length;
8174
8175 /* Get the other fields in the header. */
8176 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
8177 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
8178 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
8179 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
8180
8181 printf (_(" Table at Offset: %#" PRIx64 ":\n"), offset);
8182 printf (_(" Length: %#" PRIx64 "\n"), initial_length);
8183 printf (_(" DWARF version: %u\n"), version);
8184 printf (_(" Address size: %u\n"), address_size);
8185 printf (_(" Segment size: %u\n"), segment_selector_size);
8186 printf (_(" Offset entries: %u\n"), offset_entry_count);
8187
8188 /* Check the fields. */
8189 if (segment_selector_size != 0)
8190 {
8191 warn (_("The %s section contains "
8192 "unsupported segment selector size: %d.\n"),
8193 section->name, segment_selector_size);
8194 return 0;
8195 }
8196
8197 if (version < 5)
8198 {
8199 warn (_("Only DWARF version 5+ debug_rnglists info "
8200 "is currently supported.\n"));
8201 return 0;
8202 }
8203
8204 table_start = start;
8205
8206 if (offset_entry_count != 0)
8207 {
8208 printf (_("\n Offsets starting at %#tx:\n"),
8209 start - section->start);
8210
8211 for (i = 0; i < offset_entry_count; i++)
8212 {
8213 uint64_t entry;
8214
8215 SAFE_BYTE_GET_AND_INC (entry, start, offset_size, finish);
8216 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
8217 }
8218 }
8219 else
8220 offset_entry_count = 1;
8221
8222 for (i = 0; i < offset_entry_count; i++)
8223 {
8224 uint64_t indx = start - table_start;
8225
8226 offset = start - section->start;
8227 printf (_("\n Offset: %#" PRIx64 ", Index: %#" PRIx64 "\n"),
8228 offset, indx);
8229 printf (_(" Offset Begin End\n"));
8230 start = display_debug_rnglists_list
8231 (start, end, address_size, offset, 0, offset_size);
8232 if (start >= end)
8233 break;
8234 }
8235
8236 start = end;
8237
8238 if (start < finish)
8239 putchar ('\n');
8240 }
8241
8242 putchar ('\n');
8243 return 1;
8244 }
8245
8246 static int
8247 display_debug_ranges (struct dwarf_section *section,
8248 void *file ATTRIBUTE_UNUSED)
8249 {
8250 unsigned char *start = section->start;
8251 unsigned char *last_start = start;
8252 uint64_t bytes = section->size;
8253 unsigned char *section_begin = start;
8254 unsigned char *finish = start + bytes;
8255 unsigned int num_range_list, i;
8256 struct range_entry *range_entries;
8257 struct range_entry *range_entry_fill;
8258 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
8259 /* Initialize it due to a false compiler warning. */
8260 unsigned char address_size = 0;
8261 uint64_t last_offset = 0;
8262
8263 if (bytes == 0)
8264 {
8265 printf (_("\nThe %s section is empty.\n"), section->name);
8266 return 0;
8267 }
8268
8269 introduce (section, false);
8270
8271 if (is_rnglists)
8272 return display_debug_rnglists (section);
8273
8274 if (load_debug_info (file) == 0)
8275 {
8276 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
8277 section->name);
8278 return 0;
8279 }
8280
8281 num_range_list = 0;
8282 for (i = 0; i < num_debug_info_entries; i++)
8283 num_range_list += debug_information [i].num_range_lists;
8284
8285 if (num_range_list == 0)
8286 {
8287 /* This can happen when the file was compiled with -gsplit-debug
8288 which removes references to range lists from the primary .o file. */
8289 printf (_("No range lists in .debug_info section.\n"));
8290 return 1;
8291 }
8292
8293 range_entries = (struct range_entry *)
8294 xmalloc (sizeof (*range_entries) * num_range_list);
8295 range_entry_fill = range_entries;
8296
8297 for (i = 0; i < num_debug_info_entries; i++)
8298 {
8299 debug_info *debug_info_p = &debug_information[i];
8300 unsigned int j;
8301
8302 for (j = 0; j < debug_info_p->num_range_lists; j++)
8303 {
8304 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
8305 range_entry_fill->debug_info_p = debug_info_p;
8306 range_entry_fill++;
8307 }
8308 }
8309
8310 qsort (range_entries, num_range_list, sizeof (*range_entries),
8311 range_entry_compar);
8312
8313 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
8314 warn (_("Range lists in %s section start at %#" PRIx64 "\n"),
8315 section->name, range_entries[0].ranges_offset);
8316
8317 putchar ('\n');
8318 printf (_(" Offset Begin End\n"));
8319
8320 for (i = 0; i < num_range_list; i++)
8321 {
8322 struct range_entry *range_entry = &range_entries[i];
8323 debug_info *debug_info_p = range_entry->debug_info_p;
8324 unsigned int pointer_size;
8325 uint64_t offset;
8326 unsigned char *next;
8327 uint64_t base_address;
8328
8329 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
8330 offset = range_entry->ranges_offset;
8331 base_address = debug_info_p->base_address;
8332
8333 /* PR 17512: file: 001-101485-0.001:0.1. */
8334 if (pointer_size < 2 || pointer_size > 8)
8335 {
8336 warn (_("Corrupt pointer size (%d) in debug entry at offset %#" PRIx64 "\n"),
8337 pointer_size, offset);
8338 continue;
8339 }
8340
8341 if (offset > (size_t) (finish - section_begin))
8342 {
8343 warn (_("Corrupt offset (%#" PRIx64 ") in range entry %u\n"),
8344 offset, i);
8345 continue;
8346 }
8347
8348 next = section_begin + offset + debug_info_p->rnglists_base;
8349
8350 /* If multiple DWARF entities reference the same range then we will
8351 have multiple entries in the `range_entries' list for the same
8352 offset. Thanks to the sort above these will all be consecutive in
8353 the `range_entries' list, so we can easily ignore duplicates
8354 here. */
8355 if (i > 0 && last_offset == offset)
8356 continue;
8357 last_offset = offset;
8358
8359 if (dwarf_check != 0 && i > 0)
8360 {
8361 if (start < next)
8362 warn (_("There is a hole [%#tx - %#tx] in %s section.\n"),
8363 start - section_begin, next - section_begin, section->name);
8364 else if (start > next)
8365 {
8366 if (next == last_start)
8367 continue;
8368 warn (_("There is an overlap [%#tx - %#tx] in %s section.\n"),
8369 start - section_begin, next - section_begin, section->name);
8370 }
8371 }
8372
8373 start = next;
8374 last_start = next;
8375
8376 display_debug_ranges_list
8377 (start, finish, pointer_size, offset, base_address);
8378 }
8379 putchar ('\n');
8380
8381 free (range_entries);
8382
8383 return 1;
8384 }
8385
8386 typedef struct Frame_Chunk
8387 {
8388 struct Frame_Chunk *next;
8389 unsigned char *chunk_start;
8390 unsigned int ncols;
8391 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8392 short int *col_type;
8393 int64_t *col_offset;
8394 char *augmentation;
8395 unsigned int code_factor;
8396 int data_factor;
8397 uint64_t pc_begin;
8398 uint64_t pc_range;
8399 unsigned int cfa_reg;
8400 uint64_t cfa_offset;
8401 unsigned int ra;
8402 unsigned char fde_encoding;
8403 unsigned char cfa_exp;
8404 unsigned char ptr_size;
8405 unsigned char segment_size;
8406 }
8407 Frame_Chunk;
8408
8409 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8410 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
8411 static const char *const *dwarf_regnames;
8412 static unsigned int dwarf_regnames_count;
8413 static bool is_aarch64;
8414
8415 /* A marker for a col_type that means this column was never referenced
8416 in the frame info. */
8417 #define DW_CFA_unreferenced (-1)
8418
8419 /* Return 0 if no more space is needed, 1 if more space is needed,
8420 -1 for invalid reg. */
8421
8422 static int
8423 frame_need_space (Frame_Chunk *fc, unsigned int reg)
8424 {
8425 unsigned int prev = fc->ncols;
8426
8427 if (reg < (unsigned int) fc->ncols)
8428 return 0;
8429
8430 if (dwarf_regnames_count > 0
8431 && reg > dwarf_regnames_count)
8432 return -1;
8433
8434 fc->ncols = reg + 1;
8435 /* PR 17512: file: 10450-2643-0.004.
8436 If reg == -1 then this can happen... */
8437 if (fc->ncols == 0)
8438 return -1;
8439
8440 /* PR 17512: file: 2844a11d. */
8441 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
8442 {
8443 error (_("Unfeasibly large register number: %u\n"), reg);
8444 fc->ncols = 0;
8445 /* FIXME: 1024 is an arbitrary limit. Increase it if
8446 we ever encounter a valid binary that exceeds it. */
8447 return -1;
8448 }
8449
8450 fc->col_type = xcrealloc (fc->col_type, fc->ncols,
8451 sizeof (*fc->col_type));
8452 fc->col_offset = xcrealloc (fc->col_offset, fc->ncols,
8453 sizeof (*fc->col_offset));
8454 /* PR 17512: file:002-10025-0.005. */
8455 if (fc->col_type == NULL || fc->col_offset == NULL)
8456 {
8457 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8458 fc->ncols);
8459 fc->ncols = 0;
8460 return -1;
8461 }
8462
8463 while (prev < fc->ncols)
8464 {
8465 fc->col_type[prev] = DW_CFA_unreferenced;
8466 fc->col_offset[prev] = 0;
8467 prev++;
8468 }
8469 return 1;
8470 }
8471
8472 static const char *const dwarf_regnames_i386[] =
8473 {
8474 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8475 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8476 "eip", "eflags", NULL, /* 8 - 10 */
8477 "st0", "st1", "st2", "st3", /* 11 - 14 */
8478 "st4", "st5", "st6", "st7", /* 15 - 18 */
8479 NULL, NULL, /* 19 - 20 */
8480 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8481 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8482 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8483 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8484 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8485 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8486 "tr", "ldtr", /* 48 - 49 */
8487 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8488 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8489 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8490 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8491 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8492 NULL, NULL, NULL, /* 90 - 92 */
8493 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
8494 };
8495
8496 static const char *const dwarf_regnames_iamcu[] =
8497 {
8498 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8499 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8500 "eip", "eflags", NULL, /* 8 - 10 */
8501 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8502 NULL, NULL, /* 19 - 20 */
8503 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8504 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8505 NULL, NULL, NULL, /* 37 - 39 */
8506 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8507 "tr", "ldtr", /* 48 - 49 */
8508 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8509 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8510 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8511 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8512 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8513 NULL, NULL, NULL, /* 90 - 92 */
8514 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8515 };
8516
8517 static void
8518 init_dwarf_regnames_i386 (void)
8519 {
8520 dwarf_regnames = dwarf_regnames_i386;
8521 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8522 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8523 }
8524
8525 static void
8526 init_dwarf_regnames_iamcu (void)
8527 {
8528 dwarf_regnames = dwarf_regnames_iamcu;
8529 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8530 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8531 }
8532
8533 static const char *const DW_CFA_GNU_window_save_name[] =
8534 {
8535 "DW_CFA_GNU_window_save",
8536 "DW_CFA_AARCH64_negate_ra_state"
8537 };
8538
8539 static const char *const dwarf_regnames_x86_64[] =
8540 {
8541 "rax", "rdx", "rcx", "rbx",
8542 "rsi", "rdi", "rbp", "rsp",
8543 "r8", "r9", "r10", "r11",
8544 "r12", "r13", "r14", "r15",
8545 "rip",
8546 "xmm0", "xmm1", "xmm2", "xmm3",
8547 "xmm4", "xmm5", "xmm6", "xmm7",
8548 "xmm8", "xmm9", "xmm10", "xmm11",
8549 "xmm12", "xmm13", "xmm14", "xmm15",
8550 "st0", "st1", "st2", "st3",
8551 "st4", "st5", "st6", "st7",
8552 "mm0", "mm1", "mm2", "mm3",
8553 "mm4", "mm5", "mm6", "mm7",
8554 "rflags",
8555 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8556 "fs.base", "gs.base", NULL, NULL,
8557 "tr", "ldtr",
8558 "mxcsr", "fcw", "fsw",
8559 "xmm16", "xmm17", "xmm18", "xmm19",
8560 "xmm20", "xmm21", "xmm22", "xmm23",
8561 "xmm24", "xmm25", "xmm26", "xmm27",
8562 "xmm28", "xmm29", "xmm30", "xmm31",
8563 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8564 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8565 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8566 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8567 NULL, NULL, NULL, /* 115 - 117 */
8568 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8569 };
8570
8571 static void
8572 init_dwarf_regnames_x86_64 (void)
8573 {
8574 dwarf_regnames = dwarf_regnames_x86_64;
8575 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8576 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8577 }
8578
8579 static const char *const dwarf_regnames_aarch64[] =
8580 {
8581 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8582 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8583 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8584 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8585 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
8586 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8587 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8588 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8589 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8590 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8591 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8592 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8593 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8594 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8595 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8596 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8597 };
8598
8599 static void
8600 init_dwarf_regnames_aarch64 (void)
8601 {
8602 dwarf_regnames = dwarf_regnames_aarch64;
8603 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8604 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8605 is_aarch64 = true;
8606 }
8607
8608 static const char *const dwarf_regnames_s390[] =
8609 {
8610 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8611 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8612 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8613 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8614 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8615 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8616 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8617 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8618 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8619 "pswm", "pswa",
8620 NULL, NULL,
8621 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8622 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8623 };
8624
8625 static void
8626 init_dwarf_regnames_s390 (void)
8627 {
8628 dwarf_regnames = dwarf_regnames_s390;
8629 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8630 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8631 }
8632
8633 static const char *const dwarf_regnames_riscv[] =
8634 {
8635 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8636 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8637 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8638 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8639 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8640 "fs0", "fs1", /* 40 - 41 */
8641 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8642 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8643 "fs10", "fs11", /* 58 - 59 */
8644 "ft8", "ft9", "ft10", "ft11", /* 60 - 63 */
8645 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 64 - 71 */
8646 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 72 - 79 */
8647 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 80 - 87 */
8648 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 88 - 95 */
8649 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", /* 96 - 103 */
8650 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", /* 104 - 111 */
8651 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", /* 112 - 119 */
8652 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", /* 120 - 127 */
8653 };
8654
8655 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8656 the large number of CSRs. */
8657
8658 static const char *
8659 regname_internal_riscv (unsigned int regno)
8660 {
8661 const char *name = NULL;
8662
8663 /* Lookup in the table first, this covers GPR and FPR. */
8664 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8665 name = dwarf_regnames_riscv [regno];
8666 else if (regno >= 4096 && regno <= 8191)
8667 {
8668 /* This might be a CSR, these live in a sparse number space from 4096
8669 to 8191 These numbers are defined in the RISC-V ELF ABI
8670 document. */
8671 switch (regno)
8672 {
8673 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8674 case VALUE + 4096: name = #NAME; break;
8675 #include "opcode/riscv-opc.h"
8676 #undef DECLARE_CSR
8677
8678 default:
8679 {
8680 static char csr_name[10];
8681 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8682 name = csr_name;
8683 }
8684 break;
8685 }
8686 }
8687
8688 return name;
8689 }
8690
8691 static void
8692 init_dwarf_regnames_riscv (void)
8693 {
8694 dwarf_regnames = NULL;
8695 dwarf_regnames_count = 8192;
8696 dwarf_regnames_lookup_func = regname_internal_riscv;
8697 }
8698
8699 void
8700 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8701 {
8702 dwarf_regnames_lookup_func = NULL;
8703 is_aarch64 = false;
8704
8705 switch (e_machine)
8706 {
8707 case EM_386:
8708 init_dwarf_regnames_i386 ();
8709 break;
8710
8711 case EM_IAMCU:
8712 init_dwarf_regnames_iamcu ();
8713 break;
8714
8715 case EM_X86_64:
8716 case EM_L1OM:
8717 case EM_K1OM:
8718 init_dwarf_regnames_x86_64 ();
8719 break;
8720
8721 case EM_AARCH64:
8722 init_dwarf_regnames_aarch64 ();
8723 break;
8724
8725 case EM_S390:
8726 init_dwarf_regnames_s390 ();
8727 break;
8728
8729 case EM_RISCV:
8730 init_dwarf_regnames_riscv ();
8731 break;
8732
8733 default:
8734 break;
8735 }
8736 }
8737
8738 /* Initialize the DWARF register name lookup state based on the
8739 architecture and specific machine type of a BFD. */
8740
8741 void
8742 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8743 unsigned long mach)
8744 {
8745 dwarf_regnames_lookup_func = NULL;
8746 is_aarch64 = false;
8747
8748 switch (arch)
8749 {
8750 case bfd_arch_i386:
8751 switch (mach)
8752 {
8753 case bfd_mach_x86_64:
8754 case bfd_mach_x86_64_intel_syntax:
8755 case bfd_mach_x64_32:
8756 case bfd_mach_x64_32_intel_syntax:
8757 init_dwarf_regnames_x86_64 ();
8758 break;
8759
8760 default:
8761 init_dwarf_regnames_i386 ();
8762 break;
8763 }
8764 break;
8765
8766 case bfd_arch_iamcu:
8767 init_dwarf_regnames_iamcu ();
8768 break;
8769
8770 case bfd_arch_aarch64:
8771 init_dwarf_regnames_aarch64();
8772 break;
8773
8774 case bfd_arch_s390:
8775 init_dwarf_regnames_s390 ();
8776 break;
8777
8778 case bfd_arch_riscv:
8779 init_dwarf_regnames_riscv ();
8780 break;
8781
8782 default:
8783 break;
8784 }
8785 }
8786
8787 static const char *
8788 regname_internal_by_table_only (unsigned int regno)
8789 {
8790 if (dwarf_regnames != NULL
8791 && regno < dwarf_regnames_count
8792 && dwarf_regnames [regno] != NULL)
8793 return dwarf_regnames [regno];
8794
8795 return NULL;
8796 }
8797
8798 static const char *
8799 regname (unsigned int regno, int name_only_p)
8800 {
8801 static char reg[64];
8802
8803 const char *name = NULL;
8804
8805 if (dwarf_regnames_lookup_func != NULL)
8806 name = dwarf_regnames_lookup_func (regno);
8807
8808 if (name != NULL)
8809 {
8810 if (name_only_p)
8811 return name;
8812 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
8813 }
8814 else
8815 snprintf (reg, sizeof (reg), "r%d", regno);
8816 return reg;
8817 }
8818
8819 static void
8820 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
8821 {
8822 unsigned int r;
8823 char tmp[100];
8824
8825 if (*max_regs != fc->ncols)
8826 *max_regs = fc->ncols;
8827
8828 if (*need_col_headers)
8829 {
8830 *need_col_headers = 0;
8831
8832 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
8833
8834 for (r = 0; r < *max_regs; r++)
8835 if (fc->col_type[r] != DW_CFA_unreferenced)
8836 {
8837 if (r == fc->ra)
8838 printf ("ra ");
8839 else
8840 printf ("%-5s ", regname (r, 1));
8841 }
8842
8843 printf ("\n");
8844 }
8845
8846 print_hex (fc->pc_begin, eh_addr_size);
8847 if (fc->cfa_exp)
8848 strcpy (tmp, "exp");
8849 else
8850 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
8851 printf ("%-8s ", tmp);
8852
8853 for (r = 0; r < fc->ncols; r++)
8854 {
8855 if (fc->col_type[r] != DW_CFA_unreferenced)
8856 {
8857 switch (fc->col_type[r])
8858 {
8859 case DW_CFA_undefined:
8860 strcpy (tmp, "u");
8861 break;
8862 case DW_CFA_same_value:
8863 strcpy (tmp, "s");
8864 break;
8865 case DW_CFA_offset:
8866 sprintf (tmp, "c%+" PRId64, fc->col_offset[r]);
8867 break;
8868 case DW_CFA_val_offset:
8869 sprintf (tmp, "v%+" PRId64, fc->col_offset[r]);
8870 break;
8871 case DW_CFA_register:
8872 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
8873 break;
8874 case DW_CFA_expression:
8875 strcpy (tmp, "exp");
8876 break;
8877 case DW_CFA_val_expression:
8878 strcpy (tmp, "vexp");
8879 break;
8880 default:
8881 strcpy (tmp, "n/a");
8882 break;
8883 }
8884 printf ("%-5s ", tmp);
8885 }
8886 }
8887 printf ("\n");
8888 }
8889
8890 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
8891
8892 static unsigned char *
8893 read_cie (unsigned char *start, unsigned char *end,
8894 Frame_Chunk **p_cie, int *p_version,
8895 uint64_t *p_aug_len, unsigned char **p_aug)
8896 {
8897 int version;
8898 Frame_Chunk *fc;
8899 unsigned char *augmentation_data = NULL;
8900 uint64_t augmentation_data_len = 0;
8901
8902 * p_cie = NULL;
8903 /* PR 17512: file: 001-228113-0.004. */
8904 if (start >= end)
8905 return end;
8906
8907 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8908 memset (fc, 0, sizeof (Frame_Chunk));
8909
8910 fc->col_type = xmalloc (sizeof (*fc->col_type));
8911 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
8912
8913 version = *start++;
8914
8915 fc->augmentation = (char *) start;
8916 /* PR 17512: file: 001-228113-0.004.
8917 Skip past augmentation name, but avoid running off the end of the data. */
8918 while (start < end)
8919 if (* start ++ == '\0')
8920 break;
8921 if (start == end)
8922 {
8923 warn (_("No terminator for augmentation name\n"));
8924 goto fail;
8925 }
8926
8927 if (strcmp (fc->augmentation, "eh") == 0)
8928 {
8929 if (eh_addr_size > (size_t) (end - start))
8930 goto fail;
8931 start += eh_addr_size;
8932 }
8933
8934 if (version >= 4)
8935 {
8936 if (2 > (size_t) (end - start))
8937 goto fail;
8938 GET (fc->ptr_size, 1);
8939 if (fc->ptr_size < 1 || fc->ptr_size > 8)
8940 {
8941 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
8942 goto fail;
8943 }
8944
8945 GET (fc->segment_size, 1);
8946 /* PR 17512: file: e99d2804. */
8947 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
8948 {
8949 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
8950 goto fail;
8951 }
8952
8953 eh_addr_size = fc->ptr_size;
8954 }
8955 else
8956 {
8957 fc->ptr_size = eh_addr_size;
8958 fc->segment_size = 0;
8959 }
8960
8961 READ_ULEB (fc->code_factor, start, end);
8962 READ_SLEB (fc->data_factor, start, end);
8963
8964 if (start >= end)
8965 goto fail;
8966
8967 if (version == 1)
8968 {
8969 GET (fc->ra, 1);
8970 }
8971 else
8972 {
8973 READ_ULEB (fc->ra, start, end);
8974 }
8975
8976 if (fc->augmentation[0] == 'z')
8977 {
8978 if (start >= end)
8979 goto fail;
8980 READ_ULEB (augmentation_data_len, start, end);
8981 augmentation_data = start;
8982 /* PR 17512: file: 11042-2589-0.004. */
8983 if (augmentation_data_len > (size_t) (end - start))
8984 {
8985 warn (_("Augmentation data too long: %#" PRIx64
8986 ", expected at most %#tx\n"),
8987 augmentation_data_len, end - start);
8988 goto fail;
8989 }
8990 start += augmentation_data_len;
8991 }
8992
8993 if (augmentation_data_len)
8994 {
8995 unsigned char *p;
8996 unsigned char *q;
8997 unsigned char *qend;
8998
8999 p = (unsigned char *) fc->augmentation + 1;
9000 q = augmentation_data;
9001 qend = q + augmentation_data_len;
9002
9003 while (p < end && q < qend)
9004 {
9005 if (*p == 'L')
9006 q++;
9007 else if (*p == 'P')
9008 q += 1 + size_of_encoded_value (*q);
9009 else if (*p == 'R')
9010 fc->fde_encoding = *q++;
9011 else if (*p == 'S')
9012 ;
9013 else if (*p == 'B')
9014 ;
9015 else
9016 break;
9017 p++;
9018 }
9019 /* Note - it is OK if this loop terminates with q < qend.
9020 Padding may have been inserted to align the end of the CIE. */
9021 }
9022
9023 *p_cie = fc;
9024 if (p_version)
9025 *p_version = version;
9026 if (p_aug_len)
9027 {
9028 *p_aug_len = augmentation_data_len;
9029 *p_aug = augmentation_data;
9030 }
9031 return start;
9032
9033 fail:
9034 free (fc->col_offset);
9035 free (fc->col_type);
9036 free (fc);
9037 return end;
9038 }
9039
9040 /* Prints out the contents on the DATA array formatted as unsigned bytes.
9041 If do_wide is not enabled, then formats the output to fit into 80 columns.
9042 PRINTED contains the number of characters already written to the current
9043 output line. */
9044
9045 static void
9046 display_data (size_t printed, const unsigned char *data, size_t len)
9047 {
9048 if (do_wide || len < ((80 - printed) / 3))
9049 for (printed = 0; printed < len; ++printed)
9050 printf (" %02x", data[printed]);
9051 else
9052 {
9053 for (printed = 0; printed < len; ++printed)
9054 {
9055 if (printed % (80 / 3) == 0)
9056 putchar ('\n');
9057 printf (" %02x", data[printed]);
9058 }
9059 }
9060 }
9061
9062 /* Prints out the contents on the augmentation data array.
9063 If do_wide is not enabled, then formats the output to fit into 80 columns. */
9064
9065 static void
9066 display_augmentation_data (const unsigned char * data, uint64_t len)
9067 {
9068 size_t i;
9069
9070 i = printf (_(" Augmentation data: "));
9071 display_data (i, data, len);
9072 }
9073
9074 static int
9075 display_debug_frames (struct dwarf_section *section,
9076 void *file ATTRIBUTE_UNUSED)
9077 {
9078 unsigned char *start = section->start;
9079 unsigned char *end = start + section->size;
9080 unsigned char *section_start = start;
9081 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
9082 Frame_Chunk *remembered_state = NULL;
9083 Frame_Chunk *rs;
9084 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
9085 unsigned int max_regs = 0;
9086 const char *bad_reg = _("bad register: ");
9087 unsigned int saved_eh_addr_size = eh_addr_size;
9088
9089 introduce (section, false);
9090
9091 while (start < end)
9092 {
9093 unsigned char *saved_start;
9094 unsigned char *block_end;
9095 uint64_t length;
9096 uint64_t cie_id;
9097 Frame_Chunk *fc;
9098 Frame_Chunk *cie;
9099 int need_col_headers = 1;
9100 unsigned char *augmentation_data = NULL;
9101 uint64_t augmentation_data_len = 0;
9102 unsigned int encoded_ptr_size = saved_eh_addr_size;
9103 unsigned int offset_size;
9104 bool all_nops;
9105 static Frame_Chunk fde_fc;
9106
9107 saved_start = start;
9108
9109 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
9110
9111 if (length == 0)
9112 {
9113 printf ("\n%08tx ZERO terminator\n\n",
9114 saved_start - section_start);
9115 /* Skip any zero terminators that directly follow.
9116 A corrupt section size could have loaded a whole
9117 slew of zero filled memory bytes. eg
9118 PR 17512: file: 070-19381-0.004. */
9119 while (start < end && * start == 0)
9120 ++ start;
9121 continue;
9122 }
9123
9124 if (length == 0xffffffff)
9125 {
9126 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
9127 offset_size = 8;
9128 }
9129 else
9130 offset_size = 4;
9131
9132 if (length > (size_t) (end - start))
9133 {
9134 warn ("Invalid length %#" PRIx64 " in FDE at %#tx\n",
9135 length, saved_start - section_start);
9136 block_end = end;
9137 }
9138 else
9139 block_end = start + length;
9140
9141 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
9142
9143 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
9144 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
9145 {
9146 int version;
9147 unsigned int mreg;
9148
9149 start = read_cie (start, block_end, &cie, &version,
9150 &augmentation_data_len, &augmentation_data);
9151 /* PR 17512: file: 027-135133-0.005. */
9152 if (cie == NULL)
9153 break;
9154
9155 fc = cie;
9156 fc->next = chunks;
9157 chunks = fc;
9158 fc->chunk_start = saved_start;
9159 mreg = max_regs > 0 ? max_regs - 1 : 0;
9160 if (mreg < fc->ra)
9161 mreg = fc->ra;
9162 if (frame_need_space (fc, mreg) < 0)
9163 break;
9164 if (fc->fde_encoding)
9165 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9166
9167 printf ("\n%08tx ", saved_start - section_start);
9168 print_hex (length, fc->ptr_size);
9169 print_hex (cie_id, offset_size);
9170
9171 if (do_debug_frames_interp)
9172 {
9173 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
9174 fc->code_factor, fc->data_factor, fc->ra);
9175 }
9176 else
9177 {
9178 printf ("CIE\n");
9179 printf (" Version: %d\n", version);
9180 printf (" Augmentation: \"%s\"\n", fc->augmentation);
9181 if (version >= 4)
9182 {
9183 printf (" Pointer Size: %u\n", fc->ptr_size);
9184 printf (" Segment Size: %u\n", fc->segment_size);
9185 }
9186 printf (" Code alignment factor: %u\n", fc->code_factor);
9187 printf (" Data alignment factor: %d\n", fc->data_factor);
9188 printf (" Return address column: %d\n", fc->ra);
9189
9190 if (augmentation_data_len)
9191 display_augmentation_data (augmentation_data, augmentation_data_len);
9192
9193 putchar ('\n');
9194 }
9195 }
9196 else
9197 {
9198 unsigned char *look_for;
9199 unsigned long segment_selector;
9200 uint64_t cie_off;
9201
9202 cie_off = cie_id;
9203 if (is_eh)
9204 {
9205 uint64_t sign = (uint64_t) 1 << (offset_size * 8 - 1);
9206 cie_off = (cie_off ^ sign) - sign;
9207 cie_off = start - 4 - section_start - cie_off;
9208 }
9209
9210 look_for = section_start + cie_off;
9211 if (cie_off <= (size_t) (saved_start - section_start))
9212 {
9213 for (cie = chunks; cie ; cie = cie->next)
9214 if (cie->chunk_start == look_for)
9215 break;
9216 }
9217 else if (cie_off >= section->size)
9218 cie = NULL;
9219 else
9220 {
9221 for (cie = forward_refs; cie ; cie = cie->next)
9222 if (cie->chunk_start == look_for)
9223 break;
9224 if (!cie)
9225 {
9226 unsigned int off_size;
9227 unsigned char *cie_scan;
9228
9229 cie_scan = look_for;
9230 off_size = 4;
9231 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
9232 if (length == 0xffffffff)
9233 {
9234 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
9235 off_size = 8;
9236 }
9237 if (length != 0 && length <= (size_t) (end - cie_scan))
9238 {
9239 uint64_t c_id;
9240 unsigned char *cie_end = cie_scan + length;
9241
9242 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
9243 cie_end);
9244 if (is_eh
9245 ? c_id == 0
9246 : ((off_size == 4 && c_id == DW_CIE_ID)
9247 || (off_size == 8 && c_id == DW64_CIE_ID)))
9248 {
9249 int version;
9250 unsigned int mreg;
9251
9252 read_cie (cie_scan, cie_end, &cie, &version,
9253 &augmentation_data_len, &augmentation_data);
9254 /* PR 17512: file: 3450-2098-0.004. */
9255 if (cie == NULL)
9256 {
9257 warn (_("Failed to read CIE information\n"));
9258 break;
9259 }
9260 cie->next = forward_refs;
9261 forward_refs = cie;
9262 cie->chunk_start = look_for;
9263 mreg = max_regs > 0 ? max_regs - 1 : 0;
9264 if (mreg < cie->ra)
9265 mreg = cie->ra;
9266 if (frame_need_space (cie, mreg) < 0)
9267 {
9268 warn (_("Invalid max register\n"));
9269 break;
9270 }
9271 if (cie->fde_encoding)
9272 encoded_ptr_size
9273 = size_of_encoded_value (cie->fde_encoding);
9274 }
9275 }
9276 }
9277 }
9278
9279 fc = &fde_fc;
9280 memset (fc, 0, sizeof (Frame_Chunk));
9281
9282 if (!cie)
9283 {
9284 fc->ncols = 0;
9285 fc->col_type = xmalloc (sizeof (*fc->col_type));
9286 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
9287 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
9288 {
9289 warn (_("Invalid max register\n"));
9290 break;
9291 }
9292 cie = fc;
9293 fc->augmentation = "";
9294 fc->fde_encoding = 0;
9295 fc->ptr_size = eh_addr_size;
9296 fc->segment_size = 0;
9297 }
9298 else
9299 {
9300 fc->ncols = cie->ncols;
9301 fc->col_type = xcmalloc (fc->ncols, sizeof (*fc->col_type));
9302 fc->col_offset = xcmalloc (fc->ncols, sizeof (*fc->col_offset));
9303 memcpy (fc->col_type, cie->col_type,
9304 fc->ncols * sizeof (*fc->col_type));
9305 memcpy (fc->col_offset, cie->col_offset,
9306 fc->ncols * sizeof (*fc->col_offset));
9307 fc->augmentation = cie->augmentation;
9308 fc->ptr_size = cie->ptr_size;
9309 eh_addr_size = cie->ptr_size;
9310 fc->segment_size = cie->segment_size;
9311 fc->code_factor = cie->code_factor;
9312 fc->data_factor = cie->data_factor;
9313 fc->cfa_reg = cie->cfa_reg;
9314 fc->cfa_offset = cie->cfa_offset;
9315 fc->ra = cie->ra;
9316 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
9317 {
9318 warn (_("Invalid max register\n"));
9319 break;
9320 }
9321 fc->fde_encoding = cie->fde_encoding;
9322 }
9323
9324 if (fc->fde_encoding)
9325 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9326
9327 segment_selector = 0;
9328 if (fc->segment_size)
9329 {
9330 if (fc->segment_size > sizeof (segment_selector))
9331 {
9332 /* PR 17512: file: 9e196b3e. */
9333 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
9334 fc->segment_size = 4;
9335 }
9336 SAFE_BYTE_GET_AND_INC (segment_selector, start,
9337 fc->segment_size, block_end);
9338 }
9339
9340 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
9341 block_end);
9342
9343 /* FIXME: It appears that sometimes the final pc_range value is
9344 encoded in less than encoded_ptr_size bytes. See the x86_64
9345 run of the "objcopy on compressed debug sections" test for an
9346 example of this. */
9347 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
9348 block_end);
9349
9350 if (cie->augmentation[0] == 'z')
9351 {
9352 READ_ULEB (augmentation_data_len, start, block_end);
9353 augmentation_data = start;
9354 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
9355 if (augmentation_data_len > (size_t) (block_end - start))
9356 {
9357 warn (_("Augmentation data too long: %#" PRIx64 ", "
9358 "expected at most %#tx\n"),
9359 augmentation_data_len, block_end - start);
9360 start = block_end;
9361 augmentation_data = NULL;
9362 augmentation_data_len = 0;
9363 }
9364 start += augmentation_data_len;
9365 }
9366
9367 printf ("\n%08tx ", saved_start - section_start);
9368 print_hex (length, fc->ptr_size);
9369 print_hex (cie_id, offset_size);
9370 printf ("FDE ");
9371
9372 if (cie->chunk_start)
9373 printf ("cie=%08tx", cie->chunk_start - section_start);
9374 else
9375 /* Ideally translate "invalid " to 8 chars, trailing space
9376 is optional. */
9377 printf (_("cie=invalid "));
9378
9379 printf (" pc=");
9380 if (fc->segment_size)
9381 printf ("%04lx:", segment_selector);
9382
9383 print_hex_ns (fc->pc_begin, fc->ptr_size);
9384 printf ("..");
9385 print_hex_ns (fc->pc_begin + fc->pc_range, fc->ptr_size);
9386 printf ("\n");
9387
9388 if (! do_debug_frames_interp && augmentation_data_len)
9389 {
9390 display_augmentation_data (augmentation_data, augmentation_data_len);
9391 putchar ('\n');
9392 }
9393 }
9394
9395 /* At this point, fc is the current chunk, cie (if any) is set, and
9396 we're about to interpret instructions for the chunk. */
9397 /* ??? At present we need to do this always, since this sizes the
9398 fc->col_type and fc->col_offset arrays, which we write into always.
9399 We should probably split the interpreted and non-interpreted bits
9400 into two different routines, since there's so much that doesn't
9401 really overlap between them. */
9402 if (1 || do_debug_frames_interp)
9403 {
9404 /* Start by making a pass over the chunk, allocating storage
9405 and taking note of what registers are used. */
9406 unsigned char *tmp = start;
9407
9408 while (start < block_end)
9409 {
9410 unsigned int reg, op, opa;
9411 unsigned long temp;
9412
9413 op = *start++;
9414 opa = op & 0x3f;
9415 if (op & 0xc0)
9416 op &= 0xc0;
9417
9418 /* Warning: if you add any more cases to this switch, be
9419 sure to add them to the corresponding switch below. */
9420 reg = -1u;
9421 switch (op)
9422 {
9423 case DW_CFA_advance_loc:
9424 break;
9425 case DW_CFA_offset:
9426 SKIP_ULEB (start, block_end);
9427 reg = opa;
9428 break;
9429 case DW_CFA_restore:
9430 reg = opa;
9431 break;
9432 case DW_CFA_set_loc:
9433 if ((size_t) (block_end - start) < encoded_ptr_size)
9434 start = block_end;
9435 else
9436 start += encoded_ptr_size;
9437 break;
9438 case DW_CFA_advance_loc1:
9439 if ((size_t) (block_end - start) < 1)
9440 start = block_end;
9441 else
9442 start += 1;
9443 break;
9444 case DW_CFA_advance_loc2:
9445 if ((size_t) (block_end - start) < 2)
9446 start = block_end;
9447 else
9448 start += 2;
9449 break;
9450 case DW_CFA_advance_loc4:
9451 if ((size_t) (block_end - start) < 4)
9452 start = block_end;
9453 else
9454 start += 4;
9455 break;
9456 case DW_CFA_offset_extended:
9457 case DW_CFA_val_offset:
9458 READ_ULEB (reg, start, block_end);
9459 SKIP_ULEB (start, block_end);
9460 break;
9461 case DW_CFA_restore_extended:
9462 READ_ULEB (reg, start, block_end);
9463 break;
9464 case DW_CFA_undefined:
9465 READ_ULEB (reg, start, block_end);
9466 break;
9467 case DW_CFA_same_value:
9468 READ_ULEB (reg, start, block_end);
9469 break;
9470 case DW_CFA_register:
9471 READ_ULEB (reg, start, block_end);
9472 SKIP_ULEB (start, block_end);
9473 break;
9474 case DW_CFA_def_cfa:
9475 SKIP_ULEB (start, block_end);
9476 SKIP_ULEB (start, block_end);
9477 break;
9478 case DW_CFA_def_cfa_register:
9479 SKIP_ULEB (start, block_end);
9480 break;
9481 case DW_CFA_def_cfa_offset:
9482 SKIP_ULEB (start, block_end);
9483 break;
9484 case DW_CFA_def_cfa_expression:
9485 READ_ULEB (temp, start, block_end);
9486 if ((size_t) (block_end - start) < temp)
9487 start = block_end;
9488 else
9489 start += temp;
9490 break;
9491 case DW_CFA_expression:
9492 case DW_CFA_val_expression:
9493 READ_ULEB (reg, start, block_end);
9494 READ_ULEB (temp, start, block_end);
9495 if ((size_t) (block_end - start) < temp)
9496 start = block_end;
9497 else
9498 start += temp;
9499 break;
9500 case DW_CFA_offset_extended_sf:
9501 case DW_CFA_val_offset_sf:
9502 READ_ULEB (reg, start, block_end);
9503 SKIP_SLEB (start, block_end);
9504 break;
9505 case DW_CFA_def_cfa_sf:
9506 SKIP_ULEB (start, block_end);
9507 SKIP_SLEB (start, block_end);
9508 break;
9509 case DW_CFA_def_cfa_offset_sf:
9510 SKIP_SLEB (start, block_end);
9511 break;
9512 case DW_CFA_MIPS_advance_loc8:
9513 if ((size_t) (block_end - start) < 8)
9514 start = block_end;
9515 else
9516 start += 8;
9517 break;
9518 case DW_CFA_GNU_args_size:
9519 SKIP_ULEB (start, block_end);
9520 break;
9521 case DW_CFA_GNU_negative_offset_extended:
9522 READ_ULEB (reg, start, block_end);
9523 SKIP_ULEB (start, block_end);
9524 break;
9525 default:
9526 break;
9527 }
9528 if (reg != -1u && frame_need_space (fc, reg) >= 0)
9529 {
9530 /* Don't leave any reg as DW_CFA_unreferenced so
9531 that frame_display_row prints name of regs in
9532 header, and all referenced regs in each line. */
9533 if (reg >= cie->ncols
9534 || cie->col_type[reg] == DW_CFA_unreferenced)
9535 fc->col_type[reg] = DW_CFA_undefined;
9536 else
9537 fc->col_type[reg] = cie->col_type[reg];
9538 }
9539 }
9540 start = tmp;
9541 }
9542
9543 all_nops = true;
9544
9545 /* Now we know what registers are used, make a second pass over
9546 the chunk, this time actually printing out the info. */
9547
9548 while (start < block_end)
9549 {
9550 unsigned op, opa;
9551 /* Note: It is tempting to use an unsigned long for 'reg' but there
9552 are various functions, notably frame_space_needed() that assume that
9553 reg is an unsigned int. */
9554 unsigned int reg;
9555 int64_t sofs;
9556 uint64_t ofs;
9557 const char *reg_prefix = "";
9558
9559 op = *start++;
9560 opa = op & 0x3f;
9561 if (op & 0xc0)
9562 op &= 0xc0;
9563
9564 /* Make a note if something other than DW_CFA_nop happens. */
9565 if (op != DW_CFA_nop)
9566 all_nops = false;
9567
9568 /* Warning: if you add any more cases to this switch, be
9569 sure to add them to the corresponding switch above. */
9570 switch (op)
9571 {
9572 case DW_CFA_advance_loc:
9573 opa *= fc->code_factor;
9574 if (do_debug_frames_interp)
9575 frame_display_row (fc, &need_col_headers, &max_regs);
9576 else
9577 {
9578 printf (" DW_CFA_advance_loc: %d to ", opa);
9579 print_hex_ns (fc->pc_begin + opa, fc->ptr_size);
9580 printf ("\n");
9581 }
9582 fc->pc_begin += opa;
9583 break;
9584
9585 case DW_CFA_offset:
9586 READ_ULEB (ofs, start, block_end);
9587 ofs *= fc->data_factor;
9588 if (opa >= fc->ncols)
9589 reg_prefix = bad_reg;
9590 if (! do_debug_frames_interp || *reg_prefix != '\0')
9591 printf (" DW_CFA_offset: %s%s at cfa%+" PRId64 "\n",
9592 reg_prefix, regname (opa, 0), ofs);
9593 if (*reg_prefix == '\0')
9594 {
9595 fc->col_type[opa] = DW_CFA_offset;
9596 fc->col_offset[opa] = ofs;
9597 }
9598 break;
9599
9600 case DW_CFA_restore:
9601 if (opa >= fc->ncols)
9602 reg_prefix = bad_reg;
9603 if (! do_debug_frames_interp || *reg_prefix != '\0')
9604 printf (" DW_CFA_restore: %s%s\n",
9605 reg_prefix, regname (opa, 0));
9606 if (*reg_prefix != '\0')
9607 break;
9608
9609 if (opa >= cie->ncols
9610 || cie->col_type[opa] == DW_CFA_unreferenced)
9611 {
9612 fc->col_type[opa] = DW_CFA_undefined;
9613 fc->col_offset[opa] = 0;
9614 }
9615 else
9616 {
9617 fc->col_type[opa] = cie->col_type[opa];
9618 fc->col_offset[opa] = cie->col_offset[opa];
9619 }
9620 break;
9621
9622 case DW_CFA_set_loc:
9623 ofs = get_encoded_value (&start, fc->fde_encoding, section,
9624 block_end);
9625 if (do_debug_frames_interp)
9626 frame_display_row (fc, &need_col_headers, &max_regs);
9627 else
9628 {
9629 printf (" DW_CFA_set_loc: ");
9630 print_hex_ns (ofs, fc->ptr_size);
9631 printf ("\n");
9632 }
9633 fc->pc_begin = ofs;
9634 break;
9635
9636 case DW_CFA_advance_loc1:
9637 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9638 ofs *= fc->code_factor;
9639 if (do_debug_frames_interp)
9640 frame_display_row (fc, &need_col_headers, &max_regs);
9641 else
9642 {
9643 printf (" DW_CFA_advance_loc1: %" PRId64 " to ", ofs);
9644 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9645 printf ("\n");
9646 }
9647 fc->pc_begin += ofs;
9648 break;
9649
9650 case DW_CFA_advance_loc2:
9651 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9652 ofs *= fc->code_factor;
9653 if (do_debug_frames_interp)
9654 frame_display_row (fc, &need_col_headers, &max_regs);
9655 else
9656 {
9657 printf (" DW_CFA_advance_loc2: %" PRId64 " to ", ofs);
9658 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9659 printf ("\n");
9660 }
9661 fc->pc_begin += ofs;
9662 break;
9663
9664 case DW_CFA_advance_loc4:
9665 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9666 ofs *= fc->code_factor;
9667 if (do_debug_frames_interp)
9668 frame_display_row (fc, &need_col_headers, &max_regs);
9669 else
9670 {
9671 printf (" DW_CFA_advance_loc4: %" PRId64 " to ", ofs);
9672 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9673 printf ("\n");
9674 }
9675 fc->pc_begin += ofs;
9676 break;
9677
9678 case DW_CFA_offset_extended:
9679 READ_ULEB (reg, start, block_end);
9680 READ_ULEB (ofs, start, block_end);
9681 ofs *= fc->data_factor;
9682 if (reg >= fc->ncols)
9683 reg_prefix = bad_reg;
9684 if (! do_debug_frames_interp || *reg_prefix != '\0')
9685 printf (" DW_CFA_offset_extended: %s%s at cfa%+" PRId64 "\n",
9686 reg_prefix, regname (reg, 0), ofs);
9687 if (*reg_prefix == '\0')
9688 {
9689 fc->col_type[reg] = DW_CFA_offset;
9690 fc->col_offset[reg] = ofs;
9691 }
9692 break;
9693
9694 case DW_CFA_val_offset:
9695 READ_ULEB (reg, start, block_end);
9696 READ_ULEB (ofs, start, block_end);
9697 ofs *= fc->data_factor;
9698 if (reg >= fc->ncols)
9699 reg_prefix = bad_reg;
9700 if (! do_debug_frames_interp || *reg_prefix != '\0')
9701 printf (" DW_CFA_val_offset: %s%s is cfa%+" PRId64 "\n",
9702 reg_prefix, regname (reg, 0), ofs);
9703 if (*reg_prefix == '\0')
9704 {
9705 fc->col_type[reg] = DW_CFA_val_offset;
9706 fc->col_offset[reg] = ofs;
9707 }
9708 break;
9709
9710 case DW_CFA_restore_extended:
9711 READ_ULEB (reg, start, block_end);
9712 if (reg >= fc->ncols)
9713 reg_prefix = bad_reg;
9714 if (! do_debug_frames_interp || *reg_prefix != '\0')
9715 printf (" DW_CFA_restore_extended: %s%s\n",
9716 reg_prefix, regname (reg, 0));
9717 if (*reg_prefix != '\0')
9718 break;
9719
9720 if (reg >= cie->ncols
9721 || cie->col_type[reg] == DW_CFA_unreferenced)
9722 {
9723 fc->col_type[reg] = DW_CFA_undefined;
9724 fc->col_offset[reg] = 0;
9725 }
9726 else
9727 {
9728 fc->col_type[reg] = cie->col_type[reg];
9729 fc->col_offset[reg] = cie->col_offset[reg];
9730 }
9731 break;
9732
9733 case DW_CFA_undefined:
9734 READ_ULEB (reg, start, block_end);
9735 if (reg >= fc->ncols)
9736 reg_prefix = bad_reg;
9737 if (! do_debug_frames_interp || *reg_prefix != '\0')
9738 printf (" DW_CFA_undefined: %s%s\n",
9739 reg_prefix, regname (reg, 0));
9740 if (*reg_prefix == '\0')
9741 {
9742 fc->col_type[reg] = DW_CFA_undefined;
9743 fc->col_offset[reg] = 0;
9744 }
9745 break;
9746
9747 case DW_CFA_same_value:
9748 READ_ULEB (reg, start, block_end);
9749 if (reg >= fc->ncols)
9750 reg_prefix = bad_reg;
9751 if (! do_debug_frames_interp || *reg_prefix != '\0')
9752 printf (" DW_CFA_same_value: %s%s\n",
9753 reg_prefix, regname (reg, 0));
9754 if (*reg_prefix == '\0')
9755 {
9756 fc->col_type[reg] = DW_CFA_same_value;
9757 fc->col_offset[reg] = 0;
9758 }
9759 break;
9760
9761 case DW_CFA_register:
9762 READ_ULEB (reg, start, block_end);
9763 READ_ULEB (ofs, start, block_end);
9764 if (reg >= fc->ncols)
9765 reg_prefix = bad_reg;
9766 if (! do_debug_frames_interp || *reg_prefix != '\0')
9767 {
9768 printf (" DW_CFA_register: %s%s in ",
9769 reg_prefix, regname (reg, 0));
9770 puts (regname (ofs, 0));
9771 }
9772 if (*reg_prefix == '\0')
9773 {
9774 fc->col_type[reg] = DW_CFA_register;
9775 fc->col_offset[reg] = ofs;
9776 }
9777 break;
9778
9779 case DW_CFA_remember_state:
9780 if (! do_debug_frames_interp)
9781 printf (" DW_CFA_remember_state\n");
9782 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9783 rs->cfa_offset = fc->cfa_offset;
9784 rs->cfa_reg = fc->cfa_reg;
9785 rs->ra = fc->ra;
9786 rs->cfa_exp = fc->cfa_exp;
9787 rs->ncols = fc->ncols;
9788 rs->col_type = xcmalloc (rs->ncols, sizeof (*rs->col_type));
9789 rs->col_offset = xcmalloc (rs->ncols, sizeof (*rs->col_offset));
9790 memcpy (rs->col_type, fc->col_type,
9791 rs->ncols * sizeof (*fc->col_type));
9792 memcpy (rs->col_offset, fc->col_offset,
9793 rs->ncols * sizeof (*fc->col_offset));
9794 rs->next = remembered_state;
9795 remembered_state = rs;
9796 break;
9797
9798 case DW_CFA_restore_state:
9799 if (! do_debug_frames_interp)
9800 printf (" DW_CFA_restore_state\n");
9801 rs = remembered_state;
9802 if (rs)
9803 {
9804 remembered_state = rs->next;
9805 fc->cfa_offset = rs->cfa_offset;
9806 fc->cfa_reg = rs->cfa_reg;
9807 fc->ra = rs->ra;
9808 fc->cfa_exp = rs->cfa_exp;
9809 if (frame_need_space (fc, rs->ncols - 1) < 0)
9810 {
9811 warn (_("Invalid column number in saved frame state\n"));
9812 fc->ncols = 0;
9813 break;
9814 }
9815 memcpy (fc->col_type, rs->col_type,
9816 rs->ncols * sizeof (*rs->col_type));
9817 memcpy (fc->col_offset, rs->col_offset,
9818 rs->ncols * sizeof (*rs->col_offset));
9819 free (rs->col_type);
9820 free (rs->col_offset);
9821 free (rs);
9822 }
9823 else if (do_debug_frames_interp)
9824 printf ("Mismatched DW_CFA_restore_state\n");
9825 break;
9826
9827 case DW_CFA_def_cfa:
9828 READ_ULEB (fc->cfa_reg, start, block_end);
9829 READ_ULEB (fc->cfa_offset, start, block_end);
9830 fc->cfa_exp = 0;
9831 if (! do_debug_frames_interp)
9832 printf (" DW_CFA_def_cfa: %s ofs %d\n",
9833 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
9834 break;
9835
9836 case DW_CFA_def_cfa_register:
9837 READ_ULEB (fc->cfa_reg, start, block_end);
9838 fc->cfa_exp = 0;
9839 if (! do_debug_frames_interp)
9840 printf (" DW_CFA_def_cfa_register: %s\n",
9841 regname (fc->cfa_reg, 0));
9842 break;
9843
9844 case DW_CFA_def_cfa_offset:
9845 READ_ULEB (fc->cfa_offset, start, block_end);
9846 if (! do_debug_frames_interp)
9847 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
9848 break;
9849
9850 case DW_CFA_nop:
9851 if (! do_debug_frames_interp)
9852 printf (" DW_CFA_nop\n");
9853 break;
9854
9855 case DW_CFA_def_cfa_expression:
9856 READ_ULEB (ofs, start, block_end);
9857 if (ofs > (size_t) (block_end - start))
9858 {
9859 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
9860 "DW_CFA_def_cfa_expression", ofs);
9861 break;
9862 }
9863 if (! do_debug_frames_interp)
9864 {
9865 printf (" DW_CFA_def_cfa_expression (");
9866 decode_location_expression (start, eh_addr_size, 0, -1,
9867 ofs, 0, section);
9868 printf (")\n");
9869 }
9870 fc->cfa_exp = 1;
9871 start += ofs;
9872 break;
9873
9874 case DW_CFA_expression:
9875 READ_ULEB (reg, start, block_end);
9876 READ_ULEB (ofs, start, block_end);
9877 if (reg >= fc->ncols)
9878 reg_prefix = bad_reg;
9879 /* PR 17512: file: 069-133014-0.006. */
9880 /* PR 17512: file: 98c02eb4. */
9881 if (ofs > (size_t) (block_end - start))
9882 {
9883 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
9884 "DW_CFA_expression", ofs);
9885 break;
9886 }
9887 if (! do_debug_frames_interp || *reg_prefix != '\0')
9888 {
9889 printf (" DW_CFA_expression: %s%s (",
9890 reg_prefix, regname (reg, 0));
9891 decode_location_expression (start, eh_addr_size, 0, -1,
9892 ofs, 0, section);
9893 printf (")\n");
9894 }
9895 if (*reg_prefix == '\0')
9896 fc->col_type[reg] = DW_CFA_expression;
9897 start += ofs;
9898 break;
9899
9900 case DW_CFA_val_expression:
9901 READ_ULEB (reg, start, block_end);
9902 READ_ULEB (ofs, start, block_end);
9903 if (reg >= fc->ncols)
9904 reg_prefix = bad_reg;
9905 if (ofs > (size_t) (block_end - start))
9906 {
9907 printf (" %s: <corrupt len %" PRIu64 ">\n",
9908 "DW_CFA_val_expression", ofs);
9909 break;
9910 }
9911 if (! do_debug_frames_interp || *reg_prefix != '\0')
9912 {
9913 printf (" DW_CFA_val_expression: %s%s (",
9914 reg_prefix, regname (reg, 0));
9915 decode_location_expression (start, eh_addr_size, 0, -1,
9916 ofs, 0, section);
9917 printf (")\n");
9918 }
9919 if (*reg_prefix == '\0')
9920 fc->col_type[reg] = DW_CFA_val_expression;
9921 start += ofs;
9922 break;
9923
9924 case DW_CFA_offset_extended_sf:
9925 READ_ULEB (reg, start, block_end);
9926 READ_SLEB (sofs, start, block_end);
9927 /* data_factor multiplicaton done here as unsigned to
9928 avoid integer overflow warnings from asan on fuzzed
9929 objects. */
9930 ofs = sofs;
9931 ofs *= fc->data_factor;
9932 if (reg >= fc->ncols)
9933 reg_prefix = bad_reg;
9934 if (! do_debug_frames_interp || *reg_prefix != '\0')
9935 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+" PRId64 "\n",
9936 reg_prefix, regname (reg, 0), ofs);
9937 if (*reg_prefix == '\0')
9938 {
9939 fc->col_type[reg] = DW_CFA_offset;
9940 fc->col_offset[reg] = ofs;
9941 }
9942 break;
9943
9944 case DW_CFA_val_offset_sf:
9945 READ_ULEB (reg, start, block_end);
9946 READ_SLEB (sofs, start, block_end);
9947 ofs = sofs;
9948 ofs *= fc->data_factor;
9949 if (reg >= fc->ncols)
9950 reg_prefix = bad_reg;
9951 if (! do_debug_frames_interp || *reg_prefix != '\0')
9952 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+" PRId64 "\n",
9953 reg_prefix, regname (reg, 0), ofs);
9954 if (*reg_prefix == '\0')
9955 {
9956 fc->col_type[reg] = DW_CFA_val_offset;
9957 fc->col_offset[reg] = ofs;
9958 }
9959 break;
9960
9961 case DW_CFA_def_cfa_sf:
9962 READ_ULEB (fc->cfa_reg, start, block_end);
9963 READ_SLEB (sofs, start, block_end);
9964 ofs = sofs;
9965 ofs *= fc->data_factor;
9966 fc->cfa_offset = ofs;
9967 fc->cfa_exp = 0;
9968 if (! do_debug_frames_interp)
9969 printf (" DW_CFA_def_cfa_sf: %s ofs %" PRId64 "\n",
9970 regname (fc->cfa_reg, 0), ofs);
9971 break;
9972
9973 case DW_CFA_def_cfa_offset_sf:
9974 READ_SLEB (sofs, start, block_end);
9975 ofs = sofs;
9976 ofs *= fc->data_factor;
9977 fc->cfa_offset = ofs;
9978 if (! do_debug_frames_interp)
9979 printf (" DW_CFA_def_cfa_offset_sf: %" PRId64 "\n", ofs);
9980 break;
9981
9982 case DW_CFA_MIPS_advance_loc8:
9983 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
9984 ofs *= fc->code_factor;
9985 if (do_debug_frames_interp)
9986 frame_display_row (fc, &need_col_headers, &max_regs);
9987 else
9988 {
9989 printf (" DW_CFA_MIPS_advance_loc8: %" PRId64 " to ", ofs);
9990 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9991 printf ("\n");
9992 }
9993 fc->pc_begin += ofs;
9994 break;
9995
9996 case DW_CFA_GNU_window_save:
9997 if (! do_debug_frames_interp)
9998 printf (" %s\n", DW_CFA_GNU_window_save_name[is_aarch64]);
9999 break;
10000
10001 case DW_CFA_GNU_args_size:
10002 READ_ULEB (ofs, start, block_end);
10003 if (! do_debug_frames_interp)
10004 printf (" DW_CFA_GNU_args_size: %" PRIu64 "\n", ofs);
10005 break;
10006
10007 case DW_CFA_GNU_negative_offset_extended:
10008 READ_ULEB (reg, start, block_end);
10009 READ_SLEB (sofs, start, block_end);
10010 ofs = sofs;
10011 ofs = -ofs * fc->data_factor;
10012 if (reg >= fc->ncols)
10013 reg_prefix = bad_reg;
10014 if (! do_debug_frames_interp || *reg_prefix != '\0')
10015 printf (" DW_CFA_GNU_negative_offset_extended: %s%s "
10016 "at cfa%+" PRId64 "\n",
10017 reg_prefix, regname (reg, 0), ofs);
10018 if (*reg_prefix == '\0')
10019 {
10020 fc->col_type[reg] = DW_CFA_offset;
10021 fc->col_offset[reg] = ofs;
10022 }
10023 break;
10024
10025 default:
10026 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
10027 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
10028 else
10029 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
10030 start = block_end;
10031 }
10032 }
10033
10034 /* Interpret the CFA - as long as it is not completely full of NOPs. */
10035 if (do_debug_frames_interp && ! all_nops)
10036 frame_display_row (fc, &need_col_headers, &max_regs);
10037
10038 if (fde_fc.col_type != NULL)
10039 {
10040 free (fde_fc.col_type);
10041 fde_fc.col_type = NULL;
10042 }
10043 if (fde_fc.col_offset != NULL)
10044 {
10045 free (fde_fc.col_offset);
10046 fde_fc.col_offset = NULL;
10047 }
10048
10049 start = block_end;
10050 eh_addr_size = saved_eh_addr_size;
10051 }
10052
10053 printf ("\n");
10054
10055 while (remembered_state != NULL)
10056 {
10057 rs = remembered_state;
10058 remembered_state = rs->next;
10059 free (rs->col_type);
10060 free (rs->col_offset);
10061 rs->next = NULL; /* Paranoia. */
10062 free (rs);
10063 }
10064
10065 while (chunks != NULL)
10066 {
10067 rs = chunks;
10068 chunks = rs->next;
10069 free (rs->col_type);
10070 free (rs->col_offset);
10071 rs->next = NULL; /* Paranoia. */
10072 free (rs);
10073 }
10074
10075 while (forward_refs != NULL)
10076 {
10077 rs = forward_refs;
10078 forward_refs = rs->next;
10079 free (rs->col_type);
10080 free (rs->col_offset);
10081 rs->next = NULL; /* Paranoia. */
10082 free (rs);
10083 }
10084
10085 return 1;
10086 }
10087
10088 #undef GET
10089
10090 static int
10091 display_debug_names (struct dwarf_section *section, void *file)
10092 {
10093 unsigned char *hdrptr = section->start;
10094 uint64_t unit_length;
10095 unsigned char *unit_start;
10096 const unsigned char *const section_end = section->start + section->size;
10097 unsigned char *unit_end;
10098
10099 introduce (section, false);
10100
10101 load_debug_section_with_follow (str, file);
10102
10103 for (; hdrptr < section_end; hdrptr = unit_end)
10104 {
10105 unsigned int offset_size;
10106 uint16_t dwarf_version, padding;
10107 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
10108 uint64_t bucket_count, name_count, abbrev_table_size;
10109 uint32_t augmentation_string_size;
10110 unsigned int i;
10111 bool augmentation_printable;
10112 const char *augmentation_string;
10113 size_t total;
10114
10115 unit_start = hdrptr;
10116
10117 /* Get and check the length of the block. */
10118 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
10119
10120 if (unit_length == 0xffffffff)
10121 {
10122 /* This section is 64-bit DWARF. */
10123 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
10124 offset_size = 8;
10125 }
10126 else
10127 offset_size = 4;
10128
10129 if (unit_length > (size_t) (section_end - hdrptr)
10130 || unit_length < 2 + 2 + 4 * 7)
10131 {
10132 too_short:
10133 warn (_("Debug info is corrupted, %s header at %#tx"
10134 " has length %#" PRIx64 "\n"),
10135 section->name, unit_start - section->start, unit_length);
10136 return 0;
10137 }
10138 unit_end = hdrptr + unit_length;
10139
10140 /* Get and check the version number. */
10141 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
10142 printf (_("Version %d\n"), (int) dwarf_version);
10143
10144 /* Prior versions did not exist, and future versions may not be
10145 backwards compatible. */
10146 if (dwarf_version != 5)
10147 {
10148 warn (_("Only DWARF version 5 .debug_names "
10149 "is currently supported.\n"));
10150 return 0;
10151 }
10152
10153 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
10154 if (padding != 0)
10155 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
10156 padding);
10157
10158 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
10159 if (comp_unit_count == 0)
10160 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
10161
10162 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
10163 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
10164 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
10165 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
10166 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
10167
10168 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
10169 if (augmentation_string_size % 4 != 0)
10170 {
10171 warn (_("Augmentation string length %u must be rounded up "
10172 "to a multiple of 4 in .debug_names.\n"),
10173 augmentation_string_size);
10174 augmentation_string_size += (-augmentation_string_size) & 3;
10175 }
10176 if (augmentation_string_size > (size_t) (unit_end - hdrptr))
10177 goto too_short;
10178
10179 printf (_("Augmentation string:"));
10180
10181 augmentation_printable = true;
10182 augmentation_string = (const char *) hdrptr;
10183
10184 for (i = 0; i < augmentation_string_size; i++)
10185 {
10186 unsigned char uc;
10187
10188 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
10189 printf (" %02x", uc);
10190
10191 if (uc != 0 && !ISPRINT (uc))
10192 augmentation_printable = false;
10193 }
10194
10195 if (augmentation_printable)
10196 {
10197 printf (" (\"");
10198 for (i = 0;
10199 i < augmentation_string_size && augmentation_string[i];
10200 ++i)
10201 putchar (augmentation_string[i]);
10202 printf ("\")");
10203 }
10204 putchar ('\n');
10205
10206 printf (_("CU table:\n"));
10207 if (_mul_overflow (comp_unit_count, offset_size, &total)
10208 || total > (size_t) (unit_end - hdrptr))
10209 goto too_short;
10210 for (i = 0; i < comp_unit_count; i++)
10211 {
10212 uint64_t cu_offset;
10213
10214 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
10215 printf ("[%3u] %#" PRIx64 "\n", i, cu_offset);
10216 }
10217 putchar ('\n');
10218
10219 printf (_("TU table:\n"));
10220 if (_mul_overflow (local_type_unit_count, offset_size, &total)
10221 || total > (size_t) (unit_end - hdrptr))
10222 goto too_short;
10223 for (i = 0; i < local_type_unit_count; i++)
10224 {
10225 uint64_t tu_offset;
10226
10227 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
10228 printf ("[%3u] %#" PRIx64 "\n", i, tu_offset);
10229 }
10230 putchar ('\n');
10231
10232 printf (_("Foreign TU table:\n"));
10233 if (_mul_overflow (foreign_type_unit_count, 8, &total)
10234 || total > (size_t) (unit_end - hdrptr))
10235 goto too_short;
10236 for (i = 0; i < foreign_type_unit_count; i++)
10237 {
10238 uint64_t signature;
10239
10240 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
10241 printf (_("[%3u] "), i);
10242 print_hex_ns (signature, 8);
10243 putchar ('\n');
10244 }
10245 putchar ('\n');
10246
10247 uint64_t xtra = (bucket_count * sizeof (uint32_t)
10248 + name_count * (sizeof (uint32_t) + 2 * offset_size)
10249 + abbrev_table_size);
10250 if (xtra > (size_t) (unit_end - hdrptr))
10251 {
10252 warn (_("Entry pool offset (%#" PRIx64 ") exceeds unit size %#tx "
10253 "for unit %#tx in the debug_names\n"),
10254 xtra, unit_end - unit_start, unit_start - section->start);
10255 return 0;
10256 }
10257 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
10258 hdrptr += bucket_count * sizeof (uint32_t);
10259 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
10260 hdrptr += name_count * sizeof (uint32_t);
10261 unsigned char *const name_table_string_offsets = hdrptr;
10262 hdrptr += name_count * offset_size;
10263 unsigned char *const name_table_entry_offsets = hdrptr;
10264 hdrptr += name_count * offset_size;
10265 unsigned char *const abbrev_table = hdrptr;
10266 hdrptr += abbrev_table_size;
10267 const unsigned char *const abbrev_table_end = hdrptr;
10268 unsigned char *const entry_pool = hdrptr;
10269
10270 size_t buckets_filled = 0;
10271 size_t bucketi;
10272 for (bucketi = 0; bucketi < bucket_count; bucketi++)
10273 {
10274 const uint32_t bucket = hash_table_buckets[bucketi];
10275
10276 if (bucket != 0)
10277 ++buckets_filled;
10278 }
10279 printf (ngettext ("Used %zu of %lu bucket.\n",
10280 "Used %zu of %lu buckets.\n",
10281 (unsigned long) bucket_count),
10282 buckets_filled, (unsigned long) bucket_count);
10283
10284 if (bucket_count != 0)
10285 {
10286 uint32_t hash_prev = 0;
10287 size_t hash_clash_count = 0;
10288 size_t longest_clash = 0;
10289 size_t this_length = 0;
10290 size_t hashi;
10291 for (hashi = 0; hashi < name_count; hashi++)
10292 {
10293 const uint32_t hash_this = hash_table_hashes[hashi];
10294
10295 if (hashi > 0)
10296 {
10297 if (hash_prev % bucket_count == hash_this % bucket_count)
10298 {
10299 ++hash_clash_count;
10300 ++this_length;
10301 longest_clash = MAX (longest_clash, this_length);
10302 }
10303 else
10304 this_length = 0;
10305 }
10306 hash_prev = hash_this;
10307 }
10308 printf (_("Out of %" PRIu64 " items there are %zu bucket clashes"
10309 " (longest of %zu entries).\n"),
10310 name_count, hash_clash_count, longest_clash);
10311
10312 if (name_count != buckets_filled + hash_clash_count)
10313 warn (_("The name_count (%" PRIu64 ")"
10314 " is not the same as the used bucket_count"
10315 " (%zu) + the hash clash count (%zu)"),
10316 name_count, buckets_filled, hash_clash_count);
10317 }
10318
10319 struct abbrev_lookup_entry
10320 {
10321 uint64_t abbrev_tag;
10322 unsigned char *abbrev_lookup_ptr;
10323 };
10324 struct abbrev_lookup_entry *abbrev_lookup = NULL;
10325 size_t abbrev_lookup_used = 0;
10326 size_t abbrev_lookup_allocated = 0;
10327
10328 unsigned char *abbrevptr = abbrev_table;
10329 for (;;)
10330 {
10331 uint64_t abbrev_tag;
10332
10333 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
10334 if (abbrev_tag == 0)
10335 break;
10336 if (abbrev_lookup_used == abbrev_lookup_allocated)
10337 {
10338 abbrev_lookup_allocated = MAX (0x100,
10339 abbrev_lookup_allocated * 2);
10340 abbrev_lookup = xrealloc (abbrev_lookup,
10341 (abbrev_lookup_allocated
10342 * sizeof (*abbrev_lookup)));
10343 }
10344 assert (abbrev_lookup_used < abbrev_lookup_allocated);
10345 struct abbrev_lookup_entry *entry;
10346 for (entry = abbrev_lookup;
10347 entry < abbrev_lookup + abbrev_lookup_used;
10348 entry++)
10349 if (entry->abbrev_tag == abbrev_tag)
10350 {
10351 warn (_("Duplicate abbreviation tag %" PRIu64
10352 " in unit %#tx in the debug_names section\n"),
10353 abbrev_tag, unit_start - section->start);
10354 break;
10355 }
10356 entry = &abbrev_lookup[abbrev_lookup_used++];
10357 entry->abbrev_tag = abbrev_tag;
10358 entry->abbrev_lookup_ptr = abbrevptr;
10359
10360 /* Skip DWARF tag. */
10361 SKIP_ULEB (abbrevptr, abbrev_table_end);
10362 for (;;)
10363 {
10364 uint64_t xindex, form;
10365
10366 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10367 READ_ULEB (form, abbrevptr, abbrev_table_end);
10368 if (xindex == 0 && form == 0)
10369 break;
10370 }
10371 }
10372
10373 printf (_("\nSymbol table:\n"));
10374 uint32_t namei;
10375 for (namei = 0; namei < name_count; ++namei)
10376 {
10377 uint64_t string_offset, entry_offset;
10378 unsigned char *p;
10379
10380 p = name_table_string_offsets + namei * offset_size;
10381 SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
10382 p = name_table_entry_offsets + namei * offset_size;
10383 SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
10384
10385 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
10386 fetch_indirect_string (string_offset));
10387
10388 unsigned char *entryptr = entry_pool + entry_offset;
10389
10390 /* We need to scan first whether there is a single or multiple
10391 entries. TAGNO is -2 for the first entry, it is -1 for the
10392 initial tag read of the second entry, then it becomes 0 for the
10393 first entry for real printing etc. */
10394 int tagno = -2;
10395 /* Initialize it due to a false compiler warning. */
10396 uint64_t second_abbrev_tag = -1;
10397 for (;;)
10398 {
10399 uint64_t abbrev_tag;
10400 uint64_t dwarf_tag;
10401 const struct abbrev_lookup_entry *entry;
10402
10403 READ_ULEB (abbrev_tag, entryptr, unit_end);
10404 if (tagno == -1)
10405 {
10406 second_abbrev_tag = abbrev_tag;
10407 tagno = 0;
10408 entryptr = entry_pool + entry_offset;
10409 continue;
10410 }
10411 if (abbrev_tag == 0)
10412 break;
10413 if (tagno >= 0)
10414 printf ("%s<%" PRIu64 ">",
10415 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
10416 abbrev_tag);
10417
10418 for (entry = abbrev_lookup;
10419 entry < abbrev_lookup + abbrev_lookup_used;
10420 entry++)
10421 if (entry->abbrev_tag == abbrev_tag)
10422 break;
10423 if (entry >= abbrev_lookup + abbrev_lookup_used)
10424 {
10425 warn (_("Undefined abbreviation tag %" PRId64
10426 " in unit %#tx in the debug_names section\n"),
10427 abbrev_tag,
10428 unit_start - section->start);
10429 break;
10430 }
10431 abbrevptr = entry->abbrev_lookup_ptr;
10432 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
10433 if (tagno >= 0)
10434 printf (" %s", get_TAG_name (dwarf_tag));
10435 for (;;)
10436 {
10437 uint64_t xindex, form;
10438
10439 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10440 READ_ULEB (form, abbrevptr, abbrev_table_end);
10441 if (xindex == 0 && form == 0)
10442 break;
10443
10444 if (tagno >= 0)
10445 printf (" %s", get_IDX_name (xindex));
10446 entryptr = read_and_display_attr_value (0, form, 0,
10447 unit_start, entryptr, unit_end,
10448 0, 0, offset_size,
10449 dwarf_version, NULL,
10450 (tagno < 0), section,
10451 NULL, '=', -1);
10452 }
10453 ++tagno;
10454 }
10455 if (tagno <= 0)
10456 printf (_(" <no entries>"));
10457 putchar ('\n');
10458 }
10459
10460 free (abbrev_lookup);
10461 }
10462
10463 return 1;
10464 }
10465
10466 static int
10467 display_debug_links (struct dwarf_section * section,
10468 void * file ATTRIBUTE_UNUSED)
10469 {
10470 const unsigned char * filename;
10471 unsigned int filelen;
10472
10473 introduce (section, false);
10474
10475 /* The .gnu_debuglink section is formatted as:
10476 (c-string) Filename.
10477 (padding) If needed to reach a 4 byte boundary.
10478 (uint32_t) CRC32 value.
10479
10480 The .gun_debugaltlink section is formatted as:
10481 (c-string) Filename.
10482 (binary) Build-ID. */
10483
10484 filename = section->start;
10485 filelen = strnlen ((const char *) filename, section->size);
10486 if (filelen == section->size)
10487 {
10488 warn (_("The debuglink filename is corrupt/missing\n"));
10489 return 0;
10490 }
10491
10492 printf (_(" Separate debug info file: %s\n"), filename);
10493
10494 if (startswith (section->name, ".gnu_debuglink"))
10495 {
10496 unsigned int crc32;
10497 unsigned int crc_offset;
10498
10499 crc_offset = filelen + 1;
10500 crc_offset = (crc_offset + 3) & ~3;
10501 if (crc_offset + 4 > section->size)
10502 {
10503 warn (_("CRC offset missing/truncated\n"));
10504 return 0;
10505 }
10506
10507 crc32 = byte_get (filename + crc_offset, 4);
10508
10509 printf (_(" CRC value: %#x\n"), crc32);
10510
10511 if (crc_offset + 4 < section->size)
10512 {
10513 warn (_("There are %#" PRIx64
10514 " extraneous bytes at the end of the section\n"),
10515 section->size - (crc_offset + 4));
10516 return 0;
10517 }
10518 }
10519 else /* startswith (section->name, ".gnu_debugaltlink") */
10520 {
10521 const unsigned char *build_id = section->start + filelen + 1;
10522 size_t build_id_len = section->size - (filelen + 1);
10523 size_t printed;
10524
10525 /* FIXME: Should we support smaller build-id notes ? */
10526 if (build_id_len < 0x14)
10527 {
10528 warn (_("Build-ID is too short (%#zx bytes)\n"), build_id_len);
10529 return 0;
10530 }
10531
10532 printed = printf (_(" Build-ID (%#zx bytes):"), build_id_len);
10533 display_data (printed, build_id, build_id_len);
10534 putchar ('\n');
10535 }
10536
10537 putchar ('\n');
10538 return 1;
10539 }
10540
10541 static int
10542 display_gdb_index (struct dwarf_section *section,
10543 void *file ATTRIBUTE_UNUSED)
10544 {
10545 unsigned char *start = section->start;
10546 uint32_t version;
10547 uint32_t cu_list_offset, tu_list_offset;
10548 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10549 unsigned int cu_list_elements, tu_list_elements;
10550 unsigned int address_table_elements, symbol_table_slots;
10551 unsigned char *cu_list, *tu_list;
10552 unsigned char *address_table, *symbol_table, *constant_pool;
10553 unsigned int i;
10554
10555 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10556
10557 introduce (section, false);
10558
10559 if (section->size < 6 * sizeof (uint32_t))
10560 {
10561 warn (_("Truncated header in the %s section.\n"), section->name);
10562 return 0;
10563 }
10564
10565 version = byte_get_little_endian (start, 4);
10566 printf (_("Version %lu\n"), (unsigned long) version);
10567
10568 /* Prior versions are obsolete, and future versions may not be
10569 backwards compatible. */
10570 if (version < 3 || version > 8)
10571 {
10572 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10573 return 0;
10574 }
10575 if (version < 4)
10576 warn (_("The address table data in version 3 may be wrong.\n"));
10577 if (version < 5)
10578 warn (_("Version 4 does not support case insensitive lookups.\n"));
10579 if (version < 6)
10580 warn (_("Version 5 does not include inlined functions.\n"));
10581 if (version < 7)
10582 warn (_("Version 6 does not include symbol attributes.\n"));
10583 /* Version 7 indices generated by Gold have bad type unit references,
10584 PR binutils/15021. But we don't know if the index was generated by
10585 Gold or not, so to avoid worrying users with gdb-generated indices
10586 we say nothing for version 7 here. */
10587
10588 cu_list_offset = byte_get_little_endian (start + 4, 4);
10589 tu_list_offset = byte_get_little_endian (start + 8, 4);
10590 address_table_offset = byte_get_little_endian (start + 12, 4);
10591 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10592 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10593
10594 if (cu_list_offset > section->size
10595 || tu_list_offset > section->size
10596 || address_table_offset > section->size
10597 || symbol_table_offset > section->size
10598 || constant_pool_offset > section->size
10599 || tu_list_offset < cu_list_offset
10600 || address_table_offset < tu_list_offset
10601 || symbol_table_offset < address_table_offset
10602 || constant_pool_offset < symbol_table_offset)
10603 {
10604 warn (_("Corrupt header in the %s section.\n"), section->name);
10605 return 0;
10606 }
10607
10608 cu_list_elements = (tu_list_offset - cu_list_offset) / 16;
10609 tu_list_elements = (address_table_offset - tu_list_offset) / 24;
10610 address_table_elements = (symbol_table_offset - address_table_offset) / 20;
10611 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10612
10613 cu_list = start + cu_list_offset;
10614 tu_list = start + tu_list_offset;
10615 address_table = start + address_table_offset;
10616 symbol_table = start + symbol_table_offset;
10617 constant_pool = start + constant_pool_offset;
10618
10619 printf (_("\nCU table:\n"));
10620 for (i = 0; i < cu_list_elements; i++)
10621 {
10622 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 16, 8);
10623 uint64_t cu_length = byte_get_little_endian (cu_list + i * 16 + 8, 8);
10624
10625 printf ("[%3u] %#" PRIx64 " - %#" PRIx64 "\n",
10626 i, cu_offset, cu_offset + cu_length - 1);
10627 }
10628
10629 printf (_("\nTU table:\n"));
10630 for (i = 0; i < tu_list_elements; i++)
10631 {
10632 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 24, 8);
10633 uint64_t type_offset = byte_get_little_endian (tu_list + i * 24 + 8, 8);
10634 uint64_t signature = byte_get_little_endian (tu_list + i * 24 + 16, 8);
10635
10636 printf ("[%3u] %#" PRIx64 " %#" PRIx64 " ",
10637 i, tu_offset, type_offset);
10638 print_hex_ns (signature, 8);
10639 printf ("\n");
10640 }
10641
10642 printf (_("\nAddress table:\n"));
10643 for (i = 0; i < address_table_elements; i++)
10644 {
10645 uint64_t low = byte_get_little_endian (address_table + i * 20, 8);
10646 uint64_t high = byte_get_little_endian (address_table + i * 20 + 8, 8);
10647 uint32_t cu_index = byte_get_little_endian (address_table + i * 20 + 16, 4);
10648
10649 print_hex (low, 8);
10650 print_hex (high, 8);
10651 printf ("%" PRIu32 "\n", cu_index);
10652 }
10653
10654 printf (_("\nSymbol table:\n"));
10655 for (i = 0; i < symbol_table_slots; ++i)
10656 {
10657 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10658 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10659 uint32_t num_cus, cu;
10660
10661 if (name_offset != 0
10662 || cu_vector_offset != 0)
10663 {
10664 unsigned int j;
10665
10666 /* PR 17531: file: 5b7b07ad. */
10667 if (name_offset >= section->size - constant_pool_offset)
10668 {
10669 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10670 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10671 name_offset, i);
10672 }
10673 else
10674 printf ("[%3u] %.*s:", i,
10675 (int) (section->size - (constant_pool_offset + name_offset)),
10676 constant_pool + name_offset);
10677
10678 if (section->size - constant_pool_offset < 4
10679 || cu_vector_offset > section->size - constant_pool_offset - 4)
10680 {
10681 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10682 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10683 cu_vector_offset, i);
10684 continue;
10685 }
10686
10687 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10688
10689 if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10690 + cu_vector_offset + 4))
10691 {
10692 printf ("<invalid number of CUs: %d>\n", num_cus);
10693 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10694 num_cus, i);
10695 continue;
10696 }
10697
10698 if (num_cus > 1)
10699 printf ("\n");
10700
10701 for (j = 0; j < num_cus; ++j)
10702 {
10703 int is_static;
10704 gdb_index_symbol_kind kind;
10705
10706 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10707 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10708 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10709 cu = GDB_INDEX_CU_VALUE (cu);
10710 /* Convert to TU number if it's for a type unit. */
10711 if (cu >= cu_list_elements)
10712 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10713 (unsigned long) cu - cu_list_elements);
10714 else
10715 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10716
10717 printf (" [%s, %s]",
10718 is_static ? _("static") : _("global"),
10719 get_gdb_index_symbol_kind_name (kind));
10720 if (num_cus > 1)
10721 printf ("\n");
10722 }
10723 if (num_cus <= 1)
10724 printf ("\n");
10725 }
10726 }
10727
10728 return 1;
10729 }
10730
10731 /* Pre-allocate enough space for the CU/TU sets needed. */
10732
10733 static void
10734 prealloc_cu_tu_list (unsigned int nshndx)
10735 {
10736 if (shndx_pool == NULL)
10737 {
10738 shndx_pool_size = nshndx;
10739 shndx_pool_used = 0;
10740 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10741 sizeof (unsigned int));
10742 }
10743 else
10744 {
10745 shndx_pool_size = shndx_pool_used + nshndx;
10746 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10747 sizeof (unsigned int));
10748 }
10749 }
10750
10751 static void
10752 add_shndx_to_cu_tu_entry (unsigned int shndx)
10753 {
10754 shndx_pool [shndx_pool_used++] = shndx;
10755 }
10756
10757 static void
10758 end_cu_tu_entry (void)
10759 {
10760 shndx_pool [shndx_pool_used++] = 0;
10761 }
10762
10763 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10764
10765 static const char *
10766 get_DW_SECT_short_name (unsigned int dw_sect)
10767 {
10768 static char buf[16];
10769
10770 switch (dw_sect)
10771 {
10772 case DW_SECT_INFO:
10773 return "info";
10774 case DW_SECT_TYPES:
10775 return "types";
10776 case DW_SECT_ABBREV:
10777 return "abbrev";
10778 case DW_SECT_LINE:
10779 return "line";
10780 case DW_SECT_LOC:
10781 return "loc";
10782 case DW_SECT_STR_OFFSETS:
10783 return "str_off";
10784 case DW_SECT_MACINFO:
10785 return "macinfo";
10786 case DW_SECT_MACRO:
10787 return "macro";
10788 default:
10789 break;
10790 }
10791
10792 snprintf (buf, sizeof (buf), "%d", dw_sect);
10793 return buf;
10794 }
10795
10796 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10797 These sections are extensions for Fission.
10798 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
10799
10800 static int
10801 process_cu_tu_index (struct dwarf_section *section, int do_display)
10802 {
10803 unsigned char *phdr = section->start;
10804 unsigned char *limit = phdr + section->size;
10805 unsigned char *phash;
10806 unsigned char *pindex;
10807 unsigned char *ppool;
10808 unsigned int version;
10809 unsigned int ncols = 0;
10810 unsigned int nused;
10811 unsigned int nslots;
10812 unsigned int i;
10813 unsigned int j;
10814 uint64_t signature;
10815 size_t total;
10816
10817 /* PR 17512: file: 002-168123-0.004. */
10818 if (phdr == NULL)
10819 {
10820 warn (_("Section %s is empty\n"), section->name);
10821 return 0;
10822 }
10823 /* PR 17512: file: 002-376-0.004. */
10824 if (section->size < 24)
10825 {
10826 warn (_("Section %s is too small to contain a CU/TU header\n"),
10827 section->name);
10828 return 0;
10829 }
10830
10831 phash = phdr;
10832 SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
10833 if (version >= 2)
10834 SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
10835 SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
10836 SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
10837
10838 pindex = phash + (size_t) nslots * 8;
10839 ppool = pindex + (size_t) nslots * 4;
10840
10841 if (do_display)
10842 {
10843 introduce (section, false);
10844
10845 printf (_(" Version: %u\n"), version);
10846 if (version >= 2)
10847 printf (_(" Number of columns: %u\n"), ncols);
10848 printf (_(" Number of used entries: %u\n"), nused);
10849 printf (_(" Number of slots: %u\n\n"), nslots);
10850 }
10851
10852 /* PR 17531: file: 45d69832. */
10853 if (_mul_overflow ((size_t) nslots, 12, &total)
10854 || total > (size_t) (limit - phash))
10855 {
10856 warn (ngettext ("Section %s is too small for %u slot\n",
10857 "Section %s is too small for %u slots\n",
10858 nslots),
10859 section->name, nslots);
10860 return 0;
10861 }
10862
10863 if (version == 1)
10864 {
10865 unsigned char *shndx_list;
10866 unsigned int shndx;
10867
10868 if (!do_display)
10869 {
10870 prealloc_cu_tu_list ((limit - ppool) / 4);
10871 for (shndx_list = ppool + 4; shndx_list <= limit - 4; shndx_list += 4)
10872 {
10873 shndx = byte_get (shndx_list, 4);
10874 add_shndx_to_cu_tu_entry (shndx);
10875 }
10876 end_cu_tu_entry ();
10877 }
10878 else
10879 for (i = 0; i < nslots; i++)
10880 {
10881 SAFE_BYTE_GET (signature, phash, 8, limit);
10882 if (signature != 0)
10883 {
10884 SAFE_BYTE_GET (j, pindex, 4, limit);
10885 shndx_list = ppool + j * 4;
10886 /* PR 17531: file: 705e010d. */
10887 if (shndx_list < ppool)
10888 {
10889 warn (_("Section index pool located before start of section\n"));
10890 return 0;
10891 }
10892
10893 printf (_(" [%3d] Signature: %#" PRIx64 " Sections: "),
10894 i, signature);
10895 for (;;)
10896 {
10897 if (shndx_list >= limit)
10898 {
10899 warn (_("Section %s too small for shndx pool\n"),
10900 section->name);
10901 return 0;
10902 }
10903 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
10904 if (shndx == 0)
10905 break;
10906 printf (" %d", shndx);
10907 shndx_list += 4;
10908 }
10909 printf ("\n");
10910 }
10911 phash += 8;
10912 pindex += 4;
10913 }
10914 }
10915 else if (version == 2)
10916 {
10917 unsigned int val;
10918 unsigned int dw_sect;
10919 unsigned char *ph = phash;
10920 unsigned char *pi = pindex;
10921 unsigned char *poffsets = ppool + (size_t) ncols * 4;
10922 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
10923 bool is_tu_index;
10924 struct cu_tu_set *this_set = NULL;
10925 unsigned int row;
10926 unsigned char *prow;
10927 size_t temp;
10928
10929 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
10930
10931 /* PR 17531: file: 0dd159bf.
10932 Check for integer overflow (can occur when size_t is 32-bit)
10933 with overlarge ncols or nused values. */
10934 if (nused == -1u
10935 || _mul_overflow ((size_t) ncols, 4, &temp)
10936 || _mul_overflow ((size_t) nused + 1, temp, &total)
10937 || total > (size_t) (limit - ppool)
10938 /* PR 30227: ncols could be 0. */
10939 || _mul_overflow ((size_t) nused + 1, 4, &total)
10940 || total > (size_t) (limit - ppool))
10941 {
10942 warn (_("Section %s too small for offset and size tables\n"),
10943 section->name);
10944 return 0;
10945 }
10946
10947 if (do_display)
10948 {
10949 printf (_(" Offset table\n"));
10950 printf (" slot %-16s ",
10951 is_tu_index ? _("signature") : _("dwo_id"));
10952 }
10953 else
10954 {
10955 if (is_tu_index)
10956 {
10957 tu_count = nused;
10958 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10959 this_set = tu_sets;
10960 }
10961 else
10962 {
10963 cu_count = nused;
10964 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10965 this_set = cu_sets;
10966 }
10967 }
10968
10969 if (do_display)
10970 {
10971 for (j = 0; j < ncols; j++)
10972 {
10973 unsigned char *p = ppool + j * 4;
10974 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10975 printf (" %8s", get_DW_SECT_short_name (dw_sect));
10976 }
10977 printf ("\n");
10978 }
10979
10980 for (i = 0; i < nslots; i++)
10981 {
10982 SAFE_BYTE_GET (signature, ph, 8, limit);
10983
10984 SAFE_BYTE_GET (row, pi, 4, limit);
10985 if (row != 0)
10986 {
10987 /* PR 17531: file: a05f6ab3. */
10988 if (row > nused)
10989 {
10990 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
10991 row, nused);
10992 return 0;
10993 }
10994
10995 if (!do_display)
10996 {
10997 size_t num_copy = sizeof (uint64_t);
10998
10999 memcpy (&this_set[row - 1].signature, ph, num_copy);
11000 }
11001
11002 prow = poffsets + (row - 1) * ncols * 4;
11003 if (do_display)
11004 printf (" [%3d] %#" PRIx64, i, signature);
11005 for (j = 0; j < ncols; j++)
11006 {
11007 unsigned char *p = prow + j * 4;
11008 SAFE_BYTE_GET (val, p, 4, limit);
11009 if (do_display)
11010 printf (" %8d", val);
11011 else
11012 {
11013 p = ppool + j * 4;
11014 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11015
11016 /* PR 17531: file: 10796eb3. */
11017 if (dw_sect >= DW_SECT_MAX)
11018 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11019 else
11020 this_set [row - 1].section_offsets [dw_sect] = val;
11021 }
11022 }
11023
11024 if (do_display)
11025 printf ("\n");
11026 }
11027 ph += 8;
11028 pi += 4;
11029 }
11030
11031 ph = phash;
11032 pi = pindex;
11033 if (do_display)
11034 {
11035 printf ("\n");
11036 printf (_(" Size table\n"));
11037 printf (" slot %-16s ",
11038 is_tu_index ? _("signature") : _("dwo_id"));
11039 }
11040
11041 for (j = 0; j < ncols; j++)
11042 {
11043 unsigned char *p = ppool + j * 4;
11044 SAFE_BYTE_GET (val, p, 4, limit);
11045 if (do_display)
11046 printf (" %8s", get_DW_SECT_short_name (val));
11047 }
11048
11049 if (do_display)
11050 printf ("\n");
11051
11052 for (i = 0; i < nslots; i++)
11053 {
11054 SAFE_BYTE_GET (signature, ph, 8, limit);
11055
11056 SAFE_BYTE_GET (row, pi, 4, limit);
11057 if (row != 0)
11058 {
11059 prow = psizes + (row - 1) * ncols * 4;
11060
11061 if (do_display)
11062 printf (" [%3d] %#" PRIx64, i, signature);
11063
11064 for (j = 0; j < ncols; j++)
11065 {
11066 unsigned char *p = prow + j * 4;
11067
11068 /* PR 28645: Check for overflow. Since we do not know how
11069 many populated rows there will be, we cannot just
11070 perform a single check at the start of this function. */
11071 if (p > (limit - 4))
11072 {
11073 if (do_display)
11074 printf ("\n");
11075 warn (_("Too many rows/columns in DWARF index section %s\n"),
11076 section->name);
11077 return 0;
11078 }
11079
11080 SAFE_BYTE_GET (val, p, 4, limit);
11081
11082 if (do_display)
11083 printf (" %8d", val);
11084 else
11085 {
11086 p = ppool + j * 4;
11087 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11088 if (dw_sect >= DW_SECT_MAX)
11089 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11090 else
11091 this_set [row - 1].section_sizes [dw_sect] = val;
11092 }
11093 }
11094
11095 if (do_display)
11096 printf ("\n");
11097 }
11098
11099 ph += 8;
11100 pi += 4;
11101 }
11102 }
11103 else if (do_display)
11104 printf (_(" Unsupported version (%d)\n"), version);
11105
11106 if (do_display)
11107 printf ("\n");
11108
11109 return 1;
11110 }
11111
11112 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
11113
11114 /* Load the CU and TU indexes if present. This will build a list of
11115 section sets that we can use to associate a .debug_info.dwo section
11116 with its associated .debug_abbrev.dwo section in a .dwp file. */
11117
11118 static bool
11119 load_cu_tu_indexes (void *file)
11120 {
11121 /* If we have already loaded (or tried to load) the CU and TU indexes
11122 then do not bother to repeat the task. */
11123 if (cu_tu_indexes_read == -1)
11124 {
11125 cu_tu_indexes_read = true;
11126
11127 if (load_debug_section_with_follow (dwp_cu_index, file))
11128 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
11129 cu_tu_indexes_read = false;
11130
11131 if (load_debug_section_with_follow (dwp_tu_index, file))
11132 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
11133 cu_tu_indexes_read = false;
11134 }
11135
11136 return (bool) cu_tu_indexes_read;
11137 }
11138
11139 /* Find the set of sections that includes section SHNDX. */
11140
11141 unsigned int *
11142 find_cu_tu_set (void *file, unsigned int shndx)
11143 {
11144 unsigned int i;
11145
11146 if (! load_cu_tu_indexes (file))
11147 return NULL;
11148
11149 /* Find SHNDX in the shndx pool. */
11150 for (i = 0; i < shndx_pool_used; i++)
11151 if (shndx_pool [i] == shndx)
11152 break;
11153
11154 if (i >= shndx_pool_used)
11155 return NULL;
11156
11157 /* Now backup to find the first entry in the set. */
11158 while (i > 0 && shndx_pool [i - 1] != 0)
11159 i--;
11160
11161 return shndx_pool + i;
11162 }
11163
11164 /* Display a .debug_cu_index or .debug_tu_index section. */
11165
11166 static int
11167 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
11168 {
11169 return process_cu_tu_index (section, 1);
11170 }
11171
11172 static int
11173 display_debug_not_supported (struct dwarf_section *section,
11174 void *file ATTRIBUTE_UNUSED)
11175 {
11176 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
11177 section->name);
11178
11179 return 1;
11180 }
11181
11182 /* Like malloc, but takes two parameters like calloc.
11183 Verifies that the first parameter is not too large.
11184 Note: does *not* initialise the allocated memory to zero. */
11185
11186 void *
11187 cmalloc (uint64_t nmemb, size_t size)
11188 {
11189 /* Check for overflow. */
11190 if (nmemb >= ~(size_t) 0 / size)
11191 return NULL;
11192
11193 return xmalloc (nmemb * size);
11194 }
11195
11196 /* Like xmalloc, but takes two parameters like calloc.
11197 Verifies that the first parameter is not too large.
11198 Note: does *not* initialise the allocated memory to zero. */
11199
11200 void *
11201 xcmalloc (uint64_t nmemb, size_t size)
11202 {
11203 /* Check for overflow. */
11204 if (nmemb >= ~(size_t) 0 / size)
11205 {
11206 fprintf (stderr,
11207 _("Attempt to allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11208 nmemb);
11209 xexit (1);
11210 }
11211
11212 return xmalloc (nmemb * size);
11213 }
11214
11215 /* Like xrealloc, but takes three parameters.
11216 Verifies that the second parameter is not too large.
11217 Note: does *not* initialise any new memory to zero. */
11218
11219 void *
11220 xcrealloc (void *ptr, uint64_t nmemb, size_t size)
11221 {
11222 /* Check for overflow. */
11223 if (nmemb >= ~(size_t) 0 / size)
11224 {
11225 error (_("Attempt to re-allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11226 nmemb);
11227 xexit (1);
11228 }
11229
11230 return xrealloc (ptr, nmemb * size);
11231 }
11232
11233 /* Like xcalloc, but verifies that the first parameter is not too large. */
11234
11235 void *
11236 xcalloc2 (uint64_t nmemb, size_t size)
11237 {
11238 /* Check for overflow. */
11239 if (nmemb >= ~(size_t) 0 / size)
11240 {
11241 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#" PRIx64 "\n"),
11242 nmemb);
11243 xexit (1);
11244 }
11245
11246 return xcalloc (nmemb, size);
11247 }
11248
11249 static unsigned long
11250 calc_gnu_debuglink_crc32 (unsigned long crc,
11251 const unsigned char *buf,
11252 size_t len)
11253 {
11254 static const unsigned long crc32_table[256] =
11255 {
11256 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
11257 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
11258 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
11259 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
11260 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
11261 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
11262 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
11263 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
11264 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
11265 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
11266 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
11267 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
11268 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
11269 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
11270 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
11271 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
11272 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
11273 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
11274 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
11275 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
11276 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
11277 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
11278 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
11279 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
11280 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
11281 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
11282 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
11283 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
11284 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
11285 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
11286 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
11287 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
11288 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
11289 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
11290 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
11291 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
11292 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
11293 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
11294 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
11295 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
11296 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
11297 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
11298 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
11299 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
11300 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
11301 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
11302 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
11303 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
11304 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
11305 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
11306 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
11307 0x2d02ef8d
11308 };
11309 const unsigned char *end;
11310
11311 crc = ~crc & 0xffffffff;
11312 for (end = buf + len; buf < end; ++ buf)
11313 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
11314 return ~crc & 0xffffffff;
11315 }
11316
11317 typedef bool (*check_func_type) (const char *, void *);
11318 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
11319
11320 static bool
11321 check_gnu_debuglink (const char * pathname, void * crc_pointer)
11322 {
11323 static unsigned char buffer[8 * 1024];
11324 FILE *f;
11325 size_t count;
11326 unsigned long crc = 0;
11327 void *sep_data;
11328
11329 sep_data = open_debug_file (pathname);
11330 if (sep_data == NULL)
11331 return false;
11332
11333 /* Yes - we are opening the file twice... */
11334 f = fopen (pathname, "rb");
11335 if (f == NULL)
11336 {
11337 /* Paranoia: This should never happen. */
11338 close_debug_file (sep_data);
11339 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
11340 return false;
11341 }
11342
11343 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
11344 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
11345
11346 fclose (f);
11347
11348 if (crc != * (unsigned long *) crc_pointer)
11349 {
11350 close_debug_file (sep_data);
11351 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
11352 pathname);
11353 return false;
11354 }
11355
11356 return true;
11357 }
11358
11359 static const char *
11360 parse_gnu_debuglink (struct dwarf_section * section, void * data)
11361 {
11362 const char * name;
11363 unsigned int crc_offset;
11364 unsigned long * crc32 = (unsigned long *) data;
11365
11366 /* The name is first.
11367 The CRC value is stored after the filename, aligned up to 4 bytes. */
11368 name = (const char *) section->start;
11369
11370 crc_offset = strnlen (name, section->size) + 1;
11371 if (crc_offset == 1)
11372 return NULL;
11373 crc_offset = (crc_offset + 3) & ~3;
11374 if (crc_offset + 4 > section->size)
11375 return NULL;
11376
11377 * crc32 = byte_get (section->start + crc_offset, 4);
11378 return name;
11379 }
11380
11381 static bool
11382 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
11383 {
11384 void * sep_data = open_debug_file (filename);
11385
11386 if (sep_data == NULL)
11387 return false;
11388
11389 /* FIXME: We should now extract the build-id in the separate file
11390 and check it... */
11391
11392 return true;
11393 }
11394
11395 typedef struct build_id_data
11396 {
11397 size_t len;
11398 const unsigned char *data;
11399 } Build_id_data;
11400
11401 static const char *
11402 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11403 {
11404 const char *name;
11405 size_t namelen;
11406 size_t id_len;
11407 Build_id_data *build_id_data;
11408
11409 /* The name is first.
11410 The build-id follows immediately, with no padding, up to the section's end. */
11411
11412 name = (const char *) section->start;
11413 namelen = strnlen (name, section->size) + 1;
11414 if (namelen == 1)
11415 return NULL;
11416 if (namelen >= section->size)
11417 return NULL;
11418
11419 id_len = section->size - namelen;
11420 if (id_len < 0x14)
11421 return NULL;
11422
11423 build_id_data = (Build_id_data *) data;
11424 build_id_data->len = id_len;
11425 build_id_data->data = section->start + namelen;
11426
11427 return name;
11428 }
11429
11430 static void
11431 add_separate_debug_file (const char * filename, void * handle)
11432 {
11433 separate_info * i = xmalloc (sizeof * i);
11434
11435 i->filename = filename;
11436 i->handle = handle;
11437 i->next = first_separate_info;
11438 first_separate_info = i;
11439 }
11440
11441 #if HAVE_LIBDEBUGINFOD
11442 /* Query debuginfod servers for the target debuglink or debugaltlink
11443 file. If successful, store the path of the file in filename and
11444 return TRUE, otherwise return FALSE. */
11445
11446 static bool
11447 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11448 char ** filename,
11449 void * file)
11450 {
11451 size_t build_id_len;
11452 unsigned char * build_id;
11453
11454 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11455 {
11456 /* Get the build-id of file. */
11457 build_id = get_build_id (file);
11458 build_id_len = 0;
11459 }
11460 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11461 {
11462 /* Get the build-id of the debugaltlink file. */
11463 unsigned int filelen;
11464
11465 filelen = strnlen ((const char *)section->start, section->size);
11466 if (filelen == section->size)
11467 /* Corrupt debugaltlink. */
11468 return false;
11469
11470 build_id = section->start + filelen + 1;
11471 build_id_len = section->size - (filelen + 1);
11472
11473 if (build_id_len == 0)
11474 return false;
11475 }
11476 else
11477 return false;
11478
11479 if (build_id)
11480 {
11481 int fd;
11482 debuginfod_client * client;
11483
11484 client = debuginfod_begin ();
11485 if (client == NULL)
11486 return false;
11487
11488 /* Query debuginfod servers for the target file. If found its path
11489 will be stored in filename. */
11490 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11491 debuginfod_end (client);
11492
11493 /* Only free build_id if we allocated space for a hex string
11494 in get_build_id (). */
11495 if (build_id_len == 0)
11496 free (build_id);
11497
11498 if (fd >= 0)
11499 {
11500 /* File successfully retrieved. Close fd since we want to
11501 use open_debug_file () on filename instead. */
11502 close (fd);
11503 return true;
11504 }
11505 }
11506
11507 return false;
11508 }
11509 #endif /* HAVE_LIBDEBUGINFOD */
11510
11511 static void *
11512 load_separate_debug_info (const char * main_filename,
11513 struct dwarf_section * xlink,
11514 parse_func_type parse_func,
11515 check_func_type check_func,
11516 void * func_data,
11517 void * file ATTRIBUTE_UNUSED)
11518 {
11519 const char * separate_filename;
11520 char * debug_filename;
11521 char * canon_dir;
11522 size_t canon_dirlen;
11523 size_t dirlen;
11524 char * canon_filename;
11525 char * canon_debug_filename;
11526 bool self;
11527
11528 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11529 {
11530 warn (_("Corrupt debuglink section: %s\n"),
11531 xlink->name ? xlink->name : xlink->uncompressed_name);
11532 return NULL;
11533 }
11534
11535 /* Attempt to locate the separate file.
11536 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11537
11538 canon_filename = lrealpath (main_filename);
11539 canon_dir = xstrdup (canon_filename);
11540
11541 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11542 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11543 break;
11544 canon_dir[canon_dirlen] = '\0';
11545
11546 #ifndef DEBUGDIR
11547 #define DEBUGDIR "/lib/debug"
11548 #endif
11549 #ifndef EXTRA_DEBUG_ROOT1
11550 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11551 #endif
11552 #ifndef EXTRA_DEBUG_ROOT2
11553 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11554 #endif
11555
11556 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11557 + canon_dirlen
11558 + strlen (".debug/")
11559 #ifdef EXTRA_DEBUG_ROOT1
11560 + strlen (EXTRA_DEBUG_ROOT1)
11561 #endif
11562 #ifdef EXTRA_DEBUG_ROOT2
11563 + strlen (EXTRA_DEBUG_ROOT2)
11564 #endif
11565 + strlen (separate_filename)
11566 + 1);
11567 if (debug_filename == NULL)
11568 {
11569 warn (_("Out of memory"));
11570 free (canon_dir);
11571 free (canon_filename);
11572 return NULL;
11573 }
11574
11575 /* First try in the current directory. */
11576 sprintf (debug_filename, "%s", separate_filename);
11577 if (check_func (debug_filename, func_data))
11578 goto found;
11579
11580 /* Then try in a subdirectory called .debug. */
11581 sprintf (debug_filename, ".debug/%s", separate_filename);
11582 if (check_func (debug_filename, func_data))
11583 goto found;
11584
11585 /* Then try in the same directory as the original file. */
11586 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11587 if (check_func (debug_filename, func_data))
11588 goto found;
11589
11590 /* And the .debug subdirectory of that directory. */
11591 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11592 if (check_func (debug_filename, func_data))
11593 goto found;
11594
11595 #ifdef EXTRA_DEBUG_ROOT1
11596 /* Try the first extra debug file root. */
11597 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11598 if (check_func (debug_filename, func_data))
11599 goto found;
11600
11601 /* Try the first extra debug file root. */
11602 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11603 if (check_func (debug_filename, func_data))
11604 goto found;
11605 #endif
11606
11607 #ifdef EXTRA_DEBUG_ROOT2
11608 /* Try the second extra debug file root. */
11609 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11610 if (check_func (debug_filename, func_data))
11611 goto found;
11612 #endif
11613
11614 /* Then try in the global debug_filename directory. */
11615 strcpy (debug_filename, DEBUGDIR);
11616 dirlen = strlen (DEBUGDIR) - 1;
11617 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11618 strcat (debug_filename, "/");
11619 strcat (debug_filename, (const char *) separate_filename);
11620
11621 if (check_func (debug_filename, func_data))
11622 goto found;
11623
11624 #if HAVE_LIBDEBUGINFOD
11625 {
11626 char * tmp_filename;
11627
11628 if (use_debuginfod
11629 && debuginfod_fetch_separate_debug_info (xlink,
11630 & tmp_filename,
11631 file))
11632 {
11633 /* File successfully downloaded from server, replace
11634 debug_filename with the file's path. */
11635 free (debug_filename);
11636 debug_filename = tmp_filename;
11637 goto found;
11638 }
11639 }
11640 #endif
11641
11642 if (do_debug_links)
11643 {
11644 /* Failed to find the file. */
11645 warn (_("could not find separate debug file '%s'\n"),
11646 separate_filename);
11647 warn (_("tried: %s\n"), debug_filename);
11648
11649 #ifdef EXTRA_DEBUG_ROOT2
11650 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11651 separate_filename);
11652 warn (_("tried: %s\n"), debug_filename);
11653 #endif
11654
11655 #ifdef EXTRA_DEBUG_ROOT1
11656 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11657 canon_dir, separate_filename);
11658 warn (_("tried: %s\n"), debug_filename);
11659
11660 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11661 separate_filename);
11662 warn (_("tried: %s\n"), debug_filename);
11663 #endif
11664
11665 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11666 separate_filename);
11667 warn (_("tried: %s\n"), debug_filename);
11668
11669 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11670 warn (_("tried: %s\n"), debug_filename);
11671
11672 sprintf (debug_filename, ".debug/%s", separate_filename);
11673 warn (_("tried: %s\n"), debug_filename);
11674
11675 sprintf (debug_filename, "%s", separate_filename);
11676 warn (_("tried: %s\n"), debug_filename);
11677
11678 #if HAVE_LIBDEBUGINFOD
11679 if (use_debuginfod)
11680 {
11681 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11682
11683 if (urls == NULL)
11684 urls = "";
11685
11686 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11687 }
11688 #endif
11689 }
11690
11691 free (canon_dir);
11692 free (debug_filename);
11693 free (canon_filename);
11694 return NULL;
11695
11696 found:
11697 free (canon_dir);
11698
11699 canon_debug_filename = lrealpath (debug_filename);
11700 self = strcmp (canon_debug_filename, canon_filename) == 0;
11701 free (canon_filename);
11702 free (canon_debug_filename);
11703 if (self)
11704 {
11705 free (debug_filename);
11706 return NULL;
11707 }
11708
11709 void * debug_handle;
11710
11711 /* Now open the file.... */
11712 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11713 {
11714 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11715 free (debug_filename);
11716 return NULL;
11717 }
11718
11719 /* FIXME: We do not check to see if there are any other separate debug info
11720 files that would also match. */
11721
11722 if (do_debug_links)
11723 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11724 add_separate_debug_file (debug_filename, debug_handle);
11725
11726 /* Do not free debug_filename - it might be referenced inside
11727 the structure returned by open_debug_file(). */
11728 return debug_handle;
11729 }
11730
11731 /* Attempt to load a separate dwarf object file. */
11732
11733 static void *
11734 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11735 {
11736 char * separate_filename;
11737 void * separate_handle;
11738
11739 if (IS_ABSOLUTE_PATH (name))
11740 separate_filename = strdup (name);
11741 else
11742 /* FIXME: Skip adding / if dwo_dir ends in /. */
11743 separate_filename = concat (dir, "/", name, NULL);
11744 if (separate_filename == NULL)
11745 {
11746 warn (_("Out of memory allocating dwo filename\n"));
11747 return NULL;
11748 }
11749
11750 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11751 {
11752 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11753 free (separate_filename);
11754 return NULL;
11755 }
11756
11757 /* FIXME: We should check the dwo_id. */
11758
11759 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11760
11761 add_separate_debug_file (separate_filename, separate_handle);
11762 /* Note - separate_filename will be freed in free_debug_memory(). */
11763 return separate_handle;
11764 }
11765
11766 static void *
11767 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11768 {
11769 char * f = filename;
11770
11771 f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11772 id_len --;
11773 while (id_len --)
11774 f += sprintf (f, "%02x", (unsigned) *data++);
11775 strcpy (f, ".debug");
11776
11777 return open_debug_file (filename);
11778 }
11779
11780 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section. */
11781
11782 static void
11783 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11784 {
11785 if (! load_debug_section (note_gnu_build_id, main_file))
11786 return; /* No .note.gnu.build-id section. */
11787
11788 struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11789 if (section == NULL)
11790 {
11791 warn (_("Unable to load the .note.gnu.build-id section\n"));
11792 return;
11793 }
11794
11795 if (section->start == NULL || section->size < 0x18)
11796 {
11797 warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11798 return;
11799 }
11800
11801 /* In theory we should extract the contents of the section into
11802 a note structure and then check the fields. For now though
11803 just use hard coded offsets instead:
11804
11805 Field Bytes Contents
11806 NSize 0...3 4
11807 DSize 4...7 8+
11808 Type 8..11 3 (NT_GNU_BUILD_ID)
11809 Name 12.15 GNU\0
11810 Data 16.... */
11811
11812 /* FIXME: Check the name size, name and type fields. */
11813
11814 unsigned long build_id_size;
11815 build_id_size = byte_get (section->start + 4, 4);
11816 if (build_id_size < 8)
11817 {
11818 warn (_(".note.gnu.build-id data size is too small\n"));
11819 return;
11820 }
11821
11822 if (build_id_size > (section->size - 16))
11823 {
11824 warn (_(".note.gnu.build-id data size is too big\n"));
11825 return;
11826 }
11827
11828 char * filename;
11829 filename = xmalloc (strlen (".build-id/")
11830 + build_id_size * 2 + 2
11831 + strlen (".debug")
11832 /* The next string should be the same as the longest
11833 name found in the prefixes[] array below. */
11834 + strlen ("/usrlib64/debug/usr")
11835 + 1);
11836 void * handle;
11837
11838 static const char * prefixes[] =
11839 {
11840 "",
11841 ".debug/",
11842 "/usr/lib/debug/",
11843 "/usr/lib/debug/usr/",
11844 "/usr/lib64/debug/",
11845 "/usr/lib64/debug/usr"
11846 };
11847 long unsigned int i;
11848
11849 for (i = 0; i < ARRAY_SIZE (prefixes); i++)
11850 {
11851 handle = try_build_id_prefix (prefixes[i], filename,
11852 section->start + 16, build_id_size);
11853 if (handle != NULL)
11854 break;
11855 }
11856 /* FIXME: TYhe BFD library also tries a global debugfile directory prefix. */
11857 if (handle == NULL)
11858 {
11859 /* Failed to find a debug file associated with the build-id.
11860 This is not an error however, rather it just means that
11861 the debug info has probably not been loaded on the system,
11862 or that another method is being used to link to the debug
11863 info. */
11864 free (filename);
11865 return;
11866 }
11867
11868 add_separate_debug_file (filename, handle);
11869 }
11870
11871 /* Try to load a debug file pointed to by the .debug_sup section. */
11872
11873 static void
11874 load_debug_sup_file (const char * main_filename, void * file)
11875 {
11876 if (! load_debug_section (debug_sup, file))
11877 return; /* No .debug_sup section. */
11878
11879 struct dwarf_section * section;
11880 section = & debug_displays [debug_sup].section;
11881 assert (section != NULL);
11882
11883 if (section->start == NULL || section->size < 5)
11884 {
11885 warn (_(".debug_sup section is corrupt/empty\n"));
11886 return;
11887 }
11888
11889 if (section->start[2] != 0)
11890 return; /* This is a supplementary file. */
11891
11892 const char * filename = (const char *) section->start + 3;
11893 if (strnlen (filename, section->size - 3) == section->size - 3)
11894 {
11895 warn (_("filename in .debug_sup section is corrupt\n"));
11896 return;
11897 }
11898
11899 if (filename[0] != '/' && strchr (main_filename, '/'))
11900 {
11901 char * new_name;
11902 int new_len;
11903
11904 new_len = asprintf (& new_name, "%.*s/%s",
11905 (int) (strrchr (main_filename, '/') - main_filename),
11906 main_filename,
11907 filename);
11908 if (new_len < 3)
11909 {
11910 warn (_("unable to construct path for supplementary debug file"));
11911 if (new_len > -1)
11912 free (new_name);
11913 return;
11914 }
11915 filename = new_name;
11916 }
11917 else
11918 {
11919 /* PR 27796: Make sure that we pass a filename that can be free'd to
11920 add_separate_debug_file(). */
11921 filename = strdup (filename);
11922 if (filename == NULL)
11923 {
11924 warn (_("out of memory constructing filename for .debug_sup link\n"));
11925 return;
11926 }
11927 }
11928
11929 void * handle = open_debug_file (filename);
11930 if (handle == NULL)
11931 {
11932 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
11933 free ((void *) filename);
11934 return;
11935 }
11936
11937 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
11938
11939 /* FIXME: Compare the checksums, if present. */
11940 add_separate_debug_file (filename, handle);
11941 }
11942
11943 /* Load a debuglink section and/or a debugaltlink section, if either are present.
11944 Recursively check the loaded files for more of these sections.
11945 Also follow any links in .debug_sup sections.
11946 FIXME: Should also check for DWO_* entries in the newly loaded files. */
11947
11948 static void
11949 check_for_and_load_links (void * file, const char * filename)
11950 {
11951 void * handle = NULL;
11952
11953 if (load_debug_section (gnu_debugaltlink, file))
11954 {
11955 Build_id_data build_id_data;
11956
11957 handle = load_separate_debug_info (filename,
11958 & debug_displays[gnu_debugaltlink].section,
11959 parse_gnu_debugaltlink,
11960 check_gnu_debugaltlink,
11961 & build_id_data,
11962 file);
11963 if (handle)
11964 {
11965 assert (handle == first_separate_info->handle);
11966 check_for_and_load_links (first_separate_info->handle,
11967 first_separate_info->filename);
11968 }
11969 }
11970
11971 if (load_debug_section (gnu_debuglink, file))
11972 {
11973 unsigned long crc32;
11974
11975 handle = load_separate_debug_info (filename,
11976 & debug_displays[gnu_debuglink].section,
11977 parse_gnu_debuglink,
11978 check_gnu_debuglink,
11979 & crc32,
11980 file);
11981 if (handle)
11982 {
11983 assert (handle == first_separate_info->handle);
11984 check_for_and_load_links (first_separate_info->handle,
11985 first_separate_info->filename);
11986 }
11987 }
11988
11989 load_debug_sup_file (filename, file);
11990
11991 load_build_id_debug_file (filename, file);
11992 }
11993
11994 /* Load the separate debug info file(s) attached to FILE, if any exist.
11995 Returns TRUE if any were found, FALSE otherwise.
11996 If TRUE is returned then the linked list starting at first_separate_info
11997 will be populated with open file handles. */
11998
11999 bool
12000 load_separate_debug_files (void * file, const char * filename)
12001 {
12002 /* Skip this operation if we are not interested in debug links. */
12003 if (! do_follow_links && ! do_debug_links)
12004 return false;
12005
12006 /* See if there are any dwo links. */
12007 if (load_debug_section (str, file)
12008 && load_debug_section (abbrev, file)
12009 && load_debug_section (info, file))
12010 {
12011 /* Load the .debug_addr section, if it exists. */
12012 load_debug_section (debug_addr, file);
12013 /* Load the .debug_str_offsets section, if it exists. */
12014 load_debug_section (str_index, file);
12015 /* Load the .debug_loclists section, if it exists. */
12016 load_debug_section (loclists, file);
12017 /* Load the .debug_rnglists section, if it exists. */
12018 load_debug_section (rnglists, file);
12019
12020 free_dwo_info ();
12021
12022 if (process_debug_info (& debug_displays[info].section, file, abbrev,
12023 true, false))
12024 {
12025 bool introduced = false;
12026 dwo_info *dwinfo;
12027 const char *dir = NULL;
12028 const char *id = NULL;
12029 const char *name = NULL;
12030
12031 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
12032 {
12033 /* Accumulate NAME, DIR and ID fields. */
12034 switch (dwinfo->type)
12035 {
12036 case DWO_NAME:
12037 if (name != NULL)
12038 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
12039 name = dwinfo->value;
12040 break;
12041
12042 case DWO_DIR:
12043 /* There can be multiple DW_AT_comp_dir entries in a CU,
12044 so do not complain. */
12045 dir = dwinfo->value;
12046 break;
12047
12048 case DWO_ID:
12049 if (id != NULL)
12050 warn (_("multiple DWO_IDs encountered for the same CU\n"));
12051 id = dwinfo->value;
12052 break;
12053
12054 default:
12055 error (_("Unexpected DWO INFO type"));
12056 break;
12057 }
12058
12059 /* If we have reached the end of our list, or we are changing
12060 CUs, then display the information that we have accumulated
12061 so far. */
12062 if (name != NULL
12063 && (dwinfo->next == NULL
12064 || dwinfo->next->cu_offset != dwinfo->cu_offset))
12065 {
12066 if (do_debug_links)
12067 {
12068 if (! introduced)
12069 {
12070 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
12071 debug_displays [info].section.uncompressed_name);
12072 introduced = true;
12073 }
12074
12075 printf (_(" Name: %s\n"), name);
12076 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
12077 if (id != NULL)
12078 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
12079 else if (debug_information[0].dwarf_version != 5)
12080 printf (_(" ID: <not specified>\n"));
12081 printf ("\n\n");
12082 }
12083
12084 if (do_follow_links)
12085 load_dwo_file (filename, name, dir, id);
12086
12087 name = dir = id = NULL;
12088 }
12089 }
12090 }
12091 }
12092
12093 if (! do_follow_links)
12094 /* The other debug links will be displayed by display_debug_links()
12095 so we do not need to do any further processing here. */
12096 return false;
12097
12098 /* FIXME: We do not check for the presence of both link sections in the same file. */
12099 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
12100 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
12101
12102 check_for_and_load_links (file, filename);
12103 if (first_separate_info != NULL)
12104 return true;
12105
12106 do_follow_links = 0;
12107 return false;
12108 }
12109
12110 void
12111 free_debug_memory (void)
12112 {
12113 unsigned int i;
12114
12115 free_all_abbrevs ();
12116
12117 free (shndx_pool);
12118 shndx_pool = NULL;
12119 shndx_pool_size = 0;
12120 shndx_pool_used = 0;
12121 free (cu_sets);
12122 cu_sets = NULL;
12123 cu_count = 0;
12124 free (tu_sets);
12125 tu_sets = NULL;
12126 tu_count = 0;
12127
12128 memset (level_type_signed, 0, sizeof level_type_signed);
12129 cu_tu_indexes_read = -1;
12130
12131 for (i = 0; i < max; i++)
12132 free_debug_section ((enum dwarf_section_display_enum) i);
12133
12134 if (debug_information != NULL)
12135 {
12136 for (i = 0; i < alloc_num_debug_info_entries; i++)
12137 free_debug_information (&debug_information[i]);
12138 free (debug_information);
12139 debug_information = NULL;
12140 alloc_num_debug_info_entries = num_debug_info_entries = 0;
12141 }
12142
12143 separate_info * d;
12144 separate_info * next;
12145
12146 for (d = first_separate_info; d != NULL; d = next)
12147 {
12148 close_debug_file (d->handle);
12149 free ((void *) d->filename);
12150 next = d->next;
12151 free ((void *) d);
12152 }
12153 first_separate_info = NULL;
12154
12155 free_dwo_info ();
12156 }
12157
12158 typedef struct
12159 {
12160 const char letter;
12161 const char *option;
12162 int *variable;
12163 int val;
12164 } debug_dump_long_opts;
12165
12166 static const debug_dump_long_opts debug_option_table[] =
12167 {
12168 { 'A', "addr", &do_debug_addr, 1 },
12169 { 'a', "abbrev", &do_debug_abbrevs, 1 },
12170 { 'c', "cu_index", &do_debug_cu_index, 1 },
12171 #ifdef HAVE_LIBDEBUGINFOD
12172 { 'D', "use-debuginfod", &use_debuginfod, 1 },
12173 { 'E', "do-not-use-debuginfod", &use_debuginfod, 0 },
12174 #endif
12175 { 'F', "frames-interp", &do_debug_frames_interp, 1 },
12176 { 'f', "frames", &do_debug_frames, 1 },
12177 { 'g', "gdb_index", &do_gdb_index, 1 },
12178 { 'i', "info", &do_debug_info, 1 },
12179 { 'K', "follow-links", &do_follow_links, 1 },
12180 { 'k', "links", &do_debug_links, 1 },
12181 { 'L', "decodedline", &do_debug_lines, FLAG_DEBUG_LINES_DECODED },
12182 { 'l', "rawline", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12183 /* For compatibility with earlier versions of readelf. */
12184 { 'l', "line", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12185 { 'm', "macro", &do_debug_macinfo, 1 },
12186 { 'N', "no-follow-links", &do_follow_links, 0 },
12187 { 'O', "str-offsets", &do_debug_str_offsets, 1 },
12188 { 'o', "loc", &do_debug_loc, 1 },
12189 { 'p', "pubnames", &do_debug_pubnames, 1 },
12190 { 'R', "Ranges", &do_debug_ranges, 1 },
12191 { 'r', "aranges", &do_debug_aranges, 1 },
12192 /* For compatibility with earlier versions of readelf. */
12193 { 'r', "ranges", &do_debug_aranges, 1 },
12194 { 's', "str", &do_debug_str, 1 },
12195 { 'T', "trace_aranges", &do_trace_aranges, 1 },
12196 { 't', "pubtypes", &do_debug_pubtypes, 1 },
12197 { 'U', "trace_info", &do_trace_info, 1 },
12198 { 'u', "trace_abbrev", &do_trace_abbrevs, 1 },
12199 { 0, NULL, NULL, 0 }
12200 };
12201
12202 /* Enable display of specific DWARF sections as determined by the comma
12203 separated strings in NAMES. Returns non-zero if any displaying was
12204 enabled. */
12205
12206 int
12207 dwarf_select_sections_by_names (const char *names)
12208 {
12209 const char *p;
12210 int result = 0;
12211
12212 p = names;
12213 while (*p)
12214 {
12215 const debug_dump_long_opts *entry;
12216
12217 for (entry = debug_option_table; entry->option; entry++)
12218 {
12219 size_t len = strlen (entry->option);
12220
12221 if (strncmp (p, entry->option, len) == 0
12222 && (p[len] == ',' || p[len] == '\0'))
12223 {
12224 if (entry->val == 0)
12225 * entry->variable = 0;
12226 else
12227 * entry->variable = entry->val;
12228 result |= entry->val;
12229
12230 p += len;
12231 break;
12232 }
12233 }
12234
12235 if (entry->option == NULL)
12236 {
12237 warn (_("Unrecognized debug option '%s'\n"), p);
12238 p = strchr (p, ',');
12239 if (p == NULL)
12240 break;
12241 }
12242
12243 if (*p == ',')
12244 p++;
12245 }
12246
12247 /* The --debug-dump=frames-interp option also enables the
12248 --debug-dump=frames option. */
12249 if (do_debug_frames_interp)
12250 do_debug_frames = 1;
12251
12252 return result;
12253 }
12254
12255 /* Enable display of specific DWARF sections as determined by the characters
12256 in LETTERS. Returns non-zero if any displaying was enabled. */
12257
12258 int
12259 dwarf_select_sections_by_letters (const char *letters)
12260 {
12261 int result = 0;
12262
12263 while (* letters)
12264 {
12265 const debug_dump_long_opts *entry;
12266
12267 for (entry = debug_option_table; entry->letter; entry++)
12268 {
12269 if (entry->letter == * letters)
12270 {
12271 if (entry->val == 0)
12272 * entry->variable = 0;
12273 else
12274 * entry->variable |= entry->val;
12275 result |= entry->val;
12276 break;
12277 }
12278 }
12279
12280 if (entry->letter == 0)
12281 warn (_("Unrecognized debug letter option '%c'\n"), * letters);
12282
12283 letters ++;
12284 }
12285
12286 /* The --debug-dump=frames-interp option also enables the
12287 --debug-dump=frames option. */
12288 if (do_debug_frames_interp)
12289 do_debug_frames = 1;
12290
12291 return result;
12292 }
12293
12294 void
12295 dwarf_select_sections_all (void)
12296 {
12297 do_debug_info = 1;
12298 do_debug_abbrevs = 1;
12299 do_debug_lines = FLAG_DEBUG_LINES_RAW;
12300 do_debug_pubnames = 1;
12301 do_debug_pubtypes = 1;
12302 do_debug_aranges = 1;
12303 do_debug_ranges = 1;
12304 do_debug_frames = 1;
12305 do_debug_macinfo = 1;
12306 do_debug_str = 1;
12307 do_debug_loc = 1;
12308 do_gdb_index = 1;
12309 do_trace_info = 1;
12310 do_trace_abbrevs = 1;
12311 do_trace_aranges = 1;
12312 do_debug_addr = 1;
12313 do_debug_cu_index = 1;
12314 do_follow_links = 1;
12315 do_debug_links = 1;
12316 do_debug_str_offsets = 1;
12317 }
12318
12319 #define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
12320 #define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
12321
12322 /* N.B. The order here must match the order in section_display_enum. */
12323
12324 struct dwarf_section_display debug_displays[] =
12325 {
12326 { { ".debug_abbrev", ".zdebug_abbrev", ".dwabrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12327 { { ".debug_aranges", ".zdebug_aranges", ".dwarnge", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
12328 { { ".debug_frame", ".zdebug_frame", ".dwframe", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12329 { { ".debug_info", ".zdebug_info", ".dwinfo", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
12330 { { ".debug_line", ".zdebug_line", ".dwline", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12331 { { ".debug_pubnames", ".zdebug_pubnames", ".dwpbnms", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
12332 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
12333 { { ".eh_frame", "", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12334 { { ".debug_macinfo", ".zdebug_macinfo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12335 { { ".debug_macro", ".zdebug_macro", ".dwmac", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12336 { { ".debug_str", ".zdebug_str", ".dwstr", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12337 { { ".debug_line_str", ".zdebug_line_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12338 { { ".debug_loc", ".zdebug_loc", ".dwloc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12339 { { ".debug_loclists", ".zdebug_loclists", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12340 { { ".debug_loclists.dwo", ".zdebug_loclists.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12341 { { ".debug_pubtypes", ".zdebug_pubtypes", ".dwpbtyp", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
12342 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
12343 { { ".debug_ranges", ".zdebug_ranges", ".dwrnges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12344 { { ".debug_rnglists", ".zdebug_rnglists", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12345 { { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12346 { { ".debug_static_func", ".zdebug_static_func", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12347 { { ".debug_static_vars", ".zdebug_static_vars", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12348 { { ".debug_types", ".zdebug_types", "", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
12349 { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12350 { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
12351 { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
12352 { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
12353 { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
12354 { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
12355 { { ".debug_info.dwo", ".zdebug_info.dwo", "", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
12356 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", "", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12357 { { ".debug_types.dwo", ".zdebug_types.dwo", "", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
12358 { { ".debug_line.dwo", ".zdebug_line.dwo", "", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12359 { { ".debug_loc.dwo", ".zdebug_loc.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12360 { { ".debug_macro.dwo", ".zdebug_macro.dwo", "", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12361 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12362 { { ".debug_str.dwo", ".zdebug_str.dwo", "", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
12363 { { ".debug_str_offsets", ".zdebug_str_offsets", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12364 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12365 { { ".debug_addr", ".zdebug_addr", "", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
12366 { { ".debug_cu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12367 { { ".debug_tu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12368 { { ".gnu_debuglink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12369 { { ".gnu_debugaltlink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12370 { { ".debug_sup", "", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
12371 /* Separate debug info files can containt their own .debug_str section,
12372 and this might be in *addition* to a .debug_str section already present
12373 in the main file. Hence we need to have two entries for .debug_str. */
12374 { { ".debug_str", ".zdebug_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12375 { { ".note.gnu.build-id", "", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12376 };
12377
12378 /* A static assertion. */
12379 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];