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