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