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