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