PR29870, objdump SEGV in display_debug_lines_decoded dwarf.c:5524
[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 %#" 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: <%#" 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: <%#" 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: <%#" 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_ns (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: <%#" 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: <%#" 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 (" <%#" 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 (" <%#" 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 <%#" 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 <%#" 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: <%#" PRIx64 ">",
1647 cu_offset + uvalue);
1648 break;
1649 case DW_OP_addrx:
1650 READ_ULEB (uvalue, data, end);
1651 printf ("DW_OP_addrx <%#" PRIx64 ">", uvalue);
1652 break;
1653 case DW_OP_GNU_addr_index:
1654 READ_ULEB (uvalue, data, end);
1655 printf ("DW_OP_GNU_addr_index <%#" PRIx64 ">", uvalue);
1656 break;
1657 case DW_OP_GNU_const_index:
1658 READ_ULEB (uvalue, data, end);
1659 printf ("DW_OP_GNU_const_index <%#" 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: <%#" 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 %#x)"), op);
1720 else
1721 printf (_("(Unknown location op %#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<%#" 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<%#" PRIx64 ">", delimiter, uvalue);
2520 else
2521 printf ("%c<alt %#" 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<%#" 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 ("%c%#" 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 ("%c%#" PRIx64, delimiter, utmp);
2570 }
2571 break;
2572
2573 case DW_FORM_data16:
2574 if (!do_loc)
2575 {
2576 if (uvalue_hi == 0)
2577 printf (" %#" PRIx64, uvalue);
2578 else
2579 printf (" %#" 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: %#" PRIx64 "): %s"),
2634 delimiter, uvalue, fetch_indirect_string (uvalue));
2635 else
2636 printf (_("%c(indirect string, offset: %#" 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: %#" PRIx64 "): %s"),
2647 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2648 else
2649 printf (_("%c(indirect line string, offset: %#" 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: %#" PRIx64 "): %s"),
2671 delimiter, uvalue, strng);
2672 else
2673 printf (_("%c(indexed string: %#" 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: %#" PRIx64 ") %s"),
2684 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2685 else
2686 printf (_("%c(alt indirect string, offset: %#" 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: %#" 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: %#" PRIx64 "): %#" PRIx64),
2780 delimiter, uvalue, idx);
2781 }
2782 break;
2783
2784 case DW_FORM_strp_sup:
2785 if (!do_loc)
2786 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2787 break;
2788
2789 default:
2790 warn (_("Unrecognized form: %#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 /* Free memory allocated for one unit in debug_information. */
3513
3514 static void
3515 free_debug_information (debug_info *ent)
3516 {
3517 if (ent->max_loc_offsets)
3518 {
3519 free (ent->loc_offsets);
3520 free (ent->loc_views);
3521 free (ent->have_frame_base);
3522 }
3523 if (ent->max_range_lists)
3524 free (ent->range_lists);
3525 }
3526
3527 /* Process the contents of a .debug_info section.
3528 If do_loc is TRUE then we are scanning for location lists and dwo tags
3529 and we do not want to display anything to the user.
3530 If do_types is TRUE, we are processing a .debug_types section instead of
3531 a .debug_info section.
3532 The information displayed is restricted by the values in DWARF_START_DIE
3533 and DWARF_CUTOFF_LEVEL.
3534 Returns TRUE upon success. Otherwise an error or warning message is
3535 printed and FALSE is returned. */
3536
3537 static bool
3538 process_debug_info (struct dwarf_section * section,
3539 void *file,
3540 enum dwarf_section_display_enum abbrev_sec,
3541 bool do_loc,
3542 bool do_types)
3543 {
3544 unsigned char *start = section->start;
3545 unsigned char *end = start + section->size;
3546 unsigned char *section_begin;
3547 unsigned int unit;
3548 unsigned int num_units = 0;
3549
3550 /* First scan the section to get the number of comp units.
3551 Length sanity checks are done here. */
3552 for (section_begin = start, num_units = 0; section_begin < end;
3553 num_units ++)
3554 {
3555 uint64_t length;
3556
3557 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3558 will be the length. For a 64-bit DWARF section, it'll be
3559 the escape code 0xffffffff followed by an 8 byte length. */
3560 SAFE_BYTE_GET_AND_INC (length, section_begin, 4, end);
3561
3562 if (length == 0xffffffff)
3563 SAFE_BYTE_GET_AND_INC (length, section_begin, 8, end);
3564 else if (length >= 0xfffffff0 && length < 0xffffffff)
3565 {
3566 warn (_("Reserved length value (%#" PRIx64 ") found in section %s\n"),
3567 length, section->name);
3568 return false;
3569 }
3570
3571 /* Negative values are illegal, they may even cause infinite
3572 looping. This can happen if we can't accurately apply
3573 relocations to an object file, or if the file is corrupt. */
3574 if (length > (size_t) (end - section_begin))
3575 {
3576 warn (_("Corrupt unit length (got %#" PRIx64
3577 " expected at most %#tx) in section %s\n"),
3578 length, end - section_begin, section->name);
3579 return false;
3580 }
3581 section_begin += length;
3582 }
3583
3584 if (num_units == 0)
3585 {
3586 error (_("No comp units in %s section ?\n"), section->name);
3587 return false;
3588 }
3589
3590 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3591 && num_debug_info_entries == 0
3592 && ! do_types)
3593 {
3594
3595 /* Then allocate an array to hold the information. */
3596 debug_information = (debug_info *) cmalloc (num_units,
3597 sizeof (* debug_information));
3598 if (debug_information == NULL)
3599 {
3600 error (_("Not enough memory for a debug info array of %u entries\n"),
3601 num_units);
3602 alloc_num_debug_info_entries = num_debug_info_entries = 0;
3603 return false;
3604 }
3605
3606 /* PR 17531: file: 92ca3797.
3607 We cannot rely upon the debug_information array being initialised
3608 before it is used. A corrupt file could easily contain references
3609 to a unit for which information has not been made available. So
3610 we ensure that the array is zeroed here. */
3611 memset (debug_information, 0, num_units * sizeof (*debug_information));
3612
3613 alloc_num_debug_info_entries = num_units;
3614 }
3615
3616 if (!do_loc)
3617 {
3618 load_debug_section_with_follow (str, file);
3619 load_debug_section_with_follow (line_str, file);
3620 load_debug_section_with_follow (str_dwo, file);
3621 load_debug_section_with_follow (str_index, file);
3622 load_debug_section_with_follow (str_index_dwo, file);
3623 load_debug_section_with_follow (debug_addr, file);
3624 }
3625
3626 load_debug_section_with_follow (abbrev_sec, file);
3627 load_debug_section_with_follow (loclists, file);
3628 load_debug_section_with_follow (rnglists, file);
3629 load_debug_section_with_follow (loclists_dwo, file);
3630 load_debug_section_with_follow (rnglists_dwo, file);
3631
3632 if (debug_displays [abbrev_sec].section.start == NULL)
3633 {
3634 warn (_("Unable to locate %s section!\n"),
3635 debug_displays [abbrev_sec].section.uncompressed_name);
3636 return false;
3637 }
3638
3639 if (!do_loc && dwarf_start_die == 0)
3640 introduce (section, false);
3641
3642 free_all_abbrevs ();
3643
3644 /* In order to be able to resolve DW_FORM_ref_addr forms we need
3645 to load *all* of the abbrevs for all CUs in this .debug_info
3646 section. This does effectively mean that we (partially) read
3647 every CU header twice. */
3648 for (section_begin = start; start < end;)
3649 {
3650 DWARF2_Internal_CompUnit compunit;
3651 unsigned char *hdrptr;
3652 uint64_t abbrev_base;
3653 size_t abbrev_size;
3654 uint64_t cu_offset;
3655 unsigned int offset_size;
3656 struct cu_tu_set *this_set;
3657 unsigned char *end_cu;
3658
3659 hdrptr = start;
3660 cu_offset = start - section_begin;
3661
3662 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3663
3664 if (compunit.cu_length == 0xffffffff)
3665 {
3666 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3667 offset_size = 8;
3668 }
3669 else
3670 offset_size = 4;
3671 end_cu = hdrptr + compunit.cu_length;
3672
3673 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3674
3675 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3676
3677 if (compunit.cu_version < 5)
3678 {
3679 compunit.cu_unit_type = DW_UT_compile;
3680 /* Initialize it due to a false compiler warning. */
3681 compunit.cu_pointer_size = -1;
3682 }
3683 else
3684 {
3685 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3686 do_types = (compunit.cu_unit_type == DW_UT_type);
3687
3688 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3689 }
3690
3691 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size,
3692 end_cu);
3693
3694 if (compunit.cu_unit_type == DW_UT_split_compile
3695 || compunit.cu_unit_type == DW_UT_skeleton)
3696 {
3697 uint64_t dwo_id;
3698 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3699 }
3700
3701 if (this_set == NULL)
3702 {
3703 abbrev_base = 0;
3704 abbrev_size = debug_displays [abbrev_sec].section.size;
3705 }
3706 else
3707 {
3708 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3709 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3710 }
3711
3712 abbrev_list *list;
3713 abbrev_list *free_list;
3714 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3715 abbrev_base, abbrev_size,
3716 compunit.cu_abbrev_offset,
3717 &free_list);
3718 start = end_cu;
3719 if (list != NULL && list->first_abbrev != NULL)
3720 record_abbrev_list_for_cu (cu_offset, start - section_begin,
3721 list, free_list);
3722 else if (free_list != NULL)
3723 free_abbrev_list (free_list);
3724 }
3725
3726 for (start = section_begin, unit = 0; start < end; unit++)
3727 {
3728 DWARF2_Internal_CompUnit compunit;
3729 unsigned char *hdrptr;
3730 unsigned char *tags;
3731 int level, last_level, saved_level;
3732 uint64_t cu_offset;
3733 unsigned int offset_size;
3734 uint64_t signature = 0;
3735 uint64_t type_offset = 0;
3736 struct cu_tu_set *this_set;
3737 uint64_t abbrev_base;
3738 size_t abbrev_size;
3739 unsigned char *end_cu;
3740
3741 hdrptr = start;
3742 cu_offset = start - section_begin;
3743
3744 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3745
3746 if (compunit.cu_length == 0xffffffff)
3747 {
3748 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3749 offset_size = 8;
3750 }
3751 else
3752 offset_size = 4;
3753 end_cu = hdrptr + compunit.cu_length;
3754
3755 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3756
3757 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3758
3759 if (compunit.cu_version < 5)
3760 {
3761 compunit.cu_unit_type = DW_UT_compile;
3762 /* Initialize it due to a false compiler warning. */
3763 compunit.cu_pointer_size = -1;
3764 }
3765 else
3766 {
3767 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3768 do_types = (compunit.cu_unit_type == DW_UT_type);
3769
3770 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3771 }
3772
3773 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end_cu);
3774
3775 if (this_set == NULL)
3776 {
3777 abbrev_base = 0;
3778 abbrev_size = debug_displays [abbrev_sec].section.size;
3779 }
3780 else
3781 {
3782 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3783 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3784 }
3785
3786 if (compunit.cu_version < 5)
3787 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3788
3789 bool do_dwo_id = false;
3790 uint64_t dwo_id = 0;
3791 if (compunit.cu_unit_type == DW_UT_split_compile
3792 || compunit.cu_unit_type == DW_UT_skeleton)
3793 {
3794 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3795 do_dwo_id = true;
3796 }
3797
3798 /* PR 17512: file: 001-108546-0.001:0.1. */
3799 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3800 {
3801 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3802 compunit.cu_pointer_size, offset_size);
3803 compunit.cu_pointer_size = offset_size;
3804 }
3805
3806 if (do_types)
3807 {
3808 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, end_cu);
3809 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end_cu);
3810 }
3811
3812 if (dwarf_start_die >= (size_t) (end_cu - section_begin))
3813 {
3814 start = end_cu;
3815 continue;
3816 }
3817
3818 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3819 && num_debug_info_entries == 0
3820 && alloc_num_debug_info_entries > unit
3821 && ! do_types)
3822 {
3823 free_debug_information (&debug_information[unit]);
3824 memset (&debug_information[unit], 0, sizeof (*debug_information));
3825 debug_information[unit].pointer_size = compunit.cu_pointer_size;
3826 debug_information[unit].offset_size = offset_size;
3827 debug_information[unit].dwarf_version = compunit.cu_version;
3828 debug_information[unit].cu_offset = cu_offset;
3829 debug_information[unit].addr_base = DEBUG_INFO_UNAVAILABLE;
3830 debug_information[unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
3831 }
3832
3833 if (!do_loc && dwarf_start_die == 0)
3834 {
3835 printf (_(" Compilation Unit @ offset %#" PRIx64 ":\n"),
3836 cu_offset);
3837 printf (_(" Length: %#" PRIx64 " (%s)\n"),
3838 compunit.cu_length,
3839 offset_size == 8 ? "64-bit" : "32-bit");
3840 printf (_(" Version: %d\n"), compunit.cu_version);
3841 if (compunit.cu_version >= 5)
3842 {
3843 const char *name = get_DW_UT_name (compunit.cu_unit_type);
3844
3845 printf (_(" Unit Type: %s (%x)\n"),
3846 name ? name : "???",
3847 compunit.cu_unit_type);
3848 }
3849 printf (_(" Abbrev Offset: %#" PRIx64 "\n"),
3850 compunit.cu_abbrev_offset);
3851 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
3852 if (do_types)
3853 {
3854 printf (_(" Signature: %#" PRIx64 "\n"), signature);
3855 printf (_(" Type Offset: %#" PRIx64 "\n"), type_offset);
3856 }
3857 if (do_dwo_id)
3858 printf (_(" DWO ID: %#" PRIx64 "\n"), dwo_id);
3859 if (this_set != NULL)
3860 {
3861 uint64_t *offsets = this_set->section_offsets;
3862 size_t *sizes = this_set->section_sizes;
3863
3864 printf (_(" Section contributions:\n"));
3865 printf (_(" .debug_abbrev.dwo: %#" PRIx64 " %#zx\n"),
3866 offsets[DW_SECT_ABBREV], sizes[DW_SECT_ABBREV]);
3867 printf (_(" .debug_line.dwo: %#" PRIx64 " %#zx\n"),
3868 offsets[DW_SECT_LINE], sizes[DW_SECT_LINE]);
3869 printf (_(" .debug_loc.dwo: %#" PRIx64 " %#zx\n"),
3870 offsets[DW_SECT_LOC], sizes[DW_SECT_LOC]);
3871 printf (_(" .debug_str_offsets.dwo: %#" PRIx64 " %#zx\n"),
3872 offsets[DW_SECT_STR_OFFSETS], sizes[DW_SECT_STR_OFFSETS]);
3873 }
3874 }
3875
3876 tags = hdrptr;
3877 start = end_cu;
3878
3879 if (compunit.cu_version < 2 || compunit.cu_version > 5)
3880 {
3881 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
3882 "unsupported version number: %d.\n"),
3883 cu_offset, compunit.cu_version);
3884 continue;
3885 }
3886
3887 if (compunit.cu_unit_type != DW_UT_compile
3888 && compunit.cu_unit_type != DW_UT_partial
3889 && compunit.cu_unit_type != DW_UT_type
3890 && compunit.cu_unit_type != DW_UT_split_compile
3891 && compunit.cu_unit_type != DW_UT_skeleton)
3892 {
3893 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
3894 "unsupported unit type: %d.\n"),
3895 cu_offset, compunit.cu_unit_type);
3896 continue;
3897 }
3898
3899 /* Process the abbrevs used by this compilation unit. */
3900 abbrev_list *list;
3901 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3902 abbrev_base, abbrev_size,
3903 compunit.cu_abbrev_offset, NULL);
3904 level = 0;
3905 last_level = level;
3906 saved_level = -1;
3907 while (tags < start)
3908 {
3909 unsigned long abbrev_number;
3910 unsigned long die_offset;
3911 abbrev_entry *entry;
3912 abbrev_attr *attr;
3913 int do_printing = 1;
3914
3915 die_offset = tags - section_begin;
3916
3917 READ_ULEB (abbrev_number, tags, start);
3918
3919 /* A null DIE marks the end of a list of siblings or it may also be
3920 a section padding. */
3921 if (abbrev_number == 0)
3922 {
3923 /* Check if it can be a section padding for the last CU. */
3924 if (level == 0 && start == end)
3925 {
3926 unsigned char *chk;
3927
3928 for (chk = tags; chk < start; chk++)
3929 if (*chk != 0)
3930 break;
3931 if (chk == start)
3932 break;
3933 }
3934
3935 if (!do_loc && die_offset >= dwarf_start_die
3936 && (dwarf_cutoff_level == -1
3937 || level < dwarf_cutoff_level))
3938 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
3939 level, die_offset);
3940
3941 --level;
3942 if (level < 0)
3943 {
3944 static unsigned num_bogus_warns = 0;
3945
3946 if (num_bogus_warns < 3)
3947 {
3948 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
3949 die_offset, section->name);
3950 num_bogus_warns ++;
3951 if (num_bogus_warns == 3)
3952 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
3953 }
3954 }
3955 if (dwarf_start_die != 0 && level < saved_level)
3956 return true;
3957 continue;
3958 }
3959
3960 if (!do_loc)
3961 {
3962 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
3963 do_printing = 0;
3964 else
3965 {
3966 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
3967 saved_level = level;
3968 do_printing = (dwarf_cutoff_level == -1
3969 || level < dwarf_cutoff_level);
3970 if (do_printing)
3971 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
3972 level, die_offset, abbrev_number);
3973 else if (dwarf_cutoff_level == -1
3974 || last_level < dwarf_cutoff_level)
3975 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
3976 last_level = level;
3977 }
3978 }
3979
3980 /* Scan through the abbreviation list until we reach the
3981 correct entry. */
3982 entry = NULL;
3983 if (list != NULL)
3984 for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
3985 if (entry->number == abbrev_number)
3986 break;
3987
3988 if (entry == NULL)
3989 {
3990 if (!do_loc && do_printing)
3991 {
3992 printf ("\n");
3993 fflush (stdout);
3994 }
3995 warn (_("DIE at offset %#lx refers to abbreviation number %lu which does not exist\n"),
3996 die_offset, abbrev_number);
3997 return false;
3998 }
3999
4000 if (!do_loc && do_printing)
4001 printf (" (%s)\n", get_TAG_name (entry->tag));
4002
4003 switch (entry->tag)
4004 {
4005 default:
4006 need_base_address = 0;
4007 break;
4008 case DW_TAG_compile_unit:
4009 case DW_TAG_skeleton_unit:
4010 need_base_address = 1;
4011 need_dwo_info = do_loc;
4012 break;
4013 case DW_TAG_entry_point:
4014 case DW_TAG_subprogram:
4015 need_base_address = 0;
4016 /* Assuming that there is no DW_AT_frame_base. */
4017 have_frame_base = 0;
4018 break;
4019 }
4020
4021 debug_info *debug_info_p =
4022 (debug_information && unit < alloc_num_debug_info_entries)
4023 ? debug_information + unit : NULL;
4024
4025 assert (!debug_info_p
4026 || (debug_info_p->num_loc_offsets
4027 == debug_info_p->num_loc_views));
4028
4029 for (attr = entry->first_attr;
4030 attr && attr->attribute;
4031 attr = attr->next)
4032 {
4033 if (! do_loc && do_printing)
4034 /* Show the offset from where the tag was extracted. */
4035 printf (" <%tx>", tags - section_begin);
4036 tags = read_and_display_attr (attr->attribute,
4037 attr->form,
4038 attr->implicit_const,
4039 section_begin,
4040 tags,
4041 start,
4042 cu_offset,
4043 compunit.cu_pointer_size,
4044 offset_size,
4045 compunit.cu_version,
4046 debug_info_p,
4047 do_loc || ! do_printing,
4048 section,
4049 this_set,
4050 level);
4051 }
4052
4053 /* If a locview attribute appears before a location one,
4054 make sure we don't associate it with an earlier
4055 loclist. */
4056 if (debug_info_p)
4057 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
4058 {
4059 case 1:
4060 debug_info_p->loc_views [debug_info_p->num_loc_views] = -1;
4061 debug_info_p->num_loc_views++;
4062 assert (debug_info_p->num_loc_views
4063 == debug_info_p->num_loc_offsets);
4064 break;
4065
4066 case 0:
4067 break;
4068
4069 case -1:
4070 warn(_("DIE has locviews without loclist\n"));
4071 debug_info_p->num_loc_views--;
4072 break;
4073
4074 default:
4075 assert (0);
4076 }
4077
4078 if (entry->children)
4079 ++level;
4080 }
4081 if (list != NULL)
4082 free_abbrev_list (list);
4083 }
4084
4085 /* Set num_debug_info_entries here so that it can be used to check if
4086 we need to process .debug_loc and .debug_ranges sections. */
4087 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
4088 && num_debug_info_entries == 0
4089 && ! do_types)
4090 {
4091 if (num_units > alloc_num_debug_info_entries)
4092 num_debug_info_entries = alloc_num_debug_info_entries;
4093 else
4094 num_debug_info_entries = num_units;
4095 }
4096
4097 if (!do_loc)
4098 printf ("\n");
4099
4100 return true;
4101 }
4102
4103 /* Locate and scan the .debug_info section in the file and record the pointer
4104 sizes and offsets for the compilation units in it. Usually an executable
4105 will have just one pointer size, but this is not guaranteed, and so we try
4106 not to make any assumptions. Returns zero upon failure, or the number of
4107 compilation units upon success. */
4108
4109 static unsigned int
4110 load_debug_info (void * file)
4111 {
4112 /* If we have already tried and failed to load the .debug_info
4113 section then do not bother to repeat the task. */
4114 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4115 return 0;
4116
4117 /* If we already have the information there is nothing else to do. */
4118 if (num_debug_info_entries > 0)
4119 return num_debug_info_entries;
4120
4121 /* If this is a DWARF package file, load the CU and TU indexes. */
4122 (void) load_cu_tu_indexes (file);
4123
4124 if (load_debug_section_with_follow (info, file)
4125 && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
4126 return num_debug_info_entries;
4127
4128 if (load_debug_section_with_follow (info_dwo, file)
4129 && process_debug_info (&debug_displays [info_dwo].section, file,
4130 abbrev_dwo, true, false))
4131 return num_debug_info_entries;
4132
4133 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
4134 return 0;
4135 }
4136
4137 /* Read a DWARF .debug_line section header starting at DATA.
4138 Upon success returns an updated DATA pointer and the LINFO
4139 structure and the END_OF_SEQUENCE pointer will be filled in.
4140 Otherwise returns NULL. */
4141
4142 static unsigned char *
4143 read_debug_line_header (struct dwarf_section * section,
4144 unsigned char * data,
4145 unsigned char * end,
4146 DWARF2_Internal_LineInfo * linfo,
4147 unsigned char ** end_of_sequence)
4148 {
4149 unsigned char *hdrptr;
4150
4151 /* Extract information from the Line Number Program Header.
4152 (section 6.2.4 in the Dwarf3 doc). */
4153 hdrptr = data;
4154
4155 /* Get and check the length of the block. */
4156 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
4157
4158 if (linfo->li_length == 0xffffffff)
4159 {
4160 /* This section is 64-bit DWARF 3. */
4161 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
4162 linfo->li_offset_size = 8;
4163 }
4164 else
4165 linfo->li_offset_size = 4;
4166
4167 if (linfo->li_length > (size_t) (end - hdrptr))
4168 {
4169 /* If the length field has a relocation against it, then we should
4170 not complain if it is inaccurate (and probably negative). This
4171 happens in object files when the .debug_line section is actually
4172 comprised of several different .debug_line.* sections, (some of
4173 which may be removed by linker garbage collection), and a relocation
4174 is used to compute the correct length once that is done. */
4175 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
4176 {
4177 linfo->li_length = end - hdrptr;
4178 }
4179 else
4180 {
4181 warn (_("The length field (%#" PRIx64 ")"
4182 " in the debug_line header is wrong"
4183 " - the section is too small\n"),
4184 linfo->li_length);
4185 return NULL;
4186 }
4187 }
4188 end = hdrptr + linfo->li_length;
4189
4190 /* Get and check the version number. */
4191 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4192
4193 if (linfo->li_version != 2
4194 && linfo->li_version != 3
4195 && linfo->li_version != 4
4196 && linfo->li_version != 5)
4197 {
4198 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4199 "is currently supported.\n"));
4200 return NULL;
4201 }
4202
4203 if (linfo->li_version >= 5)
4204 {
4205 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
4206
4207 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4208 if (linfo->li_segment_size != 0)
4209 {
4210 warn (_("The %s section contains "
4211 "unsupported segment selector size: %d.\n"),
4212 section->name, linfo->li_segment_size);
4213 return NULL;
4214 }
4215 }
4216
4217 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4218 linfo->li_offset_size, end);
4219 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
4220
4221 if (linfo->li_version >= 4)
4222 {
4223 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
4224
4225 if (linfo->li_max_ops_per_insn == 0)
4226 {
4227 warn (_("Invalid maximum operations per insn.\n"));
4228 return NULL;
4229 }
4230 }
4231 else
4232 linfo->li_max_ops_per_insn = 1;
4233
4234 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
4235 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
4236 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4237 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
4238
4239 *end_of_sequence = end;
4240 return hdrptr;
4241 }
4242
4243 static unsigned char *
4244 display_formatted_table (unsigned char *data,
4245 unsigned char *start,
4246 unsigned char *end,
4247 const DWARF2_Internal_LineInfo *linfo,
4248 struct dwarf_section *section,
4249 bool is_dir)
4250 {
4251 unsigned char *format_start, format_count, *format, formati;
4252 uint64_t data_count, datai;
4253 unsigned int namepass, last_entry = 0;
4254 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
4255
4256 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4257 if (do_checks && format_count > 5)
4258 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4259 table_name, format_count);
4260
4261 format_start = data;
4262 for (formati = 0; formati < format_count; formati++)
4263 {
4264 SKIP_ULEB (data, end);
4265 SKIP_ULEB (data, end);
4266 if (data >= end)
4267 {
4268 warn (_("%s: Corrupt format description entry\n"), table_name);
4269 return data;
4270 }
4271 }
4272
4273 READ_ULEB (data_count, data, end);
4274 if (data_count == 0)
4275 {
4276 printf (_("\n The %s is empty.\n"), table_name);
4277 return data;
4278 }
4279 else if (data >= end)
4280 {
4281 warn (_("%s: Corrupt entry count - expected %#" PRIx64
4282 " but none found\n"), table_name, data_count);
4283 return data;
4284 }
4285
4286 else if (format_count == 0)
4287 {
4288 warn (_("%s: format count is zero, but the table is not empty\n"),
4289 table_name);
4290 return end;
4291 }
4292
4293 printf (_("\n The %s (offset %#tx, lines %" PRIu64 ", columns %u):\n"),
4294 table_name, data - start, data_count, format_count);
4295
4296 printf (_(" Entry"));
4297 /* Delay displaying name as the last entry for better screen layout. */
4298 for (namepass = 0; namepass < 2; namepass++)
4299 {
4300 format = format_start;
4301 for (formati = 0; formati < format_count; formati++)
4302 {
4303 uint64_t content_type;
4304
4305 READ_ULEB (content_type, format, end);
4306 if ((content_type == DW_LNCT_path) == (namepass == 1))
4307 switch (content_type)
4308 {
4309 case DW_LNCT_path:
4310 printf (_("\tName"));
4311 break;
4312 case DW_LNCT_directory_index:
4313 printf (_("\tDir"));
4314 break;
4315 case DW_LNCT_timestamp:
4316 printf (_("\tTime"));
4317 break;
4318 case DW_LNCT_size:
4319 printf (_("\tSize"));
4320 break;
4321 case DW_LNCT_MD5:
4322 printf (_("\tMD5\t\t\t"));
4323 break;
4324 default:
4325 printf (_("\t(Unknown format content type %" PRIu64 ")"),
4326 content_type);
4327 }
4328 SKIP_ULEB (format, end);
4329 }
4330 }
4331 putchar ('\n');
4332
4333 for (datai = 0; datai < data_count; datai++)
4334 {
4335 unsigned char *datapass = data;
4336
4337 printf (" %d", last_entry++);
4338 /* Delay displaying name as the last entry for better screen layout. */
4339 for (namepass = 0; namepass < 2; namepass++)
4340 {
4341 format = format_start;
4342 data = datapass;
4343 for (formati = 0; formati < format_count; formati++)
4344 {
4345 uint64_t content_type, form;
4346
4347 READ_ULEB (content_type, format, end);
4348 READ_ULEB (form, format, end);
4349 data = read_and_display_attr_value (0, form, 0, start, data, end,
4350 0, 0, linfo->li_offset_size,
4351 linfo->li_version, NULL,
4352 ((content_type == DW_LNCT_path) != (namepass == 1)),
4353 section, NULL, '\t', -1);
4354 }
4355 }
4356
4357 if (data >= end && (datai < data_count - 1))
4358 {
4359 warn (_("\n%s: Corrupt entries list\n"), table_name);
4360 return data;
4361 }
4362 putchar ('\n');
4363 }
4364 return data;
4365 }
4366
4367 static int
4368 display_debug_sup (struct dwarf_section * section,
4369 void * file ATTRIBUTE_UNUSED)
4370 {
4371 unsigned char * start = section->start;
4372 unsigned char * end = section->start + section->size;
4373 unsigned int version;
4374 char is_supplementary;
4375 const unsigned char * sup_filename;
4376 size_t sup_filename_len;
4377 unsigned int num_read;
4378 int status;
4379 uint64_t checksum_len;
4380
4381
4382 introduce (section, true);
4383 if (section->size < 4)
4384 {
4385 error (_("corrupt .debug_sup section: size is too small\n"));
4386 return 0;
4387 }
4388
4389 /* Read the data. */
4390 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4391 if (version < 5)
4392 warn (_("corrupt .debug_sup section: version < 5"));
4393
4394 SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4395 if (is_supplementary != 0 && is_supplementary != 1)
4396 warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4397
4398 sup_filename = start;
4399 if (is_supplementary && sup_filename[0] != 0)
4400 warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4401
4402 sup_filename_len = strnlen ((const char *) start, end - start);
4403 if (sup_filename_len == (size_t) (end - start))
4404 {
4405 error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4406 return 0;
4407 }
4408 start += sup_filename_len + 1;
4409
4410 checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
4411 if (status)
4412 {
4413 error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4414 checksum_len = 0;
4415 }
4416 start += num_read;
4417 if (checksum_len > (size_t) (end - start))
4418 {
4419 error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4420 checksum_len = end - start;
4421 }
4422 else if (checksum_len < (size_t) (end - start))
4423 {
4424 warn (_("corrupt .debug_sup section: there are %#" PRIx64
4425 " extra, unused bytes at the end of the section\n"),
4426 (end - start) - checksum_len);
4427 }
4428
4429 printf (_(" Version: %u\n"), version);
4430 printf (_(" Is Supp: %u\n"), is_supplementary);
4431 printf (_(" Filename: %s\n"), sup_filename);
4432 printf (_(" Checksum Len: %" PRIu64 "\n"), checksum_len);
4433 if (checksum_len > 0)
4434 {
4435 printf (_(" Checksum: "));
4436 while (checksum_len--)
4437 printf ("0x%x ", * start++ );
4438 printf ("\n");
4439 }
4440 return 1;
4441 }
4442
4443 static int
4444 display_debug_lines_raw (struct dwarf_section * section,
4445 unsigned char * data,
4446 unsigned char * end,
4447 void * file)
4448 {
4449 unsigned char *start = section->start;
4450 int verbose_view = 0;
4451
4452 introduce (section, true);
4453
4454 while (data < end)
4455 {
4456 static DWARF2_Internal_LineInfo saved_linfo;
4457 DWARF2_Internal_LineInfo linfo;
4458 unsigned char *standard_opcodes;
4459 unsigned char *end_of_sequence;
4460 int i;
4461
4462 if (startswith (section->name, ".debug_line.")
4463 /* Note: the following does not apply to .debug_line.dwo sections.
4464 These are full debug_line sections. */
4465 && strcmp (section->name, ".debug_line.dwo") != 0)
4466 {
4467 /* Sections named .debug_line.<foo> are fragments of a .debug_line
4468 section containing just the Line Number Statements. They are
4469 created by the assembler and intended to be used alongside gcc's
4470 -ffunction-sections command line option. When the linker's
4471 garbage collection decides to discard a .text.<foo> section it
4472 can then also discard the line number information in .debug_line.<foo>.
4473
4474 Since the section is a fragment it does not have the details
4475 needed to fill out a LineInfo structure, so instead we use the
4476 details from the last full debug_line section that we processed. */
4477 end_of_sequence = end;
4478 standard_opcodes = NULL;
4479 linfo = saved_linfo;
4480 /* PR 17531: file: 0522b371. */
4481 if (linfo.li_line_range == 0)
4482 {
4483 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4484 return 0;
4485 }
4486 reset_state_machine (linfo.li_default_is_stmt);
4487 }
4488 else
4489 {
4490 unsigned char * hdrptr;
4491
4492 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4493 & end_of_sequence)) == NULL)
4494 return 0;
4495
4496 printf (_(" Offset: %#tx\n"), data - start);
4497 printf (_(" Length: %" PRId64 "\n"), linfo.li_length);
4498 printf (_(" DWARF Version: %d\n"), linfo.li_version);
4499 if (linfo.li_version >= 5)
4500 {
4501 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
4502 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
4503 }
4504 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
4505 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
4506 if (linfo.li_version >= 4)
4507 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4508 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
4509 printf (_(" Line Base: %d\n"), linfo.li_line_base);
4510 printf (_(" Line Range: %d\n"), linfo.li_line_range);
4511 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
4512
4513 /* PR 17512: file: 1665-6428-0.004. */
4514 if (linfo.li_line_range == 0)
4515 {
4516 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4517 linfo.li_line_range = 1;
4518 }
4519
4520 reset_state_machine (linfo.li_default_is_stmt);
4521
4522 /* Display the contents of the Opcodes table. */
4523 standard_opcodes = hdrptr;
4524
4525 /* PR 17512: file: 002-417945-0.004. */
4526 if (standard_opcodes + linfo.li_opcode_base >= end)
4527 {
4528 warn (_("Line Base extends beyond end of section\n"));
4529 return 0;
4530 }
4531
4532 printf (_("\n Opcodes:\n"));
4533
4534 for (i = 1; i < linfo.li_opcode_base; i++)
4535 printf (ngettext (" Opcode %d has %d arg\n",
4536 " Opcode %d has %d args\n",
4537 standard_opcodes[i - 1]),
4538 i, standard_opcodes[i - 1]);
4539
4540 /* Display the contents of the Directory table. */
4541 data = standard_opcodes + linfo.li_opcode_base - 1;
4542
4543 if (linfo.li_version >= 5)
4544 {
4545 load_debug_section_with_follow (line_str, file);
4546
4547 data = display_formatted_table (data, start, end, &linfo, section,
4548 true);
4549 data = display_formatted_table (data, start, end, &linfo, section,
4550 false);
4551 }
4552 else
4553 {
4554 if (*data == 0)
4555 printf (_("\n The Directory Table is empty.\n"));
4556 else
4557 {
4558 unsigned int last_dir_entry = 0;
4559
4560 printf (_("\n The Directory Table (offset %#tx):\n"),
4561 data - start);
4562
4563 while (data < end && *data != 0)
4564 {
4565 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
4566
4567 data += strnlen ((char *) data, end - data);
4568 if (data < end)
4569 data++;
4570 }
4571
4572 /* PR 17512: file: 002-132094-0.004. */
4573 if (data >= end - 1)
4574 break;
4575 }
4576
4577 /* Skip the NUL at the end of the table. */
4578 if (data < end)
4579 data++;
4580
4581 /* Display the contents of the File Name table. */
4582 if (data >= end || *data == 0)
4583 printf (_("\n The File Name Table is empty.\n"));
4584 else
4585 {
4586 printf (_("\n The File Name Table (offset %#tx):\n"),
4587 data - start);
4588 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
4589
4590 while (data < end && *data != 0)
4591 {
4592 unsigned char *name;
4593 uint64_t val;
4594
4595 printf (" %d\t", ++state_machine_regs.last_file_entry);
4596 name = data;
4597 data += strnlen ((char *) data, end - data);
4598 if (data < end)
4599 data++;
4600
4601 READ_ULEB (val, data, end);
4602 printf ("%" PRIu64 "\t", val);
4603 READ_ULEB (val, data, end);
4604 printf ("%" PRIu64 "\t", val);
4605 READ_ULEB (val, data, end);
4606 printf ("%" PRIu64 "\t", val);
4607 printf ("%.*s\n", (int)(end - name), name);
4608
4609 if (data >= end)
4610 {
4611 warn (_("Corrupt file name table entry\n"));
4612 break;
4613 }
4614 }
4615 }
4616
4617 /* Skip the NUL at the end of the table. */
4618 if (data < end)
4619 data++;
4620 }
4621
4622 putchar ('\n');
4623 saved_linfo = linfo;
4624 }
4625
4626 /* Now display the statements. */
4627 if (data >= end_of_sequence)
4628 printf (_(" No Line Number Statements.\n"));
4629 else
4630 {
4631 printf (_(" Line Number Statements:\n"));
4632
4633 while (data < end_of_sequence)
4634 {
4635 unsigned char op_code;
4636 int64_t adv;
4637 uint64_t uladv;
4638
4639 printf (" [0x%08tx]", data - start);
4640
4641 op_code = *data++;
4642
4643 if (op_code >= linfo.li_opcode_base)
4644 {
4645 op_code -= linfo.li_opcode_base;
4646 uladv = (op_code / linfo.li_line_range);
4647 if (linfo.li_max_ops_per_insn == 1)
4648 {
4649 uladv *= linfo.li_min_insn_length;
4650 state_machine_regs.address += uladv;
4651 if (uladv)
4652 state_machine_regs.view = 0;
4653 printf (_(" Special opcode %d: "
4654 "advance Address by %" PRIu64
4655 " to %#" PRIx64 "%s"),
4656 op_code, uladv, state_machine_regs.address,
4657 verbose_view && uladv
4658 ? _(" (reset view)") : "");
4659 }
4660 else
4661 {
4662 unsigned addrdelta
4663 = ((state_machine_regs.op_index + uladv)
4664 / linfo.li_max_ops_per_insn)
4665 * linfo.li_min_insn_length;
4666
4667 state_machine_regs.address += addrdelta;
4668 state_machine_regs.op_index
4669 = (state_machine_regs.op_index + uladv)
4670 % linfo.li_max_ops_per_insn;
4671 if (addrdelta)
4672 state_machine_regs.view = 0;
4673 printf (_(" Special opcode %d: "
4674 "advance Address by %" PRIu64
4675 " to %#" PRIx64 "[%d]%s"),
4676 op_code, uladv, state_machine_regs.address,
4677 state_machine_regs.op_index,
4678 verbose_view && addrdelta
4679 ? _(" (reset view)") : "");
4680 }
4681 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4682 state_machine_regs.line += adv;
4683 printf (_(" and Line by %" PRId64 " to %d"),
4684 adv, state_machine_regs.line);
4685 if (verbose_view || state_machine_regs.view)
4686 printf (_(" (view %u)\n"), state_machine_regs.view);
4687 else
4688 putchar ('\n');
4689 state_machine_regs.view++;
4690 }
4691 else
4692 switch (op_code)
4693 {
4694 case DW_LNS_extended_op:
4695 data += process_extended_line_op (data,
4696 linfo.li_default_is_stmt,
4697 end);
4698 break;
4699
4700 case DW_LNS_copy:
4701 printf (_(" Copy"));
4702 if (verbose_view || state_machine_regs.view)
4703 printf (_(" (view %u)\n"), state_machine_regs.view);
4704 else
4705 putchar ('\n');
4706 state_machine_regs.view++;
4707 break;
4708
4709 case DW_LNS_advance_pc:
4710 READ_ULEB (uladv, data, end);
4711 if (linfo.li_max_ops_per_insn == 1)
4712 {
4713 uladv *= linfo.li_min_insn_length;
4714 state_machine_regs.address += uladv;
4715 if (uladv)
4716 state_machine_regs.view = 0;
4717 printf (_(" Advance PC by %" PRIu64
4718 " to %#" PRIx64 "%s\n"),
4719 uladv, state_machine_regs.address,
4720 verbose_view && uladv
4721 ? _(" (reset view)") : "");
4722 }
4723 else
4724 {
4725 unsigned addrdelta
4726 = ((state_machine_regs.op_index + uladv)
4727 / linfo.li_max_ops_per_insn)
4728 * linfo.li_min_insn_length;
4729 state_machine_regs.address
4730 += addrdelta;
4731 state_machine_regs.op_index
4732 = (state_machine_regs.op_index + uladv)
4733 % linfo.li_max_ops_per_insn;
4734 if (addrdelta)
4735 state_machine_regs.view = 0;
4736 printf (_(" Advance PC by %" PRIu64
4737 " to %#" PRIx64 "[%d]%s\n"),
4738 uladv, state_machine_regs.address,
4739 state_machine_regs.op_index,
4740 verbose_view && addrdelta
4741 ? _(" (reset view)") : "");
4742 }
4743 break;
4744
4745 case DW_LNS_advance_line:
4746 READ_SLEB (adv, data, end);
4747 state_machine_regs.line += adv;
4748 printf (_(" Advance Line by %" PRId64 " to %d\n"),
4749 adv, state_machine_regs.line);
4750 break;
4751
4752 case DW_LNS_set_file:
4753 READ_ULEB (uladv, data, end);
4754 printf (_(" Set File Name to entry %" PRIu64
4755 " in the File Name Table\n"), uladv);
4756 state_machine_regs.file = uladv;
4757 break;
4758
4759 case DW_LNS_set_column:
4760 READ_ULEB (uladv, data, end);
4761 printf (_(" Set column to %" PRIu64 "\n"), uladv);
4762 state_machine_regs.column = uladv;
4763 break;
4764
4765 case DW_LNS_negate_stmt:
4766 adv = state_machine_regs.is_stmt;
4767 adv = ! adv;
4768 printf (_(" Set is_stmt to %" PRId64 "\n"), adv);
4769 state_machine_regs.is_stmt = adv;
4770 break;
4771
4772 case DW_LNS_set_basic_block:
4773 printf (_(" Set basic block\n"));
4774 state_machine_regs.basic_block = 1;
4775 break;
4776
4777 case DW_LNS_const_add_pc:
4778 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4779 if (linfo.li_max_ops_per_insn)
4780 {
4781 uladv *= linfo.li_min_insn_length;
4782 state_machine_regs.address += uladv;
4783 if (uladv)
4784 state_machine_regs.view = 0;
4785 printf (_(" Advance PC by constant %" PRIu64
4786 " to %#" PRIx64 "%s\n"),
4787 uladv, state_machine_regs.address,
4788 verbose_view && uladv
4789 ? _(" (reset view)") : "");
4790 }
4791 else
4792 {
4793 unsigned addrdelta
4794 = ((state_machine_regs.op_index + uladv)
4795 / linfo.li_max_ops_per_insn)
4796 * linfo.li_min_insn_length;
4797 state_machine_regs.address
4798 += addrdelta;
4799 state_machine_regs.op_index
4800 = (state_machine_regs.op_index + uladv)
4801 % linfo.li_max_ops_per_insn;
4802 if (addrdelta)
4803 state_machine_regs.view = 0;
4804 printf (_(" Advance PC by constant %" PRIu64
4805 " to %#" PRIx64 "[%d]%s\n"),
4806 uladv, state_machine_regs.address,
4807 state_machine_regs.op_index,
4808 verbose_view && addrdelta
4809 ? _(" (reset view)") : "");
4810 }
4811 break;
4812
4813 case DW_LNS_fixed_advance_pc:
4814 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4815 state_machine_regs.address += uladv;
4816 state_machine_regs.op_index = 0;
4817 printf (_(" Advance PC by fixed size amount %" PRIu64
4818 " to %#" PRIx64 "\n"),
4819 uladv, state_machine_regs.address);
4820 /* Do NOT reset view. */
4821 break;
4822
4823 case DW_LNS_set_prologue_end:
4824 printf (_(" Set prologue_end to true\n"));
4825 break;
4826
4827 case DW_LNS_set_epilogue_begin:
4828 printf (_(" Set epilogue_begin to true\n"));
4829 break;
4830
4831 case DW_LNS_set_isa:
4832 READ_ULEB (uladv, data, end);
4833 printf (_(" Set ISA to %" PRIu64 "\n"), uladv);
4834 break;
4835
4836 default:
4837 printf (_(" Unknown opcode %d with operands: "), op_code);
4838
4839 if (standard_opcodes != NULL)
4840 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4841 {
4842 READ_ULEB (uladv, data, end);
4843 printf ("%#" PRIx64 "%s", uladv, i == 1 ? "" : ", ");
4844 }
4845 putchar ('\n');
4846 break;
4847 }
4848 }
4849 putchar ('\n');
4850 }
4851 }
4852
4853 return 1;
4854 }
4855
4856 typedef struct
4857 {
4858 unsigned char *name;
4859 unsigned int directory_index;
4860 unsigned int modification_date;
4861 unsigned int length;
4862 } File_Entry;
4863
4864 /* Output a decoded representation of the .debug_line section. */
4865
4866 static int
4867 display_debug_lines_decoded (struct dwarf_section * section,
4868 unsigned char * start,
4869 unsigned char * data,
4870 unsigned char * end,
4871 void * fileptr)
4872 {
4873 static DWARF2_Internal_LineInfo saved_linfo;
4874
4875 introduce (section, false);
4876
4877 while (data < end)
4878 {
4879 /* This loop amounts to one iteration per compilation unit. */
4880 DWARF2_Internal_LineInfo linfo;
4881 unsigned char *standard_opcodes;
4882 unsigned char *end_of_sequence;
4883 int i;
4884 File_Entry *file_table = NULL;
4885 unsigned int n_files = 0;
4886 unsigned char **directory_table = NULL;
4887 uint64_t n_directories = 0;
4888
4889 if (startswith (section->name, ".debug_line.")
4890 /* Note: the following does not apply to .debug_line.dwo sections.
4891 These are full debug_line sections. */
4892 && strcmp (section->name, ".debug_line.dwo") != 0)
4893 {
4894 /* See comment in display_debug_lines_raw(). */
4895 end_of_sequence = end;
4896 standard_opcodes = NULL;
4897 linfo = saved_linfo;
4898 /* PR 17531: file: 0522b371. */
4899 if (linfo.li_line_range == 0)
4900 {
4901 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4902 return 0;
4903 }
4904 reset_state_machine (linfo.li_default_is_stmt);
4905 }
4906 else
4907 {
4908 unsigned char *hdrptr;
4909
4910 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4911 & end_of_sequence)) == NULL)
4912 return 0;
4913
4914 /* PR 17531: file: 0522b371. */
4915 if (linfo.li_line_range == 0)
4916 {
4917 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4918 linfo.li_line_range = 1;
4919 }
4920 reset_state_machine (linfo.li_default_is_stmt);
4921
4922 /* Save a pointer to the contents of the Opcodes table. */
4923 standard_opcodes = hdrptr;
4924
4925 /* Traverse the Directory table just to count entries. */
4926 data = standard_opcodes + linfo.li_opcode_base - 1;
4927 /* PR 20440 */
4928 if (data >= end)
4929 {
4930 warn (_("opcode base of %d extends beyond end of section\n"),
4931 linfo.li_opcode_base);
4932 return 0;
4933 }
4934
4935 if (linfo.li_version >= 5)
4936 {
4937 unsigned char *format_start, format_count, *format;
4938 uint64_t formati, entryi;
4939
4940 load_debug_section_with_follow (line_str, fileptr);
4941
4942 /* Skip directories format. */
4943 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4944 if (do_checks && format_count > 1)
4945 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
4946 format_count);
4947 format_start = data;
4948 for (formati = 0; formati < format_count; formati++)
4949 {
4950 SKIP_ULEB (data, end);
4951 SKIP_ULEB (data, end);
4952 }
4953
4954 READ_ULEB (n_directories, data, end);
4955 if (data >= end)
4956 {
4957 warn (_("Corrupt directories list\n"));
4958 break;
4959 }
4960
4961 if (n_directories == 0)
4962 directory_table = NULL;
4963 else
4964 directory_table = (unsigned char **)
4965 xmalloc (n_directories * sizeof (unsigned char *));
4966
4967 for (entryi = 0; entryi < n_directories; entryi++)
4968 {
4969 unsigned char **pathp = &directory_table[entryi];
4970
4971 format = format_start;
4972 for (formati = 0; formati < format_count; formati++)
4973 {
4974 uint64_t content_type, form;
4975 uint64_t uvalue;
4976
4977 READ_ULEB (content_type, format, end);
4978 READ_ULEB (form, format, end);
4979 if (data >= end)
4980 {
4981 warn (_("Corrupt directories list\n"));
4982 break;
4983 }
4984 switch (content_type)
4985 {
4986 case DW_LNCT_path:
4987 switch (form)
4988 {
4989 case DW_FORM_string:
4990 *pathp = data;
4991 break;
4992 case DW_FORM_line_strp:
4993 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
4994 end);
4995 /* Remove const by the cast. */
4996 *pathp = (unsigned char *)
4997 fetch_indirect_line_string (uvalue);
4998 break;
4999 }
5000 break;
5001 }
5002 data = read_and_display_attr_value (0, form, 0, start,
5003 data, end, 0, 0,
5004 linfo.li_offset_size,
5005 linfo.li_version,
5006 NULL, 1, section,
5007 NULL, '\t', -1);
5008 }
5009 if (data >= end)
5010 {
5011 warn (_("Corrupt directories list\n"));
5012 break;
5013 }
5014 }
5015
5016 /* Skip files format. */
5017 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5018 if (do_checks && format_count > 5)
5019 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5020 format_count);
5021 format_start = data;
5022 for (formati = 0; formati < format_count; formati++)
5023 {
5024 SKIP_ULEB (data, end);
5025 SKIP_ULEB (data, end);
5026 }
5027
5028 READ_ULEB (n_files, data, end);
5029 if (data >= end && n_files > 0)
5030 {
5031 warn (_("Corrupt file name list\n"));
5032 break;
5033 }
5034
5035 if (n_files == 0)
5036 file_table = NULL;
5037 else
5038 file_table = (File_Entry *) xcalloc (1, n_files
5039 * sizeof (File_Entry));
5040
5041 for (entryi = 0; entryi < n_files; entryi++)
5042 {
5043 File_Entry *file = &file_table[entryi];
5044
5045 format = format_start;
5046 for (formati = 0; formati < format_count; formati++)
5047 {
5048 uint64_t content_type, form;
5049 uint64_t uvalue;
5050 unsigned char *tmp;
5051
5052 READ_ULEB (content_type, format, end);
5053 READ_ULEB (form, format, end);
5054 if (data >= end)
5055 {
5056 warn (_("Corrupt file name list\n"));
5057 break;
5058 }
5059 switch (content_type)
5060 {
5061 case DW_LNCT_path:
5062 switch (form)
5063 {
5064 case DW_FORM_string:
5065 file->name = data;
5066 break;
5067 case DW_FORM_line_strp:
5068 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5069 end);
5070 /* Remove const by the cast. */
5071 file->name = (unsigned char *)
5072 fetch_indirect_line_string (uvalue);
5073 break;
5074 }
5075 break;
5076 case DW_LNCT_directory_index:
5077 switch (form)
5078 {
5079 case DW_FORM_data1:
5080 SAFE_BYTE_GET (file->directory_index, data, 1,
5081 end);
5082 break;
5083 case DW_FORM_data2:
5084 SAFE_BYTE_GET (file->directory_index, data, 2,
5085 end);
5086 break;
5087 case DW_FORM_udata:
5088 tmp = data;
5089 READ_ULEB (file->directory_index, tmp, end);
5090 break;
5091 }
5092 break;
5093 }
5094 data = read_and_display_attr_value (0, form, 0, start,
5095 data, end, 0, 0,
5096 linfo.li_offset_size,
5097 linfo.li_version,
5098 NULL, 1, section,
5099 NULL, '\t', -1);
5100 }
5101 if (data >= end)
5102 {
5103 warn (_("Corrupt file name list\n"));
5104 break;
5105 }
5106 }
5107 }
5108 else
5109 {
5110 if (*data != 0)
5111 {
5112 unsigned char *ptr_directory_table = data;
5113
5114 while (data < end && *data != 0)
5115 {
5116 data += strnlen ((char *) data, end - data);
5117 if (data < end)
5118 data++;
5119 n_directories++;
5120 }
5121
5122 /* PR 20440 */
5123 if (data >= end)
5124 {
5125 warn (_("directory table ends unexpectedly\n"));
5126 n_directories = 0;
5127 break;
5128 }
5129
5130 /* Go through the directory table again to save the directories. */
5131 directory_table = (unsigned char **)
5132 xmalloc (n_directories * sizeof (unsigned char *));
5133
5134 i = 0;
5135 while (*ptr_directory_table != 0)
5136 {
5137 directory_table[i] = ptr_directory_table;
5138 ptr_directory_table
5139 += strlen ((char *) ptr_directory_table) + 1;
5140 i++;
5141 }
5142 }
5143 /* Skip the NUL at the end of the table. */
5144 data++;
5145
5146 /* Traverse the File Name table just to count the entries. */
5147 if (data < end && *data != 0)
5148 {
5149 unsigned char *ptr_file_name_table = data;
5150
5151 while (data < end && *data != 0)
5152 {
5153 /* Skip Name, directory index, last modification
5154 time and length of file. */
5155 data += strnlen ((char *) data, end - data);
5156 if (data < end)
5157 data++;
5158 SKIP_ULEB (data, end);
5159 SKIP_ULEB (data, end);
5160 SKIP_ULEB (data, end);
5161 n_files++;
5162 }
5163
5164 if (data >= end)
5165 {
5166 warn (_("file table ends unexpectedly\n"));
5167 n_files = 0;
5168 break;
5169 }
5170
5171 /* Go through the file table again to save the strings. */
5172 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5173
5174 i = 0;
5175 while (*ptr_file_name_table != 0)
5176 {
5177 file_table[i].name = ptr_file_name_table;
5178 ptr_file_name_table
5179 += strlen ((char *) ptr_file_name_table) + 1;
5180
5181 /* We are not interested in directory, time or size. */
5182 READ_ULEB (file_table[i].directory_index,
5183 ptr_file_name_table, end);
5184 READ_ULEB (file_table[i].modification_date,
5185 ptr_file_name_table, end);
5186 READ_ULEB (file_table[i].length,
5187 ptr_file_name_table, end);
5188 i++;
5189 }
5190 i = 0;
5191 }
5192
5193 /* Skip the NUL at the end of the table. */
5194 data++;
5195 }
5196
5197 /* Print the Compilation Unit's name and a header. */
5198 if (file_table == NULL)
5199 printf (_("CU: No directory table\n"));
5200 else if (directory_table == NULL)
5201 printf (_("CU: %s:\n"), file_table[0].name);
5202 else
5203 {
5204 unsigned int ix = file_table[0].directory_index;
5205 const char *directory;
5206
5207 if (ix == 0)
5208 directory = ".";
5209 /* PR 20439 */
5210 else if (n_directories == 0)
5211 directory = _("<unknown>");
5212 else if (ix > n_directories)
5213 {
5214 warn (_("directory index %u > number of directories %" PRIu64 "\n"),
5215 ix, n_directories);
5216 directory = _("<corrupt>");
5217 }
5218 else if (linfo.li_version >= 5)
5219 directory = (char *) directory_table[ix];
5220 else
5221 directory = (char *) directory_table[ix - 1];
5222
5223 if (do_wide)
5224 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
5225 else
5226 printf ("%s:\n", file_table[0].name);
5227 }
5228
5229 if (n_files > 0)
5230 printf (_("File name Line number Starting address View Stmt\n"));
5231 else
5232 printf (_("CU: Empty file name table\n"));
5233 saved_linfo = linfo;
5234 }
5235
5236 /* This loop iterates through the Dwarf Line Number Program. */
5237 while (data < end_of_sequence)
5238 {
5239 unsigned char op_code;
5240 int xop;
5241 int adv;
5242 unsigned long int uladv;
5243 int is_special_opcode = 0;
5244
5245 op_code = *data++;
5246 xop = op_code;
5247
5248 if (op_code >= linfo.li_opcode_base)
5249 {
5250 op_code -= linfo.li_opcode_base;
5251 uladv = (op_code / linfo.li_line_range);
5252 if (linfo.li_max_ops_per_insn == 1)
5253 {
5254 uladv *= linfo.li_min_insn_length;
5255 state_machine_regs.address += uladv;
5256 if (uladv)
5257 state_machine_regs.view = 0;
5258 }
5259 else
5260 {
5261 unsigned addrdelta
5262 = ((state_machine_regs.op_index + uladv)
5263 / linfo.li_max_ops_per_insn)
5264 * linfo.li_min_insn_length;
5265 state_machine_regs.address
5266 += addrdelta;
5267 state_machine_regs.op_index
5268 = (state_machine_regs.op_index + uladv)
5269 % linfo.li_max_ops_per_insn;
5270 if (addrdelta)
5271 state_machine_regs.view = 0;
5272 }
5273
5274 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5275 state_machine_regs.line += adv;
5276 is_special_opcode = 1;
5277 /* Increment view after printing this row. */
5278 }
5279 else
5280 switch (op_code)
5281 {
5282 case DW_LNS_extended_op:
5283 {
5284 unsigned int ext_op_code_len;
5285 unsigned char ext_op_code;
5286 unsigned char *op_code_end;
5287 unsigned char *op_code_data = data;
5288
5289 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5290 op_code_end = op_code_data + ext_op_code_len;
5291 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5292 {
5293 warn (_("Badly formed extended line op encountered!\n"));
5294 break;
5295 }
5296 ext_op_code = *op_code_data++;
5297 xop = ext_op_code;
5298 xop = -xop;
5299
5300 switch (ext_op_code)
5301 {
5302 case DW_LNE_end_sequence:
5303 /* Reset stuff after printing this row. */
5304 break;
5305 case DW_LNE_set_address:
5306 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5307 op_code_data,
5308 op_code_end - op_code_data,
5309 op_code_end);
5310 state_machine_regs.op_index = 0;
5311 state_machine_regs.view = 0;
5312 break;
5313 case DW_LNE_define_file:
5314 file_table = (File_Entry *) xrealloc
5315 (file_table, (n_files + 1) * sizeof (File_Entry));
5316
5317 ++state_machine_regs.last_file_entry;
5318 /* Source file name. */
5319 file_table[n_files].name = op_code_data;
5320 op_code_data += strlen ((char *) op_code_data) + 1;
5321 /* Directory index. */
5322 READ_ULEB (file_table[n_files].directory_index,
5323 op_code_data, op_code_end);
5324 /* Last modification time. */
5325 READ_ULEB (file_table[n_files].modification_date,
5326 op_code_data, op_code_end);
5327 /* File length. */
5328 READ_ULEB (file_table[n_files].length,
5329 op_code_data, op_code_end);
5330 n_files++;
5331 break;
5332
5333 case DW_LNE_set_discriminator:
5334 case DW_LNE_HP_set_sequence:
5335 /* Simply ignored. */
5336 break;
5337
5338 default:
5339 printf (_("UNKNOWN (%u): length %ld\n"),
5340 ext_op_code, (long int) (op_code_data - data));
5341 break;
5342 }
5343 data = op_code_end;
5344 break;
5345 }
5346 case DW_LNS_copy:
5347 /* Increment view after printing this row. */
5348 break;
5349
5350 case DW_LNS_advance_pc:
5351 READ_ULEB (uladv, data, end);
5352 if (linfo.li_max_ops_per_insn == 1)
5353 {
5354 uladv *= linfo.li_min_insn_length;
5355 state_machine_regs.address += uladv;
5356 if (uladv)
5357 state_machine_regs.view = 0;
5358 }
5359 else
5360 {
5361 unsigned addrdelta
5362 = ((state_machine_regs.op_index + uladv)
5363 / linfo.li_max_ops_per_insn)
5364 * linfo.li_min_insn_length;
5365 state_machine_regs.address
5366 += addrdelta;
5367 state_machine_regs.op_index
5368 = (state_machine_regs.op_index + uladv)
5369 % linfo.li_max_ops_per_insn;
5370 if (addrdelta)
5371 state_machine_regs.view = 0;
5372 }
5373 break;
5374
5375 case DW_LNS_advance_line:
5376 READ_SLEB (adv, data, end);
5377 state_machine_regs.line += adv;
5378 break;
5379
5380 case DW_LNS_set_file:
5381 READ_ULEB (uladv, data, end);
5382 state_machine_regs.file = uladv;
5383
5384 {
5385 unsigned file = state_machine_regs.file;
5386 unsigned dir;
5387
5388 if (linfo.li_version < 5)
5389 --file;
5390
5391 if (file_table == NULL || n_files == 0)
5392 printf (_("\n [Use file table entry %d]\n"), file);
5393 /* PR 20439 */
5394 else if (file >= n_files)
5395 {
5396 warn (_("file index %u > number of files %u\n"), file, n_files);
5397 printf (_("\n <over large file table index %u>"), file);
5398 }
5399 else if ((dir = file_table[file].directory_index) == 0)
5400 /* If directory index is 0, that means current directory. */
5401 printf ("\n./%s:[++]\n", file_table[file].name);
5402 else if (directory_table == NULL || n_directories == 0)
5403 printf (_("\n [Use file %s in directory table entry %d]\n"),
5404 file_table[file].name, dir);
5405 /* PR 20439 */
5406 else if (dir > n_directories)
5407 {
5408 warn (_("directory index %u > number of directories %" PRIu64 "\n"),
5409 dir, n_directories);
5410 printf (_("\n <over large directory table entry %u>\n"), dir);
5411 }
5412 else if (linfo.li_version >= 5)
5413 printf ("\n%s/%s:\n",
5414 /* The directory index starts counting at 0. */
5415 directory_table[dir], file_table[file].name);
5416 else
5417 printf ("\n%s/%s:\n",
5418 /* The directory index starts counting at 1. */
5419 directory_table[dir - 1], file_table[file].name);
5420 }
5421 break;
5422
5423 case DW_LNS_set_column:
5424 READ_ULEB (uladv, data, end);
5425 state_machine_regs.column = uladv;
5426 break;
5427
5428 case DW_LNS_negate_stmt:
5429 adv = state_machine_regs.is_stmt;
5430 adv = ! adv;
5431 state_machine_regs.is_stmt = adv;
5432 break;
5433
5434 case DW_LNS_set_basic_block:
5435 state_machine_regs.basic_block = 1;
5436 break;
5437
5438 case DW_LNS_const_add_pc:
5439 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5440 if (linfo.li_max_ops_per_insn == 1)
5441 {
5442 uladv *= linfo.li_min_insn_length;
5443 state_machine_regs.address += uladv;
5444 if (uladv)
5445 state_machine_regs.view = 0;
5446 }
5447 else
5448 {
5449 unsigned addrdelta
5450 = ((state_machine_regs.op_index + uladv)
5451 / linfo.li_max_ops_per_insn)
5452 * linfo.li_min_insn_length;
5453 state_machine_regs.address
5454 += addrdelta;
5455 state_machine_regs.op_index
5456 = (state_machine_regs.op_index + uladv)
5457 % linfo.li_max_ops_per_insn;
5458 if (addrdelta)
5459 state_machine_regs.view = 0;
5460 }
5461 break;
5462
5463 case DW_LNS_fixed_advance_pc:
5464 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5465 state_machine_regs.address += uladv;
5466 state_machine_regs.op_index = 0;
5467 /* Do NOT reset view. */
5468 break;
5469
5470 case DW_LNS_set_prologue_end:
5471 break;
5472
5473 case DW_LNS_set_epilogue_begin:
5474 break;
5475
5476 case DW_LNS_set_isa:
5477 READ_ULEB (uladv, data, end);
5478 printf (_(" Set ISA to %lu\n"), uladv);
5479 break;
5480
5481 default:
5482 printf (_(" Unknown opcode %d with operands: "), op_code);
5483
5484 if (standard_opcodes != NULL)
5485 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5486 {
5487 uint64_t val;
5488
5489 READ_ULEB (val, data, end);
5490 printf ("%#" PRIx64 "%s", val, i == 1 ? "" : ", ");
5491 }
5492 putchar ('\n');
5493 break;
5494 }
5495
5496 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5497 to the DWARF address/line matrix. */
5498 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5499 || (xop == DW_LNS_copy))
5500 {
5501 const unsigned int MAX_FILENAME_LENGTH = 35;
5502 char *fileName = NULL;
5503 char *newFileName = NULL;
5504 size_t fileNameLength;
5505
5506 if (file_table)
5507 {
5508 unsigned indx = state_machine_regs.file;
5509
5510 if (linfo.li_version < 5)
5511 --indx;
5512 /* PR 20439 */
5513 if (indx >= n_files)
5514 {
5515 warn (_("corrupt file index %u encountered\n"), indx);
5516 fileName = _("<corrupt>");
5517 }
5518 else
5519 fileName = (char *) file_table[indx].name;
5520 }
5521 if (!fileName)
5522 fileName = _("<unknown>");
5523
5524 fileNameLength = strlen (fileName);
5525 newFileName = fileName;
5526 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5527 {
5528 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5529 /* Truncate file name */
5530 memcpy (newFileName,
5531 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5532 MAX_FILENAME_LENGTH);
5533 newFileName[MAX_FILENAME_LENGTH] = 0;
5534 }
5535
5536 /* A row with end_seq set to true has a meaningful address, but
5537 the other information in the same row is not significant.
5538 In such a row, print line as "-", and don't print
5539 view/is_stmt. */
5540 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5541 {
5542 if (linfo.li_max_ops_per_insn == 1)
5543 {
5544 if (xop == -DW_LNE_end_sequence)
5545 printf ("%-35s %11s %#18" PRIx64,
5546 newFileName, "-",
5547 state_machine_regs.address);
5548 else
5549 printf ("%-35s %11d %#18" PRIx64,
5550 newFileName, state_machine_regs.line,
5551 state_machine_regs.address);
5552 }
5553 else
5554 {
5555 if (xop == -DW_LNE_end_sequence)
5556 printf ("%-35s %11s %#18" PRIx64 "[%d]",
5557 newFileName, "-",
5558 state_machine_regs.address,
5559 state_machine_regs.op_index);
5560 else
5561 printf ("%-35s %11d %#18" PRIx64 "[%d]",
5562 newFileName, state_machine_regs.line,
5563 state_machine_regs.address,
5564 state_machine_regs.op_index);
5565 }
5566 }
5567 else
5568 {
5569 if (linfo.li_max_ops_per_insn == 1)
5570 {
5571 if (xop == -DW_LNE_end_sequence)
5572 printf ("%s %11s %#18" PRIx64,
5573 newFileName, "-",
5574 state_machine_regs.address);
5575 else
5576 printf ("%s %11d %#18" PRIx64,
5577 newFileName, state_machine_regs.line,
5578 state_machine_regs.address);
5579 }
5580 else
5581 {
5582 if (xop == -DW_LNE_end_sequence)
5583 printf ("%s %11s %#18" PRIx64 "[%d]",
5584 newFileName, "-",
5585 state_machine_regs.address,
5586 state_machine_regs.op_index);
5587 else
5588 printf ("%s %11d %#18" PRIx64 "[%d]",
5589 newFileName, state_machine_regs.line,
5590 state_machine_regs.address,
5591 state_machine_regs.op_index);
5592 }
5593 }
5594
5595 if (xop != -DW_LNE_end_sequence)
5596 {
5597 if (state_machine_regs.view)
5598 printf (" %6u", state_machine_regs.view);
5599 else
5600 printf (" ");
5601
5602 if (state_machine_regs.is_stmt)
5603 printf (" x");
5604 }
5605
5606 putchar ('\n');
5607 state_machine_regs.view++;
5608
5609 if (xop == -DW_LNE_end_sequence)
5610 {
5611 reset_state_machine (linfo.li_default_is_stmt);
5612 putchar ('\n');
5613 }
5614
5615 if (newFileName != fileName)
5616 free (newFileName);
5617 }
5618 }
5619
5620 if (file_table)
5621 {
5622 free (file_table);
5623 file_table = NULL;
5624 n_files = 0;
5625 }
5626
5627 if (directory_table)
5628 {
5629 free (directory_table);
5630 directory_table = NULL;
5631 n_directories = 0;
5632 }
5633
5634 putchar ('\n');
5635 }
5636
5637 return 1;
5638 }
5639
5640 static int
5641 display_debug_lines (struct dwarf_section *section, void *file)
5642 {
5643 unsigned char *data = section->start;
5644 unsigned char *end = data + section->size;
5645 int retValRaw = 1;
5646 int retValDecoded = 1;
5647
5648 if (do_debug_lines == 0)
5649 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5650
5651 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5652 retValRaw = display_debug_lines_raw (section, data, end, file);
5653
5654 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5655 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5656
5657 if (!retValRaw || !retValDecoded)
5658 return 0;
5659
5660 return 1;
5661 }
5662
5663 static debug_info *
5664 find_debug_info_for_offset (uint64_t offset)
5665 {
5666 unsigned int i;
5667
5668 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5669 return NULL;
5670
5671 for (i = 0; i < num_debug_info_entries; i++)
5672 if (debug_information[i].cu_offset == offset)
5673 return debug_information + i;
5674
5675 return NULL;
5676 }
5677
5678 static const char *
5679 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5680 {
5681 /* See gdb/gdb-index.h. */
5682 static const char * const kinds[] =
5683 {
5684 N_ ("no info"),
5685 N_ ("type"),
5686 N_ ("variable"),
5687 N_ ("function"),
5688 N_ ("other"),
5689 N_ ("unused5"),
5690 N_ ("unused6"),
5691 N_ ("unused7")
5692 };
5693
5694 return _ (kinds[kind]);
5695 }
5696
5697 static int
5698 display_debug_pubnames_worker (struct dwarf_section *section,
5699 void *file ATTRIBUTE_UNUSED,
5700 int is_gnu)
5701 {
5702 DWARF2_Internal_PubNames names;
5703 unsigned char *start = section->start;
5704 unsigned char *end = start + section->size;
5705
5706 /* It does not matter if this load fails,
5707 we test for that later on. */
5708 load_debug_info (file);
5709
5710 introduce (section, false);
5711
5712 while (start < end)
5713 {
5714 unsigned char *data;
5715 unsigned long sec_off = start - section->start;
5716 unsigned int offset_size;
5717
5718 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5719 if (names.pn_length == 0xffffffff)
5720 {
5721 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5722 offset_size = 8;
5723 }
5724 else
5725 offset_size = 4;
5726
5727 if (names.pn_length > (size_t) (end - start))
5728 {
5729 warn (_("Debug info is corrupted, "
5730 "%s header at %#lx has length %#" PRIx64 "\n"),
5731 section->name, sec_off, names.pn_length);
5732 break;
5733 }
5734
5735 data = start;
5736 start += names.pn_length;
5737
5738 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
5739 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
5740
5741 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5742 && num_debug_info_entries > 0
5743 && find_debug_info_for_offset (names.pn_offset) == NULL)
5744 warn (_(".debug_info offset of %#" PRIx64
5745 " in %s section does not point to a CU header.\n"),
5746 names.pn_offset, section->name);
5747
5748 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
5749
5750 printf (_(" Length: %" PRId64 "\n"),
5751 names.pn_length);
5752 printf (_(" Version: %d\n"),
5753 names.pn_version);
5754 printf (_(" Offset into .debug_info section: %#" PRIx64 "\n"),
5755 names.pn_offset);
5756 printf (_(" Size of area in .debug_info section: %" PRId64 "\n"),
5757 names.pn_size);
5758
5759 if (names.pn_version != 2 && names.pn_version != 3)
5760 {
5761 static int warned = 0;
5762
5763 if (! warned)
5764 {
5765 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5766 warned = 1;
5767 }
5768
5769 continue;
5770 }
5771
5772 if (is_gnu)
5773 printf (_("\n Offset Kind Name\n"));
5774 else
5775 printf (_("\n Offset\tName\n"));
5776
5777 while (1)
5778 {
5779 size_t maxprint;
5780 uint64_t offset;
5781
5782 SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
5783
5784 if (offset == 0)
5785 break;
5786
5787 if (data >= start)
5788 break;
5789 maxprint = (start - data) - 1;
5790
5791 if (is_gnu)
5792 {
5793 unsigned int kind_data;
5794 gdb_index_symbol_kind kind;
5795 const char *kind_name;
5796 int is_static;
5797
5798 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
5799 maxprint --;
5800 /* GCC computes the kind as the upper byte in the CU index
5801 word, and then right shifts it by the CU index size.
5802 Left shift KIND to where the gdb-index.h accessor macros
5803 can use it. */
5804 kind_data <<= GDB_INDEX_CU_BITSIZE;
5805 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5806 kind_name = get_gdb_index_symbol_kind_name (kind);
5807 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5808 printf (" %-6" PRIx64 " %s,%-10s %.*s\n",
5809 offset, is_static ? _("s") : _("g"),
5810 kind_name, (int) maxprint, data);
5811 }
5812 else
5813 printf (" %-6" PRIx64 "\t%.*s\n",
5814 offset, (int) maxprint, data);
5815
5816 data += strnlen ((char *) data, maxprint);
5817 if (data < start)
5818 data++;
5819 if (data >= start)
5820 break;
5821 }
5822 }
5823
5824 printf ("\n");
5825 return 1;
5826 }
5827
5828 static int
5829 display_debug_pubnames (struct dwarf_section *section, void *file)
5830 {
5831 return display_debug_pubnames_worker (section, file, 0);
5832 }
5833
5834 static int
5835 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5836 {
5837 return display_debug_pubnames_worker (section, file, 1);
5838 }
5839
5840 static int
5841 display_debug_macinfo (struct dwarf_section *section,
5842 void *file ATTRIBUTE_UNUSED)
5843 {
5844 unsigned char *start = section->start;
5845 unsigned char *end = start + section->size;
5846 unsigned char *curr = start;
5847 enum dwarf_macinfo_record_type op;
5848
5849 introduce (section, false);
5850
5851 while (curr < end)
5852 {
5853 unsigned int lineno;
5854 const unsigned char *string;
5855
5856 op = (enum dwarf_macinfo_record_type) *curr;
5857 curr++;
5858
5859 switch (op)
5860 {
5861 case DW_MACINFO_start_file:
5862 {
5863 unsigned int filenum;
5864
5865 READ_ULEB (lineno, curr, end);
5866 READ_ULEB (filenum, curr, end);
5867 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5868 lineno, filenum);
5869 }
5870 break;
5871
5872 case DW_MACINFO_end_file:
5873 printf (_(" DW_MACINFO_end_file\n"));
5874 break;
5875
5876 case DW_MACINFO_define:
5877 READ_ULEB (lineno, curr, end);
5878 string = curr;
5879 curr += strnlen ((char *) string, end - string);
5880 printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
5881 lineno, (int) (curr - string), string);
5882 if (curr < end)
5883 curr++;
5884 break;
5885
5886 case DW_MACINFO_undef:
5887 READ_ULEB (lineno, curr, end);
5888 string = curr;
5889 curr += strnlen ((char *) string, end - string);
5890 printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
5891 lineno, (int) (curr - string), string);
5892 if (curr < end)
5893 curr++;
5894 break;
5895
5896 case DW_MACINFO_vendor_ext:
5897 {
5898 unsigned int constant;
5899
5900 READ_ULEB (constant, curr, end);
5901 string = curr;
5902 curr += strnlen ((char *) string, end - string);
5903 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
5904 constant, (int) (curr - string), string);
5905 if (curr < end)
5906 curr++;
5907 }
5908 break;
5909 }
5910 }
5911
5912 return 1;
5913 }
5914
5915 /* Given LINE_OFFSET into the .debug_line section, attempt to return
5916 filename and dirname corresponding to file name table entry with index
5917 FILEIDX. Return NULL on failure. */
5918
5919 static unsigned char *
5920 get_line_filename_and_dirname (uint64_t line_offset,
5921 uint64_t fileidx,
5922 unsigned char **dir_name)
5923 {
5924 struct dwarf_section *section = &debug_displays [line].section;
5925 unsigned char *hdrptr, *dirtable, *file_name;
5926 unsigned int offset_size;
5927 unsigned int version, opcode_base;
5928 uint64_t length, diridx;
5929 const unsigned char * end;
5930
5931 *dir_name = NULL;
5932 if (section->start == NULL
5933 || line_offset >= section->size
5934 || fileidx == 0)
5935 return NULL;
5936
5937 hdrptr = section->start + line_offset;
5938 end = section->start + section->size;
5939
5940 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
5941 if (length == 0xffffffff)
5942 {
5943 /* This section is 64-bit DWARF 3. */
5944 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
5945 offset_size = 8;
5946 }
5947 else
5948 offset_size = 4;
5949
5950 if (length > (size_t) (end - hdrptr)
5951 || length < 2 + offset_size + 1 + 3 + 1)
5952 return NULL;
5953 end = hdrptr + length;
5954
5955 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
5956 if (version != 2 && version != 3 && version != 4)
5957 return NULL;
5958 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
5959 if (version >= 4)
5960 hdrptr++; /* Skip max_ops_per_insn. */
5961 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
5962
5963 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
5964 if (opcode_base == 0
5965 || opcode_base - 1 >= (size_t) (end - hdrptr))
5966 return NULL;
5967
5968 hdrptr += opcode_base - 1;
5969
5970 dirtable = hdrptr;
5971 /* Skip over dirname table. */
5972 while (*hdrptr != '\0')
5973 {
5974 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5975 if (hdrptr < end)
5976 hdrptr++;
5977 if (hdrptr >= end)
5978 return NULL;
5979 }
5980 hdrptr++; /* Skip the NUL at the end of the table. */
5981
5982 /* Now skip over preceding filename table entries. */
5983 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
5984 {
5985 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5986 if (hdrptr < end)
5987 hdrptr++;
5988 SKIP_ULEB (hdrptr, end);
5989 SKIP_ULEB (hdrptr, end);
5990 SKIP_ULEB (hdrptr, end);
5991 }
5992 if (hdrptr >= end || *hdrptr == '\0')
5993 return NULL;
5994
5995 file_name = hdrptr;
5996 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5997 if (hdrptr < end)
5998 hdrptr++;
5999 if (hdrptr >= end)
6000 return NULL;
6001 READ_ULEB (diridx, hdrptr, end);
6002 if (diridx == 0)
6003 return file_name;
6004 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
6005 {
6006 dirtable += strnlen ((char *) dirtable, end - dirtable);
6007 if (dirtable < end)
6008 dirtable++;
6009 }
6010 if (dirtable >= end || *dirtable == '\0')
6011 return NULL;
6012 *dir_name = dirtable;
6013 return file_name;
6014 }
6015
6016 static int
6017 display_debug_macro (struct dwarf_section *section,
6018 void *file)
6019 {
6020 unsigned char *start = section->start;
6021 unsigned char *end = start + section->size;
6022 unsigned char *curr = start;
6023 unsigned char *extended_op_buf[256];
6024 bool is_dwo = false;
6025 const char *suffix = strrchr (section->name, '.');
6026
6027 if (suffix && strcmp (suffix, ".dwo") == 0)
6028 is_dwo = true;
6029
6030 load_debug_section_with_follow (str, file);
6031 load_debug_section_with_follow (line, file);
6032 load_debug_section_with_follow (str_index, file);
6033
6034 introduce (section, false);
6035
6036 while (curr < end)
6037 {
6038 unsigned int lineno, version, flags;
6039 unsigned int offset_size;
6040 const unsigned char *string;
6041 uint64_t line_offset = 0, sec_offset = curr - start, offset;
6042 unsigned char **extended_ops = NULL;
6043
6044 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6045 if (version != 4 && version != 5)
6046 {
6047 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6048 section->name, version);
6049 return 0;
6050 }
6051
6052 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
6053 offset_size = (flags & 1) ? 8 : 4;
6054 printf (_(" Offset: %#" PRIx64 "\n"), sec_offset);
6055 printf (_(" Version: %d\n"), version);
6056 printf (_(" Offset size: %d\n"), offset_size);
6057 if (flags & 2)
6058 {
6059 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
6060 printf (_(" Offset into .debug_line: %#" PRIx64 "\n"),
6061 line_offset);
6062 }
6063 if (flags & 4)
6064 {
6065 unsigned int i, count, op;
6066 uint64_t nargs, n;
6067
6068 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
6069
6070 memset (extended_op_buf, 0, sizeof (extended_op_buf));
6071 extended_ops = extended_op_buf;
6072 if (count)
6073 {
6074 printf (_(" Extension opcode arguments:\n"));
6075 for (i = 0; i < count; i++)
6076 {
6077 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6078 extended_ops[op] = curr;
6079 READ_ULEB (nargs, curr, end);
6080 if (nargs == 0)
6081 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
6082 else
6083 {
6084 printf (_(" DW_MACRO_%02x arguments: "), op);
6085 for (n = 0; n < nargs; n++)
6086 {
6087 unsigned int form;
6088
6089 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6090 printf ("%s%s", get_FORM_name (form),
6091 n == nargs - 1 ? "\n" : ", ");
6092 switch (form)
6093 {
6094 case DW_FORM_data1:
6095 case DW_FORM_data2:
6096 case DW_FORM_data4:
6097 case DW_FORM_data8:
6098 case DW_FORM_sdata:
6099 case DW_FORM_udata:
6100 case DW_FORM_block:
6101 case DW_FORM_block1:
6102 case DW_FORM_block2:
6103 case DW_FORM_block4:
6104 case DW_FORM_flag:
6105 case DW_FORM_string:
6106 case DW_FORM_strp:
6107 case DW_FORM_sec_offset:
6108 break;
6109 default:
6110 error (_("Invalid extension opcode form %s\n"),
6111 get_FORM_name (form));
6112 return 0;
6113 }
6114 }
6115 }
6116 }
6117 }
6118 }
6119 printf ("\n");
6120
6121 while (1)
6122 {
6123 unsigned int op;
6124
6125 if (curr >= end)
6126 {
6127 error (_(".debug_macro section not zero terminated\n"));
6128 return 0;
6129 }
6130
6131 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6132 if (op == 0)
6133 break;
6134
6135 switch (op)
6136 {
6137 case DW_MACRO_define:
6138 READ_ULEB (lineno, curr, end);
6139 string = curr;
6140 curr += strnlen ((char *) string, end - string);
6141 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6142 lineno, (int) (curr - string), string);
6143 if (curr < end)
6144 curr++;
6145 break;
6146
6147 case DW_MACRO_undef:
6148 READ_ULEB (lineno, curr, end);
6149 string = curr;
6150 curr += strnlen ((char *) string, end - string);
6151 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6152 lineno, (int) (curr - string), string);
6153 if (curr < end)
6154 curr++;
6155 break;
6156
6157 case DW_MACRO_start_file:
6158 {
6159 unsigned int filenum;
6160 unsigned char *file_name = NULL, *dir_name = NULL;
6161
6162 READ_ULEB (lineno, curr, end);
6163 READ_ULEB (filenum, curr, end);
6164
6165 if ((flags & 2) == 0)
6166 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6167 else
6168 file_name
6169 = get_line_filename_and_dirname (line_offset, filenum,
6170 &dir_name);
6171 if (file_name == NULL)
6172 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6173 lineno, filenum);
6174 else
6175 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6176 lineno, filenum,
6177 dir_name != NULL ? (const char *) dir_name : "",
6178 dir_name != NULL ? "/" : "", file_name);
6179 }
6180 break;
6181
6182 case DW_MACRO_end_file:
6183 printf (_(" DW_MACRO_end_file\n"));
6184 break;
6185
6186 case DW_MACRO_define_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_define_strp - lineno : %d macro : %s\n"),
6194 lineno, string);
6195 break;
6196
6197 case DW_MACRO_undef_strp:
6198 READ_ULEB (lineno, curr, end);
6199 if (version == 4 && is_dwo)
6200 READ_ULEB (offset, curr, end);
6201 else
6202 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6203 string = fetch_indirect_string (offset);
6204 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6205 lineno, string);
6206 break;
6207
6208 case DW_MACRO_import:
6209 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6210 printf (_(" DW_MACRO_import - offset : %#" PRIx64 "\n"),
6211 offset);
6212 break;
6213
6214 case DW_MACRO_define_sup:
6215 READ_ULEB (lineno, curr, end);
6216 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6217 printf (_(" DW_MACRO_define_sup - lineno : %d"
6218 " macro offset : %#" PRIx64 "\n"),
6219 lineno, offset);
6220 break;
6221
6222 case DW_MACRO_undef_sup:
6223 READ_ULEB (lineno, curr, end);
6224 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6225 printf (_(" DW_MACRO_undef_sup - lineno : %d"
6226 " macro offset : %#" PRIx64 "\n"),
6227 lineno, offset);
6228 break;
6229
6230 case DW_MACRO_import_sup:
6231 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6232 printf (_(" DW_MACRO_import_sup - offset : %#" PRIx64 "\n"),
6233 offset);
6234 break;
6235
6236 case DW_MACRO_define_strx:
6237 case DW_MACRO_undef_strx:
6238 READ_ULEB (lineno, curr, end);
6239 READ_ULEB (offset, curr, end);
6240 string = (const unsigned char *)
6241 fetch_indexed_string (offset, NULL, offset_size, false, 0);
6242 if (op == DW_MACRO_define_strx)
6243 printf (" DW_MACRO_define_strx ");
6244 else
6245 printf (" DW_MACRO_undef_strx ");
6246 if (do_wide)
6247 printf (_("(with offset %#" PRIx64 ") "), offset);
6248 printf (_("lineno : %d macro : %s\n"),
6249 lineno, string);
6250 break;
6251
6252 default:
6253 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6254 {
6255 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6256 break;
6257 }
6258
6259 if (extended_ops == NULL || extended_ops[op] == NULL)
6260 {
6261 error (_(" Unknown macro opcode %02x seen\n"), op);
6262 return 0;
6263 }
6264 else
6265 {
6266 /* Skip over unhandled opcodes. */
6267 uint64_t nargs, n;
6268 unsigned char *desc = extended_ops[op];
6269 READ_ULEB (nargs, desc, end);
6270 if (nargs == 0)
6271 {
6272 printf (_(" DW_MACRO_%02x\n"), op);
6273 break;
6274 }
6275 printf (_(" DW_MACRO_%02x -"), op);
6276 for (n = 0; n < nargs; n++)
6277 {
6278 int val;
6279
6280 /* DW_FORM_implicit_const is not expected here. */
6281 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6282 curr
6283 = read_and_display_attr_value (0, val, 0,
6284 start, curr, end, 0, 0,
6285 offset_size, version,
6286 NULL, 0, section,
6287 NULL, ' ', -1);
6288 if (n != nargs - 1)
6289 printf (",");
6290 }
6291 printf ("\n");
6292 }
6293 break;
6294 }
6295 }
6296
6297 printf ("\n");
6298 }
6299
6300 return 1;
6301 }
6302
6303 static int
6304 display_debug_abbrev (struct dwarf_section *section,
6305 void *file ATTRIBUTE_UNUSED)
6306 {
6307 abbrev_entry *entry;
6308 unsigned char *start = section->start;
6309
6310 introduce (section, false);
6311
6312 do
6313 {
6314 uint64_t offset = start - section->start;
6315 abbrev_list *list = find_and_process_abbrev_set (section, 0,
6316 section->size, offset,
6317 NULL);
6318 if (list == NULL)
6319 break;
6320
6321 if (list->first_abbrev)
6322 printf (_(" Number TAG (%#" PRIx64 ")\n"), offset);
6323
6324 for (entry = list->first_abbrev; entry; entry = entry->next)
6325 {
6326 abbrev_attr *attr;
6327
6328 printf (" %ld %s [%s]\n",
6329 entry->number,
6330 get_TAG_name (entry->tag),
6331 entry->children ? _("has children") : _("no children"));
6332
6333 for (attr = entry->first_attr; attr; attr = attr->next)
6334 {
6335 printf (" %-18s %s",
6336 get_AT_name (attr->attribute),
6337 get_FORM_name (attr->form));
6338 if (attr->form == DW_FORM_implicit_const)
6339 printf (": %" PRId64, attr->implicit_const);
6340 putchar ('\n');
6341 }
6342 }
6343 start = list->start_of_next_abbrevs;
6344 free_abbrev_list (list);
6345 }
6346 while (start);
6347
6348 printf ("\n");
6349
6350 return 1;
6351 }
6352
6353 /* Return true when ADDR is the maximum address, when addresses are
6354 POINTER_SIZE bytes long. */
6355
6356 static bool
6357 is_max_address (uint64_t addr, unsigned int pointer_size)
6358 {
6359 uint64_t mask = ~(~(uint64_t) 0 << 1 << (pointer_size * 8 - 1));
6360 return ((addr & mask) == mask);
6361 }
6362
6363 /* Display a view pair list starting at *VSTART_PTR and ending at
6364 VLISTEND within SECTION. */
6365
6366 static void
6367 display_view_pair_list (struct dwarf_section *section,
6368 unsigned char **vstart_ptr,
6369 unsigned int debug_info_entry,
6370 unsigned char *vlistend)
6371 {
6372 unsigned char *vstart = *vstart_ptr;
6373 unsigned char *section_end = section->start + section->size;
6374 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6375
6376 if (vlistend < section_end)
6377 section_end = vlistend;
6378
6379 putchar ('\n');
6380
6381 while (vstart < section_end)
6382 {
6383 uint64_t off = vstart - section->start;
6384 uint64_t vbegin, vend;
6385
6386 READ_ULEB (vbegin, vstart, section_end);
6387 if (vstart == section_end)
6388 break;
6389
6390 READ_ULEB (vend, vstart, section_end);
6391 printf (" %8.8" PRIx64 " ", off);
6392
6393 print_view (vbegin, pointer_size);
6394 print_view (vend, pointer_size);
6395 printf (_("location view pair\n"));
6396 }
6397
6398 putchar ('\n');
6399 *vstart_ptr = vstart;
6400 }
6401
6402 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6403
6404 static void
6405 display_loc_list (struct dwarf_section *section,
6406 unsigned char **start_ptr,
6407 unsigned int debug_info_entry,
6408 uint64_t offset,
6409 uint64_t base_address,
6410 unsigned char **vstart_ptr,
6411 int has_frame_base)
6412 {
6413 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6414 unsigned char *section_end = section->start + section->size;
6415 uint64_t cu_offset;
6416 unsigned int pointer_size;
6417 unsigned int offset_size;
6418 int dwarf_version;
6419 uint64_t begin;
6420 uint64_t end;
6421 unsigned short length;
6422 int need_frame_base;
6423
6424 if (debug_info_entry >= num_debug_info_entries)
6425 {
6426 warn (_("No debug information available for loc lists of entry: %u\n"),
6427 debug_info_entry);
6428 return;
6429 }
6430
6431 cu_offset = debug_information [debug_info_entry].cu_offset;
6432 pointer_size = debug_information [debug_info_entry].pointer_size;
6433 offset_size = debug_information [debug_info_entry].offset_size;
6434 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6435
6436 if (pointer_size < 2 || pointer_size > 8)
6437 {
6438 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6439 pointer_size, debug_info_entry);
6440 return;
6441 }
6442
6443 while (1)
6444 {
6445 uint64_t off = offset + (start - *start_ptr);
6446 uint64_t vbegin = -1, vend = -1;
6447
6448 if (2 * pointer_size > (size_t) (section_end - start))
6449 {
6450 warn (_("Location list starting at offset %#" PRIx64
6451 " is not terminated.\n"), offset);
6452 break;
6453 }
6454
6455 printf (" ");
6456 print_hex (off, 4);
6457
6458 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6459 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6460
6461 if (begin == 0 && end == 0)
6462 {
6463 /* PR 18374: In a object file we can have a location list that
6464 starts with a begin and end of 0 because there are relocations
6465 that need to be applied to the addresses. Actually applying
6466 the relocations now does not help as they will probably resolve
6467 to 0, since the object file has not been fully linked. Real
6468 end of list markers will not have any relocations against them. */
6469 if (! reloc_at (section, off)
6470 && ! reloc_at (section, off + pointer_size))
6471 {
6472 printf (_("<End of list>\n"));
6473 break;
6474 }
6475 }
6476
6477 /* Check base address specifiers. */
6478 if (is_max_address (begin, pointer_size)
6479 && !is_max_address (end, pointer_size))
6480 {
6481 base_address = end;
6482 print_hex (begin, pointer_size);
6483 print_hex (end, pointer_size);
6484 printf (_("(base address)\n"));
6485 continue;
6486 }
6487
6488 if (vstart)
6489 {
6490 off = offset + (vstart - *start_ptr);
6491
6492 READ_ULEB (vbegin, vstart, section_end);
6493 print_view (vbegin, pointer_size);
6494
6495 READ_ULEB (vend, vstart, section_end);
6496 print_view (vend, pointer_size);
6497
6498 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6499 }
6500
6501 if (2 > (size_t) (section_end - start))
6502 {
6503 warn (_("Location list starting at offset %#" PRIx64
6504 " is not terminated.\n"), offset);
6505 break;
6506 }
6507
6508 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6509
6510 if (length > (size_t) (section_end - start))
6511 {
6512 warn (_("Location list starting at offset %#" PRIx64
6513 " is not terminated.\n"), offset);
6514 break;
6515 }
6516
6517 print_hex (begin + base_address, pointer_size);
6518 print_hex (end + base_address, pointer_size);
6519
6520 putchar ('(');
6521 need_frame_base = decode_location_expression (start,
6522 pointer_size,
6523 offset_size,
6524 dwarf_version,
6525 length,
6526 cu_offset, section);
6527 putchar (')');
6528
6529 if (need_frame_base && !has_frame_base)
6530 printf (_(" [without DW_AT_frame_base]"));
6531
6532 if (begin == end && vbegin == vend)
6533 fputs (_(" (start == end)"), stdout);
6534 else if (begin > end || (begin == end && vbegin > vend))
6535 fputs (_(" (start > end)"), stdout);
6536
6537 putchar ('\n');
6538
6539 start += length;
6540 }
6541
6542 *start_ptr = start;
6543 *vstart_ptr = vstart;
6544 }
6545
6546 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6547
6548 static void
6549 display_loclists_list (struct dwarf_section * section,
6550 unsigned char ** start_ptr,
6551 unsigned int debug_info_entry,
6552 uint64_t offset,
6553 uint64_t base_address,
6554 unsigned char ** vstart_ptr,
6555 int has_frame_base)
6556 {
6557 unsigned char *start = *start_ptr;
6558 unsigned char *vstart = *vstart_ptr;
6559 unsigned char *section_end = section->start + section->size;
6560 uint64_t cu_offset;
6561 unsigned int pointer_size;
6562 unsigned int offset_size;
6563 unsigned int dwarf_version;
6564
6565 /* Initialize it due to a false compiler warning. */
6566 uint64_t begin = -1, vbegin = -1;
6567 uint64_t end = -1, vend = -1;
6568 uint64_t length;
6569 int need_frame_base;
6570
6571 if (debug_info_entry >= num_debug_info_entries)
6572 {
6573 warn (_("No debug information available for "
6574 "loclists lists of entry: %u\n"),
6575 debug_info_entry);
6576 return;
6577 }
6578
6579 cu_offset = debug_information [debug_info_entry].cu_offset;
6580 pointer_size = debug_information [debug_info_entry].pointer_size;
6581 offset_size = debug_information [debug_info_entry].offset_size;
6582 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6583
6584 if (pointer_size < 2 || pointer_size > 8)
6585 {
6586 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6587 pointer_size, debug_info_entry);
6588 return;
6589 }
6590
6591 while (1)
6592 {
6593 uint64_t off = offset + (start - *start_ptr);
6594 enum dwarf_location_list_entry_type llet;
6595
6596 if (start + 1 > section_end)
6597 {
6598 warn (_("Location list starting at offset %#" PRIx64
6599 " is not terminated.\n"), offset);
6600 break;
6601 }
6602
6603 printf (" ");
6604 print_hex (off, 4);
6605
6606 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6607
6608 if (vstart && (llet == DW_LLE_offset_pair
6609 || llet == DW_LLE_start_end
6610 || llet == DW_LLE_start_length))
6611 {
6612 off = offset + (vstart - *start_ptr);
6613
6614 READ_ULEB (vbegin, vstart, section_end);
6615 print_view (vbegin, pointer_size);
6616
6617 READ_ULEB (vend, vstart, section_end);
6618 print_view (vend, pointer_size);
6619
6620 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6621 }
6622
6623 switch (llet)
6624 {
6625 case DW_LLE_end_of_list:
6626 printf (_("<End of list>\n"));
6627 break;
6628
6629 case DW_LLE_base_addressx:
6630 READ_ULEB (base_address, start, section_end);
6631 print_hex (base_address, pointer_size);
6632 printf (_("(index into .debug_addr) "));
6633 base_address = fetch_indexed_addr (base_address, pointer_size);
6634 print_hex (base_address, pointer_size);
6635 printf (_("(base address)\n"));
6636 break;
6637
6638 case DW_LLE_startx_endx:
6639 READ_ULEB (begin, start, section_end);
6640 begin = fetch_indexed_addr (begin, pointer_size);
6641 READ_ULEB (end, start, section_end);
6642 end = fetch_indexed_addr (end, pointer_size);
6643 break;
6644
6645 case DW_LLE_startx_length:
6646 READ_ULEB (begin, start, section_end);
6647 begin = fetch_indexed_addr (begin, pointer_size);
6648 READ_ULEB (end, start, section_end);
6649 end += begin;
6650 break;
6651
6652 case DW_LLE_default_location:
6653 begin = end = 0;
6654 break;
6655
6656 case DW_LLE_offset_pair:
6657 READ_ULEB (begin, start, section_end);
6658 begin += base_address;
6659 READ_ULEB (end, start, section_end);
6660 end += base_address;
6661 break;
6662
6663 case DW_LLE_base_address:
6664 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6665 section_end);
6666 print_hex (base_address, pointer_size);
6667 printf (_("(base address)\n"));
6668 break;
6669
6670 case DW_LLE_start_end:
6671 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6672 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6673 break;
6674
6675 case DW_LLE_start_length:
6676 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6677 READ_ULEB (end, start, section_end);
6678 end += begin;
6679 break;
6680
6681 #ifdef DW_LLE_view_pair
6682 case DW_LLE_view_pair:
6683 if (vstart)
6684 printf (_("View pair entry in loclist with locviews attribute\n"));
6685 READ_ULEB (vbegin, start, section_end);
6686 print_view (vbegin, pointer_size);
6687
6688 READ_ULEB (vend, start, section_end);
6689 print_view (vend, pointer_size);
6690
6691 printf (_("views for:\n"));
6692 continue;
6693 #endif
6694
6695 default:
6696 error (_("Invalid location list entry type %d\n"), llet);
6697 return;
6698 }
6699
6700 if (llet == DW_LLE_end_of_list)
6701 break;
6702
6703 if (llet == DW_LLE_base_address
6704 || llet == DW_LLE_base_addressx)
6705 continue;
6706
6707 if (start == section_end)
6708 {
6709 warn (_("Location list starting at offset %#" PRIx64
6710 " is not terminated.\n"), offset);
6711 break;
6712 }
6713 READ_ULEB (length, start, section_end);
6714
6715 if (length > (size_t) (section_end - start))
6716 {
6717 warn (_("Location list starting at offset %#" PRIx64
6718 " is not terminated.\n"), offset);
6719 break;
6720 }
6721
6722 print_hex (begin, pointer_size);
6723 print_hex (end, pointer_size);
6724
6725 putchar ('(');
6726 need_frame_base = decode_location_expression (start,
6727 pointer_size,
6728 offset_size,
6729 dwarf_version,
6730 length,
6731 cu_offset, section);
6732 putchar (')');
6733
6734 if (need_frame_base && !has_frame_base)
6735 printf (_(" [without DW_AT_frame_base]"));
6736
6737 if (begin == end && vbegin == vend)
6738 fputs (_(" (start == end)"), stdout);
6739 else if (begin > end || (begin == end && vbegin > vend))
6740 fputs (_(" (start > end)"), stdout);
6741
6742 putchar ('\n');
6743
6744 start += length;
6745 vbegin = vend = -1;
6746 }
6747
6748 if (vbegin != (uint64_t) -1 || vend != (uint64_t) -1)
6749 printf (_("Trailing view pair not used in a range"));
6750
6751 *start_ptr = start;
6752 *vstart_ptr = vstart;
6753 }
6754
6755 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
6756 right-adjusted in a field of length LEN, and followed by a space. */
6757
6758 static void
6759 print_addr_index (unsigned int idx, unsigned int len)
6760 {
6761 static char buf[15];
6762 snprintf (buf, sizeof (buf), "[%d]", idx);
6763 printf ("%*s ", len, buf);
6764 }
6765
6766 /* Display a location list from a .dwo section. It uses address indexes rather
6767 than embedded addresses. This code closely follows display_loc_list, but the
6768 two are sufficiently different that combining things is very ugly. */
6769
6770 static void
6771 display_loc_list_dwo (struct dwarf_section *section,
6772 unsigned char **start_ptr,
6773 unsigned int debug_info_entry,
6774 uint64_t offset,
6775 unsigned char **vstart_ptr,
6776 int has_frame_base)
6777 {
6778 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6779 unsigned char *section_end = section->start + section->size;
6780 uint64_t cu_offset;
6781 unsigned int pointer_size;
6782 unsigned int offset_size;
6783 int dwarf_version;
6784 int entry_type;
6785 unsigned short length;
6786 int need_frame_base;
6787 unsigned int idx;
6788
6789 if (debug_info_entry >= num_debug_info_entries)
6790 {
6791 warn (_("No debug information for loc lists of entry: %u\n"),
6792 debug_info_entry);
6793 return;
6794 }
6795
6796 cu_offset = debug_information [debug_info_entry].cu_offset;
6797 pointer_size = debug_information [debug_info_entry].pointer_size;
6798 offset_size = debug_information [debug_info_entry].offset_size;
6799 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6800
6801 if (pointer_size < 2 || pointer_size > 8)
6802 {
6803 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6804 pointer_size, debug_info_entry);
6805 return;
6806 }
6807
6808 while (1)
6809 {
6810 printf (" ");
6811 print_hex (offset + (start - *start_ptr), 4);
6812
6813 if (start >= section_end)
6814 {
6815 warn (_("Location list starting at offset %#" PRIx64
6816 " is not terminated.\n"), offset);
6817 break;
6818 }
6819
6820 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
6821
6822 if (vstart)
6823 switch (entry_type)
6824 {
6825 default:
6826 break;
6827
6828 case 2:
6829 case 3:
6830 case 4:
6831 {
6832 uint64_t view;
6833 uint64_t off = offset + (vstart - *start_ptr);
6834
6835 READ_ULEB (view, vstart, section_end);
6836 print_view (view, 8);
6837
6838 READ_ULEB (view, vstart, section_end);
6839 print_view (view, 8);
6840
6841 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6842
6843 }
6844 break;
6845 }
6846
6847 switch (entry_type)
6848 {
6849 case 0: /* A terminating entry. */
6850 *start_ptr = start;
6851 *vstart_ptr = vstart;
6852 printf (_("<End of list>\n"));
6853 return;
6854 case 1: /* A base-address entry. */
6855 READ_ULEB (idx, start, section_end);
6856 print_addr_index (idx, 8);
6857 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
6858 printf (_("(base address selection entry)\n"));
6859 continue;
6860 case 2: /* A start/end entry. */
6861 READ_ULEB (idx, start, section_end);
6862 print_addr_index (idx, 8);
6863 READ_ULEB (idx, start, section_end);
6864 print_addr_index (idx, 8);
6865 break;
6866 case 3: /* A start/length entry. */
6867 READ_ULEB (idx, start, section_end);
6868 print_addr_index (idx, 8);
6869 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6870 printf ("%08x ", idx);
6871 break;
6872 case 4: /* An offset pair entry. */
6873 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6874 printf ("%08x ", idx);
6875 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6876 printf ("%08x ", idx);
6877 break;
6878 default:
6879 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6880 *start_ptr = start;
6881 *vstart_ptr = vstart;
6882 return;
6883 }
6884
6885 if (2 > (size_t) (section_end - start))
6886 {
6887 warn (_("Location list starting at offset %#" PRIx64
6888 " is not terminated.\n"), offset);
6889 break;
6890 }
6891
6892 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6893 if (length > (size_t) (section_end - start))
6894 {
6895 warn (_("Location list starting at offset %#" PRIx64
6896 " is not terminated.\n"), offset);
6897 break;
6898 }
6899
6900 putchar ('(');
6901 need_frame_base = decode_location_expression (start,
6902 pointer_size,
6903 offset_size,
6904 dwarf_version,
6905 length,
6906 cu_offset, section);
6907 putchar (')');
6908
6909 if (need_frame_base && !has_frame_base)
6910 printf (_(" [without DW_AT_frame_base]"));
6911
6912 putchar ('\n');
6913
6914 start += length;
6915 }
6916
6917 *start_ptr = start;
6918 *vstart_ptr = vstart;
6919 }
6920
6921 /* Sort array of indexes in ascending order of loc_offsets[idx] and
6922 loc_views. */
6923
6924 static uint64_t *loc_offsets, *loc_views;
6925
6926 static int
6927 loc_offsets_compar (const void *ap, const void *bp)
6928 {
6929 uint64_t a = loc_offsets[*(const unsigned int *) ap];
6930 uint64_t b = loc_offsets[*(const unsigned int *) bp];
6931
6932 int ret = (a > b) - (b > a);
6933 if (ret)
6934 return ret;
6935
6936 a = loc_views[*(const unsigned int *) ap];
6937 b = loc_views[*(const unsigned int *) bp];
6938
6939 ret = (a > b) - (b > a);
6940
6941 return ret;
6942 }
6943
6944 static int
6945 display_offset_entry_loclists (struct dwarf_section *section)
6946 {
6947 unsigned char * start = section->start;
6948 unsigned char * const end = start + section->size;
6949
6950 introduce (section, false);
6951
6952 do
6953 {
6954 uint64_t length;
6955 unsigned short version;
6956 unsigned char address_size;
6957 unsigned char segment_selector_size;
6958 uint32_t offset_entry_count;
6959 uint32_t i;
6960 bool is_64bit;
6961
6962 printf (_("Table at Offset %#tx\n"), start - section->start);
6963
6964 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
6965 if (length == 0xffffffff)
6966 {
6967 is_64bit = true;
6968 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
6969 }
6970 else
6971 is_64bit = false;
6972
6973 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
6974 SAFE_BYTE_GET_AND_INC (address_size, start, 1, end);
6975 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, end);
6976 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, end);
6977
6978 printf (_(" Length: %#" PRIx64 "\n"), length);
6979 printf (_(" DWARF version: %u\n"), version);
6980 printf (_(" Address size: %u\n"), address_size);
6981 printf (_(" Segment size: %u\n"), segment_selector_size);
6982 printf (_(" Offset entries: %u\n"), offset_entry_count);
6983
6984 if (version < 5)
6985 {
6986 warn (_("The %s section contains a corrupt or "
6987 "unsupported version number: %d.\n"),
6988 section->name, version);
6989 return 0;
6990 }
6991
6992 if (segment_selector_size != 0)
6993 {
6994 warn (_("The %s section contains an "
6995 "unsupported segment selector size: %d.\n"),
6996 section->name, segment_selector_size);
6997 return 0;
6998 }
6999
7000 if (offset_entry_count == 0)
7001 {
7002 warn (_("The %s section contains a table without offset\n"),
7003 section->name);
7004 return 0;
7005 }
7006
7007 printf (_("\n Offset Entries starting at %#tx:\n"),
7008 start - section->start);
7009
7010 for (i = 0; i < offset_entry_count; i++)
7011 {
7012 uint64_t entry;
7013
7014 SAFE_BYTE_GET_AND_INC (entry, start, is_64bit ? 8 : 4, end);
7015 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
7016 }
7017
7018 putchar ('\n');
7019
7020 uint32_t j;
7021
7022 for (j = 1, i = 0; i < offset_entry_count;)
7023 {
7024 unsigned char lle;
7025 uint64_t base_address = 0;
7026 uint64_t begin;
7027 uint64_t finish;
7028 uint64_t off = start - section->start;
7029
7030 if (j != i)
7031 {
7032 printf (_(" Offset Entry %u\n"), i);
7033 j = i;
7034 }
7035
7036 printf (" ");
7037 print_hex (off, 4);
7038
7039 SAFE_BYTE_GET_AND_INC (lle, start, 1, end);
7040
7041 switch (lle)
7042 {
7043 case DW_LLE_end_of_list:
7044 printf (_("<End of list>\n\n"));
7045 i ++;
7046 continue;
7047
7048 case DW_LLE_base_addressx:
7049 READ_ULEB (base_address, start, end);
7050 print_hex (base_address, address_size);
7051 printf (_("(index into .debug_addr) "));
7052 base_address = fetch_indexed_addr (base_address, address_size);
7053 print_hex (base_address, address_size);
7054 printf (_("(base address)\n"));
7055 continue;
7056
7057 case DW_LLE_startx_endx:
7058 READ_ULEB (begin, start, end);
7059 begin = fetch_indexed_addr (begin, address_size);
7060 READ_ULEB (finish, start, end);
7061 finish = fetch_indexed_addr (finish, address_size);
7062 break;
7063
7064 case DW_LLE_startx_length:
7065 READ_ULEB (begin, start, end);
7066 begin = fetch_indexed_addr (begin, address_size);
7067 READ_ULEB (finish, start, end);
7068 finish += begin;
7069 break;
7070
7071 case DW_LLE_offset_pair:
7072 READ_ULEB (begin, start, end);
7073 begin += base_address;
7074 READ_ULEB (finish, start, end);
7075 finish += base_address;
7076 break;
7077
7078 case DW_LLE_default_location:
7079 begin = finish = 0;
7080 break;
7081
7082 case DW_LLE_base_address:
7083 SAFE_BYTE_GET_AND_INC (base_address, start, address_size, end);
7084 print_hex (base_address, address_size);
7085 printf (_("(base address)\n"));
7086 continue;
7087
7088 case DW_LLE_start_end:
7089 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7090 SAFE_BYTE_GET_AND_INC (finish, start, address_size, end);
7091 break;
7092
7093 case DW_LLE_start_length:
7094 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7095 READ_ULEB (finish, start, end);
7096 finish += begin;
7097 break;
7098
7099 default:
7100 error (_("Invalid location list entry type %d\n"), lle);
7101 return 0;
7102 }
7103
7104 if (start == end)
7105 {
7106 warn (_("Location list starting at offset %#" PRIx64
7107 " is not terminated.\n"), off);
7108 break;
7109 }
7110
7111 print_hex (begin, address_size);
7112 print_hex (finish, address_size);
7113
7114 if (begin == finish)
7115 fputs (_("(start == end)"), stdout);
7116 else if (begin > finish)
7117 fputs (_("(start > end)"), stdout);
7118
7119 /* Read the counted location descriptions. */
7120 READ_ULEB (length, start, end);
7121
7122 if (length > (size_t) (end - start))
7123 {
7124 warn (_("Location list starting at offset %#" PRIx64
7125 " is not terminated.\n"), off);
7126 break;
7127 }
7128
7129 (void) decode_location_expression (start, address_size, address_size,
7130 version, length, 0, section);
7131 start += length;
7132 putchar ('\n');
7133 }
7134
7135 putchar ('\n');
7136 }
7137 while (start < end);
7138
7139 return 1;
7140 }
7141
7142 static int
7143 display_debug_loc (struct dwarf_section *section, void *file)
7144 {
7145 unsigned char *start = section->start, *vstart = NULL;
7146 uint64_t bytes;
7147 unsigned char *section_begin = start;
7148 unsigned int num_loc_list = 0;
7149 uint64_t last_offset = 0;
7150 uint64_t last_view = 0;
7151 unsigned int first = 0;
7152 unsigned int i;
7153 unsigned int j;
7154 int seen_first_offset = 0;
7155 int locs_sorted = 1;
7156 unsigned char *next = start, *vnext = vstart;
7157 unsigned int *array = NULL;
7158 const char *suffix = strrchr (section->name, '.');
7159 bool is_dwo = false;
7160 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
7161 uint64_t header_size = 0;
7162
7163 if (suffix && strcmp (suffix, ".dwo") == 0)
7164 is_dwo = true;
7165
7166 bytes = section->size;
7167
7168 if (bytes == 0)
7169 {
7170 printf (_("\nThe %s section is empty.\n"), section->name);
7171 return 0;
7172 }
7173
7174 if (is_loclists)
7175 {
7176 unsigned char *hdrptr = section_begin;
7177 uint64_t ll_length;
7178 unsigned short ll_version;
7179 unsigned char *end = section_begin + section->size;
7180 unsigned char address_size, segment_selector_size;
7181 uint32_t offset_entry_count;
7182
7183 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
7184 if (ll_length == 0xffffffff)
7185 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
7186
7187 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
7188 if (ll_version != 5)
7189 {
7190 warn (_("The %s section contains corrupt or "
7191 "unsupported version number: %d.\n"),
7192 section->name, ll_version);
7193 return 0;
7194 }
7195
7196 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7197
7198 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7199 if (segment_selector_size != 0)
7200 {
7201 warn (_("The %s section contains "
7202 "unsupported segment selector size: %d.\n"),
7203 section->name, segment_selector_size);
7204 return 0;
7205 }
7206
7207 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7208
7209 if (offset_entry_count != 0)
7210 return display_offset_entry_loclists (section);
7211
7212 header_size = hdrptr - section_begin;
7213 }
7214
7215 if (load_debug_info (file) == 0)
7216 {
7217 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7218 section->name);
7219 return 0;
7220 }
7221
7222 /* Check the order of location list in .debug_info section. If
7223 offsets of location lists are in the ascending order, we can
7224 use `debug_information' directly. */
7225 for (i = 0; i < num_debug_info_entries; i++)
7226 {
7227 unsigned int num;
7228
7229 num = debug_information [i].num_loc_offsets;
7230 if (num > num_loc_list)
7231 num_loc_list = num;
7232
7233 /* Check if we can use `debug_information' directly. */
7234 if (locs_sorted && num != 0)
7235 {
7236 if (!seen_first_offset)
7237 {
7238 /* This is the first location list. */
7239 last_offset = debug_information [i].loc_offsets [0];
7240 last_view = debug_information [i].loc_views [0];
7241 first = i;
7242 seen_first_offset = 1;
7243 j = 1;
7244 }
7245 else
7246 j = 0;
7247
7248 for (; j < num; j++)
7249 {
7250 if (last_offset >
7251 debug_information [i].loc_offsets [j]
7252 || (last_offset == debug_information [i].loc_offsets [j]
7253 && last_view > debug_information [i].loc_views [j]))
7254 {
7255 locs_sorted = 0;
7256 break;
7257 }
7258 last_offset = debug_information [i].loc_offsets [j];
7259 last_view = debug_information [i].loc_views [j];
7260 }
7261 }
7262 }
7263
7264 if (!seen_first_offset)
7265 error (_("No location lists in .debug_info section!\n"));
7266
7267 if (debug_information [first].num_loc_offsets > 0
7268 && debug_information [first].loc_offsets [0] != header_size
7269 && debug_information [first].loc_views [0] != header_size)
7270 warn (_("Location lists in %s section start at %#" PRIx64
7271 " rather than %#" PRIx64 "\n"),
7272 section->name, debug_information [first].loc_offsets [0],
7273 header_size);
7274
7275 if (!locs_sorted)
7276 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
7277
7278 introduce (section, false);
7279
7280 if (reloc_at (section, 0))
7281 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
7282
7283 printf (_(" Offset Begin End Expression\n"));
7284
7285 for (i = first; i < num_debug_info_entries; i++)
7286 {
7287 uint64_t offset = 0, voffset = 0;
7288 uint64_t base_address;
7289 unsigned int k;
7290 int has_frame_base;
7291
7292 if (!locs_sorted)
7293 {
7294 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7295 array[k] = k;
7296 loc_offsets = debug_information [i].loc_offsets;
7297 loc_views = debug_information [i].loc_views;
7298 qsort (array, debug_information [i].num_loc_offsets,
7299 sizeof (*array), loc_offsets_compar);
7300 }
7301
7302 /* .debug_loclists has a per-unit header.
7303 Update start if we are detecting it. */
7304 if (debug_information [i].dwarf_version == 5)
7305 {
7306 j = locs_sorted ? 0 : array [0];
7307
7308 if (debug_information [i].num_loc_offsets)
7309 offset = debug_information [i].loc_offsets [j];
7310
7311 if (debug_information [i].num_loc_views)
7312 voffset = debug_information [i].loc_views [j];
7313
7314 /* Assume that the size of the header is constant across CUs. */
7315 if (((start - section_begin) + header_size == offset)
7316 || ((start -section_begin) + header_size == voffset))
7317 start += header_size;
7318 }
7319
7320 int adjacent_view_loclists = 1;
7321 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7322 {
7323 j = locs_sorted ? k : array[k];
7324 if (k
7325 && (debug_information [i].loc_offsets [locs_sorted
7326 ? k - 1 : array [k - 1]]
7327 == debug_information [i].loc_offsets [j])
7328 && (debug_information [i].loc_views [locs_sorted
7329 ? k - 1 : array [k - 1]]
7330 == debug_information [i].loc_views [j]))
7331 continue;
7332 has_frame_base = debug_information [i].have_frame_base [j];
7333 offset = debug_information [i].loc_offsets [j];
7334 next = section_begin + offset;
7335 voffset = debug_information [i].loc_views [j];
7336 if (voffset != (uint64_t) -1)
7337 vnext = section_begin + voffset;
7338 else
7339 vnext = NULL;
7340 base_address = debug_information [i].base_address;
7341
7342 if (vnext && vnext < next)
7343 {
7344 vstart = vnext;
7345 display_view_pair_list (section, &vstart, i, next);
7346 if (start == vnext)
7347 start = vstart;
7348 }
7349
7350 if (start < next)
7351 {
7352 if (vnext && vnext < next)
7353 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7354 " in %s section.\n"),
7355 start - section_begin, voffset, section->name);
7356 else
7357 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7358 " in %s section.\n"),
7359 start - section_begin, offset, section->name);
7360 }
7361 else if (start > next)
7362 warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
7363 " in %s section.\n"),
7364 start - section_begin, offset, section->name);
7365 start = next;
7366 vstart = vnext;
7367
7368 if (offset >= bytes)
7369 {
7370 warn (_("Offset %#" PRIx64 " is bigger than %s section size.\n"),
7371 offset, section->name);
7372 continue;
7373 }
7374
7375 if (vnext && voffset >= bytes)
7376 {
7377 warn (_("View Offset %#" PRIx64 " is bigger than %s section size.\n"),
7378 voffset, section->name);
7379 continue;
7380 }
7381
7382 if (!is_loclists)
7383 {
7384 if (is_dwo)
7385 display_loc_list_dwo (section, &start, i, offset,
7386 &vstart, has_frame_base);
7387 else
7388 display_loc_list (section, &start, i, offset, base_address,
7389 &vstart, has_frame_base);
7390 }
7391 else
7392 {
7393 if (is_dwo)
7394 warn (_("DWO is not yet supported.\n"));
7395 else
7396 display_loclists_list (section, &start, i, offset, base_address,
7397 &vstart, has_frame_base);
7398 }
7399
7400 /* FIXME: this arrangement is quite simplistic. Nothing
7401 requires locview lists to be adjacent to corresponding
7402 loclists, and a single loclist could be augmented by
7403 different locview lists, and vice-versa, unlikely as it
7404 is that it would make sense to do so. Hopefully we'll
7405 have view pair support built into loclists before we ever
7406 need to address all these possibilities. */
7407 if (adjacent_view_loclists && vnext
7408 && vnext != start && vstart != next)
7409 {
7410 adjacent_view_loclists = 0;
7411 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7412 }
7413
7414 if (vnext && vnext == start)
7415 display_view_pair_list (section, &start, i, vstart);
7416 }
7417 }
7418
7419 if (start < section->start + section->size)
7420 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7421 "There are %ld unused bytes at the end of section %s\n",
7422 (long) (section->start + section->size - start)),
7423 (long) (section->start + section->size - start), section->name);
7424 putchar ('\n');
7425 free (array);
7426 return 1;
7427 }
7428
7429 static int
7430 display_debug_str (struct dwarf_section *section,
7431 void *file ATTRIBUTE_UNUSED)
7432 {
7433 unsigned char *start = section->start;
7434 uint64_t bytes = section->size;
7435 uint64_t addr = section->address;
7436
7437 if (bytes == 0)
7438 {
7439 printf (_("\nThe %s section is empty.\n"), section->name);
7440 return 0;
7441 }
7442
7443 introduce (section, false);
7444
7445 while (bytes)
7446 {
7447 int j;
7448 int k;
7449 int lbytes;
7450
7451 lbytes = (bytes > 16 ? 16 : bytes);
7452
7453 printf (" 0x%8.8" PRIx64 " ", addr);
7454
7455 for (j = 0; j < 16; j++)
7456 {
7457 if (j < lbytes)
7458 printf ("%2.2x", start[j]);
7459 else
7460 printf (" ");
7461
7462 if ((j & 3) == 3)
7463 printf (" ");
7464 }
7465
7466 for (j = 0; j < lbytes; j++)
7467 {
7468 k = start[j];
7469 if (k >= ' ' && k < 0x80)
7470 printf ("%c", k);
7471 else
7472 printf (".");
7473 }
7474
7475 putchar ('\n');
7476
7477 start += lbytes;
7478 addr += lbytes;
7479 bytes -= lbytes;
7480 }
7481
7482 putchar ('\n');
7483
7484 return 1;
7485 }
7486
7487 static int
7488 display_debug_info (struct dwarf_section *section, void *file)
7489 {
7490 return process_debug_info (section, file, section->abbrev_sec, false, false);
7491 }
7492
7493 static int
7494 display_debug_types (struct dwarf_section *section, void *file)
7495 {
7496 return process_debug_info (section, file, section->abbrev_sec, false, true);
7497 }
7498
7499 static int
7500 display_trace_info (struct dwarf_section *section, void *file)
7501 {
7502 return process_debug_info (section, file, section->abbrev_sec, false, true);
7503 }
7504
7505 static int
7506 display_debug_aranges (struct dwarf_section *section,
7507 void *file ATTRIBUTE_UNUSED)
7508 {
7509 unsigned char *start = section->start;
7510 unsigned char *end = start + section->size;
7511
7512 introduce (section, false);
7513
7514 /* It does not matter if this load fails,
7515 we test for that later on. */
7516 load_debug_info (file);
7517
7518 while (start < end)
7519 {
7520 unsigned char *hdrptr;
7521 DWARF2_Internal_ARange arange;
7522 unsigned char *addr_ranges;
7523 uint64_t length;
7524 uint64_t address;
7525 uint64_t sec_off;
7526 unsigned char address_size;
7527 unsigned int offset_size;
7528 unsigned char *end_ranges;
7529
7530 hdrptr = start;
7531 sec_off = hdrptr - section->start;
7532
7533 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7534 if (arange.ar_length == 0xffffffff)
7535 {
7536 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7537 offset_size = 8;
7538 }
7539 else
7540 offset_size = 4;
7541
7542 if (arange.ar_length > (size_t) (end - hdrptr))
7543 {
7544 warn (_("Debug info is corrupted, %s header at %#" PRIx64
7545 " has length %#" PRIx64 "\n"),
7546 section->name, sec_off, arange.ar_length);
7547 break;
7548 }
7549 end_ranges = hdrptr + arange.ar_length;
7550
7551 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7552 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7553 end_ranges);
7554
7555 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7556 && num_debug_info_entries > 0
7557 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7558 warn (_(".debug_info offset of %#" PRIx64
7559 " in %s section does not point to a CU header.\n"),
7560 arange.ar_info_offset, section->name);
7561
7562 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7563 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7564
7565 if (arange.ar_version != 2 && arange.ar_version != 3)
7566 {
7567 /* PR 19872: A version number of 0 probably means that there is
7568 padding at the end of the .debug_aranges section. Gold puts
7569 it there when performing an incremental link, for example.
7570 So do not generate a warning in this case. */
7571 if (arange.ar_version)
7572 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7573 break;
7574 }
7575
7576 printf (_(" Length: %" PRId64 "\n"), arange.ar_length);
7577 printf (_(" Version: %d\n"), arange.ar_version);
7578 printf (_(" Offset into .debug_info: %#" PRIx64 "\n"),
7579 arange.ar_info_offset);
7580 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7581 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7582
7583 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7584
7585 /* PR 17512: file: 001-108546-0.001:0.1. */
7586 if (address_size == 0 || address_size > 8)
7587 {
7588 error (_("Invalid address size in %s section!\n"),
7589 section->name);
7590 break;
7591 }
7592
7593 /* The DWARF spec does not require that the address size be a power
7594 of two, but we do. This will have to change if we ever encounter
7595 an uneven architecture. */
7596 if ((address_size & (address_size - 1)) != 0)
7597 {
7598 warn (_("Pointer size + Segment size is not a power of two.\n"));
7599 break;
7600 }
7601
7602 if (address_size > 4)
7603 printf (_("\n Address Length\n"));
7604 else
7605 printf (_("\n Address Length\n"));
7606
7607 addr_ranges = hdrptr;
7608
7609 /* Must pad to an alignment boundary that is twice the address size. */
7610 addr_ranges += (2 * address_size - 1
7611 - (hdrptr - start - 1) % (2 * address_size));
7612
7613 while (2 * address_size <= end_ranges - addr_ranges)
7614 {
7615 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7616 end_ranges);
7617 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7618 end_ranges);
7619 printf (" ");
7620 print_hex (address, address_size);
7621 print_hex_ns (length, address_size);
7622 putchar ('\n');
7623 }
7624
7625 start = end_ranges;
7626 }
7627
7628 printf ("\n");
7629
7630 return 1;
7631 }
7632
7633 /* Comparison function for qsort. */
7634 static int
7635 comp_addr_base (const void * v0, const void * v1)
7636 {
7637 debug_info *info0 = *(debug_info **) v0;
7638 debug_info *info1 = *(debug_info **) v1;
7639 return info0->addr_base - info1->addr_base;
7640 }
7641
7642 /* Display the debug_addr section. */
7643 static int
7644 display_debug_addr (struct dwarf_section *section,
7645 void *file)
7646 {
7647 debug_info **debug_addr_info;
7648 unsigned char *entry;
7649 unsigned char *end;
7650 unsigned int i;
7651 unsigned int count;
7652 unsigned char * header;
7653
7654 if (section->size == 0)
7655 {
7656 printf (_("\nThe %s section is empty.\n"), section->name);
7657 return 0;
7658 }
7659
7660 if (load_debug_info (file) == 0)
7661 {
7662 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7663 section->name);
7664 return 0;
7665 }
7666
7667 introduce (section, false);
7668
7669 /* PR 17531: file: cf38d01b.
7670 We use xcalloc because a corrupt file may not have initialised all of the
7671 fields in the debug_info structure, which means that the sort below might
7672 try to move uninitialised data. */
7673 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7674 sizeof (debug_info *));
7675
7676 count = 0;
7677 for (i = 0; i < num_debug_info_entries; i++)
7678 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7679 {
7680 /* PR 17531: file: cf38d01b. */
7681 if (debug_information[i].addr_base >= section->size)
7682 warn (_("Corrupt address base (%#" PRIx64 ")"
7683 " found in debug section %u\n"),
7684 debug_information[i].addr_base, i);
7685 else
7686 debug_addr_info [count++] = debug_information + i;
7687 }
7688
7689 /* Add a sentinel to make iteration convenient. */
7690 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7691 debug_addr_info [count]->addr_base = section->size;
7692 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7693
7694 header = section->start;
7695 for (i = 0; i < count; i++)
7696 {
7697 unsigned int idx;
7698 unsigned int address_size = debug_addr_info [i]->pointer_size;
7699
7700 printf (_(" For compilation unit at offset %#" PRIx64 ":\n"),
7701 debug_addr_info [i]->cu_offset);
7702
7703 printf (_("\tIndex\tAddress\n"));
7704 entry = section->start + debug_addr_info [i]->addr_base;
7705 if (debug_addr_info [i]->dwarf_version >= 5)
7706 {
7707 size_t header_size = entry - header;
7708 unsigned char *curr_header = header;
7709 uint64_t length;
7710 int version;
7711 int segment_selector_size;
7712
7713 if (header_size != 8 && header_size != 16)
7714 {
7715 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %zd instead\n"),
7716 section->name, header_size);
7717 return 0;
7718 }
7719
7720 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7721 if (length == 0xffffffff)
7722 SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7723 end = curr_header + length;
7724
7725 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7726 if (version != 5)
7727 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7728 section->name, version);
7729
7730 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7731 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7732 address_size += segment_selector_size;
7733 }
7734 else
7735 end = section->start + debug_addr_info [i + 1]->addr_base;
7736 header = end;
7737 idx = 0;
7738 while (entry < end)
7739 {
7740 uint64_t base = byte_get (entry, address_size);
7741 printf (_("\t%d:\t"), idx);
7742 print_hex_ns (base, address_size);
7743 printf ("\n");
7744 entry += address_size;
7745 idx++;
7746 }
7747 }
7748 printf ("\n");
7749
7750 free (debug_addr_info);
7751 return 1;
7752 }
7753
7754 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
7755
7756 static int
7757 display_debug_str_offsets (struct dwarf_section *section,
7758 void *file ATTRIBUTE_UNUSED)
7759 {
7760 unsigned long idx;
7761
7762 if (section->size == 0)
7763 {
7764 printf (_("\nThe %s section is empty.\n"), section->name);
7765 return 0;
7766 }
7767
7768 unsigned char *start = section->start;
7769 unsigned char *end = start + section->size;
7770 unsigned char *curr = start;
7771 uint64_t debug_str_offsets_hdr_len;
7772
7773 const char *suffix = strrchr (section->name, '.');
7774 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
7775
7776 if (dwo)
7777 load_debug_section_with_follow (str_dwo, file);
7778 else
7779 load_debug_section_with_follow (str, file);
7780
7781 introduce (section, false);
7782
7783 while (curr < end)
7784 {
7785 uint64_t length;
7786 uint64_t entry_length;
7787
7788 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7789 /* FIXME: We assume that this means 64-bit DWARF is being used. */
7790 if (length == 0xffffffff)
7791 {
7792 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
7793 entry_length = 8;
7794 debug_str_offsets_hdr_len = 16;
7795 }
7796 else
7797 {
7798 entry_length = 4;
7799 debug_str_offsets_hdr_len = 8;
7800 }
7801
7802 unsigned char *entries_end;
7803 if (length == 0)
7804 {
7805 /* This is probably an old style .debug_str_offset section which
7806 just contains offsets and no header (and the first offset is 0). */
7807 length = section->size;
7808 curr = section->start;
7809 entries_end = end;
7810
7811 printf (_(" Length: %#" PRIx64 "\n"), length);
7812 printf (_(" Index Offset [String]\n"));
7813 }
7814 else
7815 {
7816 if (length <= (size_t) (end - curr))
7817 entries_end = curr + length;
7818 else
7819 {
7820 warn (_("Section %s is too small %#" PRIx64 "\n"),
7821 section->name, section->size);
7822 entries_end = end;
7823 }
7824
7825 int version;
7826 SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
7827 if (version != 5)
7828 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7829
7830 int padding;
7831 SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
7832 if (padding != 0)
7833 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7834
7835 printf (_(" Length: %#" PRIx64 "\n"), length);
7836 printf (_(" Version: %#x\n"), version);
7837 printf (_(" Index Offset [String]\n"));
7838 }
7839
7840 for (idx = 0; curr < entries_end; idx++)
7841 {
7842 uint64_t offset;
7843 const unsigned char * string;
7844
7845 if ((size_t) (entries_end - curr) < entry_length)
7846 /* Not enough space to read one entry_length, give up. */
7847 return 0;
7848
7849 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
7850 if (dwo)
7851 string = (const unsigned char *)
7852 fetch_indexed_string (idx, NULL, entry_length, dwo, debug_str_offsets_hdr_len);
7853 else
7854 string = fetch_indirect_string (offset);
7855
7856 printf (" %8lu ", idx);
7857 print_hex (offset, entry_length);
7858 printf (" %s\n", string);
7859 }
7860 }
7861
7862 return 1;
7863 }
7864
7865 /* Each debug_information[x].range_lists[y] gets this representation for
7866 sorting purposes. */
7867
7868 struct range_entry
7869 {
7870 /* The debug_information[x].range_lists[y] value. */
7871 uint64_t ranges_offset;
7872
7873 /* Original debug_information to find parameters of the data. */
7874 debug_info *debug_info_p;
7875 };
7876
7877 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
7878
7879 static int
7880 range_entry_compar (const void *ap, const void *bp)
7881 {
7882 const struct range_entry *a_re = (const struct range_entry *) ap;
7883 const struct range_entry *b_re = (const struct range_entry *) bp;
7884 const uint64_t a = a_re->ranges_offset;
7885 const uint64_t b = b_re->ranges_offset;
7886
7887 return (a > b) - (b > a);
7888 }
7889
7890 static void
7891 display_debug_ranges_list (unsigned char * start,
7892 unsigned char * finish,
7893 unsigned int pointer_size,
7894 uint64_t offset,
7895 uint64_t base_address)
7896 {
7897 while (start < finish)
7898 {
7899 uint64_t begin;
7900 uint64_t end;
7901
7902 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7903 if (start >= finish)
7904 break;
7905 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7906
7907 printf (" ");
7908 print_hex (offset, 4);
7909
7910 if (begin == 0 && end == 0)
7911 {
7912 printf (_("<End of list>\n"));
7913 break;
7914 }
7915
7916 /* Check base address specifiers. */
7917 if (is_max_address (begin, pointer_size)
7918 && !is_max_address (end, pointer_size))
7919 {
7920 base_address = end;
7921 print_hex (begin, pointer_size);
7922 print_hex (end, pointer_size);
7923 printf ("(base address)\n");
7924 continue;
7925 }
7926
7927 print_hex (begin + base_address, pointer_size);
7928 print_hex_ns (end + base_address, pointer_size);
7929
7930 if (begin == end)
7931 fputs (_(" (start == end)"), stdout);
7932 else if (begin > end)
7933 fputs (_(" (start > end)"), stdout);
7934
7935 putchar ('\n');
7936 }
7937 }
7938
7939 static unsigned char *
7940 display_debug_rnglists_list (unsigned char * start,
7941 unsigned char * finish,
7942 unsigned int pointer_size,
7943 uint64_t offset,
7944 uint64_t base_address,
7945 unsigned int offset_size)
7946 {
7947 unsigned char *next = start;
7948 unsigned int debug_addr_section_hdr_len;
7949
7950 if (offset_size == 4)
7951 debug_addr_section_hdr_len = 8;
7952 else
7953 debug_addr_section_hdr_len = 16;
7954
7955 while (1)
7956 {
7957 uint64_t off = offset + (start - next);
7958 enum dwarf_range_list_entry rlet;
7959 /* Initialize it due to a false compiler warning. */
7960 uint64_t begin = -1, length, end = -1;
7961
7962 if (start >= finish)
7963 {
7964 warn (_("Range list starting at offset %#" PRIx64
7965 " is not terminated.\n"), offset);
7966 break;
7967 }
7968
7969 printf (" ");
7970 print_hex (off, 4);
7971
7972 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
7973
7974 switch (rlet)
7975 {
7976 case DW_RLE_end_of_list:
7977 printf (_("<End of list>\n"));
7978 break;
7979 case DW_RLE_base_addressx:
7980 READ_ULEB (base_address, start, finish);
7981 print_hex (base_address, pointer_size);
7982 printf (_("(base address index) "));
7983 base_address = fetch_indexed_addr ((base_address * pointer_size)
7984 + debug_addr_section_hdr_len, pointer_size);
7985 print_hex (base_address, pointer_size);
7986 printf (_("(base address)\n"));
7987 break;
7988 case DW_RLE_startx_endx:
7989 READ_ULEB (begin, start, finish);
7990 READ_ULEB (end, start, finish);
7991 begin = fetch_indexed_addr ((begin * pointer_size)
7992 + debug_addr_section_hdr_len, pointer_size);
7993 end = fetch_indexed_addr ((begin * pointer_size)
7994 + debug_addr_section_hdr_len, pointer_size);
7995 break;
7996 case DW_RLE_startx_length:
7997 READ_ULEB (begin, start, finish);
7998 READ_ULEB (length, start, finish);
7999 begin = fetch_indexed_addr ((begin * pointer_size)
8000 + debug_addr_section_hdr_len, pointer_size);
8001 end = begin + length;
8002 break;
8003 case DW_RLE_offset_pair:
8004 READ_ULEB (begin, start, finish);
8005 READ_ULEB (end, start, finish);
8006 break;
8007 case DW_RLE_base_address:
8008 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
8009 print_hex (base_address, pointer_size);
8010 printf (_("(base address)\n"));
8011 break;
8012 case DW_RLE_start_end:
8013 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8014 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8015 break;
8016 case DW_RLE_start_length:
8017 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8018 READ_ULEB (length, start, finish);
8019 end = begin + length;
8020 break;
8021 default:
8022 error (_("Invalid range list entry type %d\n"), rlet);
8023 rlet = DW_RLE_end_of_list;
8024 break;
8025 }
8026
8027 if (rlet == DW_RLE_end_of_list)
8028 break;
8029 if (rlet == DW_RLE_base_address || rlet == DW_RLE_base_addressx)
8030 continue;
8031
8032 /* Only a DW_RLE_offset_pair needs the base address added. */
8033 if (rlet == DW_RLE_offset_pair)
8034 {
8035 begin += base_address;
8036 end += base_address;
8037 }
8038
8039 print_hex (begin, pointer_size);
8040 print_hex (end, pointer_size);
8041
8042 if (begin == end)
8043 fputs (_(" (start == end)"), stdout);
8044 else if (begin > end)
8045 fputs (_(" (start > end)"), stdout);
8046
8047 putchar ('\n');
8048 }
8049
8050 return start;
8051 }
8052
8053 static int
8054 display_debug_rnglists (struct dwarf_section *section)
8055 {
8056 unsigned char *start = section->start;
8057 unsigned char *finish = start + section->size;
8058
8059 while (start < finish)
8060 {
8061 unsigned char *table_start;
8062 uint64_t offset = start - section->start;
8063 unsigned char *end;
8064 uint64_t initial_length;
8065 unsigned char segment_selector_size;
8066 unsigned int offset_entry_count;
8067 unsigned int i;
8068 unsigned short version;
8069 unsigned char address_size = 0;
8070 unsigned char offset_size;
8071
8072 /* Get and check the length of the block. */
8073 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
8074
8075 if (initial_length == 0xffffffff)
8076 {
8077 /* This section is 64-bit DWARF 3. */
8078 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
8079 offset_size = 8;
8080 }
8081 else
8082 offset_size = 4;
8083
8084 if (initial_length > (size_t) (finish - start))
8085 {
8086 /* If the length field has a relocation against it, then we should
8087 not complain if it is inaccurate (and probably negative).
8088 It is copied from .debug_line handling code. */
8089 if (reloc_at (section, (start - section->start) - offset_size))
8090 initial_length = finish - start;
8091 else
8092 {
8093 warn (_("The length field (%#" PRIx64
8094 ") in the debug_rnglists header is wrong"
8095 " - the section is too small\n"),
8096 initial_length);
8097 return 0;
8098 }
8099 }
8100
8101 end = start + initial_length;
8102
8103 /* Get the other fields in the header. */
8104 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
8105 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
8106 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
8107 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
8108
8109 printf (_(" Table at Offset: %#" PRIx64 ":\n"), offset);
8110 printf (_(" Length: %#" PRIx64 "\n"), initial_length);
8111 printf (_(" DWARF version: %u\n"), version);
8112 printf (_(" Address size: %u\n"), address_size);
8113 printf (_(" Segment size: %u\n"), segment_selector_size);
8114 printf (_(" Offset entries: %u\n"), offset_entry_count);
8115
8116 /* Check the fields. */
8117 if (segment_selector_size != 0)
8118 {
8119 warn (_("The %s section contains "
8120 "unsupported segment selector size: %d.\n"),
8121 section->name, segment_selector_size);
8122 return 0;
8123 }
8124
8125 if (version < 5)
8126 {
8127 warn (_("Only DWARF version 5+ debug_rnglists info "
8128 "is currently supported.\n"));
8129 return 0;
8130 }
8131
8132 table_start = start;
8133
8134 if (offset_entry_count != 0)
8135 {
8136 printf (_("\n Offsets starting at %#tx:\n"),
8137 start - section->start);
8138
8139 for (i = 0; i < offset_entry_count; i++)
8140 {
8141 uint64_t entry;
8142
8143 SAFE_BYTE_GET_AND_INC (entry, start, offset_size, finish);
8144 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
8145 }
8146 }
8147 else
8148 offset_entry_count = 1;
8149
8150 for (i = 0; i < offset_entry_count; i++)
8151 {
8152 uint64_t indx = start - table_start;
8153
8154 offset = start - section->start;
8155 printf (_("\n Offset: %#" PRIx64 ", Index: %#" PRIx64 "\n"),
8156 offset, indx);
8157 printf (_(" Offset Begin End\n"));
8158 start = display_debug_rnglists_list
8159 (start, end, address_size, offset, 0, offset_size);
8160 if (start >= end)
8161 break;
8162 }
8163
8164 start = end;
8165
8166 if (start < finish)
8167 putchar ('\n');
8168 }
8169
8170 putchar ('\n');
8171 return 1;
8172 }
8173
8174 static int
8175 display_debug_ranges (struct dwarf_section *section,
8176 void *file ATTRIBUTE_UNUSED)
8177 {
8178 unsigned char *start = section->start;
8179 unsigned char *last_start = start;
8180 uint64_t bytes = section->size;
8181 unsigned char *section_begin = start;
8182 unsigned char *finish = start + bytes;
8183 unsigned int num_range_list, i;
8184 struct range_entry *range_entries;
8185 struct range_entry *range_entry_fill;
8186 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
8187 /* Initialize it due to a false compiler warning. */
8188 unsigned char address_size = 0;
8189 uint64_t last_offset = 0;
8190
8191 if (bytes == 0)
8192 {
8193 printf (_("\nThe %s section is empty.\n"), section->name);
8194 return 0;
8195 }
8196
8197 introduce (section, false);
8198
8199 if (is_rnglists)
8200 return display_debug_rnglists (section);
8201
8202 if (load_debug_info (file) == 0)
8203 {
8204 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
8205 section->name);
8206 return 0;
8207 }
8208
8209 num_range_list = 0;
8210 for (i = 0; i < num_debug_info_entries; i++)
8211 num_range_list += debug_information [i].num_range_lists;
8212
8213 if (num_range_list == 0)
8214 {
8215 /* This can happen when the file was compiled with -gsplit-debug
8216 which removes references to range lists from the primary .o file. */
8217 printf (_("No range lists in .debug_info section.\n"));
8218 return 1;
8219 }
8220
8221 range_entries = (struct range_entry *)
8222 xmalloc (sizeof (*range_entries) * num_range_list);
8223 range_entry_fill = range_entries;
8224
8225 for (i = 0; i < num_debug_info_entries; i++)
8226 {
8227 debug_info *debug_info_p = &debug_information[i];
8228 unsigned int j;
8229
8230 for (j = 0; j < debug_info_p->num_range_lists; j++)
8231 {
8232 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
8233 range_entry_fill->debug_info_p = debug_info_p;
8234 range_entry_fill++;
8235 }
8236 }
8237
8238 qsort (range_entries, num_range_list, sizeof (*range_entries),
8239 range_entry_compar);
8240
8241 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
8242 warn (_("Range lists in %s section start at %#" PRIx64 "\n"),
8243 section->name, range_entries[0].ranges_offset);
8244
8245 putchar ('\n');
8246 printf (_(" Offset Begin End\n"));
8247
8248 for (i = 0; i < num_range_list; i++)
8249 {
8250 struct range_entry *range_entry = &range_entries[i];
8251 debug_info *debug_info_p = range_entry->debug_info_p;
8252 unsigned int pointer_size;
8253 uint64_t offset;
8254 unsigned char *next;
8255 uint64_t base_address;
8256
8257 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
8258 offset = range_entry->ranges_offset;
8259 base_address = debug_info_p->base_address;
8260
8261 /* PR 17512: file: 001-101485-0.001:0.1. */
8262 if (pointer_size < 2 || pointer_size > 8)
8263 {
8264 warn (_("Corrupt pointer size (%d) in debug entry at offset %#" PRIx64 "\n"),
8265 pointer_size, offset);
8266 continue;
8267 }
8268
8269 if (offset > (size_t) (finish - section_begin))
8270 {
8271 warn (_("Corrupt offset (%#" PRIx64 ") in range entry %u\n"),
8272 offset, i);
8273 continue;
8274 }
8275
8276 next = section_begin + offset + debug_info_p->rnglists_base;
8277
8278 /* If multiple DWARF entities reference the same range then we will
8279 have multiple entries in the `range_entries' list for the same
8280 offset. Thanks to the sort above these will all be consecutive in
8281 the `range_entries' list, so we can easily ignore duplicates
8282 here. */
8283 if (i > 0 && last_offset == offset)
8284 continue;
8285 last_offset = offset;
8286
8287 if (dwarf_check != 0 && i > 0)
8288 {
8289 if (start < next)
8290 warn (_("There is a hole [%#tx - %#tx] in %s section.\n"),
8291 start - section_begin, next - section_begin, section->name);
8292 else if (start > next)
8293 {
8294 if (next == last_start)
8295 continue;
8296 warn (_("There is an overlap [%#tx - %#tx] in %s section.\n"),
8297 start - section_begin, next - section_begin, section->name);
8298 }
8299 }
8300
8301 start = next;
8302 last_start = next;
8303
8304 display_debug_ranges_list
8305 (start, finish, pointer_size, offset, base_address);
8306 }
8307 putchar ('\n');
8308
8309 free (range_entries);
8310
8311 return 1;
8312 }
8313
8314 typedef struct Frame_Chunk
8315 {
8316 struct Frame_Chunk *next;
8317 unsigned char *chunk_start;
8318 unsigned int ncols;
8319 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8320 short int *col_type;
8321 int *col_offset;
8322 char *augmentation;
8323 unsigned int code_factor;
8324 int data_factor;
8325 uint64_t pc_begin;
8326 uint64_t pc_range;
8327 unsigned int cfa_reg;
8328 uint64_t cfa_offset;
8329 unsigned int ra;
8330 unsigned char fde_encoding;
8331 unsigned char cfa_exp;
8332 unsigned char ptr_size;
8333 unsigned char segment_size;
8334 }
8335 Frame_Chunk;
8336
8337 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8338 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
8339 static const char *const *dwarf_regnames;
8340 static unsigned int dwarf_regnames_count;
8341 static bool is_aarch64;
8342
8343 /* A marker for a col_type that means this column was never referenced
8344 in the frame info. */
8345 #define DW_CFA_unreferenced (-1)
8346
8347 /* Return 0 if no more space is needed, 1 if more space is needed,
8348 -1 for invalid reg. */
8349
8350 static int
8351 frame_need_space (Frame_Chunk *fc, unsigned int reg)
8352 {
8353 unsigned int prev = fc->ncols;
8354
8355 if (reg < (unsigned int) fc->ncols)
8356 return 0;
8357
8358 if (dwarf_regnames_count > 0
8359 && reg > dwarf_regnames_count)
8360 return -1;
8361
8362 fc->ncols = reg + 1;
8363 /* PR 17512: file: 10450-2643-0.004.
8364 If reg == -1 then this can happen... */
8365 if (fc->ncols == 0)
8366 return -1;
8367
8368 /* PR 17512: file: 2844a11d. */
8369 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
8370 {
8371 error (_("Unfeasibly large register number: %u\n"), reg);
8372 fc->ncols = 0;
8373 /* FIXME: 1024 is an arbitrary limit. Increase it if
8374 we ever encounter a valid binary that exceeds it. */
8375 return -1;
8376 }
8377
8378 fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
8379 sizeof (short int));
8380 fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
8381 /* PR 17512: file:002-10025-0.005. */
8382 if (fc->col_type == NULL || fc->col_offset == NULL)
8383 {
8384 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8385 fc->ncols);
8386 fc->ncols = 0;
8387 return -1;
8388 }
8389
8390 while (prev < fc->ncols)
8391 {
8392 fc->col_type[prev] = DW_CFA_unreferenced;
8393 fc->col_offset[prev] = 0;
8394 prev++;
8395 }
8396 return 1;
8397 }
8398
8399 static const char *const dwarf_regnames_i386[] =
8400 {
8401 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8402 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8403 "eip", "eflags", NULL, /* 8 - 10 */
8404 "st0", "st1", "st2", "st3", /* 11 - 14 */
8405 "st4", "st5", "st6", "st7", /* 15 - 18 */
8406 NULL, NULL, /* 19 - 20 */
8407 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8408 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8409 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8410 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8411 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8412 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8413 "tr", "ldtr", /* 48 - 49 */
8414 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8415 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8416 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8417 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8418 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8419 NULL, NULL, NULL, /* 90 - 92 */
8420 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
8421 };
8422
8423 static const char *const dwarf_regnames_iamcu[] =
8424 {
8425 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8426 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8427 "eip", "eflags", NULL, /* 8 - 10 */
8428 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8429 NULL, NULL, /* 19 - 20 */
8430 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8431 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8432 NULL, NULL, NULL, /* 37 - 39 */
8433 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8434 "tr", "ldtr", /* 48 - 49 */
8435 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8436 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8437 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8438 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8439 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8440 NULL, NULL, NULL, /* 90 - 92 */
8441 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8442 };
8443
8444 static void
8445 init_dwarf_regnames_i386 (void)
8446 {
8447 dwarf_regnames = dwarf_regnames_i386;
8448 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8449 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8450 }
8451
8452 static void
8453 init_dwarf_regnames_iamcu (void)
8454 {
8455 dwarf_regnames = dwarf_regnames_iamcu;
8456 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8457 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8458 }
8459
8460 static const char *const DW_CFA_GNU_window_save_name[] =
8461 {
8462 "DW_CFA_GNU_window_save",
8463 "DW_CFA_AARCH64_negate_ra_state"
8464 };
8465
8466 static const char *const dwarf_regnames_x86_64[] =
8467 {
8468 "rax", "rdx", "rcx", "rbx",
8469 "rsi", "rdi", "rbp", "rsp",
8470 "r8", "r9", "r10", "r11",
8471 "r12", "r13", "r14", "r15",
8472 "rip",
8473 "xmm0", "xmm1", "xmm2", "xmm3",
8474 "xmm4", "xmm5", "xmm6", "xmm7",
8475 "xmm8", "xmm9", "xmm10", "xmm11",
8476 "xmm12", "xmm13", "xmm14", "xmm15",
8477 "st0", "st1", "st2", "st3",
8478 "st4", "st5", "st6", "st7",
8479 "mm0", "mm1", "mm2", "mm3",
8480 "mm4", "mm5", "mm6", "mm7",
8481 "rflags",
8482 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8483 "fs.base", "gs.base", NULL, NULL,
8484 "tr", "ldtr",
8485 "mxcsr", "fcw", "fsw",
8486 "xmm16", "xmm17", "xmm18", "xmm19",
8487 "xmm20", "xmm21", "xmm22", "xmm23",
8488 "xmm24", "xmm25", "xmm26", "xmm27",
8489 "xmm28", "xmm29", "xmm30", "xmm31",
8490 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8491 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8492 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8493 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8494 NULL, NULL, NULL, /* 115 - 117 */
8495 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8496 };
8497
8498 static void
8499 init_dwarf_regnames_x86_64 (void)
8500 {
8501 dwarf_regnames = dwarf_regnames_x86_64;
8502 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8503 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8504 }
8505
8506 static const char *const dwarf_regnames_aarch64[] =
8507 {
8508 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8509 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8510 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8511 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8512 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
8513 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8514 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8515 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8516 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8517 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8518 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8519 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8520 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8521 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8522 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8523 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8524 };
8525
8526 static void
8527 init_dwarf_regnames_aarch64 (void)
8528 {
8529 dwarf_regnames = dwarf_regnames_aarch64;
8530 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8531 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8532 is_aarch64 = true;
8533 }
8534
8535 static const char *const dwarf_regnames_s390[] =
8536 {
8537 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8538 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8539 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8540 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8541 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8542 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8543 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8544 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8545 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8546 "pswm", "pswa",
8547 NULL, NULL,
8548 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8549 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8550 };
8551
8552 static void
8553 init_dwarf_regnames_s390 (void)
8554 {
8555 dwarf_regnames = dwarf_regnames_s390;
8556 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8557 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8558 }
8559
8560 static const char *const dwarf_regnames_riscv[] =
8561 {
8562 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8563 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8564 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8565 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8566 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8567 "fs0", "fs1", /* 40 - 41 */
8568 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8569 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8570 "fs10", "fs11", /* 58 - 59 */
8571 "ft8", "ft9", "ft10", "ft11", /* 60 - 63 */
8572 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 64 - 71 */
8573 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 72 - 79 */
8574 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 80 - 87 */
8575 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 88 - 95 */
8576 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", /* 96 - 103 */
8577 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", /* 104 - 111 */
8578 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", /* 112 - 119 */
8579 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", /* 120 - 127 */
8580 };
8581
8582 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8583 the large number of CSRs. */
8584
8585 static const char *
8586 regname_internal_riscv (unsigned int regno)
8587 {
8588 const char *name = NULL;
8589
8590 /* Lookup in the table first, this covers GPR and FPR. */
8591 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8592 name = dwarf_regnames_riscv [regno];
8593 else if (regno >= 4096 && regno <= 8191)
8594 {
8595 /* This might be a CSR, these live in a sparse number space from 4096
8596 to 8191 These numbers are defined in the RISC-V ELF ABI
8597 document. */
8598 switch (regno)
8599 {
8600 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8601 case VALUE + 4096: name = #NAME; break;
8602 #include "opcode/riscv-opc.h"
8603 #undef DECLARE_CSR
8604
8605 default:
8606 {
8607 static char csr_name[10];
8608 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8609 name = csr_name;
8610 }
8611 break;
8612 }
8613 }
8614
8615 return name;
8616 }
8617
8618 static void
8619 init_dwarf_regnames_riscv (void)
8620 {
8621 dwarf_regnames = NULL;
8622 dwarf_regnames_count = 8192;
8623 dwarf_regnames_lookup_func = regname_internal_riscv;
8624 }
8625
8626 void
8627 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8628 {
8629 dwarf_regnames_lookup_func = NULL;
8630 is_aarch64 = false;
8631
8632 switch (e_machine)
8633 {
8634 case EM_386:
8635 init_dwarf_regnames_i386 ();
8636 break;
8637
8638 case EM_IAMCU:
8639 init_dwarf_regnames_iamcu ();
8640 break;
8641
8642 case EM_X86_64:
8643 case EM_L1OM:
8644 case EM_K1OM:
8645 init_dwarf_regnames_x86_64 ();
8646 break;
8647
8648 case EM_AARCH64:
8649 init_dwarf_regnames_aarch64 ();
8650 break;
8651
8652 case EM_S390:
8653 init_dwarf_regnames_s390 ();
8654 break;
8655
8656 case EM_RISCV:
8657 init_dwarf_regnames_riscv ();
8658 break;
8659
8660 default:
8661 break;
8662 }
8663 }
8664
8665 /* Initialize the DWARF register name lookup state based on the
8666 architecture and specific machine type of a BFD. */
8667
8668 void
8669 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8670 unsigned long mach)
8671 {
8672 dwarf_regnames_lookup_func = NULL;
8673 is_aarch64 = false;
8674
8675 switch (arch)
8676 {
8677 case bfd_arch_i386:
8678 switch (mach)
8679 {
8680 case bfd_mach_x86_64:
8681 case bfd_mach_x86_64_intel_syntax:
8682 case bfd_mach_x64_32:
8683 case bfd_mach_x64_32_intel_syntax:
8684 init_dwarf_regnames_x86_64 ();
8685 break;
8686
8687 default:
8688 init_dwarf_regnames_i386 ();
8689 break;
8690 }
8691 break;
8692
8693 case bfd_arch_iamcu:
8694 init_dwarf_regnames_iamcu ();
8695 break;
8696
8697 case bfd_arch_aarch64:
8698 init_dwarf_regnames_aarch64();
8699 break;
8700
8701 case bfd_arch_s390:
8702 init_dwarf_regnames_s390 ();
8703 break;
8704
8705 case bfd_arch_riscv:
8706 init_dwarf_regnames_riscv ();
8707 break;
8708
8709 default:
8710 break;
8711 }
8712 }
8713
8714 static const char *
8715 regname_internal_by_table_only (unsigned int regno)
8716 {
8717 if (dwarf_regnames != NULL
8718 && regno < dwarf_regnames_count
8719 && dwarf_regnames [regno] != NULL)
8720 return dwarf_regnames [regno];
8721
8722 return NULL;
8723 }
8724
8725 static const char *
8726 regname (unsigned int regno, int name_only_p)
8727 {
8728 static char reg[64];
8729
8730 const char *name = NULL;
8731
8732 if (dwarf_regnames_lookup_func != NULL)
8733 name = dwarf_regnames_lookup_func (regno);
8734
8735 if (name != NULL)
8736 {
8737 if (name_only_p)
8738 return name;
8739 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
8740 }
8741 else
8742 snprintf (reg, sizeof (reg), "r%d", regno);
8743 return reg;
8744 }
8745
8746 static void
8747 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
8748 {
8749 unsigned int r;
8750 char tmp[100];
8751
8752 if (*max_regs != fc->ncols)
8753 *max_regs = fc->ncols;
8754
8755 if (*need_col_headers)
8756 {
8757 *need_col_headers = 0;
8758
8759 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
8760
8761 for (r = 0; r < *max_regs; r++)
8762 if (fc->col_type[r] != DW_CFA_unreferenced)
8763 {
8764 if (r == fc->ra)
8765 printf ("ra ");
8766 else
8767 printf ("%-5s ", regname (r, 1));
8768 }
8769
8770 printf ("\n");
8771 }
8772
8773 print_hex (fc->pc_begin, eh_addr_size);
8774 if (fc->cfa_exp)
8775 strcpy (tmp, "exp");
8776 else
8777 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
8778 printf ("%-8s ", tmp);
8779
8780 for (r = 0; r < fc->ncols; r++)
8781 {
8782 if (fc->col_type[r] != DW_CFA_unreferenced)
8783 {
8784 switch (fc->col_type[r])
8785 {
8786 case DW_CFA_undefined:
8787 strcpy (tmp, "u");
8788 break;
8789 case DW_CFA_same_value:
8790 strcpy (tmp, "s");
8791 break;
8792 case DW_CFA_offset:
8793 sprintf (tmp, "c%+d", fc->col_offset[r]);
8794 break;
8795 case DW_CFA_val_offset:
8796 sprintf (tmp, "v%+d", fc->col_offset[r]);
8797 break;
8798 case DW_CFA_register:
8799 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
8800 break;
8801 case DW_CFA_expression:
8802 strcpy (tmp, "exp");
8803 break;
8804 case DW_CFA_val_expression:
8805 strcpy (tmp, "vexp");
8806 break;
8807 default:
8808 strcpy (tmp, "n/a");
8809 break;
8810 }
8811 printf ("%-5s ", tmp);
8812 }
8813 }
8814 printf ("\n");
8815 }
8816
8817 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
8818
8819 static unsigned char *
8820 read_cie (unsigned char *start, unsigned char *end,
8821 Frame_Chunk **p_cie, int *p_version,
8822 uint64_t *p_aug_len, unsigned char **p_aug)
8823 {
8824 int version;
8825 Frame_Chunk *fc;
8826 unsigned char *augmentation_data = NULL;
8827 uint64_t augmentation_data_len = 0;
8828
8829 * p_cie = NULL;
8830 /* PR 17512: file: 001-228113-0.004. */
8831 if (start >= end)
8832 return end;
8833
8834 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8835 memset (fc, 0, sizeof (Frame_Chunk));
8836
8837 fc->col_type = (short int *) xmalloc (sizeof (short int));
8838 fc->col_offset = (int *) xmalloc (sizeof (int));
8839
8840 version = *start++;
8841
8842 fc->augmentation = (char *) start;
8843 /* PR 17512: file: 001-228113-0.004.
8844 Skip past augmentation name, but avoid running off the end of the data. */
8845 while (start < end)
8846 if (* start ++ == '\0')
8847 break;
8848 if (start == end)
8849 {
8850 warn (_("No terminator for augmentation name\n"));
8851 goto fail;
8852 }
8853
8854 if (strcmp (fc->augmentation, "eh") == 0)
8855 {
8856 if (eh_addr_size > (size_t) (end - start))
8857 goto fail;
8858 start += eh_addr_size;
8859 }
8860
8861 if (version >= 4)
8862 {
8863 if (2 > (size_t) (end - start))
8864 goto fail;
8865 GET (fc->ptr_size, 1);
8866 if (fc->ptr_size < 1 || fc->ptr_size > 8)
8867 {
8868 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
8869 goto fail;
8870 }
8871
8872 GET (fc->segment_size, 1);
8873 /* PR 17512: file: e99d2804. */
8874 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
8875 {
8876 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
8877 goto fail;
8878 }
8879
8880 eh_addr_size = fc->ptr_size;
8881 }
8882 else
8883 {
8884 fc->ptr_size = eh_addr_size;
8885 fc->segment_size = 0;
8886 }
8887
8888 READ_ULEB (fc->code_factor, start, end);
8889 READ_SLEB (fc->data_factor, start, end);
8890
8891 if (start >= end)
8892 goto fail;
8893
8894 if (version == 1)
8895 {
8896 GET (fc->ra, 1);
8897 }
8898 else
8899 {
8900 READ_ULEB (fc->ra, start, end);
8901 }
8902
8903 if (fc->augmentation[0] == 'z')
8904 {
8905 if (start >= end)
8906 goto fail;
8907 READ_ULEB (augmentation_data_len, start, end);
8908 augmentation_data = start;
8909 /* PR 17512: file: 11042-2589-0.004. */
8910 if (augmentation_data_len > (size_t) (end - start))
8911 {
8912 warn (_("Augmentation data too long: %#" PRIx64
8913 ", expected at most %#tx\n"),
8914 augmentation_data_len, end - start);
8915 goto fail;
8916 }
8917 start += augmentation_data_len;
8918 }
8919
8920 if (augmentation_data_len)
8921 {
8922 unsigned char *p;
8923 unsigned char *q;
8924 unsigned char *qend;
8925
8926 p = (unsigned char *) fc->augmentation + 1;
8927 q = augmentation_data;
8928 qend = q + augmentation_data_len;
8929
8930 while (p < end && q < qend)
8931 {
8932 if (*p == 'L')
8933 q++;
8934 else if (*p == 'P')
8935 q += 1 + size_of_encoded_value (*q);
8936 else if (*p == 'R')
8937 fc->fde_encoding = *q++;
8938 else if (*p == 'S')
8939 ;
8940 else if (*p == 'B')
8941 ;
8942 else
8943 break;
8944 p++;
8945 }
8946 /* Note - it is OK if this loop terminates with q < qend.
8947 Padding may have been inserted to align the end of the CIE. */
8948 }
8949
8950 *p_cie = fc;
8951 if (p_version)
8952 *p_version = version;
8953 if (p_aug_len)
8954 {
8955 *p_aug_len = augmentation_data_len;
8956 *p_aug = augmentation_data;
8957 }
8958 return start;
8959
8960 fail:
8961 free (fc->col_offset);
8962 free (fc->col_type);
8963 free (fc);
8964 return end;
8965 }
8966
8967 /* Prints out the contents on the DATA array formatted as unsigned bytes.
8968 If do_wide is not enabled, then formats the output to fit into 80 columns.
8969 PRINTED contains the number of characters already written to the current
8970 output line. */
8971
8972 static void
8973 display_data (size_t printed, const unsigned char *data, size_t len)
8974 {
8975 if (do_wide || len < ((80 - printed) / 3))
8976 for (printed = 0; printed < len; ++printed)
8977 printf (" %02x", data[printed]);
8978 else
8979 {
8980 for (printed = 0; printed < len; ++printed)
8981 {
8982 if (printed % (80 / 3) == 0)
8983 putchar ('\n');
8984 printf (" %02x", data[printed]);
8985 }
8986 }
8987 }
8988
8989 /* Prints out the contents on the augmentation data array.
8990 If do_wide is not enabled, then formats the output to fit into 80 columns. */
8991
8992 static void
8993 display_augmentation_data (const unsigned char * data, uint64_t len)
8994 {
8995 size_t i;
8996
8997 i = printf (_(" Augmentation data: "));
8998 display_data (i, data, len);
8999 }
9000
9001 static int
9002 display_debug_frames (struct dwarf_section *section,
9003 void *file ATTRIBUTE_UNUSED)
9004 {
9005 unsigned char *start = section->start;
9006 unsigned char *end = start + section->size;
9007 unsigned char *section_start = start;
9008 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
9009 Frame_Chunk *remembered_state = NULL;
9010 Frame_Chunk *rs;
9011 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
9012 unsigned int max_regs = 0;
9013 const char *bad_reg = _("bad register: ");
9014 unsigned int saved_eh_addr_size = eh_addr_size;
9015
9016 introduce (section, false);
9017
9018 while (start < end)
9019 {
9020 unsigned char *saved_start;
9021 unsigned char *block_end;
9022 uint64_t length;
9023 uint64_t cie_id;
9024 Frame_Chunk *fc;
9025 Frame_Chunk *cie;
9026 int need_col_headers = 1;
9027 unsigned char *augmentation_data = NULL;
9028 uint64_t augmentation_data_len = 0;
9029 unsigned int encoded_ptr_size = saved_eh_addr_size;
9030 unsigned int offset_size;
9031 bool all_nops;
9032 static Frame_Chunk fde_fc;
9033
9034 saved_start = start;
9035
9036 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
9037
9038 if (length == 0)
9039 {
9040 printf ("\n%08tx ZERO terminator\n\n",
9041 saved_start - section_start);
9042 /* Skip any zero terminators that directly follow.
9043 A corrupt section size could have loaded a whole
9044 slew of zero filled memory bytes. eg
9045 PR 17512: file: 070-19381-0.004. */
9046 while (start < end && * start == 0)
9047 ++ start;
9048 continue;
9049 }
9050
9051 if (length == 0xffffffff)
9052 {
9053 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
9054 offset_size = 8;
9055 }
9056 else
9057 offset_size = 4;
9058
9059 if (length > (size_t) (end - start))
9060 {
9061 warn ("Invalid length %#" PRIx64 " in FDE at %#tx\n",
9062 length, saved_start - section_start);
9063 block_end = end;
9064 }
9065 else
9066 block_end = start + length;
9067
9068 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
9069
9070 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
9071 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
9072 {
9073 int version;
9074 unsigned int mreg;
9075
9076 start = read_cie (start, block_end, &cie, &version,
9077 &augmentation_data_len, &augmentation_data);
9078 /* PR 17512: file: 027-135133-0.005. */
9079 if (cie == NULL)
9080 break;
9081
9082 fc = cie;
9083 fc->next = chunks;
9084 chunks = fc;
9085 fc->chunk_start = saved_start;
9086 mreg = max_regs > 0 ? max_regs - 1 : 0;
9087 if (mreg < fc->ra)
9088 mreg = fc->ra;
9089 if (frame_need_space (fc, mreg) < 0)
9090 break;
9091 if (fc->fde_encoding)
9092 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9093
9094 printf ("\n%08tx ", saved_start - section_start);
9095 print_hex (length, fc->ptr_size);
9096 print_hex (cie_id, offset_size);
9097
9098 if (do_debug_frames_interp)
9099 {
9100 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
9101 fc->code_factor, fc->data_factor, fc->ra);
9102 }
9103 else
9104 {
9105 printf ("CIE\n");
9106 printf (" Version: %d\n", version);
9107 printf (" Augmentation: \"%s\"\n", fc->augmentation);
9108 if (version >= 4)
9109 {
9110 printf (" Pointer Size: %u\n", fc->ptr_size);
9111 printf (" Segment Size: %u\n", fc->segment_size);
9112 }
9113 printf (" Code alignment factor: %u\n", fc->code_factor);
9114 printf (" Data alignment factor: %d\n", fc->data_factor);
9115 printf (" Return address column: %d\n", fc->ra);
9116
9117 if (augmentation_data_len)
9118 display_augmentation_data (augmentation_data, augmentation_data_len);
9119
9120 putchar ('\n');
9121 }
9122 }
9123 else
9124 {
9125 unsigned char *look_for;
9126 unsigned long segment_selector;
9127 uint64_t cie_off;
9128
9129 cie_off = cie_id;
9130 if (is_eh)
9131 {
9132 uint64_t sign = (uint64_t) 1 << (offset_size * 8 - 1);
9133 cie_off = (cie_off ^ sign) - sign;
9134 cie_off = start - 4 - section_start - cie_off;
9135 }
9136
9137 look_for = section_start + cie_off;
9138 if (cie_off <= (size_t) (saved_start - section_start))
9139 {
9140 for (cie = chunks; cie ; cie = cie->next)
9141 if (cie->chunk_start == look_for)
9142 break;
9143 }
9144 else if (cie_off >= section->size)
9145 cie = NULL;
9146 else
9147 {
9148 for (cie = forward_refs; cie ; cie = cie->next)
9149 if (cie->chunk_start == look_for)
9150 break;
9151 if (!cie)
9152 {
9153 unsigned int off_size;
9154 unsigned char *cie_scan;
9155
9156 cie_scan = look_for;
9157 off_size = 4;
9158 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
9159 if (length == 0xffffffff)
9160 {
9161 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
9162 off_size = 8;
9163 }
9164 if (length != 0 && length <= (size_t) (end - cie_scan))
9165 {
9166 uint64_t c_id;
9167 unsigned char *cie_end = cie_scan + length;
9168
9169 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
9170 cie_end);
9171 if (is_eh
9172 ? c_id == 0
9173 : ((off_size == 4 && c_id == DW_CIE_ID)
9174 || (off_size == 8 && c_id == DW64_CIE_ID)))
9175 {
9176 int version;
9177 unsigned int mreg;
9178
9179 read_cie (cie_scan, cie_end, &cie, &version,
9180 &augmentation_data_len, &augmentation_data);
9181 /* PR 17512: file: 3450-2098-0.004. */
9182 if (cie == NULL)
9183 {
9184 warn (_("Failed to read CIE information\n"));
9185 break;
9186 }
9187 cie->next = forward_refs;
9188 forward_refs = cie;
9189 cie->chunk_start = look_for;
9190 mreg = max_regs > 0 ? max_regs - 1 : 0;
9191 if (mreg < cie->ra)
9192 mreg = cie->ra;
9193 if (frame_need_space (cie, mreg) < 0)
9194 {
9195 warn (_("Invalid max register\n"));
9196 break;
9197 }
9198 if (cie->fde_encoding)
9199 encoded_ptr_size
9200 = size_of_encoded_value (cie->fde_encoding);
9201 }
9202 }
9203 }
9204 }
9205
9206 fc = &fde_fc;
9207 memset (fc, 0, sizeof (Frame_Chunk));
9208
9209 if (!cie)
9210 {
9211 fc->ncols = 0;
9212 fc->col_type = (short int *) xmalloc (sizeof (short int));
9213 fc->col_offset = (int *) xmalloc (sizeof (int));
9214 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
9215 {
9216 warn (_("Invalid max register\n"));
9217 break;
9218 }
9219 cie = fc;
9220 fc->augmentation = "";
9221 fc->fde_encoding = 0;
9222 fc->ptr_size = eh_addr_size;
9223 fc->segment_size = 0;
9224 }
9225 else
9226 {
9227 fc->ncols = cie->ncols;
9228 fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
9229 fc->col_offset = (int *) xcmalloc (fc->ncols, sizeof (int));
9230 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
9231 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
9232 fc->augmentation = cie->augmentation;
9233 fc->ptr_size = cie->ptr_size;
9234 eh_addr_size = cie->ptr_size;
9235 fc->segment_size = cie->segment_size;
9236 fc->code_factor = cie->code_factor;
9237 fc->data_factor = cie->data_factor;
9238 fc->cfa_reg = cie->cfa_reg;
9239 fc->cfa_offset = cie->cfa_offset;
9240 fc->ra = cie->ra;
9241 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
9242 {
9243 warn (_("Invalid max register\n"));
9244 break;
9245 }
9246 fc->fde_encoding = cie->fde_encoding;
9247 }
9248
9249 if (fc->fde_encoding)
9250 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9251
9252 segment_selector = 0;
9253 if (fc->segment_size)
9254 {
9255 if (fc->segment_size > sizeof (segment_selector))
9256 {
9257 /* PR 17512: file: 9e196b3e. */
9258 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
9259 fc->segment_size = 4;
9260 }
9261 SAFE_BYTE_GET_AND_INC (segment_selector, start,
9262 fc->segment_size, block_end);
9263 }
9264
9265 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
9266 block_end);
9267
9268 /* FIXME: It appears that sometimes the final pc_range value is
9269 encoded in less than encoded_ptr_size bytes. See the x86_64
9270 run of the "objcopy on compressed debug sections" test for an
9271 example of this. */
9272 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
9273 block_end);
9274
9275 if (cie->augmentation[0] == 'z')
9276 {
9277 READ_ULEB (augmentation_data_len, start, block_end);
9278 augmentation_data = start;
9279 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
9280 if (augmentation_data_len > (size_t) (block_end - start))
9281 {
9282 warn (_("Augmentation data too long: %#" PRIx64 ", "
9283 "expected at most %#tx\n"),
9284 augmentation_data_len, block_end - start);
9285 start = block_end;
9286 augmentation_data = NULL;
9287 augmentation_data_len = 0;
9288 }
9289 start += augmentation_data_len;
9290 }
9291
9292 printf ("\n%08tx ", saved_start - section_start);
9293 print_hex (length, fc->ptr_size);
9294 print_hex (cie_id, offset_size);
9295 printf ("FDE ");
9296
9297 if (cie->chunk_start)
9298 printf ("cie=%08tx", cie->chunk_start - section_start);
9299 else
9300 /* Ideally translate "invalid " to 8 chars, trailing space
9301 is optional. */
9302 printf (_("cie=invalid "));
9303
9304 printf (" pc=");
9305 if (fc->segment_size)
9306 printf ("%04lx:", segment_selector);
9307
9308 print_hex_ns (fc->pc_begin, fc->ptr_size);
9309 printf ("..");
9310 print_hex_ns (fc->pc_begin + fc->pc_range, fc->ptr_size);
9311 printf ("\n");
9312
9313 if (! do_debug_frames_interp && augmentation_data_len)
9314 {
9315 display_augmentation_data (augmentation_data, augmentation_data_len);
9316 putchar ('\n');
9317 }
9318 }
9319
9320 /* At this point, fc is the current chunk, cie (if any) is set, and
9321 we're about to interpret instructions for the chunk. */
9322 /* ??? At present we need to do this always, since this sizes the
9323 fc->col_type and fc->col_offset arrays, which we write into always.
9324 We should probably split the interpreted and non-interpreted bits
9325 into two different routines, since there's so much that doesn't
9326 really overlap between them. */
9327 if (1 || do_debug_frames_interp)
9328 {
9329 /* Start by making a pass over the chunk, allocating storage
9330 and taking note of what registers are used. */
9331 unsigned char *tmp = start;
9332
9333 while (start < block_end)
9334 {
9335 unsigned int reg, op, opa;
9336 unsigned long temp;
9337
9338 op = *start++;
9339 opa = op & 0x3f;
9340 if (op & 0xc0)
9341 op &= 0xc0;
9342
9343 /* Warning: if you add any more cases to this switch, be
9344 sure to add them to the corresponding switch below. */
9345 reg = -1u;
9346 switch (op)
9347 {
9348 case DW_CFA_advance_loc:
9349 break;
9350 case DW_CFA_offset:
9351 SKIP_ULEB (start, block_end);
9352 reg = opa;
9353 break;
9354 case DW_CFA_restore:
9355 reg = opa;
9356 break;
9357 case DW_CFA_set_loc:
9358 if ((size_t) (block_end - start) < encoded_ptr_size)
9359 start = block_end;
9360 else
9361 start += encoded_ptr_size;
9362 break;
9363 case DW_CFA_advance_loc1:
9364 if ((size_t) (block_end - start) < 1)
9365 start = block_end;
9366 else
9367 start += 1;
9368 break;
9369 case DW_CFA_advance_loc2:
9370 if ((size_t) (block_end - start) < 2)
9371 start = block_end;
9372 else
9373 start += 2;
9374 break;
9375 case DW_CFA_advance_loc4:
9376 if ((size_t) (block_end - start) < 4)
9377 start = block_end;
9378 else
9379 start += 4;
9380 break;
9381 case DW_CFA_offset_extended:
9382 case DW_CFA_val_offset:
9383 READ_ULEB (reg, start, block_end);
9384 SKIP_ULEB (start, block_end);
9385 break;
9386 case DW_CFA_restore_extended:
9387 READ_ULEB (reg, start, block_end);
9388 break;
9389 case DW_CFA_undefined:
9390 READ_ULEB (reg, start, block_end);
9391 break;
9392 case DW_CFA_same_value:
9393 READ_ULEB (reg, start, block_end);
9394 break;
9395 case DW_CFA_register:
9396 READ_ULEB (reg, start, block_end);
9397 SKIP_ULEB (start, block_end);
9398 break;
9399 case DW_CFA_def_cfa:
9400 SKIP_ULEB (start, block_end);
9401 SKIP_ULEB (start, block_end);
9402 break;
9403 case DW_CFA_def_cfa_register:
9404 SKIP_ULEB (start, block_end);
9405 break;
9406 case DW_CFA_def_cfa_offset:
9407 SKIP_ULEB (start, block_end);
9408 break;
9409 case DW_CFA_def_cfa_expression:
9410 READ_ULEB (temp, start, block_end);
9411 if ((size_t) (block_end - start) < temp)
9412 start = block_end;
9413 else
9414 start += temp;
9415 break;
9416 case DW_CFA_expression:
9417 case DW_CFA_val_expression:
9418 READ_ULEB (reg, start, block_end);
9419 READ_ULEB (temp, start, block_end);
9420 if ((size_t) (block_end - start) < temp)
9421 start = block_end;
9422 else
9423 start += temp;
9424 break;
9425 case DW_CFA_offset_extended_sf:
9426 case DW_CFA_val_offset_sf:
9427 READ_ULEB (reg, start, block_end);
9428 SKIP_SLEB (start, block_end);
9429 break;
9430 case DW_CFA_def_cfa_sf:
9431 SKIP_ULEB (start, block_end);
9432 SKIP_SLEB (start, block_end);
9433 break;
9434 case DW_CFA_def_cfa_offset_sf:
9435 SKIP_SLEB (start, block_end);
9436 break;
9437 case DW_CFA_MIPS_advance_loc8:
9438 if ((size_t) (block_end - start) < 8)
9439 start = block_end;
9440 else
9441 start += 8;
9442 break;
9443 case DW_CFA_GNU_args_size:
9444 SKIP_ULEB (start, block_end);
9445 break;
9446 case DW_CFA_GNU_negative_offset_extended:
9447 READ_ULEB (reg, start, block_end);
9448 SKIP_ULEB (start, block_end);
9449 break;
9450 default:
9451 break;
9452 }
9453 if (reg != -1u && frame_need_space (fc, reg) >= 0)
9454 {
9455 /* Don't leave any reg as DW_CFA_unreferenced so
9456 that frame_display_row prints name of regs in
9457 header, and all referenced regs in each line. */
9458 if (reg >= cie->ncols
9459 || cie->col_type[reg] == DW_CFA_unreferenced)
9460 fc->col_type[reg] = DW_CFA_undefined;
9461 else
9462 fc->col_type[reg] = cie->col_type[reg];
9463 }
9464 }
9465 start = tmp;
9466 }
9467
9468 all_nops = true;
9469
9470 /* Now we know what registers are used, make a second pass over
9471 the chunk, this time actually printing out the info. */
9472
9473 while (start < block_end)
9474 {
9475 unsigned op, opa;
9476 unsigned long ul, roffs;
9477 /* Note: It is tempting to use an unsigned long for 'reg' but there
9478 are various functions, notably frame_space_needed() that assume that
9479 reg is an unsigned int. */
9480 unsigned int reg;
9481 int64_t l;
9482 uint64_t ofs;
9483 uint64_t vma;
9484 const char *reg_prefix = "";
9485
9486 op = *start++;
9487 opa = op & 0x3f;
9488 if (op & 0xc0)
9489 op &= 0xc0;
9490
9491 /* Make a note if something other than DW_CFA_nop happens. */
9492 if (op != DW_CFA_nop)
9493 all_nops = false;
9494
9495 /* Warning: if you add any more cases to this switch, be
9496 sure to add them to the corresponding switch above. */
9497 switch (op)
9498 {
9499 case DW_CFA_advance_loc:
9500 if (do_debug_frames_interp)
9501 frame_display_row (fc, &need_col_headers, &max_regs);
9502 else
9503 {
9504 printf (" DW_CFA_advance_loc: %d to ",
9505 opa * fc->code_factor);
9506 print_hex_ns (fc->pc_begin + opa * fc->code_factor,
9507 fc->ptr_size);
9508 printf ("\n");
9509 }
9510 fc->pc_begin += opa * fc->code_factor;
9511 break;
9512
9513 case DW_CFA_offset:
9514 READ_ULEB (roffs, start, block_end);
9515 if (opa >= fc->ncols)
9516 reg_prefix = bad_reg;
9517 if (! do_debug_frames_interp || *reg_prefix != '\0')
9518 printf (" DW_CFA_offset: %s%s at cfa%+ld\n",
9519 reg_prefix, regname (opa, 0),
9520 roffs * fc->data_factor);
9521 if (*reg_prefix == '\0')
9522 {
9523 fc->col_type[opa] = DW_CFA_offset;
9524 fc->col_offset[opa] = roffs * fc->data_factor;
9525 }
9526 break;
9527
9528 case DW_CFA_restore:
9529 if (opa >= fc->ncols)
9530 reg_prefix = bad_reg;
9531 if (! do_debug_frames_interp || *reg_prefix != '\0')
9532 printf (" DW_CFA_restore: %s%s\n",
9533 reg_prefix, regname (opa, 0));
9534 if (*reg_prefix != '\0')
9535 break;
9536
9537 if (opa >= cie->ncols
9538 || cie->col_type[opa] == DW_CFA_unreferenced)
9539 {
9540 fc->col_type[opa] = DW_CFA_undefined;
9541 fc->col_offset[opa] = 0;
9542 }
9543 else
9544 {
9545 fc->col_type[opa] = cie->col_type[opa];
9546 fc->col_offset[opa] = cie->col_offset[opa];
9547 }
9548 break;
9549
9550 case DW_CFA_set_loc:
9551 vma = get_encoded_value (&start, fc->fde_encoding, section,
9552 block_end);
9553 if (do_debug_frames_interp)
9554 frame_display_row (fc, &need_col_headers, &max_regs);
9555 else
9556 {
9557 printf (" DW_CFA_set_loc: ");
9558 print_hex_ns (vma, fc->ptr_size);
9559 printf ("\n");
9560 }
9561 fc->pc_begin = vma;
9562 break;
9563
9564 case DW_CFA_advance_loc1:
9565 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9566 if (do_debug_frames_interp)
9567 frame_display_row (fc, &need_col_headers, &max_regs);
9568 else
9569 {
9570 printf (" DW_CFA_advance_loc1: %" PRId64 " to ",
9571 ofs * fc->code_factor);
9572 print_hex_ns (fc->pc_begin + ofs * fc->code_factor,
9573 fc->ptr_size);
9574 printf ("\n");
9575 }
9576 fc->pc_begin += ofs * fc->code_factor;
9577 break;
9578
9579 case DW_CFA_advance_loc2:
9580 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9581 if (do_debug_frames_interp)
9582 frame_display_row (fc, &need_col_headers, &max_regs);
9583 else
9584 {
9585 printf (" DW_CFA_advance_loc2: %" PRId64 " to ",
9586 ofs * fc->code_factor);
9587 print_hex_ns (fc->pc_begin + ofs * fc->code_factor,
9588 fc->ptr_size);
9589 printf ("\n");
9590 }
9591 fc->pc_begin += ofs * fc->code_factor;
9592 break;
9593
9594 case DW_CFA_advance_loc4:
9595 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9596 if (do_debug_frames_interp)
9597 frame_display_row (fc, &need_col_headers, &max_regs);
9598 else
9599 {
9600 printf (" DW_CFA_advance_loc4: %" PRId64 " to ",
9601 ofs * fc->code_factor);
9602 print_hex_ns (fc->pc_begin + ofs * fc->code_factor,
9603 fc->ptr_size);
9604 printf ("\n");
9605 }
9606 fc->pc_begin += ofs * fc->code_factor;
9607 break;
9608
9609 case DW_CFA_offset_extended:
9610 READ_ULEB (reg, start, block_end);
9611 READ_ULEB (roffs, start, block_end);
9612 if (reg >= fc->ncols)
9613 reg_prefix = bad_reg;
9614 if (! do_debug_frames_interp || *reg_prefix != '\0')
9615 printf (" DW_CFA_offset_extended: %s%s at cfa%+ld\n",
9616 reg_prefix, regname (reg, 0),
9617 roffs * fc->data_factor);
9618 if (*reg_prefix == '\0')
9619 {
9620 fc->col_type[reg] = DW_CFA_offset;
9621 fc->col_offset[reg] = roffs * fc->data_factor;
9622 }
9623 break;
9624
9625 case DW_CFA_val_offset:
9626 READ_ULEB (reg, start, block_end);
9627 READ_ULEB (roffs, 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_val_offset: %s%s is cfa%+ld\n",
9632 reg_prefix, regname (reg, 0),
9633 roffs * fc->data_factor);
9634 if (*reg_prefix == '\0')
9635 {
9636 fc->col_type[reg] = DW_CFA_val_offset;
9637 fc->col_offset[reg] = roffs * fc->data_factor;
9638 }
9639 break;
9640
9641 case DW_CFA_restore_extended:
9642 READ_ULEB (reg, start, block_end);
9643 if (reg >= fc->ncols)
9644 reg_prefix = bad_reg;
9645 if (! do_debug_frames_interp || *reg_prefix != '\0')
9646 printf (" DW_CFA_restore_extended: %s%s\n",
9647 reg_prefix, regname (reg, 0));
9648 if (*reg_prefix != '\0')
9649 break;
9650
9651 if (reg >= cie->ncols
9652 || cie->col_type[reg] == DW_CFA_unreferenced)
9653 {
9654 fc->col_type[reg] = DW_CFA_undefined;
9655 fc->col_offset[reg] = 0;
9656 }
9657 else
9658 {
9659 fc->col_type[reg] = cie->col_type[reg];
9660 fc->col_offset[reg] = cie->col_offset[reg];
9661 }
9662 break;
9663
9664 case DW_CFA_undefined:
9665 READ_ULEB (reg, start, block_end);
9666 if (reg >= fc->ncols)
9667 reg_prefix = bad_reg;
9668 if (! do_debug_frames_interp || *reg_prefix != '\0')
9669 printf (" DW_CFA_undefined: %s%s\n",
9670 reg_prefix, regname (reg, 0));
9671 if (*reg_prefix == '\0')
9672 {
9673 fc->col_type[reg] = DW_CFA_undefined;
9674 fc->col_offset[reg] = 0;
9675 }
9676 break;
9677
9678 case DW_CFA_same_value:
9679 READ_ULEB (reg, start, block_end);
9680 if (reg >= fc->ncols)
9681 reg_prefix = bad_reg;
9682 if (! do_debug_frames_interp || *reg_prefix != '\0')
9683 printf (" DW_CFA_same_value: %s%s\n",
9684 reg_prefix, regname (reg, 0));
9685 if (*reg_prefix == '\0')
9686 {
9687 fc->col_type[reg] = DW_CFA_same_value;
9688 fc->col_offset[reg] = 0;
9689 }
9690 break;
9691
9692 case DW_CFA_register:
9693 READ_ULEB (reg, start, block_end);
9694 READ_ULEB (roffs, start, block_end);
9695 if (reg >= fc->ncols)
9696 reg_prefix = bad_reg;
9697 if (! do_debug_frames_interp || *reg_prefix != '\0')
9698 {
9699 printf (" DW_CFA_register: %s%s in ",
9700 reg_prefix, regname (reg, 0));
9701 puts (regname (roffs, 0));
9702 }
9703 if (*reg_prefix == '\0')
9704 {
9705 fc->col_type[reg] = DW_CFA_register;
9706 fc->col_offset[reg] = roffs;
9707 }
9708 break;
9709
9710 case DW_CFA_remember_state:
9711 if (! do_debug_frames_interp)
9712 printf (" DW_CFA_remember_state\n");
9713 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9714 rs->cfa_offset = fc->cfa_offset;
9715 rs->cfa_reg = fc->cfa_reg;
9716 rs->ra = fc->ra;
9717 rs->cfa_exp = fc->cfa_exp;
9718 rs->ncols = fc->ncols;
9719 rs->col_type = (short int *) xcmalloc (rs->ncols,
9720 sizeof (* rs->col_type));
9721 rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
9722 memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
9723 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
9724 rs->next = remembered_state;
9725 remembered_state = rs;
9726 break;
9727
9728 case DW_CFA_restore_state:
9729 if (! do_debug_frames_interp)
9730 printf (" DW_CFA_restore_state\n");
9731 rs = remembered_state;
9732 if (rs)
9733 {
9734 remembered_state = rs->next;
9735 fc->cfa_offset = rs->cfa_offset;
9736 fc->cfa_reg = rs->cfa_reg;
9737 fc->ra = rs->ra;
9738 fc->cfa_exp = rs->cfa_exp;
9739 if (frame_need_space (fc, rs->ncols - 1) < 0)
9740 {
9741 warn (_("Invalid column number in saved frame state\n"));
9742 fc->ncols = 0;
9743 break;
9744 }
9745 memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
9746 memcpy (fc->col_offset, rs->col_offset,
9747 rs->ncols * sizeof (* rs->col_offset));
9748 free (rs->col_type);
9749 free (rs->col_offset);
9750 free (rs);
9751 }
9752 else if (do_debug_frames_interp)
9753 printf ("Mismatched DW_CFA_restore_state\n");
9754 break;
9755
9756 case DW_CFA_def_cfa:
9757 READ_ULEB (fc->cfa_reg, start, block_end);
9758 READ_ULEB (fc->cfa_offset, start, block_end);
9759 fc->cfa_exp = 0;
9760 if (! do_debug_frames_interp)
9761 printf (" DW_CFA_def_cfa: %s ofs %d\n",
9762 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
9763 break;
9764
9765 case DW_CFA_def_cfa_register:
9766 READ_ULEB (fc->cfa_reg, start, block_end);
9767 fc->cfa_exp = 0;
9768 if (! do_debug_frames_interp)
9769 printf (" DW_CFA_def_cfa_register: %s\n",
9770 regname (fc->cfa_reg, 0));
9771 break;
9772
9773 case DW_CFA_def_cfa_offset:
9774 READ_ULEB (fc->cfa_offset, start, block_end);
9775 if (! do_debug_frames_interp)
9776 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
9777 break;
9778
9779 case DW_CFA_nop:
9780 if (! do_debug_frames_interp)
9781 printf (" DW_CFA_nop\n");
9782 break;
9783
9784 case DW_CFA_def_cfa_expression:
9785 READ_ULEB (ul, start, block_end);
9786 if (ul > (size_t) (block_end - start))
9787 {
9788 printf (_(" DW_CFA_def_cfa_expression: <corrupt len %lu>\n"), ul);
9789 break;
9790 }
9791 if (! do_debug_frames_interp)
9792 {
9793 printf (" DW_CFA_def_cfa_expression (");
9794 decode_location_expression (start, eh_addr_size, 0, -1,
9795 ul, 0, section);
9796 printf (")\n");
9797 }
9798 fc->cfa_exp = 1;
9799 start += ul;
9800 break;
9801
9802 case DW_CFA_expression:
9803 READ_ULEB (reg, start, block_end);
9804 READ_ULEB (ul, start, block_end);
9805 if (reg >= fc->ncols)
9806 reg_prefix = bad_reg;
9807 /* PR 17512: file: 069-133014-0.006. */
9808 /* PR 17512: file: 98c02eb4. */
9809 if (ul > (size_t) (block_end - start))
9810 {
9811 printf (_(" DW_CFA_expression: <corrupt len %lu>\n"), ul);
9812 break;
9813 }
9814 if (! do_debug_frames_interp || *reg_prefix != '\0')
9815 {
9816 printf (" DW_CFA_expression: %s%s (",
9817 reg_prefix, regname (reg, 0));
9818 decode_location_expression (start, eh_addr_size, 0, -1,
9819 ul, 0, section);
9820 printf (")\n");
9821 }
9822 if (*reg_prefix == '\0')
9823 fc->col_type[reg] = DW_CFA_expression;
9824 start += ul;
9825 break;
9826
9827 case DW_CFA_val_expression:
9828 READ_ULEB (reg, start, block_end);
9829 READ_ULEB (ul, start, block_end);
9830 if (reg >= fc->ncols)
9831 reg_prefix = bad_reg;
9832 if (ul > (size_t) (block_end - start))
9833 {
9834 printf (" DW_CFA_val_expression: <corrupt len %lu>\n", ul);
9835 break;
9836 }
9837 if (! do_debug_frames_interp || *reg_prefix != '\0')
9838 {
9839 printf (" DW_CFA_val_expression: %s%s (",
9840 reg_prefix, regname (reg, 0));
9841 decode_location_expression (start, eh_addr_size, 0, -1,
9842 ul, 0, section);
9843 printf (")\n");
9844 }
9845 if (*reg_prefix == '\0')
9846 fc->col_type[reg] = DW_CFA_val_expression;
9847 start += ul;
9848 break;
9849
9850 case DW_CFA_offset_extended_sf:
9851 READ_ULEB (reg, start, block_end);
9852 READ_SLEB (l, start, block_end);
9853 if (reg >= fc->ncols)
9854 reg_prefix = bad_reg;
9855 if (! do_debug_frames_interp || *reg_prefix != '\0')
9856 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+" PRId64 "\n",
9857 reg_prefix, regname (reg, 0),
9858 l * fc->data_factor);
9859 if (*reg_prefix == '\0')
9860 {
9861 fc->col_type[reg] = DW_CFA_offset;
9862 fc->col_offset[reg] = l * fc->data_factor;
9863 }
9864 break;
9865
9866 case DW_CFA_val_offset_sf:
9867 READ_ULEB (reg, start, block_end);
9868 READ_SLEB (l, start, block_end);
9869 if (reg >= fc->ncols)
9870 reg_prefix = bad_reg;
9871 if (! do_debug_frames_interp || *reg_prefix != '\0')
9872 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+" PRId64 "\n",
9873 reg_prefix, regname (reg, 0),
9874 l * fc->data_factor);
9875 if (*reg_prefix == '\0')
9876 {
9877 fc->col_type[reg] = DW_CFA_val_offset;
9878 fc->col_offset[reg] = l * fc->data_factor;
9879 }
9880 break;
9881
9882 case DW_CFA_def_cfa_sf:
9883 READ_ULEB (fc->cfa_reg, start, block_end);
9884 READ_SLEB (l, start, block_end);
9885 l *= fc->data_factor;
9886 fc->cfa_offset = l;
9887 fc->cfa_exp = 0;
9888 if (! do_debug_frames_interp)
9889 printf (" DW_CFA_def_cfa_sf: %s ofs %" PRId64 "\n",
9890 regname (fc->cfa_reg, 0), l);
9891 break;
9892
9893 case DW_CFA_def_cfa_offset_sf:
9894 READ_SLEB (l, start, block_end);
9895 l *= fc->data_factor;
9896 fc->cfa_offset = l;
9897 if (! do_debug_frames_interp)
9898 printf (" DW_CFA_def_cfa_offset_sf: %" PRId64 "\n", l);
9899 break;
9900
9901 case DW_CFA_MIPS_advance_loc8:
9902 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
9903 if (do_debug_frames_interp)
9904 frame_display_row (fc, &need_col_headers, &max_regs);
9905 else
9906 {
9907 printf (" DW_CFA_MIPS_advance_loc8: %" PRId64 " to ",
9908 ofs * fc->code_factor);
9909 print_hex_ns (fc->pc_begin + ofs * fc->code_factor,
9910 fc->ptr_size);
9911 printf ("\n");
9912 }
9913 fc->pc_begin += ofs * fc->code_factor;
9914 break;
9915
9916 case DW_CFA_GNU_window_save:
9917 if (! do_debug_frames_interp)
9918 printf (" %s\n", DW_CFA_GNU_window_save_name[is_aarch64]);
9919 break;
9920
9921 case DW_CFA_GNU_args_size:
9922 READ_ULEB (ul, start, block_end);
9923 if (! do_debug_frames_interp)
9924 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
9925 break;
9926
9927 case DW_CFA_GNU_negative_offset_extended:
9928 READ_ULEB (reg, start, block_end);
9929 READ_SLEB (l, start, block_end);
9930 l = - l;
9931 if (reg >= fc->ncols)
9932 reg_prefix = bad_reg;
9933 if (! do_debug_frames_interp || *reg_prefix != '\0')
9934 printf (" DW_CFA_GNU_negative_offset_extended: %s%s "
9935 "at cfa%+" PRId64 "\n",
9936 reg_prefix, regname (reg, 0),
9937 l * fc->data_factor);
9938 if (*reg_prefix == '\0')
9939 {
9940 fc->col_type[reg] = DW_CFA_offset;
9941 fc->col_offset[reg] = l * fc->data_factor;
9942 }
9943 break;
9944
9945 default:
9946 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
9947 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
9948 else
9949 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
9950 start = block_end;
9951 }
9952 }
9953
9954 /* Interpret the CFA - as long as it is not completely full of NOPs. */
9955 if (do_debug_frames_interp && ! all_nops)
9956 frame_display_row (fc, &need_col_headers, &max_regs);
9957
9958 if (fde_fc.col_type != NULL)
9959 {
9960 free (fde_fc.col_type);
9961 fde_fc.col_type = NULL;
9962 }
9963 if (fde_fc.col_offset != NULL)
9964 {
9965 free (fde_fc.col_offset);
9966 fde_fc.col_offset = NULL;
9967 }
9968
9969 start = block_end;
9970 eh_addr_size = saved_eh_addr_size;
9971 }
9972
9973 printf ("\n");
9974
9975 while (remembered_state != NULL)
9976 {
9977 rs = remembered_state;
9978 remembered_state = rs->next;
9979 free (rs->col_type);
9980 free (rs->col_offset);
9981 rs->next = NULL; /* Paranoia. */
9982 free (rs);
9983 }
9984
9985 while (chunks != NULL)
9986 {
9987 rs = chunks;
9988 chunks = rs->next;
9989 free (rs->col_type);
9990 free (rs->col_offset);
9991 rs->next = NULL; /* Paranoia. */
9992 free (rs);
9993 }
9994
9995 while (forward_refs != NULL)
9996 {
9997 rs = forward_refs;
9998 forward_refs = rs->next;
9999 free (rs->col_type);
10000 free (rs->col_offset);
10001 rs->next = NULL; /* Paranoia. */
10002 free (rs);
10003 }
10004
10005 return 1;
10006 }
10007
10008 #undef GET
10009
10010 static int
10011 display_debug_names (struct dwarf_section *section, void *file)
10012 {
10013 unsigned char *hdrptr = section->start;
10014 uint64_t unit_length;
10015 unsigned char *unit_start;
10016 const unsigned char *const section_end = section->start + section->size;
10017 unsigned char *unit_end;
10018
10019 introduce (section, false);
10020
10021 load_debug_section_with_follow (str, file);
10022
10023 for (; hdrptr < section_end; hdrptr = unit_end)
10024 {
10025 unsigned int offset_size;
10026 uint16_t dwarf_version, padding;
10027 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
10028 uint64_t bucket_count, name_count, abbrev_table_size;
10029 uint32_t augmentation_string_size;
10030 unsigned int i;
10031 bool augmentation_printable;
10032 const char *augmentation_string;
10033 size_t total;
10034
10035 unit_start = hdrptr;
10036
10037 /* Get and check the length of the block. */
10038 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
10039
10040 if (unit_length == 0xffffffff)
10041 {
10042 /* This section is 64-bit DWARF. */
10043 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
10044 offset_size = 8;
10045 }
10046 else
10047 offset_size = 4;
10048
10049 if (unit_length > (size_t) (section_end - hdrptr)
10050 || unit_length < 2 + 2 + 4 * 7)
10051 {
10052 too_short:
10053 warn (_("Debug info is corrupted, %s header at %#tx"
10054 " has length %#" PRIx64 "\n"),
10055 section->name, unit_start - section->start, unit_length);
10056 return 0;
10057 }
10058 unit_end = hdrptr + unit_length;
10059
10060 /* Get and check the version number. */
10061 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
10062 printf (_("Version %d\n"), (int) dwarf_version);
10063
10064 /* Prior versions did not exist, and future versions may not be
10065 backwards compatible. */
10066 if (dwarf_version != 5)
10067 {
10068 warn (_("Only DWARF version 5 .debug_names "
10069 "is currently supported.\n"));
10070 return 0;
10071 }
10072
10073 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
10074 if (padding != 0)
10075 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
10076 padding);
10077
10078 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
10079 if (comp_unit_count == 0)
10080 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
10081
10082 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
10083 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
10084 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
10085 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
10086 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
10087
10088 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
10089 if (augmentation_string_size % 4 != 0)
10090 {
10091 warn (_("Augmentation string length %u must be rounded up "
10092 "to a multiple of 4 in .debug_names.\n"),
10093 augmentation_string_size);
10094 augmentation_string_size += (-augmentation_string_size) & 3;
10095 }
10096 if (augmentation_string_size > (size_t) (unit_end - hdrptr))
10097 goto too_short;
10098
10099 printf (_("Augmentation string:"));
10100
10101 augmentation_printable = true;
10102 augmentation_string = (const char *) hdrptr;
10103
10104 for (i = 0; i < augmentation_string_size; i++)
10105 {
10106 unsigned char uc;
10107
10108 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
10109 printf (" %02x", uc);
10110
10111 if (uc != 0 && !ISPRINT (uc))
10112 augmentation_printable = false;
10113 }
10114
10115 if (augmentation_printable)
10116 {
10117 printf (" (\"");
10118 for (i = 0;
10119 i < augmentation_string_size && augmentation_string[i];
10120 ++i)
10121 putchar (augmentation_string[i]);
10122 printf ("\")");
10123 }
10124 putchar ('\n');
10125
10126 printf (_("CU table:\n"));
10127 if (_mul_overflow (comp_unit_count, offset_size, &total)
10128 || total > (size_t) (unit_end - hdrptr))
10129 goto too_short;
10130 for (i = 0; i < comp_unit_count; i++)
10131 {
10132 uint64_t cu_offset;
10133
10134 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
10135 printf ("[%3u] %#" PRIx64 "\n", i, cu_offset);
10136 }
10137 putchar ('\n');
10138
10139 printf (_("TU table:\n"));
10140 if (_mul_overflow (local_type_unit_count, offset_size, &total)
10141 || total > (size_t) (unit_end - hdrptr))
10142 goto too_short;
10143 for (i = 0; i < local_type_unit_count; i++)
10144 {
10145 uint64_t tu_offset;
10146
10147 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
10148 printf ("[%3u] %#" PRIx64 "\n", i, tu_offset);
10149 }
10150 putchar ('\n');
10151
10152 printf (_("Foreign TU table:\n"));
10153 if (_mul_overflow (foreign_type_unit_count, 8, &total)
10154 || total > (size_t) (unit_end - hdrptr))
10155 goto too_short;
10156 for (i = 0; i < foreign_type_unit_count; i++)
10157 {
10158 uint64_t signature;
10159
10160 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
10161 printf (_("[%3u] "), i);
10162 print_hex_ns (signature, 8);
10163 putchar ('\n');
10164 }
10165 putchar ('\n');
10166
10167 uint64_t xtra = (bucket_count * sizeof (uint32_t)
10168 + name_count * (sizeof (uint32_t) + 2 * offset_size)
10169 + abbrev_table_size);
10170 if (xtra > (size_t) (unit_end - hdrptr))
10171 {
10172 warn (_("Entry pool offset (%#" PRIx64 ") exceeds unit size %#tx "
10173 "for unit %#tx in the debug_names\n"),
10174 xtra, unit_end - unit_start, unit_start - section->start);
10175 return 0;
10176 }
10177 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
10178 hdrptr += bucket_count * sizeof (uint32_t);
10179 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
10180 hdrptr += name_count * sizeof (uint32_t);
10181 unsigned char *const name_table_string_offsets = hdrptr;
10182 hdrptr += name_count * offset_size;
10183 unsigned char *const name_table_entry_offsets = hdrptr;
10184 hdrptr += name_count * offset_size;
10185 unsigned char *const abbrev_table = hdrptr;
10186 hdrptr += abbrev_table_size;
10187 const unsigned char *const abbrev_table_end = hdrptr;
10188 unsigned char *const entry_pool = hdrptr;
10189
10190 size_t buckets_filled = 0;
10191 size_t bucketi;
10192 for (bucketi = 0; bucketi < bucket_count; bucketi++)
10193 {
10194 const uint32_t bucket = hash_table_buckets[bucketi];
10195
10196 if (bucket != 0)
10197 ++buckets_filled;
10198 }
10199 printf (ngettext ("Used %zu of %lu bucket.\n",
10200 "Used %zu of %lu buckets.\n",
10201 (unsigned long) bucket_count),
10202 buckets_filled, (unsigned long) bucket_count);
10203
10204 if (bucket_count != 0)
10205 {
10206 uint32_t hash_prev = 0;
10207 size_t hash_clash_count = 0;
10208 size_t longest_clash = 0;
10209 size_t this_length = 0;
10210 size_t hashi;
10211 for (hashi = 0; hashi < name_count; hashi++)
10212 {
10213 const uint32_t hash_this = hash_table_hashes[hashi];
10214
10215 if (hashi > 0)
10216 {
10217 if (hash_prev % bucket_count == hash_this % bucket_count)
10218 {
10219 ++hash_clash_count;
10220 ++this_length;
10221 longest_clash = MAX (longest_clash, this_length);
10222 }
10223 else
10224 this_length = 0;
10225 }
10226 hash_prev = hash_this;
10227 }
10228 printf (_("Out of %" PRIu64 " items there are %zu bucket clashes"
10229 " (longest of %zu entries).\n"),
10230 name_count, hash_clash_count, longest_clash);
10231
10232 if (name_count != buckets_filled + hash_clash_count)
10233 warn (_("The name_count (%" PRIu64 ")"
10234 " is not the same as the used bucket_count"
10235 " (%zu) + the hash clash count (%zu)"),
10236 name_count, buckets_filled, hash_clash_count);
10237 }
10238
10239 struct abbrev_lookup_entry
10240 {
10241 uint64_t abbrev_tag;
10242 unsigned char *abbrev_lookup_ptr;
10243 };
10244 struct abbrev_lookup_entry *abbrev_lookup = NULL;
10245 size_t abbrev_lookup_used = 0;
10246 size_t abbrev_lookup_allocated = 0;
10247
10248 unsigned char *abbrevptr = abbrev_table;
10249 for (;;)
10250 {
10251 uint64_t abbrev_tag;
10252
10253 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
10254 if (abbrev_tag == 0)
10255 break;
10256 if (abbrev_lookup_used == abbrev_lookup_allocated)
10257 {
10258 abbrev_lookup_allocated = MAX (0x100,
10259 abbrev_lookup_allocated * 2);
10260 abbrev_lookup = xrealloc (abbrev_lookup,
10261 (abbrev_lookup_allocated
10262 * sizeof (*abbrev_lookup)));
10263 }
10264 assert (abbrev_lookup_used < abbrev_lookup_allocated);
10265 struct abbrev_lookup_entry *entry;
10266 for (entry = abbrev_lookup;
10267 entry < abbrev_lookup + abbrev_lookup_used;
10268 entry++)
10269 if (entry->abbrev_tag == abbrev_tag)
10270 {
10271 warn (_("Duplicate abbreviation tag %" PRIu64
10272 " in unit %#tx in the debug_names section\n"),
10273 abbrev_tag, unit_start - section->start);
10274 break;
10275 }
10276 entry = &abbrev_lookup[abbrev_lookup_used++];
10277 entry->abbrev_tag = abbrev_tag;
10278 entry->abbrev_lookup_ptr = abbrevptr;
10279
10280 /* Skip DWARF tag. */
10281 SKIP_ULEB (abbrevptr, abbrev_table_end);
10282 for (;;)
10283 {
10284 uint64_t xindex, form;
10285
10286 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10287 READ_ULEB (form, abbrevptr, abbrev_table_end);
10288 if (xindex == 0 && form == 0)
10289 break;
10290 }
10291 }
10292
10293 printf (_("\nSymbol table:\n"));
10294 uint32_t namei;
10295 for (namei = 0; namei < name_count; ++namei)
10296 {
10297 uint64_t string_offset, entry_offset;
10298 unsigned char *p;
10299
10300 p = name_table_string_offsets + namei * offset_size;
10301 SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
10302 p = name_table_entry_offsets + namei * offset_size;
10303 SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
10304
10305 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
10306 fetch_indirect_string (string_offset));
10307
10308 unsigned char *entryptr = entry_pool + entry_offset;
10309
10310 /* We need to scan first whether there is a single or multiple
10311 entries. TAGNO is -2 for the first entry, it is -1 for the
10312 initial tag read of the second entry, then it becomes 0 for the
10313 first entry for real printing etc. */
10314 int tagno = -2;
10315 /* Initialize it due to a false compiler warning. */
10316 uint64_t second_abbrev_tag = -1;
10317 for (;;)
10318 {
10319 uint64_t abbrev_tag;
10320 uint64_t dwarf_tag;
10321 const struct abbrev_lookup_entry *entry;
10322
10323 READ_ULEB (abbrev_tag, entryptr, unit_end);
10324 if (tagno == -1)
10325 {
10326 second_abbrev_tag = abbrev_tag;
10327 tagno = 0;
10328 entryptr = entry_pool + entry_offset;
10329 continue;
10330 }
10331 if (abbrev_tag == 0)
10332 break;
10333 if (tagno >= 0)
10334 printf ("%s<%" PRIu64 ">",
10335 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
10336 abbrev_tag);
10337
10338 for (entry = abbrev_lookup;
10339 entry < abbrev_lookup + abbrev_lookup_used;
10340 entry++)
10341 if (entry->abbrev_tag == abbrev_tag)
10342 break;
10343 if (entry >= abbrev_lookup + abbrev_lookup_used)
10344 {
10345 warn (_("Undefined abbreviation tag %" PRId64
10346 " in unit %#tx in the debug_names section\n"),
10347 abbrev_tag,
10348 unit_start - section->start);
10349 break;
10350 }
10351 abbrevptr = entry->abbrev_lookup_ptr;
10352 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
10353 if (tagno >= 0)
10354 printf (" %s", get_TAG_name (dwarf_tag));
10355 for (;;)
10356 {
10357 uint64_t xindex, form;
10358
10359 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10360 READ_ULEB (form, abbrevptr, abbrev_table_end);
10361 if (xindex == 0 && form == 0)
10362 break;
10363
10364 if (tagno >= 0)
10365 printf (" %s", get_IDX_name (xindex));
10366 entryptr = read_and_display_attr_value (0, form, 0,
10367 unit_start, entryptr, unit_end,
10368 0, 0, offset_size,
10369 dwarf_version, NULL,
10370 (tagno < 0), section,
10371 NULL, '=', -1);
10372 }
10373 ++tagno;
10374 }
10375 if (tagno <= 0)
10376 printf (_(" <no entries>"));
10377 putchar ('\n');
10378 }
10379
10380 free (abbrev_lookup);
10381 }
10382
10383 return 1;
10384 }
10385
10386 static int
10387 display_debug_links (struct dwarf_section * section,
10388 void * file ATTRIBUTE_UNUSED)
10389 {
10390 const unsigned char * filename;
10391 unsigned int filelen;
10392
10393 introduce (section, false);
10394
10395 /* The .gnu_debuglink section is formatted as:
10396 (c-string) Filename.
10397 (padding) If needed to reach a 4 byte boundary.
10398 (uint32_t) CRC32 value.
10399
10400 The .gun_debugaltlink section is formatted as:
10401 (c-string) Filename.
10402 (binary) Build-ID. */
10403
10404 filename = section->start;
10405 filelen = strnlen ((const char *) filename, section->size);
10406 if (filelen == section->size)
10407 {
10408 warn (_("The debuglink filename is corrupt/missing\n"));
10409 return 0;
10410 }
10411
10412 printf (_(" Separate debug info file: %s\n"), filename);
10413
10414 if (startswith (section->name, ".gnu_debuglink"))
10415 {
10416 unsigned int crc32;
10417 unsigned int crc_offset;
10418
10419 crc_offset = filelen + 1;
10420 crc_offset = (crc_offset + 3) & ~3;
10421 if (crc_offset + 4 > section->size)
10422 {
10423 warn (_("CRC offset missing/truncated\n"));
10424 return 0;
10425 }
10426
10427 crc32 = byte_get (filename + crc_offset, 4);
10428
10429 printf (_(" CRC value: %#x\n"), crc32);
10430
10431 if (crc_offset + 4 < section->size)
10432 {
10433 warn (_("There are %#" PRIx64
10434 " extraneous bytes at the end of the section\n"),
10435 section->size - (crc_offset + 4));
10436 return 0;
10437 }
10438 }
10439 else /* startswith (section->name, ".gnu_debugaltlink") */
10440 {
10441 const unsigned char *build_id = section->start + filelen + 1;
10442 size_t build_id_len = section->size - (filelen + 1);
10443 size_t printed;
10444
10445 /* FIXME: Should we support smaller build-id notes ? */
10446 if (build_id_len < 0x14)
10447 {
10448 warn (_("Build-ID is too short (%#zx bytes)\n"), build_id_len);
10449 return 0;
10450 }
10451
10452 printed = printf (_(" Build-ID (%#zx bytes):"), build_id_len);
10453 display_data (printed, build_id, build_id_len);
10454 putchar ('\n');
10455 }
10456
10457 putchar ('\n');
10458 return 1;
10459 }
10460
10461 static int
10462 display_gdb_index (struct dwarf_section *section,
10463 void *file ATTRIBUTE_UNUSED)
10464 {
10465 unsigned char *start = section->start;
10466 uint32_t version;
10467 uint32_t cu_list_offset, tu_list_offset;
10468 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10469 unsigned int cu_list_elements, tu_list_elements;
10470 unsigned int address_table_elements, symbol_table_slots;
10471 unsigned char *cu_list, *tu_list;
10472 unsigned char *address_table, *symbol_table, *constant_pool;
10473 unsigned int i;
10474
10475 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10476
10477 introduce (section, false);
10478
10479 if (section->size < 6 * sizeof (uint32_t))
10480 {
10481 warn (_("Truncated header in the %s section.\n"), section->name);
10482 return 0;
10483 }
10484
10485 version = byte_get_little_endian (start, 4);
10486 printf (_("Version %lu\n"), (unsigned long) version);
10487
10488 /* Prior versions are obsolete, and future versions may not be
10489 backwards compatible. */
10490 if (version < 3 || version > 8)
10491 {
10492 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10493 return 0;
10494 }
10495 if (version < 4)
10496 warn (_("The address table data in version 3 may be wrong.\n"));
10497 if (version < 5)
10498 warn (_("Version 4 does not support case insensitive lookups.\n"));
10499 if (version < 6)
10500 warn (_("Version 5 does not include inlined functions.\n"));
10501 if (version < 7)
10502 warn (_("Version 6 does not include symbol attributes.\n"));
10503 /* Version 7 indices generated by Gold have bad type unit references,
10504 PR binutils/15021. But we don't know if the index was generated by
10505 Gold or not, so to avoid worrying users with gdb-generated indices
10506 we say nothing for version 7 here. */
10507
10508 cu_list_offset = byte_get_little_endian (start + 4, 4);
10509 tu_list_offset = byte_get_little_endian (start + 8, 4);
10510 address_table_offset = byte_get_little_endian (start + 12, 4);
10511 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10512 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10513
10514 if (cu_list_offset > section->size
10515 || tu_list_offset > section->size
10516 || address_table_offset > section->size
10517 || symbol_table_offset > section->size
10518 || constant_pool_offset > section->size
10519 || tu_list_offset < cu_list_offset
10520 || address_table_offset < tu_list_offset
10521 || symbol_table_offset < address_table_offset
10522 || constant_pool_offset < symbol_table_offset)
10523 {
10524 warn (_("Corrupt header in the %s section.\n"), section->name);
10525 return 0;
10526 }
10527
10528 cu_list_elements = (tu_list_offset - cu_list_offset) / 16;
10529 tu_list_elements = (address_table_offset - tu_list_offset) / 24;
10530 address_table_elements = (symbol_table_offset - address_table_offset) / 20;
10531 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10532
10533 cu_list = start + cu_list_offset;
10534 tu_list = start + tu_list_offset;
10535 address_table = start + address_table_offset;
10536 symbol_table = start + symbol_table_offset;
10537 constant_pool = start + constant_pool_offset;
10538
10539 printf (_("\nCU table:\n"));
10540 for (i = 0; i < cu_list_elements; i++)
10541 {
10542 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 16, 8);
10543 uint64_t cu_length = byte_get_little_endian (cu_list + i * 16 + 8, 8);
10544
10545 printf ("[%3u] %#" PRIx64 " - %#" PRIx64 "\n",
10546 i, cu_offset, cu_offset + cu_length - 1);
10547 }
10548
10549 printf (_("\nTU table:\n"));
10550 for (i = 0; i < tu_list_elements; i++)
10551 {
10552 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 24, 8);
10553 uint64_t type_offset = byte_get_little_endian (tu_list + i * 24 + 8, 8);
10554 uint64_t signature = byte_get_little_endian (tu_list + i * 24 + 16, 8);
10555
10556 printf ("[%3u] %#" PRIx64 " %#" PRIx64 " ",
10557 i, tu_offset, type_offset);
10558 print_hex_ns (signature, 8);
10559 printf ("\n");
10560 }
10561
10562 printf (_("\nAddress table:\n"));
10563 for (i = 0; i < address_table_elements; i++)
10564 {
10565 uint64_t low = byte_get_little_endian (address_table + i * 20, 8);
10566 uint64_t high = byte_get_little_endian (address_table + i * 20 + 8, 8);
10567 uint32_t cu_index = byte_get_little_endian (address_table + i * 20 + 16, 4);
10568
10569 print_hex (low, 8);
10570 print_hex (high, 8);
10571 printf ("%" PRIu32 "\n", cu_index);
10572 }
10573
10574 printf (_("\nSymbol table:\n"));
10575 for (i = 0; i < symbol_table_slots; ++i)
10576 {
10577 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10578 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10579 uint32_t num_cus, cu;
10580
10581 if (name_offset != 0
10582 || cu_vector_offset != 0)
10583 {
10584 unsigned int j;
10585
10586 /* PR 17531: file: 5b7b07ad. */
10587 if (name_offset >= section->size - constant_pool_offset)
10588 {
10589 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10590 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10591 name_offset, i);
10592 }
10593 else
10594 printf ("[%3u] %.*s:", i,
10595 (int) (section->size - (constant_pool_offset + name_offset)),
10596 constant_pool + name_offset);
10597
10598 if (section->size - constant_pool_offset < 4
10599 || cu_vector_offset > section->size - constant_pool_offset - 4)
10600 {
10601 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10602 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10603 cu_vector_offset, i);
10604 continue;
10605 }
10606
10607 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10608
10609 if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10610 + cu_vector_offset + 4))
10611 {
10612 printf ("<invalid number of CUs: %d>\n", num_cus);
10613 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10614 num_cus, i);
10615 continue;
10616 }
10617
10618 if (num_cus > 1)
10619 printf ("\n");
10620
10621 for (j = 0; j < num_cus; ++j)
10622 {
10623 int is_static;
10624 gdb_index_symbol_kind kind;
10625
10626 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10627 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10628 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10629 cu = GDB_INDEX_CU_VALUE (cu);
10630 /* Convert to TU number if it's for a type unit. */
10631 if (cu >= cu_list_elements)
10632 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10633 (unsigned long) cu - cu_list_elements);
10634 else
10635 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10636
10637 printf (" [%s, %s]",
10638 is_static ? _("static") : _("global"),
10639 get_gdb_index_symbol_kind_name (kind));
10640 if (num_cus > 1)
10641 printf ("\n");
10642 }
10643 if (num_cus <= 1)
10644 printf ("\n");
10645 }
10646 }
10647
10648 return 1;
10649 }
10650
10651 /* Pre-allocate enough space for the CU/TU sets needed. */
10652
10653 static void
10654 prealloc_cu_tu_list (unsigned int nshndx)
10655 {
10656 if (shndx_pool == NULL)
10657 {
10658 shndx_pool_size = nshndx;
10659 shndx_pool_used = 0;
10660 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10661 sizeof (unsigned int));
10662 }
10663 else
10664 {
10665 shndx_pool_size = shndx_pool_used + nshndx;
10666 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10667 sizeof (unsigned int));
10668 }
10669 }
10670
10671 static void
10672 add_shndx_to_cu_tu_entry (unsigned int shndx)
10673 {
10674 shndx_pool [shndx_pool_used++] = shndx;
10675 }
10676
10677 static void
10678 end_cu_tu_entry (void)
10679 {
10680 shndx_pool [shndx_pool_used++] = 0;
10681 }
10682
10683 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10684
10685 static const char *
10686 get_DW_SECT_short_name (unsigned int dw_sect)
10687 {
10688 static char buf[16];
10689
10690 switch (dw_sect)
10691 {
10692 case DW_SECT_INFO:
10693 return "info";
10694 case DW_SECT_TYPES:
10695 return "types";
10696 case DW_SECT_ABBREV:
10697 return "abbrev";
10698 case DW_SECT_LINE:
10699 return "line";
10700 case DW_SECT_LOC:
10701 return "loc";
10702 case DW_SECT_STR_OFFSETS:
10703 return "str_off";
10704 case DW_SECT_MACINFO:
10705 return "macinfo";
10706 case DW_SECT_MACRO:
10707 return "macro";
10708 default:
10709 break;
10710 }
10711
10712 snprintf (buf, sizeof (buf), "%d", dw_sect);
10713 return buf;
10714 }
10715
10716 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10717 These sections are extensions for Fission.
10718 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
10719
10720 static int
10721 process_cu_tu_index (struct dwarf_section *section, int do_display)
10722 {
10723 unsigned char *phdr = section->start;
10724 unsigned char *limit = phdr + section->size;
10725 unsigned char *phash;
10726 unsigned char *pindex;
10727 unsigned char *ppool;
10728 unsigned int version;
10729 unsigned int ncols = 0;
10730 unsigned int nused;
10731 unsigned int nslots;
10732 unsigned int i;
10733 unsigned int j;
10734 uint64_t signature;
10735 size_t total;
10736
10737 /* PR 17512: file: 002-168123-0.004. */
10738 if (phdr == NULL)
10739 {
10740 warn (_("Section %s is empty\n"), section->name);
10741 return 0;
10742 }
10743 /* PR 17512: file: 002-376-0.004. */
10744 if (section->size < 24)
10745 {
10746 warn (_("Section %s is too small to contain a CU/TU header\n"),
10747 section->name);
10748 return 0;
10749 }
10750
10751 phash = phdr;
10752 SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
10753 if (version >= 2)
10754 SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
10755 SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
10756 SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
10757
10758 pindex = phash + (size_t) nslots * 8;
10759 ppool = pindex + (size_t) nslots * 4;
10760
10761 if (do_display)
10762 {
10763 introduce (section, false);
10764
10765 printf (_(" Version: %u\n"), version);
10766 if (version >= 2)
10767 printf (_(" Number of columns: %u\n"), ncols);
10768 printf (_(" Number of used entries: %u\n"), nused);
10769 printf (_(" Number of slots: %u\n\n"), nslots);
10770 }
10771
10772 /* PR 17531: file: 45d69832. */
10773 if (_mul_overflow ((size_t) nslots, 12, &total)
10774 || total > (size_t) (limit - phash))
10775 {
10776 warn (ngettext ("Section %s is too small for %u slot\n",
10777 "Section %s is too small for %u slots\n",
10778 nslots),
10779 section->name, nslots);
10780 return 0;
10781 }
10782
10783 if (version == 1)
10784 {
10785 unsigned char *shndx_list;
10786 unsigned int shndx;
10787
10788 if (!do_display)
10789 {
10790 prealloc_cu_tu_list ((limit - ppool) / 4);
10791 for (shndx_list = ppool + 4; shndx_list <= limit - 4; shndx_list += 4)
10792 {
10793 shndx = byte_get (shndx_list, 4);
10794 add_shndx_to_cu_tu_entry (shndx);
10795 }
10796 end_cu_tu_entry ();
10797 }
10798 else
10799 for (i = 0; i < nslots; i++)
10800 {
10801 SAFE_BYTE_GET (signature, phash, 8, limit);
10802 if (signature != 0)
10803 {
10804 SAFE_BYTE_GET (j, pindex, 4, limit);
10805 shndx_list = ppool + j * 4;
10806 /* PR 17531: file: 705e010d. */
10807 if (shndx_list < ppool)
10808 {
10809 warn (_("Section index pool located before start of section\n"));
10810 return 0;
10811 }
10812
10813 printf (_(" [%3d] Signature: %#" PRIx64 " Sections: "),
10814 i, signature);
10815 for (;;)
10816 {
10817 if (shndx_list >= limit)
10818 {
10819 warn (_("Section %s too small for shndx pool\n"),
10820 section->name);
10821 return 0;
10822 }
10823 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
10824 if (shndx == 0)
10825 break;
10826 printf (" %d", shndx);
10827 shndx_list += 4;
10828 }
10829 printf ("\n");
10830 }
10831 phash += 8;
10832 pindex += 4;
10833 }
10834 }
10835 else if (version == 2)
10836 {
10837 unsigned int val;
10838 unsigned int dw_sect;
10839 unsigned char *ph = phash;
10840 unsigned char *pi = pindex;
10841 unsigned char *poffsets = ppool + (size_t) ncols * 4;
10842 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
10843 bool is_tu_index;
10844 struct cu_tu_set *this_set = NULL;
10845 unsigned int row;
10846 unsigned char *prow;
10847 size_t temp;
10848
10849 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
10850
10851 /* PR 17531: file: 0dd159bf.
10852 Check for integer overflow (can occur when size_t is 32-bit)
10853 with overlarge ncols or nused values. */
10854 if (nused == -1u
10855 || _mul_overflow ((size_t) ncols, 4, &temp)
10856 || _mul_overflow ((size_t) nused + 1, temp, &total)
10857 || total > (size_t) (limit - ppool))
10858 {
10859 warn (_("Section %s too small for offset and size tables\n"),
10860 section->name);
10861 return 0;
10862 }
10863
10864 if (do_display)
10865 {
10866 printf (_(" Offset table\n"));
10867 printf (" slot %-16s ",
10868 is_tu_index ? _("signature") : _("dwo_id"));
10869 }
10870 else
10871 {
10872 if (is_tu_index)
10873 {
10874 tu_count = nused;
10875 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10876 this_set = tu_sets;
10877 }
10878 else
10879 {
10880 cu_count = nused;
10881 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10882 this_set = cu_sets;
10883 }
10884 }
10885
10886 if (do_display)
10887 {
10888 for (j = 0; j < ncols; j++)
10889 {
10890 unsigned char *p = ppool + j * 4;
10891 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10892 printf (" %8s", get_DW_SECT_short_name (dw_sect));
10893 }
10894 printf ("\n");
10895 }
10896
10897 for (i = 0; i < nslots; i++)
10898 {
10899 SAFE_BYTE_GET (signature, ph, 8, limit);
10900
10901 SAFE_BYTE_GET (row, pi, 4, limit);
10902 if (row != 0)
10903 {
10904 /* PR 17531: file: a05f6ab3. */
10905 if (row > nused)
10906 {
10907 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
10908 row, nused);
10909 return 0;
10910 }
10911
10912 if (!do_display)
10913 {
10914 size_t num_copy = sizeof (uint64_t);
10915
10916 memcpy (&this_set[row - 1].signature, ph, num_copy);
10917 }
10918
10919 prow = poffsets + (row - 1) * ncols * 4;
10920 if (do_display)
10921 printf (" [%3d] %#" PRIx64, i, signature);
10922 for (j = 0; j < ncols; j++)
10923 {
10924 unsigned char *p = prow + j * 4;
10925 SAFE_BYTE_GET (val, p, 4, limit);
10926 if (do_display)
10927 printf (" %8d", val);
10928 else
10929 {
10930 p = ppool + j * 4;
10931 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10932
10933 /* PR 17531: file: 10796eb3. */
10934 if (dw_sect >= DW_SECT_MAX)
10935 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10936 else
10937 this_set [row - 1].section_offsets [dw_sect] = val;
10938 }
10939 }
10940
10941 if (do_display)
10942 printf ("\n");
10943 }
10944 ph += 8;
10945 pi += 4;
10946 }
10947
10948 ph = phash;
10949 pi = pindex;
10950 if (do_display)
10951 {
10952 printf ("\n");
10953 printf (_(" Size table\n"));
10954 printf (" slot %-16s ",
10955 is_tu_index ? _("signature") : _("dwo_id"));
10956 }
10957
10958 for (j = 0; j < ncols; j++)
10959 {
10960 unsigned char *p = ppool + j * 4;
10961 SAFE_BYTE_GET (val, p, 4, limit);
10962 if (do_display)
10963 printf (" %8s", get_DW_SECT_short_name (val));
10964 }
10965
10966 if (do_display)
10967 printf ("\n");
10968
10969 for (i = 0; i < nslots; i++)
10970 {
10971 SAFE_BYTE_GET (signature, ph, 8, limit);
10972
10973 SAFE_BYTE_GET (row, pi, 4, limit);
10974 if (row != 0)
10975 {
10976 prow = psizes + (row - 1) * ncols * 4;
10977
10978 if (do_display)
10979 printf (" [%3d] %#" PRIx64, i, signature);
10980
10981 for (j = 0; j < ncols; j++)
10982 {
10983 unsigned char *p = prow + j * 4;
10984
10985 /* PR 28645: Check for overflow. Since we do not know how
10986 many populated rows there will be, we cannot just
10987 perform a single check at the start of this function. */
10988 if (p > (limit - 4))
10989 {
10990 if (do_display)
10991 printf ("\n");
10992 warn (_("Too many rows/columns in DWARF index section %s\n"),
10993 section->name);
10994 return 0;
10995 }
10996
10997 SAFE_BYTE_GET (val, p, 4, limit);
10998
10999 if (do_display)
11000 printf (" %8d", val);
11001 else
11002 {
11003 p = ppool + j * 4;
11004 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11005 if (dw_sect >= DW_SECT_MAX)
11006 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11007 else
11008 this_set [row - 1].section_sizes [dw_sect] = val;
11009 }
11010 }
11011
11012 if (do_display)
11013 printf ("\n");
11014 }
11015
11016 ph += 8;
11017 pi += 4;
11018 }
11019 }
11020 else if (do_display)
11021 printf (_(" Unsupported version (%d)\n"), version);
11022
11023 if (do_display)
11024 printf ("\n");
11025
11026 return 1;
11027 }
11028
11029 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
11030
11031 /* Load the CU and TU indexes if present. This will build a list of
11032 section sets that we can use to associate a .debug_info.dwo section
11033 with its associated .debug_abbrev.dwo section in a .dwp file. */
11034
11035 static bool
11036 load_cu_tu_indexes (void *file)
11037 {
11038 /* If we have already loaded (or tried to load) the CU and TU indexes
11039 then do not bother to repeat the task. */
11040 if (cu_tu_indexes_read == -1)
11041 {
11042 cu_tu_indexes_read = true;
11043
11044 if (load_debug_section_with_follow (dwp_cu_index, file))
11045 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
11046 cu_tu_indexes_read = false;
11047
11048 if (load_debug_section_with_follow (dwp_tu_index, file))
11049 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
11050 cu_tu_indexes_read = false;
11051 }
11052
11053 return (bool) cu_tu_indexes_read;
11054 }
11055
11056 /* Find the set of sections that includes section SHNDX. */
11057
11058 unsigned int *
11059 find_cu_tu_set (void *file, unsigned int shndx)
11060 {
11061 unsigned int i;
11062
11063 if (! load_cu_tu_indexes (file))
11064 return NULL;
11065
11066 /* Find SHNDX in the shndx pool. */
11067 for (i = 0; i < shndx_pool_used; i++)
11068 if (shndx_pool [i] == shndx)
11069 break;
11070
11071 if (i >= shndx_pool_used)
11072 return NULL;
11073
11074 /* Now backup to find the first entry in the set. */
11075 while (i > 0 && shndx_pool [i - 1] != 0)
11076 i--;
11077
11078 return shndx_pool + i;
11079 }
11080
11081 /* Display a .debug_cu_index or .debug_tu_index section. */
11082
11083 static int
11084 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
11085 {
11086 return process_cu_tu_index (section, 1);
11087 }
11088
11089 static int
11090 display_debug_not_supported (struct dwarf_section *section,
11091 void *file ATTRIBUTE_UNUSED)
11092 {
11093 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
11094 section->name);
11095
11096 return 1;
11097 }
11098
11099 /* Like malloc, but takes two parameters like calloc.
11100 Verifies that the first parameter is not too large.
11101 Note: does *not* initialise the allocated memory to zero. */
11102
11103 void *
11104 cmalloc (uint64_t nmemb, size_t size)
11105 {
11106 /* Check for overflow. */
11107 if (nmemb >= ~(size_t) 0 / size)
11108 return NULL;
11109
11110 return xmalloc (nmemb * size);
11111 }
11112
11113 /* Like xmalloc, but takes two parameters like calloc.
11114 Verifies that the first parameter is not too large.
11115 Note: does *not* initialise the allocated memory to zero. */
11116
11117 void *
11118 xcmalloc (uint64_t nmemb, size_t size)
11119 {
11120 /* Check for overflow. */
11121 if (nmemb >= ~(size_t) 0 / size)
11122 {
11123 fprintf (stderr,
11124 _("Attempt to allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11125 nmemb);
11126 xexit (1);
11127 }
11128
11129 return xmalloc (nmemb * size);
11130 }
11131
11132 /* Like xrealloc, but takes three parameters.
11133 Verifies that the second parameter is not too large.
11134 Note: does *not* initialise any new memory to zero. */
11135
11136 void *
11137 xcrealloc (void *ptr, uint64_t nmemb, size_t size)
11138 {
11139 /* Check for overflow. */
11140 if (nmemb >= ~(size_t) 0 / size)
11141 {
11142 error (_("Attempt to re-allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11143 nmemb);
11144 xexit (1);
11145 }
11146
11147 return xrealloc (ptr, nmemb * size);
11148 }
11149
11150 /* Like xcalloc, but verifies that the first parameter is not too large. */
11151
11152 void *
11153 xcalloc2 (uint64_t nmemb, size_t size)
11154 {
11155 /* Check for overflow. */
11156 if (nmemb >= ~(size_t) 0 / size)
11157 {
11158 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#" PRIx64 "\n"),
11159 nmemb);
11160 xexit (1);
11161 }
11162
11163 return xcalloc (nmemb, size);
11164 }
11165
11166 static unsigned long
11167 calc_gnu_debuglink_crc32 (unsigned long crc,
11168 const unsigned char *buf,
11169 size_t len)
11170 {
11171 static const unsigned long crc32_table[256] =
11172 {
11173 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
11174 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
11175 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
11176 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
11177 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
11178 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
11179 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
11180 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
11181 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
11182 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
11183 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
11184 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
11185 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
11186 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
11187 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
11188 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
11189 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
11190 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
11191 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
11192 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
11193 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
11194 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
11195 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
11196 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
11197 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
11198 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
11199 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
11200 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
11201 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
11202 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
11203 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
11204 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
11205 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
11206 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
11207 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
11208 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
11209 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
11210 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
11211 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
11212 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
11213 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
11214 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
11215 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
11216 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
11217 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
11218 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
11219 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
11220 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
11221 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
11222 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
11223 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
11224 0x2d02ef8d
11225 };
11226 const unsigned char *end;
11227
11228 crc = ~crc & 0xffffffff;
11229 for (end = buf + len; buf < end; ++ buf)
11230 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
11231 return ~crc & 0xffffffff;
11232 }
11233
11234 typedef bool (*check_func_type) (const char *, void *);
11235 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
11236
11237 static bool
11238 check_gnu_debuglink (const char * pathname, void * crc_pointer)
11239 {
11240 static unsigned char buffer[8 * 1024];
11241 FILE *f;
11242 size_t count;
11243 unsigned long crc = 0;
11244 void *sep_data;
11245
11246 sep_data = open_debug_file (pathname);
11247 if (sep_data == NULL)
11248 return false;
11249
11250 /* Yes - we are opening the file twice... */
11251 f = fopen (pathname, "rb");
11252 if (f == NULL)
11253 {
11254 /* Paranoia: This should never happen. */
11255 close_debug_file (sep_data);
11256 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
11257 return false;
11258 }
11259
11260 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
11261 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
11262
11263 fclose (f);
11264
11265 if (crc != * (unsigned long *) crc_pointer)
11266 {
11267 close_debug_file (sep_data);
11268 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
11269 pathname);
11270 return false;
11271 }
11272
11273 return true;
11274 }
11275
11276 static const char *
11277 parse_gnu_debuglink (struct dwarf_section * section, void * data)
11278 {
11279 const char * name;
11280 unsigned int crc_offset;
11281 unsigned long * crc32 = (unsigned long *) data;
11282
11283 /* The name is first.
11284 The CRC value is stored after the filename, aligned up to 4 bytes. */
11285 name = (const char *) section->start;
11286
11287 crc_offset = strnlen (name, section->size) + 1;
11288 if (crc_offset == 1)
11289 return NULL;
11290 crc_offset = (crc_offset + 3) & ~3;
11291 if (crc_offset + 4 > section->size)
11292 return NULL;
11293
11294 * crc32 = byte_get (section->start + crc_offset, 4);
11295 return name;
11296 }
11297
11298 static bool
11299 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
11300 {
11301 void * sep_data = open_debug_file (filename);
11302
11303 if (sep_data == NULL)
11304 return false;
11305
11306 /* FIXME: We should now extract the build-id in the separate file
11307 and check it... */
11308
11309 return true;
11310 }
11311
11312 typedef struct build_id_data
11313 {
11314 size_t len;
11315 const unsigned char *data;
11316 } Build_id_data;
11317
11318 static const char *
11319 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11320 {
11321 const char *name;
11322 size_t namelen;
11323 size_t id_len;
11324 Build_id_data *build_id_data;
11325
11326 /* The name is first.
11327 The build-id follows immediately, with no padding, up to the section's end. */
11328
11329 name = (const char *) section->start;
11330 namelen = strnlen (name, section->size) + 1;
11331 if (namelen == 1)
11332 return NULL;
11333 if (namelen >= section->size)
11334 return NULL;
11335
11336 id_len = section->size - namelen;
11337 if (id_len < 0x14)
11338 return NULL;
11339
11340 build_id_data = (Build_id_data *) data;
11341 build_id_data->len = id_len;
11342 build_id_data->data = section->start + namelen;
11343
11344 return name;
11345 }
11346
11347 static void
11348 add_separate_debug_file (const char * filename, void * handle)
11349 {
11350 separate_info * i = xmalloc (sizeof * i);
11351
11352 i->filename = filename;
11353 i->handle = handle;
11354 i->next = first_separate_info;
11355 first_separate_info = i;
11356 }
11357
11358 #if HAVE_LIBDEBUGINFOD
11359 /* Query debuginfod servers for the target debuglink or debugaltlink
11360 file. If successful, store the path of the file in filename and
11361 return TRUE, otherwise return FALSE. */
11362
11363 static bool
11364 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11365 char ** filename,
11366 void * file)
11367 {
11368 size_t build_id_len;
11369 unsigned char * build_id;
11370
11371 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11372 {
11373 /* Get the build-id of file. */
11374 build_id = get_build_id (file);
11375 build_id_len = 0;
11376 }
11377 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11378 {
11379 /* Get the build-id of the debugaltlink file. */
11380 unsigned int filelen;
11381
11382 filelen = strnlen ((const char *)section->start, section->size);
11383 if (filelen == section->size)
11384 /* Corrupt debugaltlink. */
11385 return false;
11386
11387 build_id = section->start + filelen + 1;
11388 build_id_len = section->size - (filelen + 1);
11389
11390 if (build_id_len == 0)
11391 return false;
11392 }
11393 else
11394 return false;
11395
11396 if (build_id)
11397 {
11398 int fd;
11399 debuginfod_client * client;
11400
11401 client = debuginfod_begin ();
11402 if (client == NULL)
11403 return false;
11404
11405 /* Query debuginfod servers for the target file. If found its path
11406 will be stored in filename. */
11407 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11408 debuginfod_end (client);
11409
11410 /* Only free build_id if we allocated space for a hex string
11411 in get_build_id (). */
11412 if (build_id_len == 0)
11413 free (build_id);
11414
11415 if (fd >= 0)
11416 {
11417 /* File successfully retrieved. Close fd since we want to
11418 use open_debug_file () on filename instead. */
11419 close (fd);
11420 return true;
11421 }
11422 }
11423
11424 return false;
11425 }
11426 #endif /* HAVE_LIBDEBUGINFOD */
11427
11428 static void *
11429 load_separate_debug_info (const char * main_filename,
11430 struct dwarf_section * xlink,
11431 parse_func_type parse_func,
11432 check_func_type check_func,
11433 void * func_data,
11434 void * file ATTRIBUTE_UNUSED)
11435 {
11436 const char * separate_filename;
11437 char * debug_filename;
11438 char * canon_dir;
11439 size_t canon_dirlen;
11440 size_t dirlen;
11441 char * canon_filename;
11442 char * canon_debug_filename;
11443 bool self;
11444
11445 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11446 {
11447 warn (_("Corrupt debuglink section: %s\n"),
11448 xlink->name ? xlink->name : xlink->uncompressed_name);
11449 return NULL;
11450 }
11451
11452 /* Attempt to locate the separate file.
11453 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11454
11455 canon_filename = lrealpath (main_filename);
11456 canon_dir = xstrdup (canon_filename);
11457
11458 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11459 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11460 break;
11461 canon_dir[canon_dirlen] = '\0';
11462
11463 #ifndef DEBUGDIR
11464 #define DEBUGDIR "/lib/debug"
11465 #endif
11466 #ifndef EXTRA_DEBUG_ROOT1
11467 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11468 #endif
11469 #ifndef EXTRA_DEBUG_ROOT2
11470 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11471 #endif
11472
11473 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11474 + canon_dirlen
11475 + strlen (".debug/")
11476 #ifdef EXTRA_DEBUG_ROOT1
11477 + strlen (EXTRA_DEBUG_ROOT1)
11478 #endif
11479 #ifdef EXTRA_DEBUG_ROOT2
11480 + strlen (EXTRA_DEBUG_ROOT2)
11481 #endif
11482 + strlen (separate_filename)
11483 + 1);
11484 if (debug_filename == NULL)
11485 {
11486 warn (_("Out of memory"));
11487 free (canon_dir);
11488 free (canon_filename);
11489 return NULL;
11490 }
11491
11492 /* First try in the current directory. */
11493 sprintf (debug_filename, "%s", separate_filename);
11494 if (check_func (debug_filename, func_data))
11495 goto found;
11496
11497 /* Then try in a subdirectory called .debug. */
11498 sprintf (debug_filename, ".debug/%s", separate_filename);
11499 if (check_func (debug_filename, func_data))
11500 goto found;
11501
11502 /* Then try in the same directory as the original file. */
11503 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11504 if (check_func (debug_filename, func_data))
11505 goto found;
11506
11507 /* And the .debug subdirectory of that directory. */
11508 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11509 if (check_func (debug_filename, func_data))
11510 goto found;
11511
11512 #ifdef EXTRA_DEBUG_ROOT1
11513 /* Try the first extra debug file root. */
11514 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11515 if (check_func (debug_filename, func_data))
11516 goto found;
11517
11518 /* Try the first extra debug file root. */
11519 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11520 if (check_func (debug_filename, func_data))
11521 goto found;
11522 #endif
11523
11524 #ifdef EXTRA_DEBUG_ROOT2
11525 /* Try the second extra debug file root. */
11526 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11527 if (check_func (debug_filename, func_data))
11528 goto found;
11529 #endif
11530
11531 /* Then try in the global debug_filename directory. */
11532 strcpy (debug_filename, DEBUGDIR);
11533 dirlen = strlen (DEBUGDIR) - 1;
11534 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11535 strcat (debug_filename, "/");
11536 strcat (debug_filename, (const char *) separate_filename);
11537
11538 if (check_func (debug_filename, func_data))
11539 goto found;
11540
11541 #if HAVE_LIBDEBUGINFOD
11542 {
11543 char * tmp_filename;
11544
11545 if (use_debuginfod
11546 && debuginfod_fetch_separate_debug_info (xlink,
11547 & tmp_filename,
11548 file))
11549 {
11550 /* File successfully downloaded from server, replace
11551 debug_filename with the file's path. */
11552 free (debug_filename);
11553 debug_filename = tmp_filename;
11554 goto found;
11555 }
11556 }
11557 #endif
11558
11559 if (do_debug_links)
11560 {
11561 /* Failed to find the file. */
11562 warn (_("could not find separate debug file '%s'\n"),
11563 separate_filename);
11564 warn (_("tried: %s\n"), debug_filename);
11565
11566 #ifdef EXTRA_DEBUG_ROOT2
11567 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11568 separate_filename);
11569 warn (_("tried: %s\n"), debug_filename);
11570 #endif
11571
11572 #ifdef EXTRA_DEBUG_ROOT1
11573 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11574 canon_dir, separate_filename);
11575 warn (_("tried: %s\n"), debug_filename);
11576
11577 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11578 separate_filename);
11579 warn (_("tried: %s\n"), debug_filename);
11580 #endif
11581
11582 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11583 separate_filename);
11584 warn (_("tried: %s\n"), debug_filename);
11585
11586 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11587 warn (_("tried: %s\n"), debug_filename);
11588
11589 sprintf (debug_filename, ".debug/%s", separate_filename);
11590 warn (_("tried: %s\n"), debug_filename);
11591
11592 sprintf (debug_filename, "%s", separate_filename);
11593 warn (_("tried: %s\n"), debug_filename);
11594
11595 #if HAVE_LIBDEBUGINFOD
11596 if (use_debuginfod)
11597 {
11598 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11599
11600 if (urls == NULL)
11601 urls = "";
11602
11603 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11604 }
11605 #endif
11606 }
11607
11608 free (canon_dir);
11609 free (debug_filename);
11610 free (canon_filename);
11611 return NULL;
11612
11613 found:
11614 free (canon_dir);
11615
11616 canon_debug_filename = lrealpath (debug_filename);
11617 self = strcmp (canon_debug_filename, canon_filename) == 0;
11618 free (canon_filename);
11619 free (canon_debug_filename);
11620 if (self)
11621 {
11622 free (debug_filename);
11623 return NULL;
11624 }
11625
11626 void * debug_handle;
11627
11628 /* Now open the file.... */
11629 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11630 {
11631 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11632 free (debug_filename);
11633 return NULL;
11634 }
11635
11636 /* FIXME: We do not check to see if there are any other separate debug info
11637 files that would also match. */
11638
11639 if (do_debug_links)
11640 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11641 add_separate_debug_file (debug_filename, debug_handle);
11642
11643 /* Do not free debug_filename - it might be referenced inside
11644 the structure returned by open_debug_file(). */
11645 return debug_handle;
11646 }
11647
11648 /* Attempt to load a separate dwarf object file. */
11649
11650 static void *
11651 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11652 {
11653 char * separate_filename;
11654 void * separate_handle;
11655
11656 if (IS_ABSOLUTE_PATH (name))
11657 separate_filename = strdup (name);
11658 else
11659 /* FIXME: Skip adding / if dwo_dir ends in /. */
11660 separate_filename = concat (dir, "/", name, NULL);
11661 if (separate_filename == NULL)
11662 {
11663 warn (_("Out of memory allocating dwo filename\n"));
11664 return NULL;
11665 }
11666
11667 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11668 {
11669 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11670 free (separate_filename);
11671 return NULL;
11672 }
11673
11674 /* FIXME: We should check the dwo_id. */
11675
11676 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11677
11678 add_separate_debug_file (separate_filename, separate_handle);
11679 /* Note - separate_filename will be freed in free_debug_memory(). */
11680 return separate_handle;
11681 }
11682
11683 static void *
11684 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11685 {
11686 char * f = filename;
11687
11688 f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11689 id_len --;
11690 while (id_len --)
11691 f += sprintf (f, "%02x", (unsigned) *data++);
11692 strcpy (f, ".debug");
11693
11694 return open_debug_file (filename);
11695 }
11696
11697 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section. */
11698
11699 static void
11700 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11701 {
11702 if (! load_debug_section (note_gnu_build_id, main_file))
11703 return; /* No .note.gnu.build-id section. */
11704
11705 struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11706 if (section == NULL)
11707 {
11708 warn (_("Unable to load the .note.gnu.build-id section\n"));
11709 return;
11710 }
11711
11712 if (section->start == NULL || section->size < 0x18)
11713 {
11714 warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11715 return;
11716 }
11717
11718 /* In theory we should extract the contents of the section into
11719 a note structure and then check the fields. For now though
11720 just use hard coded offsets instead:
11721
11722 Field Bytes Contents
11723 NSize 0...3 4
11724 DSize 4...7 8+
11725 Type 8..11 3 (NT_GNU_BUILD_ID)
11726 Name 12.15 GNU\0
11727 Data 16.... */
11728
11729 /* FIXME: Check the name size, name and type fields. */
11730
11731 unsigned long build_id_size;
11732 build_id_size = byte_get (section->start + 4, 4);
11733 if (build_id_size < 8)
11734 {
11735 warn (_(".note.gnu.build-id data size is too small\n"));
11736 return;
11737 }
11738
11739 if (build_id_size > (section->size - 16))
11740 {
11741 warn (_(".note.gnu.build-id data size is too big\n"));
11742 return;
11743 }
11744
11745 char * filename;
11746 filename = xmalloc (strlen (".build-id/")
11747 + build_id_size * 2 + 2
11748 + strlen (".debug")
11749 /* The next string should be the same as the longest
11750 name found in the prefixes[] array below. */
11751 + strlen ("/usrlib64/debug/usr")
11752 + 1);
11753 void * handle;
11754
11755 static const char * prefixes[] =
11756 {
11757 "",
11758 ".debug/",
11759 "/usr/lib/debug/",
11760 "/usr/lib/debug/usr/",
11761 "/usr/lib64/debug/",
11762 "/usr/lib64/debug/usr"
11763 };
11764 long unsigned int i;
11765
11766 for (i = 0; i < ARRAY_SIZE (prefixes); i++)
11767 {
11768 handle = try_build_id_prefix (prefixes[i], filename,
11769 section->start + 16, build_id_size);
11770 if (handle != NULL)
11771 break;
11772 }
11773 /* FIXME: TYhe BFD library also tries a global debugfile directory prefix. */
11774 if (handle == NULL)
11775 {
11776 /* Failed to find a debug file associated with the build-id.
11777 This is not an error however, rather it just means that
11778 the debug info has probably not been loaded on the system,
11779 or that another method is being used to link to the debug
11780 info. */
11781 free (filename);
11782 return;
11783 }
11784
11785 add_separate_debug_file (filename, handle);
11786 }
11787
11788 /* Try to load a debug file pointed to by the .debug_sup section. */
11789
11790 static void
11791 load_debug_sup_file (const char * main_filename, void * file)
11792 {
11793 if (! load_debug_section (debug_sup, file))
11794 return; /* No .debug_sup section. */
11795
11796 struct dwarf_section * section;
11797 section = & debug_displays [debug_sup].section;
11798 assert (section != NULL);
11799
11800 if (section->start == NULL || section->size < 5)
11801 {
11802 warn (_(".debug_sup section is corrupt/empty\n"));
11803 return;
11804 }
11805
11806 if (section->start[2] != 0)
11807 return; /* This is a supplementary file. */
11808
11809 const char * filename = (const char *) section->start + 3;
11810 if (strnlen (filename, section->size - 3) == section->size - 3)
11811 {
11812 warn (_("filename in .debug_sup section is corrupt\n"));
11813 return;
11814 }
11815
11816 if (filename[0] != '/' && strchr (main_filename, '/'))
11817 {
11818 char * new_name;
11819 int new_len;
11820
11821 new_len = asprintf (& new_name, "%.*s/%s",
11822 (int) (strrchr (main_filename, '/') - main_filename),
11823 main_filename,
11824 filename);
11825 if (new_len < 3)
11826 {
11827 warn (_("unable to construct path for supplementary debug file"));
11828 if (new_len > -1)
11829 free (new_name);
11830 return;
11831 }
11832 filename = new_name;
11833 }
11834 else
11835 {
11836 /* PR 27796: Make sure that we pass a filename that can be free'd to
11837 add_separate_debug_file(). */
11838 filename = strdup (filename);
11839 if (filename == NULL)
11840 {
11841 warn (_("out of memory constructing filename for .debug_sup link\n"));
11842 return;
11843 }
11844 }
11845
11846 void * handle = open_debug_file (filename);
11847 if (handle == NULL)
11848 {
11849 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
11850 free ((void *) filename);
11851 return;
11852 }
11853
11854 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
11855
11856 /* FIXME: Compare the checksums, if present. */
11857 add_separate_debug_file (filename, handle);
11858 }
11859
11860 /* Load a debuglink section and/or a debugaltlink section, if either are present.
11861 Recursively check the loaded files for more of these sections.
11862 Also follow any links in .debug_sup sections.
11863 FIXME: Should also check for DWO_* entries in the newly loaded files. */
11864
11865 static void
11866 check_for_and_load_links (void * file, const char * filename)
11867 {
11868 void * handle = NULL;
11869
11870 if (load_debug_section (gnu_debugaltlink, file))
11871 {
11872 Build_id_data build_id_data;
11873
11874 handle = load_separate_debug_info (filename,
11875 & debug_displays[gnu_debugaltlink].section,
11876 parse_gnu_debugaltlink,
11877 check_gnu_debugaltlink,
11878 & build_id_data,
11879 file);
11880 if (handle)
11881 {
11882 assert (handle == first_separate_info->handle);
11883 check_for_and_load_links (first_separate_info->handle,
11884 first_separate_info->filename);
11885 }
11886 }
11887
11888 if (load_debug_section (gnu_debuglink, file))
11889 {
11890 unsigned long crc32;
11891
11892 handle = load_separate_debug_info (filename,
11893 & debug_displays[gnu_debuglink].section,
11894 parse_gnu_debuglink,
11895 check_gnu_debuglink,
11896 & crc32,
11897 file);
11898 if (handle)
11899 {
11900 assert (handle == first_separate_info->handle);
11901 check_for_and_load_links (first_separate_info->handle,
11902 first_separate_info->filename);
11903 }
11904 }
11905
11906 load_debug_sup_file (filename, file);
11907
11908 load_build_id_debug_file (filename, file);
11909 }
11910
11911 /* Load the separate debug info file(s) attached to FILE, if any exist.
11912 Returns TRUE if any were found, FALSE otherwise.
11913 If TRUE is returned then the linked list starting at first_separate_info
11914 will be populated with open file handles. */
11915
11916 bool
11917 load_separate_debug_files (void * file, const char * filename)
11918 {
11919 /* Skip this operation if we are not interested in debug links. */
11920 if (! do_follow_links && ! do_debug_links)
11921 return false;
11922
11923 /* See if there are any dwo links. */
11924 if (load_debug_section (str, file)
11925 && load_debug_section (abbrev, file)
11926 && load_debug_section (info, file))
11927 {
11928 /* Load the .debug_addr section, if it exists. */
11929 load_debug_section (debug_addr, file);
11930 /* Load the .debug_str_offsets section, if it exists. */
11931 load_debug_section (str_index, file);
11932 /* Load the .debug_loclists section, if it exists. */
11933 load_debug_section (loclists, file);
11934 /* Load the .debug_rnglists section, if it exists. */
11935 load_debug_section (rnglists, file);
11936
11937 free_dwo_info ();
11938
11939 if (process_debug_info (& debug_displays[info].section, file, abbrev,
11940 true, false))
11941 {
11942 bool introduced = false;
11943 dwo_info *dwinfo;
11944 const char *dir = NULL;
11945 const char *id = NULL;
11946 const char *name = NULL;
11947
11948 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
11949 {
11950 /* Accumulate NAME, DIR and ID fields. */
11951 switch (dwinfo->type)
11952 {
11953 case DWO_NAME:
11954 if (name != NULL)
11955 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
11956 name = dwinfo->value;
11957 break;
11958
11959 case DWO_DIR:
11960 /* There can be multiple DW_AT_comp_dir entries in a CU,
11961 so do not complain. */
11962 dir = dwinfo->value;
11963 break;
11964
11965 case DWO_ID:
11966 if (id != NULL)
11967 warn (_("multiple DWO_IDs encountered for the same CU\n"));
11968 id = dwinfo->value;
11969 break;
11970
11971 default:
11972 error (_("Unexpected DWO INFO type"));
11973 break;
11974 }
11975
11976 /* If we have reached the end of our list, or we are changing
11977 CUs, then display the information that we have accumulated
11978 so far. */
11979 if (name != NULL
11980 && (dwinfo->next == NULL
11981 || dwinfo->next->cu_offset != dwinfo->cu_offset))
11982 {
11983 if (do_debug_links)
11984 {
11985 if (! introduced)
11986 {
11987 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
11988 debug_displays [info].section.uncompressed_name);
11989 introduced = true;
11990 }
11991
11992 printf (_(" Name: %s\n"), name);
11993 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
11994 if (id != NULL)
11995 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
11996 else if (debug_information[0].dwarf_version != 5)
11997 printf (_(" ID: <not specified>\n"));
11998 printf ("\n\n");
11999 }
12000
12001 if (do_follow_links)
12002 load_dwo_file (filename, name, dir, id);
12003
12004 name = dir = id = NULL;
12005 }
12006 }
12007 }
12008 }
12009
12010 if (! do_follow_links)
12011 /* The other debug links will be displayed by display_debug_links()
12012 so we do not need to do any further processing here. */
12013 return false;
12014
12015 /* FIXME: We do not check for the presence of both link sections in the same file. */
12016 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
12017 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
12018
12019 check_for_and_load_links (file, filename);
12020 if (first_separate_info != NULL)
12021 return true;
12022
12023 do_follow_links = 0;
12024 return false;
12025 }
12026
12027 void
12028 free_debug_memory (void)
12029 {
12030 unsigned int i;
12031
12032 free_all_abbrevs ();
12033
12034 free (shndx_pool);
12035 shndx_pool = NULL;
12036 shndx_pool_size = 0;
12037 shndx_pool_used = 0;
12038 free (cu_sets);
12039 cu_sets = NULL;
12040 cu_count = 0;
12041 free (tu_sets);
12042 tu_sets = NULL;
12043 tu_count = 0;
12044
12045 memset (level_type_signed, 0, sizeof level_type_signed);
12046 cu_tu_indexes_read = -1;
12047
12048 for (i = 0; i < max; i++)
12049 free_debug_section ((enum dwarf_section_display_enum) i);
12050
12051 if (debug_information != NULL)
12052 {
12053 for (i = 0; i < alloc_num_debug_info_entries; i++)
12054 free_debug_information (&debug_information[i]);
12055 free (debug_information);
12056 debug_information = NULL;
12057 alloc_num_debug_info_entries = num_debug_info_entries = 0;
12058 }
12059
12060 separate_info * d;
12061 separate_info * next;
12062
12063 for (d = first_separate_info; d != NULL; d = next)
12064 {
12065 close_debug_file (d->handle);
12066 free ((void *) d->filename);
12067 next = d->next;
12068 free ((void *) d);
12069 }
12070 first_separate_info = NULL;
12071
12072 free_dwo_info ();
12073 }
12074
12075 typedef struct
12076 {
12077 const char letter;
12078 const char *option;
12079 int *variable;
12080 int val;
12081 } debug_dump_long_opts;
12082
12083 static const debug_dump_long_opts debug_option_table[] =
12084 {
12085 { 'A', "addr", &do_debug_addr, 1 },
12086 { 'a', "abbrev", &do_debug_abbrevs, 1 },
12087 { 'c', "cu_index", &do_debug_cu_index, 1 },
12088 #ifdef HAVE_LIBDEBUGINFOD
12089 { 'D', "use-debuginfod", &use_debuginfod, 1 },
12090 { 'E', "do-not-use-debuginfod", &use_debuginfod, 0 },
12091 #endif
12092 { 'F', "frames-interp", &do_debug_frames_interp, 1 },
12093 { 'f', "frames", &do_debug_frames, 1 },
12094 { 'g', "gdb_index", &do_gdb_index, 1 },
12095 { 'i', "info", &do_debug_info, 1 },
12096 { 'K', "follow-links", &do_follow_links, 1 },
12097 { 'k', "links", &do_debug_links, 1 },
12098 { 'L', "decodedline", &do_debug_lines, FLAG_DEBUG_LINES_DECODED },
12099 { 'l', "rawline", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12100 /* For compatibility with earlier versions of readelf. */
12101 { 'l', "line", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12102 { 'm', "macro", &do_debug_macinfo, 1 },
12103 { 'N', "no-follow-links", &do_follow_links, 0 },
12104 { 'O', "str-offsets", &do_debug_str_offsets, 1 },
12105 { 'o', "loc", &do_debug_loc, 1 },
12106 { 'p', "pubnames", &do_debug_pubnames, 1 },
12107 { 'R', "Ranges", &do_debug_ranges, 1 },
12108 { 'r', "aranges", &do_debug_aranges, 1 },
12109 /* For compatibility with earlier versions of readelf. */
12110 { 'r', "ranges", &do_debug_aranges, 1 },
12111 { 's', "str", &do_debug_str, 1 },
12112 { 'T', "trace_aranges", &do_trace_aranges, 1 },
12113 { 't', "pubtypes", &do_debug_pubtypes, 1 },
12114 { 'U', "trace_info", &do_trace_info, 1 },
12115 { 'u', "trace_abbrev", &do_trace_abbrevs, 1 },
12116 { 0, NULL, NULL, 0 }
12117 };
12118
12119 /* Enable display of specific DWARF sections as determined by the comma
12120 separated strings in NAMES. Returns non-zero if any displaying was
12121 enabled. */
12122
12123 int
12124 dwarf_select_sections_by_names (const char *names)
12125 {
12126 const char *p;
12127 int result = 0;
12128
12129 p = names;
12130 while (*p)
12131 {
12132 const debug_dump_long_opts *entry;
12133
12134 for (entry = debug_option_table; entry->option; entry++)
12135 {
12136 size_t len = strlen (entry->option);
12137
12138 if (strncmp (p, entry->option, len) == 0
12139 && (p[len] == ',' || p[len] == '\0'))
12140 {
12141 if (entry->val == 0)
12142 * entry->variable = 0;
12143 else
12144 * entry->variable = entry->val;
12145 result |= entry->val;
12146
12147 p += len;
12148 break;
12149 }
12150 }
12151
12152 if (entry->option == NULL)
12153 {
12154 warn (_("Unrecognized debug option '%s'\n"), p);
12155 p = strchr (p, ',');
12156 if (p == NULL)
12157 break;
12158 }
12159
12160 if (*p == ',')
12161 p++;
12162 }
12163
12164 /* The --debug-dump=frames-interp option also enables the
12165 --debug-dump=frames option. */
12166 if (do_debug_frames_interp)
12167 do_debug_frames = 1;
12168
12169 return result;
12170 }
12171
12172 /* Enable display of specific DWARF sections as determined by the characters
12173 in LETTERS. Returns non-zero if any displaying was enabled. */
12174
12175 int
12176 dwarf_select_sections_by_letters (const char *letters)
12177 {
12178 int result = 0;
12179
12180 while (* letters)
12181 {
12182 const debug_dump_long_opts *entry;
12183
12184 for (entry = debug_option_table; entry->letter; entry++)
12185 {
12186 if (entry->letter == * letters)
12187 {
12188 if (entry->val == 0)
12189 * entry->variable = 0;
12190 else
12191 * entry->variable |= entry->val;
12192 result |= entry->val;
12193 break;
12194 }
12195 }
12196
12197 if (entry->letter == 0)
12198 warn (_("Unrecognized debug letter option '%c'\n"), * letters);
12199
12200 letters ++;
12201 }
12202
12203 /* The --debug-dump=frames-interp option also enables the
12204 --debug-dump=frames option. */
12205 if (do_debug_frames_interp)
12206 do_debug_frames = 1;
12207
12208 return result;
12209 }
12210
12211 void
12212 dwarf_select_sections_all (void)
12213 {
12214 do_debug_info = 1;
12215 do_debug_abbrevs = 1;
12216 do_debug_lines = FLAG_DEBUG_LINES_RAW;
12217 do_debug_pubnames = 1;
12218 do_debug_pubtypes = 1;
12219 do_debug_aranges = 1;
12220 do_debug_ranges = 1;
12221 do_debug_frames = 1;
12222 do_debug_macinfo = 1;
12223 do_debug_str = 1;
12224 do_debug_loc = 1;
12225 do_gdb_index = 1;
12226 do_trace_info = 1;
12227 do_trace_abbrevs = 1;
12228 do_trace_aranges = 1;
12229 do_debug_addr = 1;
12230 do_debug_cu_index = 1;
12231 do_follow_links = 1;
12232 do_debug_links = 1;
12233 do_debug_str_offsets = 1;
12234 }
12235
12236 #define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
12237 #define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
12238
12239 /* N.B. The order here must match the order in section_display_enum. */
12240
12241 struct dwarf_section_display debug_displays[] =
12242 {
12243 { { ".debug_abbrev", ".zdebug_abbrev", ".dwabrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12244 { { ".debug_aranges", ".zdebug_aranges", ".dwarnge", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
12245 { { ".debug_frame", ".zdebug_frame", ".dwframe", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12246 { { ".debug_info", ".zdebug_info", ".dwinfo", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
12247 { { ".debug_line", ".zdebug_line", ".dwline", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12248 { { ".debug_pubnames", ".zdebug_pubnames", ".dwpbnms", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
12249 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
12250 { { ".eh_frame", "", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12251 { { ".debug_macinfo", ".zdebug_macinfo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12252 { { ".debug_macro", ".zdebug_macro", ".dwmac", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12253 { { ".debug_str", ".zdebug_str", ".dwstr", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12254 { { ".debug_line_str", ".zdebug_line_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12255 { { ".debug_loc", ".zdebug_loc", ".dwloc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12256 { { ".debug_loclists", ".zdebug_loclists", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12257 { { ".debug_loclists.dwo", ".zdebug_loclists.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12258 { { ".debug_pubtypes", ".zdebug_pubtypes", ".dwpbtyp", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
12259 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
12260 { { ".debug_ranges", ".zdebug_ranges", ".dwrnges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12261 { { ".debug_rnglists", ".zdebug_rnglists", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12262 { { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12263 { { ".debug_static_func", ".zdebug_static_func", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12264 { { ".debug_static_vars", ".zdebug_static_vars", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12265 { { ".debug_types", ".zdebug_types", "", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
12266 { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12267 { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
12268 { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
12269 { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
12270 { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
12271 { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
12272 { { ".debug_info.dwo", ".zdebug_info.dwo", "", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
12273 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", "", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12274 { { ".debug_types.dwo", ".zdebug_types.dwo", "", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
12275 { { ".debug_line.dwo", ".zdebug_line.dwo", "", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12276 { { ".debug_loc.dwo", ".zdebug_loc.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12277 { { ".debug_macro.dwo", ".zdebug_macro.dwo", "", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12278 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12279 { { ".debug_str.dwo", ".zdebug_str.dwo", "", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
12280 { { ".debug_str_offsets", ".zdebug_str_offsets", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12281 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12282 { { ".debug_addr", ".zdebug_addr", "", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
12283 { { ".debug_cu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12284 { { ".debug_tu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12285 { { ".gnu_debuglink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12286 { { ".gnu_debugaltlink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12287 { { ".debug_sup", "", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
12288 /* Separate debug info files can containt their own .debug_str section,
12289 and this might be in *addition* to a .debug_str section already present
12290 in the main file. Hence we need to have two entries for .debug_str. */
12291 { { ".debug_str", ".zdebug_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12292 { { ".note.gnu.build-id", "", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12293 };
12294
12295 /* A static assertion. */
12296 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];