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