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