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