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