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