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