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