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