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