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