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