420ae5d6810c98028740c45b08600c2fc5c27345
[deliverable/binutils-gdb.git] / binutils / dwarf.c
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #include "bfd_stdint.h"
25 #include "bucomm.h"
26 #include "elfcomm.h"
27 #include "elf/common.h"
28 #include "dwarf2.h"
29 #include "dwarf.h"
30 #include "gdb/gdb-index.h"
31
32 static const char *regname (unsigned int regno, int row);
33
34 static int have_frame_base;
35 static int need_base_address;
36
37 static unsigned int last_pointer_size = 0;
38 static int warned_about_missing_comp_units = FALSE;
39
40 static unsigned int num_debug_info_entries = 0;
41 static unsigned int alloc_num_debug_info_entries = 0;
42 static debug_info *debug_information = NULL;
43 /* Special value for num_debug_info_entries to indicate
44 that the .debug_info section could not be loaded/parsed. */
45 #define DEBUG_INFO_UNAVAILABLE (unsigned int) -1
46
47 unsigned int eh_addr_size;
48
49 int do_debug_info;
50 int do_debug_abbrevs;
51 int do_debug_lines;
52 int do_debug_pubnames;
53 int do_debug_pubtypes;
54 int do_debug_aranges;
55 int do_debug_ranges;
56 int do_debug_frames;
57 int do_debug_frames_interp;
58 int do_debug_macinfo;
59 int do_debug_str;
60 int do_debug_loc;
61 int do_gdb_index;
62 int do_trace_info;
63 int do_trace_abbrevs;
64 int do_trace_aranges;
65 int do_debug_addr;
66 int do_debug_cu_index;
67 int do_wide;
68
69 int dwarf_cutoff_level = -1;
70 unsigned long dwarf_start_die;
71
72 int dwarf_check = 0;
73
74 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
75 sections. For version 1 package files, each set is stored in SHNDX_POOL
76 as a zero-terminated list of section indexes comprising one set of debug
77 sections from a .dwo file. */
78
79 static unsigned int *shndx_pool = NULL;
80 static unsigned int shndx_pool_size = 0;
81 static unsigned int shndx_pool_used = 0;
82
83 /* For version 2 package files, each set contains an array of section offsets
84 and an array of section sizes, giving the offset and size of the
85 contribution from a CU or TU within one of the debug sections.
86 When displaying debug info from a package file, we need to use these
87 tables to locate the corresponding contributions to each section. */
88
89 struct cu_tu_set
90 {
91 uint64_t signature;
92 dwarf_vma section_offsets[DW_SECT_MAX];
93 size_t section_sizes[DW_SECT_MAX];
94 };
95
96 static int cu_count = 0;
97 static int tu_count = 0;
98 static struct cu_tu_set *cu_sets = NULL;
99 static struct cu_tu_set *tu_sets = NULL;
100
101 static bfd_boolean load_cu_tu_indexes (void *);
102
103 /* Values for do_debug_lines. */
104 #define FLAG_DEBUG_LINES_RAW 1
105 #define FLAG_DEBUG_LINES_DECODED 2
106
107 static unsigned int
108 size_of_encoded_value (int encoding)
109 {
110 switch (encoding & 0x7)
111 {
112 default: /* ??? */
113 case 0: return eh_addr_size;
114 case 2: return 2;
115 case 3: return 4;
116 case 4: return 8;
117 }
118 }
119
120 static dwarf_vma
121 get_encoded_value (unsigned char **pdata,
122 int encoding,
123 struct dwarf_section *section,
124 unsigned char * end)
125 {
126 unsigned char * data = * pdata;
127 unsigned int size = size_of_encoded_value (encoding);
128 dwarf_vma val;
129
130 if (data + size >= end)
131 {
132 warn (_("Encoded value extends past end of section\n"));
133 * pdata = end;
134 return 0;
135 }
136
137 /* PR 17512: file: 002-829853-0.004. */
138 if (size > 8)
139 {
140 warn (_("Encoded size of %d is too large to read\n"), size);
141 * pdata = end;
142 return 0;
143 }
144
145 /* PR 17512: file: 1085-5603-0.004. */
146 if (size == 0)
147 {
148 warn (_("Encoded size of 0 is too small to read\n"));
149 * pdata = end;
150 return 0;
151 }
152
153 if (encoding & DW_EH_PE_signed)
154 val = byte_get_signed (data, size);
155 else
156 val = byte_get (data, size);
157
158 if ((encoding & 0x70) == DW_EH_PE_pcrel)
159 val += section->address + (data - section->start);
160
161 * pdata = data + size;
162 return val;
163 }
164
165 #if defined HAVE_LONG_LONG && SIZEOF_LONG_LONG > SIZEOF_LONG
166 # ifndef __MINGW32__
167 # define DWARF_VMA_FMT "ll"
168 # define DWARF_VMA_FMT_LONG "%16.16llx"
169 # else
170 # define DWARF_VMA_FMT "I64"
171 # define DWARF_VMA_FMT_LONG "%016I64x"
172 # endif
173 #else
174 # define DWARF_VMA_FMT "l"
175 # define DWARF_VMA_FMT_LONG "%16.16lx"
176 #endif
177
178 /* Convert a dwarf vma value into a string. Returns a pointer to a static
179 buffer containing the converted VALUE. The value is converted according
180 to the printf formating character FMTCH. If NUM_BYTES is non-zero then
181 it specifies the maximum number of bytes to be displayed in the converted
182 value and FMTCH is ignored - hex is always used. */
183
184 static const char *
185 dwarf_vmatoa_1 (const char *fmtch, dwarf_vma value, unsigned num_bytes)
186 {
187 /* As dwarf_vmatoa is used more then once in a printf call
188 for output, we are cycling through an fixed array of pointers
189 for return address. */
190 static int buf_pos = 0;
191 static struct dwarf_vmatoa_buf
192 {
193 char place[64];
194 } buf[16];
195 char *ret;
196
197 ret = buf[buf_pos++].place;
198 buf_pos %= ARRAY_SIZE (buf);
199
200 if (num_bytes)
201 {
202 /* Printf does not have a way of specifying a maximum field width for an
203 integer value, so we print the full value into a buffer and then select
204 the precision we need. */
205 snprintf (ret, sizeof (buf[0].place), DWARF_VMA_FMT_LONG, value);
206 if (num_bytes > 8)
207 num_bytes = 8;
208 return ret + (16 - 2 * num_bytes);
209 }
210 else
211 {
212 char fmt[32];
213
214 sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
215 snprintf (ret, sizeof (buf[0].place), fmt, value);
216 return ret;
217 }
218 }
219
220 static inline const char *
221 dwarf_vmatoa (const char * fmtch, dwarf_vma value)
222 {
223 return dwarf_vmatoa_1 (fmtch, value, 0);
224 }
225
226 /* Print a dwarf_vma value (typically an address, offset or length) in
227 hexadecimal format, followed by a space. The length of the VALUE (and
228 hence the precision displayed) is determined by the NUM_BYTES parameter. */
229
230 static void
231 print_dwarf_vma (dwarf_vma value, unsigned num_bytes)
232 {
233 printf ("%s ", dwarf_vmatoa_1 (NULL, value, num_bytes));
234 }
235
236 /* Format a 64-bit value, given as two 32-bit values, in hex.
237 For reentrancy, this uses a buffer provided by the caller. */
238
239 static const char *
240 dwarf_vmatoa64 (dwarf_vma hvalue, dwarf_vma lvalue, char *buf,
241 unsigned int buf_len)
242 {
243 int len = 0;
244
245 if (hvalue == 0)
246 snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", lvalue);
247 else
248 {
249 len = snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", hvalue);
250 snprintf (buf + len, buf_len - len,
251 "%08" DWARF_VMA_FMT "x", lvalue);
252 }
253
254 return buf;
255 }
256
257 /* Read in a LEB128 encoded value starting at address DATA.
258 If SIGN is true, return a signed LEB128 value.
259 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
260 No bytes will be read at address END or beyond. */
261
262 dwarf_vma
263 read_leb128 (unsigned char *data,
264 unsigned int *length_return,
265 bfd_boolean sign,
266 const unsigned char * const end)
267 {
268 dwarf_vma result = 0;
269 unsigned int num_read = 0;
270 unsigned int shift = 0;
271 unsigned char byte = 0;
272
273 while (data < end)
274 {
275 byte = *data++;
276 num_read++;
277
278 result |= ((dwarf_vma) (byte & 0x7f)) << shift;
279
280 shift += 7;
281 if ((byte & 0x80) == 0)
282 break;
283
284 /* PR 17512: file: 0ca183b8.
285 FIXME: Should we signal this error somehow ? */
286 if (shift >= sizeof (result) * 8)
287 break;
288 }
289
290 if (length_return != NULL)
291 *length_return = num_read;
292
293 if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
294 result |= -((dwarf_vma) 1 << shift);
295
296 return result;
297 }
298
299 /* Create a signed version to avoid painful typecasts. */
300 static inline dwarf_signed_vma
301 read_sleb128 (unsigned char * data,
302 unsigned int * length_return,
303 const unsigned char * const end)
304 {
305 return (dwarf_signed_vma) read_leb128 (data, length_return, TRUE, end);
306 }
307
308 static inline dwarf_vma
309 read_uleb128 (unsigned char * data,
310 unsigned int * length_return,
311 const unsigned char * const end)
312 {
313 return read_leb128 (data, length_return, FALSE, end);
314 }
315
316 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END) \
317 do \
318 { \
319 unsigned int amount = (AMOUNT); \
320 if (sizeof (VAL) < amount) \
321 { \
322 error (_("internal error: attempt to read %d bytes of data in to %d sized variable"),\
323 amount, (int) sizeof (VAL)); \
324 amount = sizeof (VAL); \
325 } \
326 if (((PTR) + amount) >= (END)) \
327 { \
328 if ((PTR) < (END)) \
329 amount = (END) - (PTR); \
330 else \
331 amount = 0; \
332 } \
333 if (amount == 0 || amount > 8) \
334 VAL = 0; \
335 else \
336 VAL = byte_get ((PTR), amount); \
337 } \
338 while (0)
339
340 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
341 do \
342 { \
343 SAFE_BYTE_GET (VAL, PTR, AMOUNT, END); \
344 PTR += AMOUNT; \
345 } \
346 while (0)
347
348 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END) \
349 do \
350 { \
351 unsigned int amount = (AMOUNT); \
352 if (((PTR) + amount) >= (END)) \
353 { \
354 if ((PTR) < (END)) \
355 amount = (END) - (PTR); \
356 else \
357 amount = 0; \
358 } \
359 if (amount) \
360 VAL = byte_get_signed ((PTR), amount); \
361 else \
362 VAL = 0; \
363 } \
364 while (0)
365
366 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
367 do \
368 { \
369 SAFE_SIGNED_BYTE_GET (VAL, PTR, AMOUNT, END); \
370 PTR += AMOUNT; \
371 } \
372 while (0)
373
374 #define SAFE_BYTE_GET64(PTR, HIGH, LOW, END) \
375 do \
376 { \
377 if (((PTR) + 8) <= (END)) \
378 { \
379 byte_get_64 ((PTR), (HIGH), (LOW)); \
380 } \
381 else \
382 { \
383 * (LOW) = * (HIGH) = 0; \
384 } \
385 } \
386 while (0)
387
388 typedef struct State_Machine_Registers
389 {
390 dwarf_vma address;
391 unsigned int file;
392 unsigned int line;
393 unsigned int column;
394 int is_stmt;
395 int basic_block;
396 unsigned char op_index;
397 unsigned char end_sequence;
398 /* This variable hold the number of the last entry seen
399 in the File Table. */
400 unsigned int last_file_entry;
401 } SMR;
402
403 static SMR state_machine_regs;
404
405 static void
406 reset_state_machine (int is_stmt)
407 {
408 state_machine_regs.address = 0;
409 state_machine_regs.op_index = 0;
410 state_machine_regs.file = 1;
411 state_machine_regs.line = 1;
412 state_machine_regs.column = 0;
413 state_machine_regs.is_stmt = is_stmt;
414 state_machine_regs.basic_block = 0;
415 state_machine_regs.end_sequence = 0;
416 state_machine_regs.last_file_entry = 0;
417 }
418
419 /* Handled an extend line op.
420 Returns the number of bytes read. */
421
422 static int
423 process_extended_line_op (unsigned char * data,
424 int is_stmt,
425 unsigned char * end)
426 {
427 unsigned char op_code;
428 unsigned int bytes_read;
429 unsigned int len;
430 unsigned char *name;
431 unsigned char *orig_data = data;
432 dwarf_vma adr;
433
434 len = read_uleb128 (data, & bytes_read, end);
435 data += bytes_read;
436
437 if (len == 0 || data == end || len > (uintptr_t) (end - data))
438 {
439 warn (_("Badly formed extended line op encountered!\n"));
440 return bytes_read;
441 }
442
443 len += bytes_read;
444 op_code = *data++;
445
446 printf (_(" Extended opcode %d: "), op_code);
447
448 switch (op_code)
449 {
450 case DW_LNE_end_sequence:
451 printf (_("End of Sequence\n\n"));
452 reset_state_machine (is_stmt);
453 break;
454
455 case DW_LNE_set_address:
456 /* PR 17512: file: 002-100480-0.004. */
457 if (len - bytes_read - 1 > 8)
458 {
459 warn (_("Length (%d) of DW_LNE_set_address op is too long\n"),
460 len - bytes_read - 1);
461 adr = 0;
462 }
463 else
464 SAFE_BYTE_GET (adr, data, len - bytes_read - 1, end);
465 printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
466 state_machine_regs.address = adr;
467 state_machine_regs.op_index = 0;
468 break;
469
470 case DW_LNE_define_file:
471 printf (_("define new File Table entry\n"));
472 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
473 printf (" %d\t", ++state_machine_regs.last_file_entry);
474
475 name = data;
476 data += strnlen ((char *) data, end - data) + 1;
477 printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
478 data += bytes_read;
479 printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
480 data += bytes_read;
481 printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
482 data += bytes_read;
483 printf ("%s\n\n", name);
484
485 if (((unsigned int) (data - orig_data) != len) || data == end)
486 warn (_("DW_LNE_define_file: Bad opcode length\n"));
487 break;
488
489 case DW_LNE_set_discriminator:
490 printf (_("set Discriminator to %s\n"),
491 dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
492 break;
493
494 /* HP extensions. */
495 case DW_LNE_HP_negate_is_UV_update:
496 printf ("DW_LNE_HP_negate_is_UV_update\n");
497 break;
498 case DW_LNE_HP_push_context:
499 printf ("DW_LNE_HP_push_context\n");
500 break;
501 case DW_LNE_HP_pop_context:
502 printf ("DW_LNE_HP_pop_context\n");
503 break;
504 case DW_LNE_HP_set_file_line_column:
505 printf ("DW_LNE_HP_set_file_line_column\n");
506 break;
507 case DW_LNE_HP_set_routine_name:
508 printf ("DW_LNE_HP_set_routine_name\n");
509 break;
510 case DW_LNE_HP_set_sequence:
511 printf ("DW_LNE_HP_set_sequence\n");
512 break;
513 case DW_LNE_HP_negate_post_semantics:
514 printf ("DW_LNE_HP_negate_post_semantics\n");
515 break;
516 case DW_LNE_HP_negate_function_exit:
517 printf ("DW_LNE_HP_negate_function_exit\n");
518 break;
519 case DW_LNE_HP_negate_front_end_logical:
520 printf ("DW_LNE_HP_negate_front_end_logical\n");
521 break;
522 case DW_LNE_HP_define_proc:
523 printf ("DW_LNE_HP_define_proc\n");
524 break;
525 case DW_LNE_HP_source_file_correlation:
526 {
527 unsigned char *edata = data + len - bytes_read - 1;
528
529 printf ("DW_LNE_HP_source_file_correlation\n");
530
531 while (data < edata)
532 {
533 unsigned int opc;
534
535 opc = read_uleb128 (data, & bytes_read, edata);
536 data += bytes_read;
537
538 switch (opc)
539 {
540 case DW_LNE_HP_SFC_formfeed:
541 printf (" DW_LNE_HP_SFC_formfeed\n");
542 break;
543 case DW_LNE_HP_SFC_set_listing_line:
544 printf (" DW_LNE_HP_SFC_set_listing_line (%s)\n",
545 dwarf_vmatoa ("u",
546 read_uleb128 (data, & bytes_read, edata)));
547 data += bytes_read;
548 break;
549 case DW_LNE_HP_SFC_associate:
550 printf (" DW_LNE_HP_SFC_associate ");
551 printf ("(%s",
552 dwarf_vmatoa ("u",
553 read_uleb128 (data, & bytes_read, edata)));
554 data += bytes_read;
555 printf (",%s",
556 dwarf_vmatoa ("u",
557 read_uleb128 (data, & bytes_read, edata)));
558 data += bytes_read;
559 printf (",%s)\n",
560 dwarf_vmatoa ("u",
561 read_uleb128 (data, & bytes_read, edata)));
562 data += bytes_read;
563 break;
564 default:
565 printf (_(" UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
566 data = edata;
567 break;
568 }
569 }
570 }
571 break;
572
573 default:
574 {
575 unsigned int rlen = len - bytes_read - 1;
576
577 if (op_code >= DW_LNE_lo_user
578 /* The test against DW_LNW_hi_user is redundant due to
579 the limited range of the unsigned char data type used
580 for op_code. */
581 /*&& op_code <= DW_LNE_hi_user*/)
582 printf (_("user defined: "));
583 else
584 printf (_("UNKNOWN: "));
585 printf (_("length %d ["), rlen);
586 for (; rlen; rlen--)
587 printf (" %02x", *data++);
588 printf ("]\n");
589 }
590 break;
591 }
592
593 return len;
594 }
595
596 static const unsigned char *
597 fetch_indirect_string (dwarf_vma offset)
598 {
599 struct dwarf_section *section = &debug_displays [str].section;
600
601 if (section->start == NULL)
602 return (const unsigned char *) _("<no .debug_str section>");
603
604 if (offset > section->size)
605 {
606 warn (_("DW_FORM_strp offset too big: %s\n"),
607 dwarf_vmatoa ("x", offset));
608 return (const unsigned char *) _("<offset is too big>");
609 }
610
611 return (const unsigned char *) section->start + offset;
612 }
613
614 static const unsigned char *
615 fetch_indirect_line_string (dwarf_vma offset)
616 {
617 struct dwarf_section *section = &debug_displays [line_str].section;
618
619 if (section->start == NULL)
620 return (const unsigned char *) _("<no .debug_line_str section>");
621
622 if (offset > section->size)
623 {
624 warn (_("DW_FORM_line_strp offset too big: %s\n"),
625 dwarf_vmatoa ("x", offset));
626 return (const unsigned char *) _("<offset is too big>");
627 }
628
629 return (const unsigned char *) section->start + offset;
630 }
631
632 static const char *
633 fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
634 dwarf_vma offset_size, int dwo)
635 {
636 enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
637 enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
638 struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
639 struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
640 dwarf_vma index_offset = idx * offset_size;
641 dwarf_vma str_offset;
642
643 if (index_section->start == NULL)
644 return (dwo ? _("<no .debug_str_offsets.dwo section>")
645 : _("<no .debug_str_offsets section>"));
646
647 if (this_set != NULL)
648 index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
649 if (index_offset > index_section->size)
650 {
651 warn (_("DW_FORM_GNU_str_index offset too big: %s\n"),
652 dwarf_vmatoa ("x", index_offset));
653 return _("<index offset is too big>");
654 }
655
656 if (str_section->start == NULL)
657 return (dwo ? _("<no .debug_str.dwo section>")
658 : _("<no .debug_str section>"));
659
660 str_offset = byte_get (index_section->start + index_offset, offset_size);
661 str_offset -= str_section->address;
662 if (str_offset > str_section->size)
663 {
664 warn (_("DW_FORM_GNU_str_index indirect offset too big: %s\n"),
665 dwarf_vmatoa ("x", str_offset));
666 return _("<indirect index offset is too big>");
667 }
668
669 return (const char *) str_section->start + str_offset;
670 }
671
672 static const char *
673 fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
674 {
675 struct dwarf_section *section = &debug_displays [debug_addr].section;
676
677 if (section->start == NULL)
678 return (_("<no .debug_addr section>"));
679
680 if (offset + bytes > section->size)
681 {
682 warn (_("Offset into section %s too big: %s\n"),
683 section->name, dwarf_vmatoa ("x", offset));
684 return "<offset too big>";
685 }
686
687 return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
688 }
689
690
691 /* FIXME: There are better and more efficient ways to handle
692 these structures. For now though, I just want something that
693 is simple to implement. */
694 typedef struct abbrev_attr
695 {
696 unsigned long attribute;
697 unsigned long form;
698 bfd_signed_vma implicit_const;
699 struct abbrev_attr *next;
700 }
701 abbrev_attr;
702
703 typedef struct abbrev_entry
704 {
705 unsigned long entry;
706 unsigned long tag;
707 int children;
708 struct abbrev_attr *first_attr;
709 struct abbrev_attr *last_attr;
710 struct abbrev_entry *next;
711 }
712 abbrev_entry;
713
714 static abbrev_entry *first_abbrev = NULL;
715 static abbrev_entry *last_abbrev = NULL;
716
717 static void
718 free_abbrevs (void)
719 {
720 abbrev_entry *abbrv;
721
722 for (abbrv = first_abbrev; abbrv;)
723 {
724 abbrev_entry *next_abbrev = abbrv->next;
725 abbrev_attr *attr;
726
727 for (attr = abbrv->first_attr; attr;)
728 {
729 abbrev_attr *next_attr = attr->next;
730
731 free (attr);
732 attr = next_attr;
733 }
734
735 free (abbrv);
736 abbrv = next_abbrev;
737 }
738
739 last_abbrev = first_abbrev = NULL;
740 }
741
742 static void
743 add_abbrev (unsigned long number, unsigned long tag, int children)
744 {
745 abbrev_entry *entry;
746
747 entry = (abbrev_entry *) malloc (sizeof (*entry));
748 if (entry == NULL)
749 /* ugg */
750 return;
751
752 entry->entry = number;
753 entry->tag = tag;
754 entry->children = children;
755 entry->first_attr = NULL;
756 entry->last_attr = NULL;
757 entry->next = NULL;
758
759 if (first_abbrev == NULL)
760 first_abbrev = entry;
761 else
762 last_abbrev->next = entry;
763
764 last_abbrev = entry;
765 }
766
767 static void
768 add_abbrev_attr (unsigned long attribute, unsigned long form,
769 bfd_signed_vma implicit_const)
770 {
771 abbrev_attr *attr;
772
773 attr = (abbrev_attr *) malloc (sizeof (*attr));
774 if (attr == NULL)
775 /* ugg */
776 return;
777
778 attr->attribute = attribute;
779 attr->form = form;
780 attr->implicit_const = implicit_const;
781 attr->next = NULL;
782
783 if (last_abbrev->first_attr == NULL)
784 last_abbrev->first_attr = attr;
785 else
786 last_abbrev->last_attr->next = attr;
787
788 last_abbrev->last_attr = attr;
789 }
790
791 /* Processes the (partial) contents of a .debug_abbrev section.
792 Returns NULL if the end of the section was encountered.
793 Returns the address after the last byte read if the end of
794 an abbreviation set was found. */
795
796 static unsigned char *
797 process_abbrev_section (unsigned char *start, unsigned char *end)
798 {
799 if (first_abbrev != NULL)
800 return NULL;
801
802 while (start < end)
803 {
804 unsigned int bytes_read;
805 unsigned long entry;
806 unsigned long tag;
807 unsigned long attribute;
808 int children;
809
810 entry = read_uleb128 (start, & bytes_read, end);
811 start += bytes_read;
812
813 /* A single zero is supposed to end the section according
814 to the standard. If there's more, then signal that to
815 the caller. */
816 if (start == end)
817 return NULL;
818 if (entry == 0)
819 return start;
820
821 tag = read_uleb128 (start, & bytes_read, end);
822 start += bytes_read;
823 if (start == end)
824 return NULL;
825
826 children = *start++;
827
828 add_abbrev (entry, tag, children);
829
830 do
831 {
832 unsigned long form;
833 /* Initialize it due to a false compiler warning. */
834 bfd_signed_vma implicit_const = -1;
835
836 attribute = read_uleb128 (start, & bytes_read, end);
837 start += bytes_read;
838 if (start == end)
839 break;
840
841 form = read_uleb128 (start, & bytes_read, end);
842 start += bytes_read;
843 if (start == end)
844 break;
845
846 if (form == DW_FORM_implicit_const)
847 {
848 implicit_const = read_sleb128 (start, & bytes_read, end);
849 start += bytes_read;
850 if (start == end)
851 break;
852 }
853
854 add_abbrev_attr (attribute, form, implicit_const);
855 }
856 while (attribute != 0);
857 }
858
859 /* Report the missing single zero which ends the section. */
860 error (_(".debug_abbrev section not zero terminated\n"));
861
862 return NULL;
863 }
864
865 static const char *
866 get_TAG_name (unsigned long tag)
867 {
868 const char *name = get_DW_TAG_name ((unsigned int)tag);
869
870 if (name == NULL)
871 {
872 static char buffer[100];
873
874 snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
875 return buffer;
876 }
877
878 return name;
879 }
880
881 static const char *
882 get_FORM_name (unsigned long form)
883 {
884 const char *name;
885
886 if (form == 0)
887 return "DW_FORM value: 0";
888
889 name = get_DW_FORM_name (form);
890 if (name == NULL)
891 {
892 static char buffer[100];
893
894 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
895 return buffer;
896 }
897
898 return name;
899 }
900
901 static unsigned char *
902 display_block (unsigned char *data,
903 dwarf_vma length,
904 const unsigned char * const end, char delimiter)
905 {
906 dwarf_vma maxlen;
907
908 printf (_("%c%s byte block: "), delimiter, dwarf_vmatoa ("u", length));
909 if (data > end)
910 return (unsigned char *) end;
911
912 maxlen = (dwarf_vma) (end - data);
913 length = length > maxlen ? maxlen : length;
914
915 while (length --)
916 printf ("%lx ", (unsigned long) byte_get (data++, 1));
917
918 return data;
919 }
920
921 static int
922 decode_location_expression (unsigned char * data,
923 unsigned int pointer_size,
924 unsigned int offset_size,
925 int dwarf_version,
926 dwarf_vma length,
927 dwarf_vma cu_offset,
928 struct dwarf_section * section)
929 {
930 unsigned op;
931 unsigned int bytes_read;
932 dwarf_vma uvalue;
933 dwarf_signed_vma svalue;
934 unsigned char *end = data + length;
935 int need_frame_base = 0;
936
937 while (data < end)
938 {
939 op = *data++;
940
941 switch (op)
942 {
943 case DW_OP_addr:
944 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
945 printf ("DW_OP_addr: %s", dwarf_vmatoa ("x", uvalue));
946 break;
947 case DW_OP_deref:
948 printf ("DW_OP_deref");
949 break;
950 case DW_OP_const1u:
951 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
952 printf ("DW_OP_const1u: %lu", (unsigned long) uvalue);
953 break;
954 case DW_OP_const1s:
955 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
956 printf ("DW_OP_const1s: %ld", (long) svalue);
957 break;
958 case DW_OP_const2u:
959 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
960 printf ("DW_OP_const2u: %lu", (unsigned long) uvalue);
961 break;
962 case DW_OP_const2s:
963 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
964 printf ("DW_OP_const2s: %ld", (long) svalue);
965 break;
966 case DW_OP_const4u:
967 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
968 printf ("DW_OP_const4u: %lu", (unsigned long) uvalue);
969 break;
970 case DW_OP_const4s:
971 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
972 printf ("DW_OP_const4s: %ld", (long) svalue);
973 break;
974 case DW_OP_const8u:
975 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
976 printf ("DW_OP_const8u: %lu ", (unsigned long) uvalue);
977 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
978 printf ("%lu", (unsigned long) uvalue);
979 break;
980 case DW_OP_const8s:
981 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
982 printf ("DW_OP_const8s: %ld ", (long) svalue);
983 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
984 printf ("%ld", (long) svalue);
985 break;
986 case DW_OP_constu:
987 printf ("DW_OP_constu: %s",
988 dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
989 data += bytes_read;
990 break;
991 case DW_OP_consts:
992 printf ("DW_OP_consts: %s",
993 dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
994 data += bytes_read;
995 break;
996 case DW_OP_dup:
997 printf ("DW_OP_dup");
998 break;
999 case DW_OP_drop:
1000 printf ("DW_OP_drop");
1001 break;
1002 case DW_OP_over:
1003 printf ("DW_OP_over");
1004 break;
1005 case DW_OP_pick:
1006 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1007 printf ("DW_OP_pick: %ld", (unsigned long) uvalue);
1008 break;
1009 case DW_OP_swap:
1010 printf ("DW_OP_swap");
1011 break;
1012 case DW_OP_rot:
1013 printf ("DW_OP_rot");
1014 break;
1015 case DW_OP_xderef:
1016 printf ("DW_OP_xderef");
1017 break;
1018 case DW_OP_abs:
1019 printf ("DW_OP_abs");
1020 break;
1021 case DW_OP_and:
1022 printf ("DW_OP_and");
1023 break;
1024 case DW_OP_div:
1025 printf ("DW_OP_div");
1026 break;
1027 case DW_OP_minus:
1028 printf ("DW_OP_minus");
1029 break;
1030 case DW_OP_mod:
1031 printf ("DW_OP_mod");
1032 break;
1033 case DW_OP_mul:
1034 printf ("DW_OP_mul");
1035 break;
1036 case DW_OP_neg:
1037 printf ("DW_OP_neg");
1038 break;
1039 case DW_OP_not:
1040 printf ("DW_OP_not");
1041 break;
1042 case DW_OP_or:
1043 printf ("DW_OP_or");
1044 break;
1045 case DW_OP_plus:
1046 printf ("DW_OP_plus");
1047 break;
1048 case DW_OP_plus_uconst:
1049 printf ("DW_OP_plus_uconst: %s",
1050 dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1051 data += bytes_read;
1052 break;
1053 case DW_OP_shl:
1054 printf ("DW_OP_shl");
1055 break;
1056 case DW_OP_shr:
1057 printf ("DW_OP_shr");
1058 break;
1059 case DW_OP_shra:
1060 printf ("DW_OP_shra");
1061 break;
1062 case DW_OP_xor:
1063 printf ("DW_OP_xor");
1064 break;
1065 case DW_OP_bra:
1066 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1067 printf ("DW_OP_bra: %ld", (long) svalue);
1068 break;
1069 case DW_OP_eq:
1070 printf ("DW_OP_eq");
1071 break;
1072 case DW_OP_ge:
1073 printf ("DW_OP_ge");
1074 break;
1075 case DW_OP_gt:
1076 printf ("DW_OP_gt");
1077 break;
1078 case DW_OP_le:
1079 printf ("DW_OP_le");
1080 break;
1081 case DW_OP_lt:
1082 printf ("DW_OP_lt");
1083 break;
1084 case DW_OP_ne:
1085 printf ("DW_OP_ne");
1086 break;
1087 case DW_OP_skip:
1088 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1089 printf ("DW_OP_skip: %ld", (long) svalue);
1090 break;
1091
1092 case DW_OP_lit0:
1093 case DW_OP_lit1:
1094 case DW_OP_lit2:
1095 case DW_OP_lit3:
1096 case DW_OP_lit4:
1097 case DW_OP_lit5:
1098 case DW_OP_lit6:
1099 case DW_OP_lit7:
1100 case DW_OP_lit8:
1101 case DW_OP_lit9:
1102 case DW_OP_lit10:
1103 case DW_OP_lit11:
1104 case DW_OP_lit12:
1105 case DW_OP_lit13:
1106 case DW_OP_lit14:
1107 case DW_OP_lit15:
1108 case DW_OP_lit16:
1109 case DW_OP_lit17:
1110 case DW_OP_lit18:
1111 case DW_OP_lit19:
1112 case DW_OP_lit20:
1113 case DW_OP_lit21:
1114 case DW_OP_lit22:
1115 case DW_OP_lit23:
1116 case DW_OP_lit24:
1117 case DW_OP_lit25:
1118 case DW_OP_lit26:
1119 case DW_OP_lit27:
1120 case DW_OP_lit28:
1121 case DW_OP_lit29:
1122 case DW_OP_lit30:
1123 case DW_OP_lit31:
1124 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1125 break;
1126
1127 case DW_OP_reg0:
1128 case DW_OP_reg1:
1129 case DW_OP_reg2:
1130 case DW_OP_reg3:
1131 case DW_OP_reg4:
1132 case DW_OP_reg5:
1133 case DW_OP_reg6:
1134 case DW_OP_reg7:
1135 case DW_OP_reg8:
1136 case DW_OP_reg9:
1137 case DW_OP_reg10:
1138 case DW_OP_reg11:
1139 case DW_OP_reg12:
1140 case DW_OP_reg13:
1141 case DW_OP_reg14:
1142 case DW_OP_reg15:
1143 case DW_OP_reg16:
1144 case DW_OP_reg17:
1145 case DW_OP_reg18:
1146 case DW_OP_reg19:
1147 case DW_OP_reg20:
1148 case DW_OP_reg21:
1149 case DW_OP_reg22:
1150 case DW_OP_reg23:
1151 case DW_OP_reg24:
1152 case DW_OP_reg25:
1153 case DW_OP_reg26:
1154 case DW_OP_reg27:
1155 case DW_OP_reg28:
1156 case DW_OP_reg29:
1157 case DW_OP_reg30:
1158 case DW_OP_reg31:
1159 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1160 regname (op - DW_OP_reg0, 1));
1161 break;
1162
1163 case DW_OP_breg0:
1164 case DW_OP_breg1:
1165 case DW_OP_breg2:
1166 case DW_OP_breg3:
1167 case DW_OP_breg4:
1168 case DW_OP_breg5:
1169 case DW_OP_breg6:
1170 case DW_OP_breg7:
1171 case DW_OP_breg8:
1172 case DW_OP_breg9:
1173 case DW_OP_breg10:
1174 case DW_OP_breg11:
1175 case DW_OP_breg12:
1176 case DW_OP_breg13:
1177 case DW_OP_breg14:
1178 case DW_OP_breg15:
1179 case DW_OP_breg16:
1180 case DW_OP_breg17:
1181 case DW_OP_breg18:
1182 case DW_OP_breg19:
1183 case DW_OP_breg20:
1184 case DW_OP_breg21:
1185 case DW_OP_breg22:
1186 case DW_OP_breg23:
1187 case DW_OP_breg24:
1188 case DW_OP_breg25:
1189 case DW_OP_breg26:
1190 case DW_OP_breg27:
1191 case DW_OP_breg28:
1192 case DW_OP_breg29:
1193 case DW_OP_breg30:
1194 case DW_OP_breg31:
1195 printf ("DW_OP_breg%d (%s): %s",
1196 op - DW_OP_breg0,
1197 regname (op - DW_OP_breg0, 1),
1198 dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1199 data += bytes_read;
1200 break;
1201
1202 case DW_OP_regx:
1203 uvalue = read_uleb128 (data, &bytes_read, end);
1204 data += bytes_read;
1205 printf ("DW_OP_regx: %s (%s)",
1206 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1207 break;
1208 case DW_OP_fbreg:
1209 need_frame_base = 1;
1210 printf ("DW_OP_fbreg: %s",
1211 dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1212 data += bytes_read;
1213 break;
1214 case DW_OP_bregx:
1215 uvalue = read_uleb128 (data, &bytes_read, end);
1216 data += bytes_read;
1217 printf ("DW_OP_bregx: %s (%s) %s",
1218 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
1219 dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1220 data += bytes_read;
1221 break;
1222 case DW_OP_piece:
1223 printf ("DW_OP_piece: %s",
1224 dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1225 data += bytes_read;
1226 break;
1227 case DW_OP_deref_size:
1228 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1229 printf ("DW_OP_deref_size: %ld", (long) uvalue);
1230 break;
1231 case DW_OP_xderef_size:
1232 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1233 printf ("DW_OP_xderef_size: %ld", (long) uvalue);
1234 break;
1235 case DW_OP_nop:
1236 printf ("DW_OP_nop");
1237 break;
1238
1239 /* DWARF 3 extensions. */
1240 case DW_OP_push_object_address:
1241 printf ("DW_OP_push_object_address");
1242 break;
1243 case DW_OP_call2:
1244 /* XXX: Strictly speaking for 64-bit DWARF3 files
1245 this ought to be an 8-byte wide computation. */
1246 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1247 printf ("DW_OP_call2: <0x%s>",
1248 dwarf_vmatoa ("x", svalue + cu_offset));
1249 break;
1250 case DW_OP_call4:
1251 /* XXX: Strictly speaking for 64-bit DWARF3 files
1252 this ought to be an 8-byte wide computation. */
1253 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1254 printf ("DW_OP_call4: <0x%s>",
1255 dwarf_vmatoa ("x", svalue + cu_offset));
1256 break;
1257 case DW_OP_call_ref:
1258 /* XXX: Strictly speaking for 64-bit DWARF3 files
1259 this ought to be an 8-byte wide computation. */
1260 if (dwarf_version == -1)
1261 {
1262 printf (_("(DW_OP_call_ref in frame info)"));
1263 /* No way to tell where the next op is, so just bail. */
1264 return need_frame_base;
1265 }
1266 if (dwarf_version == 2)
1267 {
1268 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1269 }
1270 else
1271 {
1272 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1273 }
1274 printf ("DW_OP_call_ref: <0x%s>", dwarf_vmatoa ("x", uvalue));
1275 break;
1276 case DW_OP_form_tls_address:
1277 printf ("DW_OP_form_tls_address");
1278 break;
1279 case DW_OP_call_frame_cfa:
1280 printf ("DW_OP_call_frame_cfa");
1281 break;
1282 case DW_OP_bit_piece:
1283 printf ("DW_OP_bit_piece: ");
1284 printf (_("size: %s "),
1285 dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1286 data += bytes_read;
1287 printf (_("offset: %s "),
1288 dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1289 data += bytes_read;
1290 break;
1291
1292 /* DWARF 4 extensions. */
1293 case DW_OP_stack_value:
1294 printf ("DW_OP_stack_value");
1295 break;
1296
1297 case DW_OP_implicit_value:
1298 printf ("DW_OP_implicit_value");
1299 uvalue = read_uleb128 (data, &bytes_read, end);
1300 data += bytes_read;
1301 data = display_block (data, uvalue, end, ' ');
1302 break;
1303
1304 /* GNU extensions. */
1305 case DW_OP_GNU_push_tls_address:
1306 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1307 break;
1308 case DW_OP_GNU_uninit:
1309 printf ("DW_OP_GNU_uninit");
1310 /* FIXME: Is there data associated with this OP ? */
1311 break;
1312 case DW_OP_GNU_encoded_addr:
1313 {
1314 int encoding = 0;
1315 dwarf_vma addr;
1316
1317 if (data < end)
1318 encoding = *data++;
1319 addr = get_encoded_value (&data, encoding, section, end);
1320
1321 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1322 print_dwarf_vma (addr, pointer_size);
1323 }
1324 break;
1325 case DW_OP_GNU_implicit_pointer:
1326 /* XXX: Strictly speaking for 64-bit DWARF3 files
1327 this ought to be an 8-byte wide computation. */
1328 if (dwarf_version == -1)
1329 {
1330 printf (_("(DW_OP_GNU_implicit_pointer in frame info)"));
1331 /* No way to tell where the next op is, so just bail. */
1332 return need_frame_base;
1333 }
1334 if (dwarf_version == 2)
1335 {
1336 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1337 }
1338 else
1339 {
1340 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1341 }
1342 printf ("DW_OP_GNU_implicit_pointer: <0x%s> %s",
1343 dwarf_vmatoa ("x", uvalue),
1344 dwarf_vmatoa ("d", read_sleb128 (data,
1345 &bytes_read, end)));
1346 data += bytes_read;
1347 break;
1348 case DW_OP_entry_value:
1349 case DW_OP_GNU_entry_value:
1350 uvalue = read_uleb128 (data, &bytes_read, end);
1351 data += bytes_read;
1352 /* PR 17531: file: 0cc9cd00. */
1353 if (uvalue > (dwarf_vma) (end - data))
1354 uvalue = end - data;
1355 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1356 : "DW_OP_GNU_entry_value"));
1357 if (decode_location_expression (data, pointer_size, offset_size,
1358 dwarf_version, uvalue,
1359 cu_offset, section))
1360 need_frame_base = 1;
1361 putchar (')');
1362 data += uvalue;
1363 if (data > end)
1364 data = end;
1365 break;
1366 case DW_OP_GNU_const_type:
1367 uvalue = read_uleb128 (data, &bytes_read, end);
1368 data += bytes_read;
1369 printf ("DW_OP_GNU_const_type: <0x%s> ",
1370 dwarf_vmatoa ("x", cu_offset + uvalue));
1371 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1372 data = display_block (data, uvalue, end, ' ');
1373 break;
1374 case DW_OP_GNU_regval_type:
1375 uvalue = read_uleb128 (data, &bytes_read, end);
1376 data += bytes_read;
1377 printf ("DW_OP_GNU_regval_type: %s (%s)",
1378 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1379 uvalue = read_uleb128 (data, &bytes_read, end);
1380 data += bytes_read;
1381 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1382 break;
1383 case DW_OP_GNU_deref_type:
1384 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1385 printf ("DW_OP_GNU_deref_type: %ld", (long) uvalue);
1386 uvalue = read_uleb128 (data, &bytes_read, end);
1387 data += bytes_read;
1388 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1389 break;
1390 case DW_OP_GNU_convert:
1391 uvalue = read_uleb128 (data, &bytes_read, end);
1392 data += bytes_read;
1393 printf ("DW_OP_GNU_convert <0x%s>",
1394 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1395 break;
1396 case DW_OP_GNU_reinterpret:
1397 uvalue = read_uleb128 (data, &bytes_read, end);
1398 data += bytes_read;
1399 printf ("DW_OP_GNU_reinterpret <0x%s>",
1400 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1401 break;
1402 case DW_OP_GNU_parameter_ref:
1403 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1404 printf ("DW_OP_GNU_parameter_ref: <0x%s>",
1405 dwarf_vmatoa ("x", cu_offset + uvalue));
1406 break;
1407 case DW_OP_GNU_addr_index:
1408 uvalue = read_uleb128 (data, &bytes_read, end);
1409 data += bytes_read;
1410 printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1411 break;
1412 case DW_OP_GNU_const_index:
1413 uvalue = read_uleb128 (data, &bytes_read, end);
1414 data += bytes_read;
1415 printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1416 break;
1417
1418 /* HP extensions. */
1419 case DW_OP_HP_is_value:
1420 printf ("DW_OP_HP_is_value");
1421 /* FIXME: Is there data associated with this OP ? */
1422 break;
1423 case DW_OP_HP_fltconst4:
1424 printf ("DW_OP_HP_fltconst4");
1425 /* FIXME: Is there data associated with this OP ? */
1426 break;
1427 case DW_OP_HP_fltconst8:
1428 printf ("DW_OP_HP_fltconst8");
1429 /* FIXME: Is there data associated with this OP ? */
1430 break;
1431 case DW_OP_HP_mod_range:
1432 printf ("DW_OP_HP_mod_range");
1433 /* FIXME: Is there data associated with this OP ? */
1434 break;
1435 case DW_OP_HP_unmod_range:
1436 printf ("DW_OP_HP_unmod_range");
1437 /* FIXME: Is there data associated with this OP ? */
1438 break;
1439 case DW_OP_HP_tls:
1440 printf ("DW_OP_HP_tls");
1441 /* FIXME: Is there data associated with this OP ? */
1442 break;
1443
1444 /* PGI (STMicroelectronics) extensions. */
1445 case DW_OP_PGI_omp_thread_num:
1446 /* Pushes the thread number for the current thread as it would be
1447 returned by the standard OpenMP library function:
1448 omp_get_thread_num(). The "current thread" is the thread for
1449 which the expression is being evaluated. */
1450 printf ("DW_OP_PGI_omp_thread_num");
1451 break;
1452
1453 default:
1454 if (op >= DW_OP_lo_user
1455 && op <= DW_OP_hi_user)
1456 printf (_("(User defined location op 0x%x)"), op);
1457 else
1458 printf (_("(Unknown location op 0x%x)"), op);
1459 /* No way to tell where the next op is, so just bail. */
1460 return need_frame_base;
1461 }
1462
1463 /* Separate the ops. */
1464 if (data < end)
1465 printf ("; ");
1466 }
1467
1468 return need_frame_base;
1469 }
1470
1471 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1472 This is used for DWARF package files. */
1473
1474 static struct cu_tu_set *
1475 find_cu_tu_set_v2 (dwarf_vma cu_offset, int do_types)
1476 {
1477 struct cu_tu_set *p;
1478 unsigned int nsets;
1479 unsigned int dw_sect;
1480
1481 if (do_types)
1482 {
1483 p = tu_sets;
1484 nsets = tu_count;
1485 dw_sect = DW_SECT_TYPES;
1486 }
1487 else
1488 {
1489 p = cu_sets;
1490 nsets = cu_count;
1491 dw_sect = DW_SECT_INFO;
1492 }
1493 while (nsets > 0)
1494 {
1495 if (p->section_offsets [dw_sect] == cu_offset)
1496 return p;
1497 p++;
1498 nsets--;
1499 }
1500 return NULL;
1501 }
1502
1503 /* Add INC to HIGH_BITS:LOW_BITS. */
1504 static void
1505 add64 (dwarf_vma * high_bits, dwarf_vma * low_bits, dwarf_vma inc)
1506 {
1507 dwarf_vma tmp = * low_bits;
1508
1509 tmp += inc;
1510
1511 /* FIXME: There is probably a better way of handling this:
1512
1513 We need to cope with dwarf_vma being a 32-bit or 64-bit
1514 type. Plus regardless of its size LOW_BITS is meant to
1515 only hold 32-bits, so if there is overflow or wrap around
1516 we must propagate into HIGH_BITS. */
1517 if (tmp < * low_bits)
1518 {
1519 ++ * high_bits;
1520 }
1521 else if (sizeof (tmp) > 8
1522 && (tmp >> 31) > 1)
1523 {
1524 ++ * high_bits;
1525 tmp &= 0xFFFFFFFF;
1526 }
1527
1528 * low_bits = tmp;
1529 }
1530
1531 static unsigned char *
1532 read_and_display_attr_value (unsigned long attribute,
1533 unsigned long form,
1534 dwarf_signed_vma implicit_const,
1535 unsigned char * data,
1536 unsigned char * end,
1537 dwarf_vma cu_offset,
1538 dwarf_vma pointer_size,
1539 dwarf_vma offset_size,
1540 int dwarf_version,
1541 debug_info * debug_info_p,
1542 int do_loc,
1543 struct dwarf_section * section,
1544 struct cu_tu_set * this_set, char delimiter)
1545 {
1546 dwarf_vma uvalue = 0;
1547 unsigned char *block_start = NULL;
1548 unsigned char * orig_data = data;
1549 unsigned int bytes_read;
1550
1551 if (data > end || (data == end && form != DW_FORM_flag_present))
1552 {
1553 warn (_("Corrupt attribute\n"));
1554 return data;
1555 }
1556
1557 switch (form)
1558 {
1559 default:
1560 break;
1561
1562 case DW_FORM_ref_addr:
1563 if (dwarf_version == 2)
1564 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1565 else if (dwarf_version == 3 || dwarf_version == 4)
1566 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1567 else
1568 error (_("Internal error: DWARF version is not 2, 3 or 4.\n"));
1569
1570 break;
1571
1572 case DW_FORM_addr:
1573 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1574 break;
1575
1576 case DW_FORM_strp:
1577 case DW_FORM_line_strp:
1578 case DW_FORM_sec_offset:
1579 case DW_FORM_GNU_ref_alt:
1580 case DW_FORM_GNU_strp_alt:
1581 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1582 break;
1583
1584 case DW_FORM_flag_present:
1585 uvalue = 1;
1586 break;
1587
1588 case DW_FORM_ref1:
1589 case DW_FORM_flag:
1590 case DW_FORM_data1:
1591 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1592 break;
1593
1594 case DW_FORM_ref2:
1595 case DW_FORM_data2:
1596 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1597 break;
1598
1599 case DW_FORM_ref4:
1600 case DW_FORM_data4:
1601 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1602 break;
1603
1604 case DW_FORM_sdata:
1605 uvalue = read_sleb128 (data, & bytes_read, end);
1606 data += bytes_read;
1607 break;
1608
1609 case DW_FORM_GNU_str_index:
1610 uvalue = read_uleb128 (data, & bytes_read, end);
1611 data += bytes_read;
1612 break;
1613
1614 case DW_FORM_ref_udata:
1615 case DW_FORM_udata:
1616 uvalue = read_uleb128 (data, & bytes_read, end);
1617 data += bytes_read;
1618 break;
1619
1620 case DW_FORM_indirect:
1621 form = read_uleb128 (data, & bytes_read, end);
1622 data += bytes_read;
1623 if (!do_loc)
1624 printf ("%c%s", delimiter, get_FORM_name (form));
1625 if (form == DW_FORM_implicit_const)
1626 {
1627 implicit_const = read_sleb128 (data, & bytes_read, end);
1628 data += bytes_read;
1629 }
1630 return read_and_display_attr_value (attribute, form, implicit_const, data,
1631 end, cu_offset, pointer_size,
1632 offset_size, dwarf_version,
1633 debug_info_p, do_loc,
1634 section, this_set, delimiter);
1635 case DW_FORM_GNU_addr_index:
1636 uvalue = read_uleb128 (data, & bytes_read, end);
1637 data += bytes_read;
1638 break;
1639 }
1640
1641 switch (form)
1642 {
1643 case DW_FORM_ref_addr:
1644 if (!do_loc)
1645 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x",uvalue));
1646 break;
1647
1648 case DW_FORM_GNU_ref_alt:
1649 if (!do_loc)
1650 printf ("%c<alt 0x%s>", delimiter, dwarf_vmatoa ("x",uvalue));
1651 break;
1652
1653 case DW_FORM_ref1:
1654 case DW_FORM_ref2:
1655 case DW_FORM_ref4:
1656 case DW_FORM_ref_udata:
1657 if (!do_loc)
1658 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
1659 break;
1660
1661 case DW_FORM_data4:
1662 case DW_FORM_addr:
1663 case DW_FORM_sec_offset:
1664 if (!do_loc)
1665 printf ("%c0x%s", delimiter, dwarf_vmatoa ("x", uvalue));
1666 break;
1667
1668 case DW_FORM_flag_present:
1669 case DW_FORM_flag:
1670 case DW_FORM_data1:
1671 case DW_FORM_data2:
1672 case DW_FORM_sdata:
1673 case DW_FORM_udata:
1674 if (!do_loc)
1675 printf ("%c%s", delimiter, dwarf_vmatoa ("d", uvalue));
1676 break;
1677
1678 case DW_FORM_implicit_const:
1679 if (!do_loc)
1680 printf ("%c%s", delimiter, dwarf_vmatoa ("d", implicit_const));
1681 break;
1682
1683 case DW_FORM_ref8:
1684 case DW_FORM_data8:
1685 if (!do_loc)
1686 {
1687 dwarf_vma high_bits;
1688 dwarf_vma utmp;
1689 char buf[64];
1690
1691 SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1692 utmp = uvalue;
1693 if (form == DW_FORM_ref8)
1694 add64 (& high_bits, & utmp, cu_offset);
1695 printf ("%c0x%s", delimiter,
1696 dwarf_vmatoa64 (high_bits, utmp, buf, sizeof (buf)));
1697 }
1698
1699 if ((do_loc || do_debug_loc || do_debug_ranges)
1700 && num_debug_info_entries == 0)
1701 {
1702 if (sizeof (uvalue) == 8)
1703 SAFE_BYTE_GET (uvalue, data, 8, end);
1704 else
1705 error (_("DW_FORM_data8 is unsupported when sizeof (dwarf_vma) != 8\n"));
1706 }
1707
1708 data += 8;
1709 break;
1710
1711 case DW_FORM_string:
1712 if (!do_loc)
1713 printf ("%c%.*s", delimiter, (int) (end - data), data);
1714 data += strnlen ((char *) data, end - data) + 1;
1715 break;
1716
1717 case DW_FORM_block:
1718 case DW_FORM_exprloc:
1719 uvalue = read_uleb128 (data, & bytes_read, end);
1720 block_start = data + bytes_read;
1721 if (block_start >= end)
1722 {
1723 warn (_("Block ends prematurely\n"));
1724 uvalue = 0;
1725 block_start = end;
1726 }
1727 /* FIXME: Testing "(block_start + uvalue) < block_start" miscompiles with
1728 gcc 4.8.3 running on an x86_64 host in 32-bit mode. So we pre-compute
1729 block_start + uvalue here. */
1730 data = block_start + uvalue;
1731 /* PR 17512: file: 008-103549-0.001:0.1. */
1732 if (block_start + uvalue > end || data < block_start)
1733 {
1734 warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1735 uvalue = end - block_start;
1736 }
1737 if (do_loc)
1738 data = block_start + uvalue;
1739 else
1740 data = display_block (block_start, uvalue, end, delimiter);
1741 break;
1742
1743 case DW_FORM_block1:
1744 SAFE_BYTE_GET (uvalue, data, 1, end);
1745 block_start = data + 1;
1746 if (block_start >= end)
1747 {
1748 warn (_("Block ends prematurely\n"));
1749 uvalue = 0;
1750 block_start = end;
1751 }
1752 data = block_start + uvalue;
1753 if (block_start + uvalue > end || data < block_start)
1754 {
1755 warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1756 uvalue = end - block_start;
1757 }
1758 if (do_loc)
1759 data = block_start + uvalue;
1760 else
1761 data = display_block (block_start, uvalue, end, delimiter);
1762 break;
1763
1764 case DW_FORM_block2:
1765 SAFE_BYTE_GET (uvalue, data, 2, end);
1766 block_start = data + 2;
1767 if (block_start >= end)
1768 {
1769 warn (_("Block ends prematurely\n"));
1770 uvalue = 0;
1771 block_start = end;
1772 }
1773 data = block_start + uvalue;
1774 if (block_start + uvalue > end || data < block_start)
1775 {
1776 warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1777 uvalue = end - block_start;
1778 }
1779 if (do_loc)
1780 data = block_start + uvalue;
1781 else
1782 data = display_block (block_start, uvalue, end, delimiter);
1783 break;
1784
1785 case DW_FORM_block4:
1786 SAFE_BYTE_GET (uvalue, data, 4, end);
1787 block_start = data + 4;
1788 /* PR 17512: file: 3371-3907-0.004. */
1789 if (block_start >= end)
1790 {
1791 warn (_("Block ends prematurely\n"));
1792 uvalue = 0;
1793 block_start = end;
1794 }
1795 data = block_start + uvalue;
1796 if (block_start + uvalue > end
1797 /* PR 17531: file: 5b5f0592. */
1798 || data < block_start)
1799 {
1800 warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1801 uvalue = end - block_start;
1802 }
1803 if (do_loc)
1804 data = block_start + uvalue;
1805 else
1806 data = display_block (block_start, uvalue, end, delimiter);
1807 break;
1808
1809 case DW_FORM_strp:
1810 if (!do_loc)
1811 printf (_("%c(indirect string, offset: 0x%s): %s"), delimiter,
1812 dwarf_vmatoa ("x", uvalue),
1813 fetch_indirect_string (uvalue));
1814 break;
1815
1816 case DW_FORM_line_strp:
1817 if (!do_loc)
1818 printf (_("%c(indirect line string, offset: 0x%s): %s"), delimiter,
1819 dwarf_vmatoa ("x", uvalue),
1820 fetch_indirect_line_string (uvalue));
1821 break;
1822
1823 case DW_FORM_GNU_str_index:
1824 if (!do_loc)
1825 {
1826 const char *suffix = strrchr (section->name, '.');
1827 int dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? 1 : 0;
1828
1829 printf (_("%c(indexed string: 0x%s): %s"), delimiter,
1830 dwarf_vmatoa ("x", uvalue),
1831 fetch_indexed_string (uvalue, this_set, offset_size, dwo));
1832 }
1833 break;
1834
1835 case DW_FORM_GNU_strp_alt:
1836 if (!do_loc)
1837 printf (_("%c(alt indirect string, offset: 0x%s)"), delimiter,
1838 dwarf_vmatoa ("x", uvalue));
1839 break;
1840
1841 case DW_FORM_indirect:
1842 /* Handled above. */
1843 break;
1844
1845 case DW_FORM_ref_sig8:
1846 if (!do_loc)
1847 {
1848 dwarf_vma high_bits;
1849 char buf[64];
1850
1851 SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1852 printf ("%csignature: 0x%s", delimiter,
1853 dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
1854 }
1855 data += 8;
1856 break;
1857
1858 case DW_FORM_GNU_addr_index:
1859 if (!do_loc)
1860 printf (_("%c(addr_index: 0x%s): %s"), delimiter,
1861 dwarf_vmatoa ("x", uvalue),
1862 fetch_indexed_value (uvalue * pointer_size, pointer_size));
1863 break;
1864
1865 default:
1866 warn (_("Unrecognized form: %lu\n"), form);
1867 break;
1868 }
1869
1870 if ((do_loc || do_debug_loc || do_debug_ranges)
1871 && num_debug_info_entries == 0
1872 && debug_info_p != NULL)
1873 {
1874 switch (attribute)
1875 {
1876 case DW_AT_frame_base:
1877 have_frame_base = 1;
1878 /* Fall through. */
1879 case DW_AT_location:
1880 case DW_AT_string_length:
1881 case DW_AT_return_addr:
1882 case DW_AT_data_member_location:
1883 case DW_AT_vtable_elem_location:
1884 case DW_AT_segment:
1885 case DW_AT_static_link:
1886 case DW_AT_use_location:
1887 case DW_AT_GNU_call_site_value:
1888 case DW_AT_GNU_call_site_data_value:
1889 case DW_AT_GNU_call_site_target:
1890 case DW_AT_GNU_call_site_target_clobbered:
1891 if ((dwarf_version < 4
1892 && (form == DW_FORM_data4 || form == DW_FORM_data8))
1893 || form == DW_FORM_sec_offset)
1894 {
1895 /* Process location list. */
1896 unsigned int lmax = debug_info_p->max_loc_offsets;
1897 unsigned int num = debug_info_p->num_loc_offsets;
1898
1899 if (lmax == 0 || num >= lmax)
1900 {
1901 lmax += 1024;
1902 debug_info_p->loc_offsets = (dwarf_vma *)
1903 xcrealloc (debug_info_p->loc_offsets,
1904 lmax, sizeof (*debug_info_p->loc_offsets));
1905 debug_info_p->have_frame_base = (int *)
1906 xcrealloc (debug_info_p->have_frame_base,
1907 lmax, sizeof (*debug_info_p->have_frame_base));
1908 debug_info_p->max_loc_offsets = lmax;
1909 }
1910 if (this_set != NULL)
1911 uvalue += this_set->section_offsets [DW_SECT_LOC];
1912 debug_info_p->loc_offsets [num] = uvalue;
1913 debug_info_p->have_frame_base [num] = have_frame_base;
1914 debug_info_p->num_loc_offsets++;
1915 }
1916 break;
1917
1918 case DW_AT_low_pc:
1919 if (need_base_address)
1920 debug_info_p->base_address = uvalue;
1921 break;
1922
1923 case DW_AT_GNU_addr_base:
1924 debug_info_p->addr_base = uvalue;
1925 break;
1926
1927 case DW_AT_GNU_ranges_base:
1928 debug_info_p->ranges_base = uvalue;
1929 break;
1930
1931 case DW_AT_ranges:
1932 if ((dwarf_version < 4
1933 && (form == DW_FORM_data4 || form == DW_FORM_data8))
1934 || form == DW_FORM_sec_offset)
1935 {
1936 /* Process range list. */
1937 unsigned int lmax = debug_info_p->max_range_lists;
1938 unsigned int num = debug_info_p->num_range_lists;
1939
1940 if (lmax == 0 || num >= lmax)
1941 {
1942 lmax += 1024;
1943 debug_info_p->range_lists = (dwarf_vma *)
1944 xcrealloc (debug_info_p->range_lists,
1945 lmax, sizeof (*debug_info_p->range_lists));
1946 debug_info_p->max_range_lists = lmax;
1947 }
1948 debug_info_p->range_lists [num] = uvalue;
1949 debug_info_p->num_range_lists++;
1950 }
1951 break;
1952
1953 default:
1954 break;
1955 }
1956 }
1957
1958 if (do_loc || attribute == 0)
1959 return data;
1960
1961 /* For some attributes we can display further information. */
1962 switch (attribute)
1963 {
1964 case DW_AT_inline:
1965 printf ("\t");
1966 switch (uvalue)
1967 {
1968 case DW_INL_not_inlined:
1969 printf (_("(not inlined)"));
1970 break;
1971 case DW_INL_inlined:
1972 printf (_("(inlined)"));
1973 break;
1974 case DW_INL_declared_not_inlined:
1975 printf (_("(declared as inline but ignored)"));
1976 break;
1977 case DW_INL_declared_inlined:
1978 printf (_("(declared as inline and inlined)"));
1979 break;
1980 default:
1981 printf (_(" (Unknown inline attribute value: %s)"),
1982 dwarf_vmatoa ("x", uvalue));
1983 break;
1984 }
1985 break;
1986
1987 case DW_AT_language:
1988 printf ("\t");
1989 switch (uvalue)
1990 {
1991 /* Ordered by the numeric value of these constants. */
1992 case DW_LANG_C89: printf ("(ANSI C)"); break;
1993 case DW_LANG_C: printf ("(non-ANSI C)"); break;
1994 case DW_LANG_Ada83: printf ("(Ada)"); break;
1995 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
1996 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
1997 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
1998 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
1999 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
2000 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
2001 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
2002 /* DWARF 2.1 values. */
2003 case DW_LANG_Java: printf ("(Java)"); break;
2004 case DW_LANG_C99: printf ("(ANSI C99)"); break;
2005 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
2006 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
2007 /* DWARF 3 values. */
2008 case DW_LANG_PLI: printf ("(PLI)"); break;
2009 case DW_LANG_ObjC: printf ("(Objective C)"); break;
2010 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
2011 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
2012 case DW_LANG_D: printf ("(D)"); break;
2013 /* DWARF 4 values. */
2014 case DW_LANG_Python: printf ("(Python)"); break;
2015 /* DWARF 5 values. */
2016 case DW_LANG_Go: printf ("(Go)"); break;
2017 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
2018 case DW_LANG_C11: printf ("(C11)"); break;
2019 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
2020 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
2021 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
2022 /* MIPS extension. */
2023 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
2024 /* UPC extension. */
2025 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
2026 default:
2027 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
2028 printf (_("(implementation defined: %s)"),
2029 dwarf_vmatoa ("x", uvalue));
2030 else
2031 printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
2032 break;
2033 }
2034 break;
2035
2036 case DW_AT_encoding:
2037 printf ("\t");
2038 switch (uvalue)
2039 {
2040 case DW_ATE_void: printf ("(void)"); break;
2041 case DW_ATE_address: printf ("(machine address)"); break;
2042 case DW_ATE_boolean: printf ("(boolean)"); break;
2043 case DW_ATE_complex_float: printf ("(complex float)"); break;
2044 case DW_ATE_float: printf ("(float)"); break;
2045 case DW_ATE_signed: printf ("(signed)"); break;
2046 case DW_ATE_signed_char: printf ("(signed char)"); break;
2047 case DW_ATE_unsigned: printf ("(unsigned)"); break;
2048 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
2049 /* DWARF 2.1 values: */
2050 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
2051 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
2052 /* DWARF 3 values: */
2053 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
2054 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
2055 case DW_ATE_edited: printf ("(edited)"); break;
2056 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
2057 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
2058 /* HP extensions: */
2059 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
2060 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
2061 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
2062 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
2063 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
2064 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
2065 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
2066 /* DWARF 4 values: */
2067 case DW_ATE_UTF: printf ("(unicode string)"); break;
2068
2069 default:
2070 if (uvalue >= DW_ATE_lo_user
2071 && uvalue <= DW_ATE_hi_user)
2072 printf (_("(user defined type)"));
2073 else
2074 printf (_("(unknown type)"));
2075 break;
2076 }
2077 break;
2078
2079 case DW_AT_accessibility:
2080 printf ("\t");
2081 switch (uvalue)
2082 {
2083 case DW_ACCESS_public: printf ("(public)"); break;
2084 case DW_ACCESS_protected: printf ("(protected)"); break;
2085 case DW_ACCESS_private: printf ("(private)"); break;
2086 default:
2087 printf (_("(unknown accessibility)"));
2088 break;
2089 }
2090 break;
2091
2092 case DW_AT_visibility:
2093 printf ("\t");
2094 switch (uvalue)
2095 {
2096 case DW_VIS_local: printf ("(local)"); break;
2097 case DW_VIS_exported: printf ("(exported)"); break;
2098 case DW_VIS_qualified: printf ("(qualified)"); break;
2099 default: printf (_("(unknown visibility)")); break;
2100 }
2101 break;
2102
2103 case DW_AT_virtuality:
2104 printf ("\t");
2105 switch (uvalue)
2106 {
2107 case DW_VIRTUALITY_none: printf ("(none)"); break;
2108 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
2109 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
2110 default: printf (_("(unknown virtuality)")); break;
2111 }
2112 break;
2113
2114 case DW_AT_identifier_case:
2115 printf ("\t");
2116 switch (uvalue)
2117 {
2118 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
2119 case DW_ID_up_case: printf ("(up_case)"); break;
2120 case DW_ID_down_case: printf ("(down_case)"); break;
2121 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
2122 default: printf (_("(unknown case)")); break;
2123 }
2124 break;
2125
2126 case DW_AT_calling_convention:
2127 printf ("\t");
2128 switch (uvalue)
2129 {
2130 case DW_CC_normal: printf ("(normal)"); break;
2131 case DW_CC_program: printf ("(program)"); break;
2132 case DW_CC_nocall: printf ("(nocall)"); break;
2133 default:
2134 if (uvalue >= DW_CC_lo_user
2135 && uvalue <= DW_CC_hi_user)
2136 printf (_("(user defined)"));
2137 else
2138 printf (_("(unknown convention)"));
2139 }
2140 break;
2141
2142 case DW_AT_ordering:
2143 printf ("\t");
2144 switch (uvalue)
2145 {
2146 case -1: printf (_("(undefined)")); break;
2147 case 0: printf ("(row major)"); break;
2148 case 1: printf ("(column major)"); break;
2149 }
2150 break;
2151
2152 case DW_AT_frame_base:
2153 have_frame_base = 1;
2154 /* Fall through. */
2155 case DW_AT_location:
2156 case DW_AT_string_length:
2157 case DW_AT_return_addr:
2158 case DW_AT_data_member_location:
2159 case DW_AT_vtable_elem_location:
2160 case DW_AT_segment:
2161 case DW_AT_static_link:
2162 case DW_AT_use_location:
2163 case DW_AT_GNU_call_site_value:
2164 case DW_AT_GNU_call_site_data_value:
2165 case DW_AT_GNU_call_site_target:
2166 case DW_AT_GNU_call_site_target_clobbered:
2167 if ((dwarf_version < 4
2168 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2169 || form == DW_FORM_sec_offset)
2170 printf (_(" (location list)"));
2171 /* Fall through. */
2172 case DW_AT_allocated:
2173 case DW_AT_associated:
2174 case DW_AT_data_location:
2175 case DW_AT_stride:
2176 case DW_AT_upper_bound:
2177 case DW_AT_lower_bound:
2178 if (block_start)
2179 {
2180 int need_frame_base;
2181
2182 printf ("\t(");
2183 need_frame_base = decode_location_expression (block_start,
2184 pointer_size,
2185 offset_size,
2186 dwarf_version,
2187 uvalue,
2188 cu_offset, section);
2189 printf (")");
2190 if (need_frame_base && !have_frame_base)
2191 printf (_(" [without DW_AT_frame_base]"));
2192 }
2193 break;
2194
2195 case DW_AT_import:
2196 {
2197 if (form == DW_FORM_ref_sig8
2198 || form == DW_FORM_GNU_ref_alt)
2199 break;
2200
2201 if (form == DW_FORM_ref1
2202 || form == DW_FORM_ref2
2203 || form == DW_FORM_ref4
2204 || form == DW_FORM_ref_udata)
2205 uvalue += cu_offset;
2206
2207 if (uvalue >= section->size)
2208 warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset 0x%lx is too big.\n"),
2209 dwarf_vmatoa ("x", uvalue),
2210 (unsigned long) (orig_data - section->start));
2211 else
2212 {
2213 unsigned long abbrev_number;
2214 abbrev_entry * entry;
2215
2216 abbrev_number = read_uleb128 (section->start + uvalue, NULL, end);
2217
2218 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
2219 /* Don't look up abbrev for DW_FORM_ref_addr, as it very often will
2220 use different abbrev table, and we don't track .debug_info chunks
2221 yet. */
2222 if (form != DW_FORM_ref_addr)
2223 {
2224 for (entry = first_abbrev; entry != NULL; entry = entry->next)
2225 if (entry->entry == abbrev_number)
2226 break;
2227 if (entry != NULL)
2228 printf (" (%s)", get_TAG_name (entry->tag));
2229 }
2230 printf ("]");
2231 }
2232 }
2233 break;
2234
2235 default:
2236 break;
2237 }
2238
2239 return data;
2240 }
2241
2242 static const char *
2243 get_AT_name (unsigned long attribute)
2244 {
2245 const char *name;
2246
2247 if (attribute == 0)
2248 return "DW_AT value: 0";
2249
2250 /* One value is shared by the MIPS and HP extensions: */
2251 if (attribute == DW_AT_MIPS_fde)
2252 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
2253
2254 name = get_DW_AT_name (attribute);
2255
2256 if (name == NULL)
2257 {
2258 static char buffer[100];
2259
2260 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
2261 attribute);
2262 return buffer;
2263 }
2264
2265 return name;
2266 }
2267
2268 static unsigned char *
2269 read_and_display_attr (unsigned long attribute,
2270 unsigned long form,
2271 dwarf_signed_vma implicit_const,
2272 unsigned char * data,
2273 unsigned char * end,
2274 dwarf_vma cu_offset,
2275 dwarf_vma pointer_size,
2276 dwarf_vma offset_size,
2277 int dwarf_version,
2278 debug_info * debug_info_p,
2279 int do_loc,
2280 struct dwarf_section * section,
2281 struct cu_tu_set * this_set)
2282 {
2283 if (!do_loc)
2284 printf (" %-18s:", get_AT_name (attribute));
2285 data = read_and_display_attr_value (attribute, form, implicit_const, data, end,
2286 cu_offset, pointer_size, offset_size,
2287 dwarf_version, debug_info_p,
2288 do_loc, section, this_set, ' ');
2289 if (!do_loc)
2290 printf ("\n");
2291 return data;
2292 }
2293
2294 /* Process the contents of a .debug_info section. If do_loc is non-zero
2295 then we are scanning for location lists and we do not want to display
2296 anything to the user. If do_types is non-zero, we are processing
2297 a .debug_types section instead of a .debug_info section. */
2298
2299 static int
2300 process_debug_info (struct dwarf_section *section,
2301 void *file,
2302 enum dwarf_section_display_enum abbrev_sec,
2303 int do_loc,
2304 int do_types)
2305 {
2306 unsigned char *start = section->start;
2307 unsigned char *end = start + section->size;
2308 unsigned char *section_begin;
2309 unsigned int unit;
2310 unsigned int num_units = 0;
2311
2312 if ((do_loc || do_debug_loc || do_debug_ranges)
2313 && num_debug_info_entries == 0
2314 && ! do_types)
2315 {
2316 dwarf_vma length;
2317
2318 /* First scan the section to get the number of comp units. */
2319 for (section_begin = start, num_units = 0; section_begin < end;
2320 num_units ++)
2321 {
2322 /* Read the first 4 bytes. For a 32-bit DWARF section, this
2323 will be the length. For a 64-bit DWARF section, it'll be
2324 the escape code 0xffffffff followed by an 8 byte length. */
2325 SAFE_BYTE_GET (length, section_begin, 4, end);
2326
2327 if (length == 0xffffffff)
2328 {
2329 SAFE_BYTE_GET (length, section_begin + 4, 8, end);
2330 section_begin += length + 12;
2331 }
2332 else if (length >= 0xfffffff0 && length < 0xffffffff)
2333 {
2334 warn (_("Reserved length value (0x%s) found in section %s\n"),
2335 dwarf_vmatoa ("x", length), section->name);
2336 return 0;
2337 }
2338 else
2339 section_begin += length + 4;
2340
2341 /* Negative values are illegal, they may even cause infinite
2342 looping. This can happen if we can't accurately apply
2343 relocations to an object file, or if the file is corrupt. */
2344 if ((signed long) length <= 0 || section_begin < start)
2345 {
2346 warn (_("Corrupt unit length (0x%s) found in section %s\n"),
2347 dwarf_vmatoa ("x", length), section->name);
2348 return 0;
2349 }
2350 }
2351
2352 if (num_units == 0)
2353 {
2354 error (_("No comp units in %s section ?\n"), section->name);
2355 return 0;
2356 }
2357
2358 /* Then allocate an array to hold the information. */
2359 debug_information = (debug_info *) cmalloc (num_units,
2360 sizeof (* debug_information));
2361 if (debug_information == NULL)
2362 {
2363 error (_("Not enough memory for a debug info array of %u entries\n"),
2364 num_units);
2365 alloc_num_debug_info_entries = num_debug_info_entries = 0;
2366 return 0;
2367 }
2368 /* PR 17531: file: 92ca3797.
2369 We cannot rely upon the debug_information array being initialised
2370 before it is used. A corrupt file could easily contain references
2371 to a unit for which information has not been made available. So
2372 we ensure that the array is zeroed here. */
2373 memset (debug_information, 0, num_units * sizeof (*debug_information));
2374
2375 alloc_num_debug_info_entries = num_units;
2376 }
2377
2378 if (!do_loc)
2379 {
2380 if (dwarf_start_die == 0)
2381 printf (_("Contents of the %s section:\n\n"), section->name);
2382
2383 load_debug_section (str, file);
2384 load_debug_section (line_str, file);
2385 load_debug_section (str_dwo, file);
2386 load_debug_section (str_index, file);
2387 load_debug_section (str_index_dwo, file);
2388 load_debug_section (debug_addr, file);
2389 }
2390
2391 load_debug_section (abbrev_sec, file);
2392 if (debug_displays [abbrev_sec].section.start == NULL)
2393 {
2394 warn (_("Unable to locate %s section!\n"),
2395 debug_displays [abbrev_sec].section.name);
2396 return 0;
2397 }
2398
2399 for (section_begin = start, unit = 0; start < end; unit++)
2400 {
2401 DWARF2_Internal_CompUnit compunit;
2402 unsigned char *hdrptr;
2403 unsigned char *tags;
2404 int level, last_level, saved_level;
2405 dwarf_vma cu_offset;
2406 unsigned int offset_size;
2407 int initial_length_size;
2408 dwarf_vma signature_high = 0;
2409 dwarf_vma signature_low = 0;
2410 dwarf_vma type_offset = 0;
2411 struct cu_tu_set *this_set;
2412 dwarf_vma abbrev_base;
2413 size_t abbrev_size;
2414
2415 hdrptr = start;
2416
2417 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
2418
2419 if (compunit.cu_length == 0xffffffff)
2420 {
2421 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
2422 offset_size = 8;
2423 initial_length_size = 12;
2424 }
2425 else
2426 {
2427 offset_size = 4;
2428 initial_length_size = 4;
2429 }
2430
2431 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end);
2432
2433 cu_offset = start - section_begin;
2434
2435 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
2436
2437 if (compunit.cu_version < 5)
2438 {
2439 compunit.cu_unit_type = DW_UT_compile;
2440 /* Initialize it due to a false compiler warning. */
2441 compunit.cu_pointer_size = -1;
2442 }
2443 else
2444 {
2445 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end);
2446 do_types = (compunit.cu_unit_type == DW_UT_type);
2447
2448 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
2449 }
2450
2451 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end);
2452
2453 if (this_set == NULL)
2454 {
2455 abbrev_base = 0;
2456 abbrev_size = debug_displays [abbrev_sec].section.size;
2457 }
2458 else
2459 {
2460 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
2461 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
2462 }
2463
2464 if (compunit.cu_version < 5)
2465 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
2466
2467 /* PR 17512: file: 001-108546-0.001:0.1. */
2468 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
2469 {
2470 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
2471 compunit.cu_pointer_size, offset_size);
2472 compunit.cu_pointer_size = offset_size;
2473 }
2474
2475 if (do_types)
2476 {
2477 SAFE_BYTE_GET64 (hdrptr, &signature_high, &signature_low, end);
2478 hdrptr += 8;
2479 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end);
2480 }
2481
2482 if ((do_loc || do_debug_loc || do_debug_ranges)
2483 && num_debug_info_entries == 0
2484 && ! do_types)
2485 {
2486 debug_information [unit].cu_offset = cu_offset;
2487 debug_information [unit].pointer_size
2488 = compunit.cu_pointer_size;
2489 debug_information [unit].offset_size = offset_size;
2490 debug_information [unit].dwarf_version = compunit.cu_version;
2491 debug_information [unit].base_address = 0;
2492 debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
2493 debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
2494 debug_information [unit].loc_offsets = NULL;
2495 debug_information [unit].have_frame_base = NULL;
2496 debug_information [unit].max_loc_offsets = 0;
2497 debug_information [unit].num_loc_offsets = 0;
2498 debug_information [unit].range_lists = NULL;
2499 debug_information [unit].max_range_lists= 0;
2500 debug_information [unit].num_range_lists = 0;
2501 }
2502
2503 if (!do_loc && dwarf_start_die == 0)
2504 {
2505 printf (_(" Compilation Unit @ offset 0x%s:\n"),
2506 dwarf_vmatoa ("x", cu_offset));
2507 printf (_(" Length: 0x%s (%s)\n"),
2508 dwarf_vmatoa ("x", compunit.cu_length),
2509 offset_size == 8 ? "64-bit" : "32-bit");
2510 printf (_(" Version: %d\n"), compunit.cu_version);
2511 printf (_(" Abbrev Offset: 0x%s\n"),
2512 dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
2513 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
2514 if (do_types)
2515 {
2516 char buf[64];
2517
2518 printf (_(" Signature: 0x%s\n"),
2519 dwarf_vmatoa64 (signature_high, signature_low,
2520 buf, sizeof (buf)));
2521 printf (_(" Type Offset: 0x%s\n"),
2522 dwarf_vmatoa ("x", type_offset));
2523 }
2524 if (this_set != NULL)
2525 {
2526 dwarf_vma *offsets = this_set->section_offsets;
2527 size_t *sizes = this_set->section_sizes;
2528
2529 printf (_(" Section contributions:\n"));
2530 printf (_(" .debug_abbrev.dwo: 0x%s 0x%s\n"),
2531 dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
2532 dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
2533 printf (_(" .debug_line.dwo: 0x%s 0x%s\n"),
2534 dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
2535 dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
2536 printf (_(" .debug_loc.dwo: 0x%s 0x%s\n"),
2537 dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
2538 dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
2539 printf (_(" .debug_str_offsets.dwo: 0x%s 0x%s\n"),
2540 dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
2541 dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
2542 }
2543 }
2544
2545 if (cu_offset + compunit.cu_length + initial_length_size
2546 > section->size)
2547 {
2548 warn (_("Debug info is corrupted, length of CU at %s"
2549 " extends beyond end of section (length = %s)\n"),
2550 dwarf_vmatoa ("x", cu_offset),
2551 dwarf_vmatoa ("x", compunit.cu_length));
2552 num_units = unit;
2553 break;
2554 }
2555 tags = hdrptr;
2556 start += compunit.cu_length + initial_length_size;
2557
2558 if (start > end)
2559 {
2560 warn (_("Debug info is corrupt. CU at %s extends beyond end of section"),
2561 dwarf_vmatoa ("x", cu_offset));
2562 start = end;
2563 }
2564
2565 if (compunit.cu_version < 2 || compunit.cu_version > 5)
2566 {
2567 warn (_("CU at offset %s contains corrupt or "
2568 "unsupported version number: %d.\n"),
2569 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
2570 continue;
2571 }
2572
2573 if (compunit.cu_unit_type != DW_UT_compile
2574 && compunit.cu_unit_type != DW_UT_type)
2575 {
2576 warn (_("CU at offset %s contains corrupt or "
2577 "unsupported unit type: %d.\n"),
2578 dwarf_vmatoa ("x", cu_offset), compunit.cu_unit_type);
2579 continue;
2580 }
2581
2582 free_abbrevs ();
2583
2584 /* Process the abbrevs used by this compilation unit. */
2585 if (compunit.cu_abbrev_offset >= abbrev_size)
2586 warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
2587 (unsigned long) compunit.cu_abbrev_offset,
2588 (unsigned long) abbrev_size);
2589 /* PR 17531: file:4bcd9ce9. */
2590 else if ((abbrev_base + abbrev_size)
2591 > debug_displays [abbrev_sec].section.size)
2592 warn (_("Debug info is corrupted, abbrev size (%lx) is larger than abbrev section size (%lx)\n"),
2593 (unsigned long) abbrev_base + abbrev_size,
2594 (unsigned long) debug_displays [abbrev_sec].section.size);
2595 else
2596 process_abbrev_section
2597 (((unsigned char *) debug_displays [abbrev_sec].section.start
2598 + abbrev_base + compunit.cu_abbrev_offset),
2599 ((unsigned char *) debug_displays [abbrev_sec].section.start
2600 + abbrev_base + abbrev_size));
2601
2602 level = 0;
2603 last_level = level;
2604 saved_level = -1;
2605 while (tags < start)
2606 {
2607 unsigned int bytes_read;
2608 unsigned long abbrev_number;
2609 unsigned long die_offset;
2610 abbrev_entry *entry;
2611 abbrev_attr *attr;
2612 int do_printing = 1;
2613
2614 die_offset = tags - section_begin;
2615
2616 abbrev_number = read_uleb128 (tags, & bytes_read, start);
2617 tags += bytes_read;
2618
2619 /* A null DIE marks the end of a list of siblings or it may also be
2620 a section padding. */
2621 if (abbrev_number == 0)
2622 {
2623 /* Check if it can be a section padding for the last CU. */
2624 if (level == 0 && start == end)
2625 {
2626 unsigned char *chk;
2627
2628 for (chk = tags; chk < start; chk++)
2629 if (*chk != 0)
2630 break;
2631 if (chk == start)
2632 break;
2633 }
2634
2635 if (!do_loc && die_offset >= dwarf_start_die
2636 && (dwarf_cutoff_level == -1
2637 || level < dwarf_cutoff_level))
2638 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
2639 level, die_offset);
2640
2641 --level;
2642 if (level < 0)
2643 {
2644 static unsigned num_bogus_warns = 0;
2645
2646 if (num_bogus_warns < 3)
2647 {
2648 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
2649 die_offset, section->name);
2650 num_bogus_warns ++;
2651 if (num_bogus_warns == 3)
2652 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
2653 }
2654 }
2655 if (dwarf_start_die != 0 && level < saved_level)
2656 return 1;
2657 continue;
2658 }
2659
2660 if (!do_loc)
2661 {
2662 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
2663 do_printing = 0;
2664 else
2665 {
2666 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
2667 saved_level = level;
2668 do_printing = (dwarf_cutoff_level == -1
2669 || level < dwarf_cutoff_level);
2670 if (do_printing)
2671 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
2672 level, die_offset, abbrev_number);
2673 else if (dwarf_cutoff_level == -1
2674 || last_level < dwarf_cutoff_level)
2675 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
2676 last_level = level;
2677 }
2678 }
2679
2680 /* Scan through the abbreviation list until we reach the
2681 correct entry. */
2682 for (entry = first_abbrev;
2683 entry && entry->entry != abbrev_number;
2684 entry = entry->next)
2685 continue;
2686
2687 if (entry == NULL)
2688 {
2689 if (!do_loc && do_printing)
2690 {
2691 printf ("\n");
2692 fflush (stdout);
2693 }
2694 warn (_("DIE at offset 0x%lx refers to abbreviation number %lu which does not exist\n"),
2695 die_offset, abbrev_number);
2696 return 0;
2697 }
2698
2699 if (!do_loc && do_printing)
2700 printf (" (%s)\n", get_TAG_name (entry->tag));
2701
2702 switch (entry->tag)
2703 {
2704 default:
2705 need_base_address = 0;
2706 break;
2707 case DW_TAG_compile_unit:
2708 need_base_address = 1;
2709 break;
2710 case DW_TAG_entry_point:
2711 case DW_TAG_subprogram:
2712 need_base_address = 0;
2713 /* Assuming that there is no DW_AT_frame_base. */
2714 have_frame_base = 0;
2715 break;
2716 }
2717
2718 for (attr = entry->first_attr;
2719 attr && attr->attribute;
2720 attr = attr->next)
2721 {
2722 debug_info *arg;
2723
2724 if (! do_loc && do_printing)
2725 /* Show the offset from where the tag was extracted. */
2726 printf (" <%lx>", (unsigned long)(tags - section_begin));
2727
2728 if (debug_information && unit < alloc_num_debug_info_entries)
2729 arg = debug_information + unit;
2730 else
2731 arg = NULL;
2732
2733 tags = read_and_display_attr (attr->attribute,
2734 attr->form,
2735 attr->implicit_const,
2736 tags,
2737 end,
2738 cu_offset,
2739 compunit.cu_pointer_size,
2740 offset_size,
2741 compunit.cu_version,
2742 arg,
2743 do_loc || ! do_printing,
2744 section,
2745 this_set);
2746 }
2747
2748 if (entry->children)
2749 ++level;
2750 }
2751 }
2752
2753 /* Set num_debug_info_entries here so that it can be used to check if
2754 we need to process .debug_loc and .debug_ranges sections. */
2755 if ((do_loc || do_debug_loc || do_debug_ranges)
2756 && num_debug_info_entries == 0
2757 && ! do_types)
2758 {
2759 if (num_units > alloc_num_debug_info_entries)
2760 num_debug_info_entries = alloc_num_debug_info_entries;
2761 else
2762 num_debug_info_entries = num_units;
2763 }
2764
2765 if (!do_loc)
2766 printf ("\n");
2767
2768 return 1;
2769 }
2770
2771 /* Locate and scan the .debug_info section in the file and record the pointer
2772 sizes and offsets for the compilation units in it. Usually an executable
2773 will have just one pointer size, but this is not guaranteed, and so we try
2774 not to make any assumptions. Returns zero upon failure, or the number of
2775 compilation units upon success. */
2776
2777 static unsigned int
2778 load_debug_info (void * file)
2779 {
2780 /* Reset the last pointer size so that we can issue correct error
2781 messages if we are displaying the contents of more than one section. */
2782 last_pointer_size = 0;
2783 warned_about_missing_comp_units = FALSE;
2784
2785 /* If we have already tried and failed to load the .debug_info
2786 section then do not bother to repeat the task. */
2787 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2788 return 0;
2789
2790 /* If we already have the information there is nothing else to do. */
2791 if (num_debug_info_entries > 0)
2792 return num_debug_info_entries;
2793
2794 /* If this is a DWARF package file, load the CU and TU indexes. */
2795 (void) load_cu_tu_indexes (file);
2796
2797 if (load_debug_section (info, file)
2798 && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
2799 return num_debug_info_entries;
2800
2801 if (load_debug_section (info_dwo, file)
2802 && process_debug_info (&debug_displays [info_dwo].section, file,
2803 abbrev_dwo, 1, 0))
2804 return num_debug_info_entries;
2805
2806 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2807 return 0;
2808 }
2809
2810 /* Read a DWARF .debug_line section header starting at DATA.
2811 Upon success returns an updated DATA pointer and the LINFO
2812 structure and the END_OF_SEQUENCE pointer will be filled in.
2813 Otherwise returns NULL. */
2814
2815 static unsigned char *
2816 read_debug_line_header (struct dwarf_section * section,
2817 unsigned char * data,
2818 unsigned char * end,
2819 DWARF2_Internal_LineInfo * linfo,
2820 unsigned char ** end_of_sequence)
2821 {
2822 unsigned char *hdrptr;
2823 unsigned int initial_length_size;
2824 unsigned char address_size, segment_selector_size;
2825
2826 /* Extract information from the Line Number Program Header.
2827 (section 6.2.4 in the Dwarf3 doc). */
2828 hdrptr = data;
2829
2830 /* Get and check the length of the block. */
2831 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
2832
2833 if (linfo->li_length == 0xffffffff)
2834 {
2835 /* This section is 64-bit DWARF 3. */
2836 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
2837 linfo->li_offset_size = 8;
2838 initial_length_size = 12;
2839 }
2840 else
2841 {
2842 linfo->li_offset_size = 4;
2843 initial_length_size = 4;
2844 }
2845
2846 if (linfo->li_length + initial_length_size > section->size)
2847 {
2848 /* If the length field has a relocation against it, then we should
2849 not complain if it is inaccurate (and probably negative). This
2850 happens in object files when the .debug_line section is actually
2851 comprised of several different .debug_line.* sections, (some of
2852 which may be removed by linker garbage collection), and a relocation
2853 is used to compute the correct length once that is done. */
2854 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
2855 {
2856 linfo->li_length = (end - data) - initial_length_size;
2857 }
2858 else
2859 {
2860 warn (_("The length field (0x%lx) in the debug_line header is wrong - the section is too small\n"),
2861 (long) linfo->li_length);
2862 return NULL;
2863 }
2864 }
2865
2866 /* Get and check the version number. */
2867 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
2868
2869 if (linfo->li_version != 2
2870 && linfo->li_version != 3
2871 && linfo->li_version != 4
2872 && linfo->li_version != 5)
2873 {
2874 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
2875 "is currently supported.\n"));
2876 return NULL;
2877 }
2878
2879 if (linfo->li_version >= 5)
2880 {
2881 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
2882
2883 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
2884 if (segment_selector_size != 0)
2885 {
2886 warn (_("The %s section contains "
2887 "unsupported segment selector size: %d.\n"),
2888 section->name, segment_selector_size);
2889 return 0;
2890 }
2891 }
2892
2893 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
2894 linfo->li_offset_size, end);
2895 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
2896
2897 if (linfo->li_version >= 4)
2898 {
2899 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
2900
2901 if (linfo->li_max_ops_per_insn == 0)
2902 {
2903 warn (_("Invalid maximum operations per insn.\n"));
2904 return NULL;
2905 }
2906 }
2907 else
2908 linfo->li_max_ops_per_insn = 1;
2909
2910 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
2911 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
2912 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
2913 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
2914
2915 * end_of_sequence = data + linfo->li_length + initial_length_size;
2916 /* PR 17512: file:002-117414-0.004. */
2917 if (* end_of_sequence > end)
2918 {
2919 warn (_("Line length %s extends beyond end of section\n"),
2920 dwarf_vmatoa ("u", linfo->li_length));
2921 * end_of_sequence = end;
2922 return NULL;
2923 }
2924
2925 return hdrptr;
2926 }
2927
2928 static unsigned char *
2929 display_formatted_table (unsigned char *data,
2930 unsigned char *start, unsigned char *end,
2931 const DWARF2_Internal_LineInfo *linfo,
2932 struct dwarf_section *section, const char *what)
2933 {
2934 unsigned char *format_start, format_count, *format, formati;
2935 dwarf_vma data_count, datai;
2936 unsigned int bytes_read, namepass, last_entry = 0;
2937
2938 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
2939 format_start = data;
2940 for (formati = 0; formati < format_count; formati++)
2941 {
2942 read_uleb128 (data, & bytes_read, end);
2943 data += bytes_read;
2944 read_uleb128 (data, & bytes_read, end);
2945 data += bytes_read;
2946 if (data == end)
2947 {
2948 warn (_("Corrupt %s entry format table entry\n"), what);
2949 return data;
2950 }
2951 }
2952
2953 data_count = read_uleb128 (data, & bytes_read, end);
2954 data += bytes_read;
2955 if (data == end)
2956 {
2957 warn (_("Corrupt %s list\n"), what);
2958 return data;
2959 }
2960
2961 if (data_count == 0)
2962 {
2963 printf (_("\n The %s Table is empty.\n"), what);
2964 return data;
2965 }
2966
2967 printf (_("\n The %s Table (offset 0x%lx):\n"), what,
2968 (long)(data - start));
2969
2970 printf (_(" Entry"));
2971 /* Delay displaying name as the last entry for better screen layout. */
2972 for (namepass = 0; namepass < 2; namepass++)
2973 {
2974 format = format_start;
2975 for (formati = 0; formati < format_count; formati++)
2976 {
2977 dwarf_vma content_type;
2978
2979 content_type = read_uleb128 (format, & bytes_read, end);
2980 format += bytes_read;
2981 if ((content_type == DW_LNCT_path) == (namepass == 1))
2982 switch (content_type)
2983 {
2984 case DW_LNCT_path:
2985 printf (_("\tName"));
2986 break;
2987 case DW_LNCT_directory_index:
2988 printf (_("\tDir"));
2989 break;
2990 case DW_LNCT_timestamp:
2991 printf (_("\tTime"));
2992 break;
2993 case DW_LNCT_size:
2994 printf (_("\tSize"));
2995 break;
2996 case DW_LNCT_MD5:
2997 printf (_("\tMD5"));
2998 break;
2999 default:
3000 printf (_("\t(Unknown format content type %s)"),
3001 dwarf_vmatoa ("u", content_type));
3002 }
3003 read_uleb128 (format, & bytes_read, end);
3004 format += bytes_read;
3005 }
3006 }
3007 putchar ('\n');
3008
3009 for (datai = 0; datai < data_count; datai++)
3010 {
3011 unsigned char *datapass = data;
3012
3013 printf (" %d", last_entry++);
3014 /* Delay displaying name as the last entry for better screen layout. */
3015 for (namepass = 0; namepass < 2; namepass++)
3016 {
3017 format = format_start;
3018 data = datapass;
3019 for (formati = 0; formati < format_count; formati++)
3020 {
3021 dwarf_vma content_type, form;
3022
3023 content_type = read_uleb128 (format, & bytes_read, end);
3024 format += bytes_read;
3025 form = read_uleb128 (format, & bytes_read, end);
3026 format += bytes_read;
3027 data = read_and_display_attr_value (0, form, 0, data, end, 0, 0,
3028 linfo->li_offset_size,
3029 linfo->li_version, NULL,
3030 ((content_type == DW_LNCT_path) != (namepass == 1)),
3031 section, NULL, '\t');
3032 }
3033 }
3034 if (data == end)
3035 {
3036 warn (_("Corrupt %s entries list\n"), what);
3037 return data;
3038 }
3039 putchar ('\n');
3040 }
3041 return data;
3042 }
3043
3044 static int
3045 display_debug_lines_raw (struct dwarf_section *section,
3046 unsigned char *data,
3047 unsigned char *end, void *file)
3048 {
3049 unsigned char *start = section->start;
3050
3051 printf (_("Raw dump of debug contents of section %s:\n\n"),
3052 section->name);
3053
3054 while (data < end)
3055 {
3056 static DWARF2_Internal_LineInfo saved_linfo;
3057 DWARF2_Internal_LineInfo linfo;
3058 unsigned char *standard_opcodes;
3059 unsigned char *end_of_sequence;
3060 int i;
3061
3062 if (const_strneq (section->name, ".debug_line.")
3063 /* Note: the following does not apply to .debug_line.dwo sections.
3064 These are full debug_line sections. */
3065 && strcmp (section->name, ".debug_line.dwo") != 0)
3066 {
3067 /* Sections named .debug_line.<foo> are fragments of a .debug_line
3068 section containing just the Line Number Statements. They are
3069 created by the assembler and intended to be used alongside gcc's
3070 -ffunction-sections command line option. When the linker's
3071 garbage collection decides to discard a .text.<foo> section it
3072 can then also discard the line number information in .debug_line.<foo>.
3073
3074 Since the section is a fragment it does not have the details
3075 needed to fill out a LineInfo structure, so instead we use the
3076 details from the last full debug_line section that we processed. */
3077 end_of_sequence = end;
3078 standard_opcodes = NULL;
3079 linfo = saved_linfo;
3080 /* PR 17531: file: 0522b371. */
3081 if (linfo.li_line_range == 0)
3082 {
3083 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
3084 return 0;
3085 }
3086 reset_state_machine (linfo.li_default_is_stmt);
3087 }
3088 else
3089 {
3090 unsigned char * hdrptr;
3091
3092 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
3093 & end_of_sequence)) == NULL)
3094 return 0;
3095
3096 printf (_(" Offset: 0x%lx\n"), (long)(data - start));
3097 printf (_(" Length: %ld\n"), (long) linfo.li_length);
3098 printf (_(" DWARF Version: %d\n"), linfo.li_version);
3099 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
3100 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
3101 if (linfo.li_version >= 4)
3102 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
3103 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
3104 printf (_(" Line Base: %d\n"), linfo.li_line_base);
3105 printf (_(" Line Range: %d\n"), linfo.li_line_range);
3106 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
3107
3108 /* PR 17512: file: 1665-6428-0.004. */
3109 if (linfo.li_line_range == 0)
3110 {
3111 warn (_("Line range of 0 is invalid, using 1 instead\n"));
3112 linfo.li_line_range = 1;
3113 }
3114
3115 reset_state_machine (linfo.li_default_is_stmt);
3116
3117 /* Display the contents of the Opcodes table. */
3118 standard_opcodes = hdrptr;
3119
3120 /* PR 17512: file: 002-417945-0.004. */
3121 if (standard_opcodes + linfo.li_opcode_base >= end)
3122 {
3123 warn (_("Line Base extends beyond end of section\n"));
3124 return 0;
3125 }
3126
3127 printf (_("\n Opcodes:\n"));
3128
3129 for (i = 1; i < linfo.li_opcode_base; i++)
3130 printf (_(" Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
3131
3132 /* Display the contents of the Directory table. */
3133 data = standard_opcodes + linfo.li_opcode_base - 1;
3134
3135 if (linfo.li_version >= 5)
3136 {
3137 load_debug_section (line_str, file);
3138
3139 data = display_formatted_table (data, start, end, &linfo, section,
3140 _("Directory"));
3141 data = display_formatted_table (data, start, end, &linfo, section,
3142 _("File name"));
3143 }
3144 else
3145 {
3146 if (*data == 0)
3147 printf (_("\n The Directory Table is empty.\n"));
3148 else
3149 {
3150 unsigned int last_dir_entry = 0;
3151
3152 printf (_("\n The Directory Table (offset 0x%lx):\n"),
3153 (long)(data - start));
3154
3155 while (data < end && *data != 0)
3156 {
3157 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
3158
3159 data += strnlen ((char *) data, end - data) + 1;
3160 }
3161
3162 /* PR 17512: file: 002-132094-0.004. */
3163 if (data >= end - 1)
3164 break;
3165 }
3166
3167 /* Skip the NUL at the end of the table. */
3168 data++;
3169
3170 /* Display the contents of the File Name table. */
3171 if (*data == 0)
3172 printf (_("\n The File Name Table is empty.\n"));
3173 else
3174 {
3175 printf (_("\n The File Name Table (offset 0x%lx):\n"),
3176 (long)(data - start));
3177 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
3178
3179 while (data < end && *data != 0)
3180 {
3181 unsigned char *name;
3182 unsigned int bytes_read;
3183
3184 printf (" %d\t", ++state_machine_regs.last_file_entry);
3185 name = data;
3186 data += strnlen ((char *) data, end - data) + 1;
3187
3188 printf ("%s\t",
3189 dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
3190 data += bytes_read;
3191 printf ("%s\t",
3192 dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
3193 data += bytes_read;
3194 printf ("%s\t",
3195 dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
3196 data += bytes_read;
3197 printf ("%.*s\n", (int)(end - name), name);
3198
3199 if (data == end)
3200 {
3201 warn (_("Corrupt file name table entry\n"));
3202 break;
3203 }
3204 }
3205 }
3206
3207 /* Skip the NUL at the end of the table. */
3208 data++;
3209 }
3210
3211 putchar ('\n');
3212 saved_linfo = linfo;
3213 }
3214
3215 /* Now display the statements. */
3216 if (data >= end_of_sequence)
3217 printf (_(" No Line Number Statements.\n"));
3218 else
3219 {
3220 printf (_(" Line Number Statements:\n"));
3221
3222 while (data < end_of_sequence)
3223 {
3224 unsigned char op_code;
3225 dwarf_signed_vma adv;
3226 dwarf_vma uladv;
3227 unsigned int bytes_read;
3228
3229 printf (" [0x%08lx]", (long)(data - start));
3230
3231 op_code = *data++;
3232
3233 if (op_code >= linfo.li_opcode_base)
3234 {
3235 op_code -= linfo.li_opcode_base;
3236 uladv = (op_code / linfo.li_line_range);
3237 if (linfo.li_max_ops_per_insn == 1)
3238 {
3239 uladv *= linfo.li_min_insn_length;
3240 state_machine_regs.address += uladv;
3241 printf (_(" Special opcode %d: "
3242 "advance Address by %s to 0x%s"),
3243 op_code, dwarf_vmatoa ("u", uladv),
3244 dwarf_vmatoa ("x", state_machine_regs.address));
3245 }
3246 else
3247 {
3248 state_machine_regs.address
3249 += ((state_machine_regs.op_index + uladv)
3250 / linfo.li_max_ops_per_insn)
3251 * linfo.li_min_insn_length;
3252 state_machine_regs.op_index
3253 = (state_machine_regs.op_index + uladv)
3254 % linfo.li_max_ops_per_insn;
3255 printf (_(" Special opcode %d: "
3256 "advance Address by %s to 0x%s[%d]"),
3257 op_code, dwarf_vmatoa ("u", uladv),
3258 dwarf_vmatoa ("x", state_machine_regs.address),
3259 state_machine_regs.op_index);
3260 }
3261 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
3262 state_machine_regs.line += adv;
3263 printf (_(" and Line by %s to %d\n"),
3264 dwarf_vmatoa ("d", adv), state_machine_regs.line);
3265 }
3266 else switch (op_code)
3267 {
3268 case DW_LNS_extended_op:
3269 data += process_extended_line_op (data, linfo.li_default_is_stmt, end);
3270 break;
3271
3272 case DW_LNS_copy:
3273 printf (_(" Copy\n"));
3274 break;
3275
3276 case DW_LNS_advance_pc:
3277 uladv = read_uleb128 (data, & bytes_read, end);
3278 data += bytes_read;
3279 if (linfo.li_max_ops_per_insn == 1)
3280 {
3281 uladv *= linfo.li_min_insn_length;
3282 state_machine_regs.address += uladv;
3283 printf (_(" Advance PC by %s to 0x%s\n"),
3284 dwarf_vmatoa ("u", uladv),
3285 dwarf_vmatoa ("x", state_machine_regs.address));
3286 }
3287 else
3288 {
3289 state_machine_regs.address
3290 += ((state_machine_regs.op_index + uladv)
3291 / linfo.li_max_ops_per_insn)
3292 * linfo.li_min_insn_length;
3293 state_machine_regs.op_index
3294 = (state_machine_regs.op_index + uladv)
3295 % linfo.li_max_ops_per_insn;
3296 printf (_(" Advance PC by %s to 0x%s[%d]\n"),
3297 dwarf_vmatoa ("u", uladv),
3298 dwarf_vmatoa ("x", state_machine_regs.address),
3299 state_machine_regs.op_index);
3300 }
3301 break;
3302
3303 case DW_LNS_advance_line:
3304 adv = read_sleb128 (data, & bytes_read, end);
3305 data += bytes_read;
3306 state_machine_regs.line += adv;
3307 printf (_(" Advance Line by %s to %d\n"),
3308 dwarf_vmatoa ("d", adv),
3309 state_machine_regs.line);
3310 break;
3311
3312 case DW_LNS_set_file:
3313 adv = read_uleb128 (data, & bytes_read, end);
3314 data += bytes_read;
3315 printf (_(" Set File Name to entry %s in the File Name Table\n"),
3316 dwarf_vmatoa ("d", adv));
3317 state_machine_regs.file = adv;
3318 break;
3319
3320 case DW_LNS_set_column:
3321 uladv = read_uleb128 (data, & bytes_read, end);
3322 data += bytes_read;
3323 printf (_(" Set column to %s\n"),
3324 dwarf_vmatoa ("u", uladv));
3325 state_machine_regs.column = uladv;
3326 break;
3327
3328 case DW_LNS_negate_stmt:
3329 adv = state_machine_regs.is_stmt;
3330 adv = ! adv;
3331 printf (_(" Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
3332 state_machine_regs.is_stmt = adv;
3333 break;
3334
3335 case DW_LNS_set_basic_block:
3336 printf (_(" Set basic block\n"));
3337 state_machine_regs.basic_block = 1;
3338 break;
3339
3340 case DW_LNS_const_add_pc:
3341 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
3342 if (linfo.li_max_ops_per_insn)
3343 {
3344 uladv *= linfo.li_min_insn_length;
3345 state_machine_regs.address += uladv;
3346 printf (_(" Advance PC by constant %s to 0x%s\n"),
3347 dwarf_vmatoa ("u", uladv),
3348 dwarf_vmatoa ("x", state_machine_regs.address));
3349 }
3350 else
3351 {
3352 state_machine_regs.address
3353 += ((state_machine_regs.op_index + uladv)
3354 / linfo.li_max_ops_per_insn)
3355 * linfo.li_min_insn_length;
3356 state_machine_regs.op_index
3357 = (state_machine_regs.op_index + uladv)
3358 % linfo.li_max_ops_per_insn;
3359 printf (_(" Advance PC by constant %s to 0x%s[%d]\n"),
3360 dwarf_vmatoa ("u", uladv),
3361 dwarf_vmatoa ("x", state_machine_regs.address),
3362 state_machine_regs.op_index);
3363 }
3364 break;
3365
3366 case DW_LNS_fixed_advance_pc:
3367 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
3368 state_machine_regs.address += uladv;
3369 state_machine_regs.op_index = 0;
3370 printf (_(" Advance PC by fixed size amount %s to 0x%s\n"),
3371 dwarf_vmatoa ("u", uladv),
3372 dwarf_vmatoa ("x", state_machine_regs.address));
3373 break;
3374
3375 case DW_LNS_set_prologue_end:
3376 printf (_(" Set prologue_end to true\n"));
3377 break;
3378
3379 case DW_LNS_set_epilogue_begin:
3380 printf (_(" Set epilogue_begin to true\n"));
3381 break;
3382
3383 case DW_LNS_set_isa:
3384 uladv = read_uleb128 (data, & bytes_read, end);
3385 data += bytes_read;
3386 printf (_(" Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
3387 break;
3388
3389 default:
3390 printf (_(" Unknown opcode %d with operands: "), op_code);
3391
3392 if (standard_opcodes != NULL)
3393 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
3394 {
3395 printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
3396 &bytes_read, end)),
3397 i == 1 ? "" : ", ");
3398 data += bytes_read;
3399 }
3400 putchar ('\n');
3401 break;
3402 }
3403 }
3404 putchar ('\n');
3405 }
3406 }
3407
3408 return 1;
3409 }
3410
3411 typedef struct
3412 {
3413 unsigned char *name;
3414 unsigned int directory_index;
3415 unsigned int modification_date;
3416 unsigned int length;
3417 } File_Entry;
3418
3419 /* Output a decoded representation of the .debug_line section. */
3420
3421 static int
3422 display_debug_lines_decoded (struct dwarf_section *section,
3423 unsigned char *data,
3424 unsigned char *end, void *fileptr)
3425 {
3426 static DWARF2_Internal_LineInfo saved_linfo;
3427
3428 printf (_("Decoded dump of debug contents of section %s:\n\n"),
3429 section->name);
3430
3431 while (data < end)
3432 {
3433 /* This loop amounts to one iteration per compilation unit. */
3434 DWARF2_Internal_LineInfo linfo;
3435 unsigned char *standard_opcodes;
3436 unsigned char *end_of_sequence;
3437 int i;
3438 File_Entry *file_table = NULL;
3439 unsigned int n_files = 0;
3440 unsigned char **directory_table = NULL;
3441 dwarf_vma n_directories = 0;
3442
3443 if (const_strneq (section->name, ".debug_line.")
3444 /* Note: the following does not apply to .debug_line.dwo sections.
3445 These are full debug_line sections. */
3446 && strcmp (section->name, ".debug_line.dwo") != 0)
3447 {
3448 /* See comment in display_debug_lines_raw(). */
3449 end_of_sequence = end;
3450 standard_opcodes = NULL;
3451 linfo = saved_linfo;
3452 /* PR 17531: file: 0522b371. */
3453 if (linfo.li_line_range == 0)
3454 {
3455 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
3456 return 0;
3457 }
3458 reset_state_machine (linfo.li_default_is_stmt);
3459 }
3460 else
3461 {
3462 unsigned char *hdrptr;
3463
3464 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
3465 & end_of_sequence)) == NULL)
3466 return 0;
3467
3468 /* PR 17531: file: 0522b371. */
3469 if (linfo.li_line_range == 0)
3470 {
3471 warn (_("Line range of 0 is invalid, using 1 instead\n"));
3472 linfo.li_line_range = 1;
3473 }
3474 reset_state_machine (linfo.li_default_is_stmt);
3475
3476 /* Save a pointer to the contents of the Opcodes table. */
3477 standard_opcodes = hdrptr;
3478
3479 /* Traverse the Directory table just to count entries. */
3480 data = standard_opcodes + linfo.li_opcode_base - 1;
3481 /* PR 20440 */
3482 if (data >= end)
3483 {
3484 warn (_("opcode base of %d extends beyond end of section\n"),
3485 linfo.li_opcode_base);
3486 return 0;
3487 }
3488
3489 if (linfo.li_version >= 5)
3490 {
3491 unsigned char *format_start, format_count, *format;
3492 dwarf_vma formati, entryi;
3493 unsigned int bytes_read;
3494
3495 load_debug_section (line_str, fileptr);
3496
3497 /* Skip directories format. */
3498 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
3499 format_start = data;
3500 for (formati = 0; formati < format_count; formati++)
3501 {
3502 read_uleb128 (data, & bytes_read, end);
3503 data += bytes_read;
3504 read_uleb128 (data, & bytes_read, end);
3505 data += bytes_read;
3506 }
3507
3508 n_directories = read_uleb128 (data, & bytes_read, end);
3509 data += bytes_read;
3510 if (data == end)
3511 {
3512 warn (_("Corrupt directories list\n"));
3513 break;
3514 }
3515
3516 directory_table = (unsigned char **)
3517 xmalloc (n_directories * sizeof (unsigned char *));
3518
3519 for (entryi = 0; entryi < n_directories; entryi++)
3520 {
3521 unsigned char **pathp = &directory_table[entryi];
3522
3523 format = format_start;
3524 for (formati = 0; formati < format_count; formati++)
3525 {
3526 dwarf_vma content_type, form;
3527 dwarf_vma uvalue;
3528
3529 content_type = read_uleb128 (format, & bytes_read, end);
3530 format += bytes_read;
3531 form = read_uleb128 (format, & bytes_read, end);
3532 format += bytes_read;
3533 if (data == end)
3534 {
3535 warn (_("Corrupt directories list\n"));
3536 break;
3537 }
3538 switch (content_type)
3539 {
3540 case DW_LNCT_path:
3541 switch (form)
3542 {
3543 case DW_FORM_string:
3544 *pathp = data;
3545 break;
3546 case DW_FORM_line_strp:
3547 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
3548 end);
3549 /* Remove const by the cast. */
3550 *pathp = (unsigned char *)
3551 fetch_indirect_line_string (uvalue);
3552 break;
3553 }
3554 break;
3555 }
3556 data = read_and_display_attr_value (0, form, 0, data, end,
3557 0, 0,
3558 linfo.li_offset_size,
3559 linfo.li_version,
3560 NULL, 1, section,
3561 NULL, '\t');
3562 }
3563 if (data == end)
3564 {
3565 warn (_("Corrupt directories list\n"));
3566 break;
3567 }
3568 }
3569
3570 /* Skip files format. */
3571 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
3572 format_start = data;
3573 for (formati = 0; formati < format_count; formati++)
3574 {
3575 read_uleb128 (data, & bytes_read, end);
3576 data += bytes_read;
3577 read_uleb128 (data, & bytes_read, end);
3578 data += bytes_read;
3579 }
3580
3581 n_files = read_uleb128 (data, & bytes_read, end);
3582 data += bytes_read;
3583 if (data == end)
3584 {
3585 warn (_("Corrupt file name list\n"));
3586 break;
3587 }
3588
3589 file_table = (File_Entry *) xcalloc (1, n_files
3590 * sizeof (File_Entry));
3591
3592 for (entryi = 0; entryi < n_files; entryi++)
3593 {
3594 File_Entry *file = &file_table[entryi];
3595
3596 format = format_start;
3597 for (formati = 0; formati < format_count; formati++)
3598 {
3599 dwarf_vma content_type, form;
3600 dwarf_vma uvalue;
3601
3602 content_type = read_uleb128 (format, & bytes_read, end);
3603 format += bytes_read;
3604 form = read_uleb128 (format, & bytes_read, end);
3605 format += bytes_read;
3606 if (data == end)
3607 {
3608 warn (_("Corrupt file name list\n"));
3609 break;
3610 }
3611 switch (content_type)
3612 {
3613 case DW_LNCT_path:
3614 switch (form)
3615 {
3616 case DW_FORM_string:
3617 file->name = data;
3618 break;
3619 case DW_FORM_line_strp:
3620 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
3621 end);
3622 /* Remove const by the cast. */
3623 file->name = (unsigned char *)
3624 fetch_indirect_line_string (uvalue);
3625 break;
3626 }
3627 break;
3628 case DW_LNCT_directory_index:
3629 switch (form)
3630 {
3631 case DW_FORM_data1:
3632 SAFE_BYTE_GET (file->directory_index, data, 1,
3633 end);
3634 break;
3635 case DW_FORM_data2:
3636 SAFE_BYTE_GET (file->directory_index, data, 2,
3637 end);
3638 break;
3639 case DW_FORM_udata:
3640 file->directory_index = read_uleb128 (data, NULL,
3641 end);
3642 break;
3643 }
3644 break;
3645 }
3646 data = read_and_display_attr_value (0, form, 0, data, end,
3647 0, 0,
3648 linfo.li_offset_size,
3649 linfo.li_version,
3650 NULL, 1, section,
3651 NULL, '\t');
3652 }
3653 if (data == end)
3654 {
3655 warn (_("Corrupt file name list\n"));
3656 break;
3657 }
3658 }
3659 }
3660 else
3661 {
3662 if (*data != 0)
3663 {
3664 unsigned char *ptr_directory_table = data;
3665
3666 while (data < end && *data != 0)
3667 {
3668 data += strnlen ((char *) data, end - data) + 1;
3669 n_directories++;
3670 }
3671
3672 /* PR 20440 */
3673 if (data >= end)
3674 {
3675 warn (_("directory table ends unexpectedly\n"));
3676 n_directories = 0;
3677 break;
3678 }
3679
3680 /* Go through the directory table again to save the directories. */
3681 directory_table = (unsigned char **)
3682 xmalloc (n_directories * sizeof (unsigned char *));
3683
3684 i = 0;
3685 while (*ptr_directory_table != 0)
3686 {
3687 directory_table[i] = ptr_directory_table;
3688 ptr_directory_table += strnlen ((char *) ptr_directory_table,
3689 ptr_directory_table - end) + 1;
3690 i++;
3691 }
3692 }
3693 /* Skip the NUL at the end of the table. */
3694 data++;
3695
3696 /* Traverse the File Name table just to count the entries. */
3697 if (data < end && *data != 0)
3698 {
3699 unsigned char *ptr_file_name_table = data;
3700
3701 while (data < end && *data != 0)
3702 {
3703 unsigned int bytes_read;
3704
3705 /* Skip Name, directory index, last modification time and length
3706 of file. */
3707 data += strnlen ((char *) data, end - data) + 1;
3708 read_uleb128 (data, & bytes_read, end);
3709 data += bytes_read;
3710 read_uleb128 (data, & bytes_read, end);
3711 data += bytes_read;
3712 read_uleb128 (data, & bytes_read, end);
3713 data += bytes_read;
3714
3715 n_files++;
3716 }
3717
3718 if (data >= end)
3719 {
3720 warn (_("file table ends unexpectedly\n"));
3721 n_files = 0;
3722 break;
3723 }
3724
3725 /* Go through the file table again to save the strings. */
3726 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
3727
3728 i = 0;
3729 while (*ptr_file_name_table != 0)
3730 {
3731 unsigned int bytes_read;
3732
3733 file_table[i].name = ptr_file_name_table;
3734 ptr_file_name_table += strnlen ((char *) ptr_file_name_table,
3735 end - ptr_file_name_table) + 1;
3736
3737 /* We are not interested in directory, time or size. */
3738 file_table[i].directory_index = read_uleb128 (ptr_file_name_table,
3739 & bytes_read, end);
3740 ptr_file_name_table += bytes_read;
3741 file_table[i].modification_date = read_uleb128 (ptr_file_name_table,
3742 & bytes_read, end);
3743 ptr_file_name_table += bytes_read;
3744 file_table[i].length = read_uleb128 (ptr_file_name_table, & bytes_read, end);
3745 ptr_file_name_table += bytes_read;
3746 i++;
3747 }
3748 i = 0;
3749 }
3750
3751 /* Skip the NUL at the end of the table. */
3752 data++;
3753 }
3754
3755 /* Print the Compilation Unit's name and a header. */
3756 if (directory_table == NULL)
3757 {
3758 printf (_("CU: %s:\n"), file_table[0].name);
3759 printf (_("File name Line number Starting address\n"));
3760 }
3761 else
3762 {
3763 unsigned int ix = file_table[0].directory_index;
3764 const char *directory;
3765
3766 if (ix == 0)
3767 directory = ".";
3768 /* PR 20439 */
3769 else if (n_directories == 0)
3770 directory = _("<unknown>");
3771 else if (ix > n_directories)
3772 {
3773 warn (_("directory index %u > number of directories %s\n"),
3774 ix, dwarf_vmatoa ("u", n_directories));
3775 directory = _("<corrupt>");
3776 }
3777 else
3778 directory = (char *) directory_table[ix - 1];
3779
3780 if (do_wide || strlen (directory) < 76)
3781 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
3782 else
3783 printf ("%s:\n", file_table[0].name);
3784
3785 printf (_("File name Line number Starting address\n"));
3786 }
3787
3788 saved_linfo = linfo;
3789 }
3790
3791 /* This loop iterates through the Dwarf Line Number Program. */
3792 while (data < end_of_sequence)
3793 {
3794 unsigned char op_code;
3795 int adv;
3796 unsigned long int uladv;
3797 unsigned int bytes_read;
3798 int is_special_opcode = 0;
3799
3800 op_code = *data++;
3801
3802 if (op_code >= linfo.li_opcode_base)
3803 {
3804 op_code -= linfo.li_opcode_base;
3805 uladv = (op_code / linfo.li_line_range);
3806 if (linfo.li_max_ops_per_insn == 1)
3807 {
3808 uladv *= linfo.li_min_insn_length;
3809 state_machine_regs.address += uladv;
3810 }
3811 else
3812 {
3813 state_machine_regs.address
3814 += ((state_machine_regs.op_index + uladv)
3815 / linfo.li_max_ops_per_insn)
3816 * linfo.li_min_insn_length;
3817 state_machine_regs.op_index
3818 = (state_machine_regs.op_index + uladv)
3819 % linfo.li_max_ops_per_insn;
3820 }
3821
3822 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
3823 state_machine_regs.line += adv;
3824 is_special_opcode = 1;
3825 }
3826 else switch (op_code)
3827 {
3828 case DW_LNS_extended_op:
3829 {
3830 unsigned int ext_op_code_len;
3831 unsigned char ext_op_code;
3832 unsigned char *op_code_data = data;
3833
3834 ext_op_code_len = read_uleb128 (op_code_data, &bytes_read,
3835 end_of_sequence);
3836 op_code_data += bytes_read;
3837
3838 if (ext_op_code_len == 0)
3839 {
3840 warn (_("Badly formed extended line op encountered!\n"));
3841 break;
3842 }
3843 ext_op_code_len += bytes_read;
3844 ext_op_code = *op_code_data++;
3845
3846 switch (ext_op_code)
3847 {
3848 case DW_LNE_end_sequence:
3849 reset_state_machine (linfo.li_default_is_stmt);
3850 break;
3851 case DW_LNE_set_address:
3852 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
3853 op_code_data,
3854 ext_op_code_len - bytes_read - 1,
3855 end);
3856 state_machine_regs.op_index = 0;
3857 break;
3858 case DW_LNE_define_file:
3859 {
3860 file_table = (File_Entry *) xrealloc
3861 (file_table, (n_files + 1) * sizeof (File_Entry));
3862
3863 ++state_machine_regs.last_file_entry;
3864 /* Source file name. */
3865 file_table[n_files].name = op_code_data;
3866 op_code_data += strlen ((char *) op_code_data) + 1;
3867 /* Directory index. */
3868 file_table[n_files].directory_index =
3869 read_uleb128 (op_code_data, & bytes_read,
3870 end_of_sequence);
3871 op_code_data += bytes_read;
3872 /* Last modification time. */
3873 file_table[n_files].modification_date =
3874 read_uleb128 (op_code_data, & bytes_read,
3875 end_of_sequence);
3876 op_code_data += bytes_read;
3877 /* File length. */
3878 file_table[n_files].length =
3879 read_uleb128 (op_code_data, & bytes_read,
3880 end_of_sequence);
3881
3882 n_files++;
3883 break;
3884 }
3885 case DW_LNE_set_discriminator:
3886 case DW_LNE_HP_set_sequence:
3887 /* Simply ignored. */
3888 break;
3889
3890 default:
3891 printf (_("UNKNOWN (%u): length %d\n"),
3892 ext_op_code, ext_op_code_len - bytes_read);
3893 break;
3894 }
3895 data += ext_op_code_len;
3896 break;
3897 }
3898 case DW_LNS_copy:
3899 break;
3900
3901 case DW_LNS_advance_pc:
3902 uladv = read_uleb128 (data, & bytes_read, end);
3903 data += bytes_read;
3904 if (linfo.li_max_ops_per_insn == 1)
3905 {
3906 uladv *= linfo.li_min_insn_length;
3907 state_machine_regs.address += uladv;
3908 }
3909 else
3910 {
3911 state_machine_regs.address
3912 += ((state_machine_regs.op_index + uladv)
3913 / linfo.li_max_ops_per_insn)
3914 * linfo.li_min_insn_length;
3915 state_machine_regs.op_index
3916 = (state_machine_regs.op_index + uladv)
3917 % linfo.li_max_ops_per_insn;
3918 }
3919 break;
3920
3921 case DW_LNS_advance_line:
3922 adv = read_sleb128 (data, & bytes_read, end);
3923 data += bytes_read;
3924 state_machine_regs.line += adv;
3925 break;
3926
3927 case DW_LNS_set_file:
3928 adv = read_uleb128 (data, & bytes_read, end);
3929 data += bytes_read;
3930 state_machine_regs.file = adv;
3931
3932 {
3933 unsigned file = state_machine_regs.file - 1;
3934 unsigned dir;
3935
3936 if (file_table == NULL || n_files == 0)
3937 printf (_("\n [Use file table entry %d]\n"), file);
3938 /* PR 20439 */
3939 else if (file >= n_files)
3940 {
3941 warn (_("file index %u > number of files %u\n"), file + 1, n_files);
3942 printf (_("\n <over large file table index %u>"), file);
3943 }
3944 else if ((dir = file_table[file].directory_index) == 0)
3945 /* If directory index is 0, that means current directory. */
3946 printf ("\n./%s:[++]\n", file_table[file].name);
3947 else if (directory_table == NULL || n_directories == 0)
3948 printf (_("\n [Use file %s in directory table entry %d]\n"),
3949 file_table[file].name, dir);
3950 /* PR 20439 */
3951 else if (dir > n_directories)
3952 {
3953 warn (_("directory index %u > number of directories %s\n"),
3954 dir, dwarf_vmatoa ("u", n_directories));
3955 printf (_("\n <over large directory table entry %u>\n"), dir);
3956 }
3957 else
3958 printf ("\n%s/%s:\n",
3959 /* The directory index starts counting at 1. */
3960 directory_table[dir - 1], file_table[file].name);
3961 }
3962 break;
3963
3964 case DW_LNS_set_column:
3965 uladv = read_uleb128 (data, & bytes_read, end);
3966 data += bytes_read;
3967 state_machine_regs.column = uladv;
3968 break;
3969
3970 case DW_LNS_negate_stmt:
3971 adv = state_machine_regs.is_stmt;
3972 adv = ! adv;
3973 state_machine_regs.is_stmt = adv;
3974 break;
3975
3976 case DW_LNS_set_basic_block:
3977 state_machine_regs.basic_block = 1;
3978 break;
3979
3980 case DW_LNS_const_add_pc:
3981 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
3982 if (linfo.li_max_ops_per_insn == 1)
3983 {
3984 uladv *= linfo.li_min_insn_length;
3985 state_machine_regs.address += uladv;
3986 }
3987 else
3988 {
3989 state_machine_regs.address
3990 += ((state_machine_regs.op_index + uladv)
3991 / linfo.li_max_ops_per_insn)
3992 * linfo.li_min_insn_length;
3993 state_machine_regs.op_index
3994 = (state_machine_regs.op_index + uladv)
3995 % linfo.li_max_ops_per_insn;
3996 }
3997 break;
3998
3999 case DW_LNS_fixed_advance_pc:
4000 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4001 state_machine_regs.address += uladv;
4002 state_machine_regs.op_index = 0;
4003 break;
4004
4005 case DW_LNS_set_prologue_end:
4006 break;
4007
4008 case DW_LNS_set_epilogue_begin:
4009 break;
4010
4011 case DW_LNS_set_isa:
4012 uladv = read_uleb128 (data, & bytes_read, end);
4013 data += bytes_read;
4014 printf (_(" Set ISA to %lu\n"), uladv);
4015 break;
4016
4017 default:
4018 printf (_(" Unknown opcode %d with operands: "), op_code);
4019
4020 if (standard_opcodes != NULL)
4021 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4022 {
4023 printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
4024 &bytes_read, end)),
4025 i == 1 ? "" : ", ");
4026 data += bytes_read;
4027 }
4028 putchar ('\n');
4029 break;
4030 }
4031
4032 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
4033 to the DWARF address/line matrix. */
4034 if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
4035 || (op_code == DW_LNS_copy))
4036 {
4037 const unsigned int MAX_FILENAME_LENGTH = 35;
4038 char *fileName;
4039 char *newFileName = NULL;
4040 size_t fileNameLength;
4041
4042 if (file_table)
4043 {
4044 unsigned indx = state_machine_regs.file - 1;
4045 /* PR 20439 */
4046 if (indx >= n_files)
4047 {
4048 warn (_("corrupt file index %u encountered\n"), indx);
4049 fileName = _("<corrupt>");
4050 }
4051 else
4052 fileName = (char *) file_table[indx].name;
4053 }
4054 else
4055 fileName = _("<unknown>");
4056
4057 fileNameLength = strlen (fileName);
4058
4059 if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
4060 {
4061 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
4062 /* Truncate file name */
4063 strncpy (newFileName,
4064 fileName + fileNameLength - MAX_FILENAME_LENGTH,
4065 MAX_FILENAME_LENGTH + 1);
4066 }
4067 else
4068 {
4069 newFileName = (char *) xmalloc (fileNameLength + 1);
4070 strncpy (newFileName, fileName, fileNameLength + 1);
4071 }
4072
4073 if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
4074 {
4075 if (linfo.li_max_ops_per_insn == 1)
4076 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x\n",
4077 newFileName, state_machine_regs.line,
4078 state_machine_regs.address);
4079 else
4080 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x[%d]\n",
4081 newFileName, state_machine_regs.line,
4082 state_machine_regs.address,
4083 state_machine_regs.op_index);
4084 }
4085 else
4086 {
4087 if (linfo.li_max_ops_per_insn == 1)
4088 printf ("%s %11d %#18" DWARF_VMA_FMT "x\n",
4089 newFileName, state_machine_regs.line,
4090 state_machine_regs.address);
4091 else
4092 printf ("%s %11d %#18" DWARF_VMA_FMT "x[%d]\n",
4093 newFileName, state_machine_regs.line,
4094 state_machine_regs.address,
4095 state_machine_regs.op_index);
4096 }
4097
4098 if (op_code == DW_LNE_end_sequence)
4099 printf ("\n");
4100
4101 free (newFileName);
4102 }
4103 }
4104
4105 if (file_table)
4106 {
4107 free (file_table);
4108 file_table = NULL;
4109 n_files = 0;
4110 }
4111
4112 if (directory_table)
4113 {
4114 free (directory_table);
4115 directory_table = NULL;
4116 n_directories = 0;
4117 }
4118
4119 putchar ('\n');
4120 }
4121
4122 return 1;
4123 }
4124
4125 static int
4126 display_debug_lines (struct dwarf_section *section, void *file)
4127 {
4128 unsigned char *data = section->start;
4129 unsigned char *end = data + section->size;
4130 int retValRaw = 1;
4131 int retValDecoded = 1;
4132
4133 if (do_debug_lines == 0)
4134 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
4135
4136 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
4137 retValRaw = display_debug_lines_raw (section, data, end, file);
4138
4139 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
4140 retValDecoded = display_debug_lines_decoded (section, data, end, file);
4141
4142 if (!retValRaw || !retValDecoded)
4143 return 0;
4144
4145 return 1;
4146 }
4147
4148 static debug_info *
4149 find_debug_info_for_offset (unsigned long offset)
4150 {
4151 unsigned int i;
4152
4153 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4154 return NULL;
4155
4156 for (i = 0; i < num_debug_info_entries; i++)
4157 if (debug_information[i].cu_offset == offset)
4158 return debug_information + i;
4159
4160 return NULL;
4161 }
4162
4163 static const char *
4164 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
4165 {
4166 /* See gdb/gdb-index.h. */
4167 static const char * const kinds[] =
4168 {
4169 N_ ("no info"),
4170 N_ ("type"),
4171 N_ ("variable"),
4172 N_ ("function"),
4173 N_ ("other"),
4174 N_ ("unused5"),
4175 N_ ("unused6"),
4176 N_ ("unused7")
4177 };
4178
4179 return _ (kinds[kind]);
4180 }
4181
4182 static int
4183 display_debug_pubnames_worker (struct dwarf_section *section,
4184 void *file ATTRIBUTE_UNUSED,
4185 int is_gnu)
4186 {
4187 DWARF2_Internal_PubNames names;
4188 unsigned char *start = section->start;
4189 unsigned char *end = start + section->size;
4190
4191 /* It does not matter if this load fails,
4192 we test for that later on. */
4193 load_debug_info (file);
4194
4195 printf (_("Contents of the %s section:\n\n"), section->name);
4196
4197 while (start < end)
4198 {
4199 unsigned char *data;
4200 unsigned char *adr;
4201 dwarf_vma offset;
4202 unsigned int offset_size, initial_length_size;
4203
4204 data = start;
4205
4206 SAFE_BYTE_GET_AND_INC (names.pn_length, data, 4, end);
4207 if (names.pn_length == 0xffffffff)
4208 {
4209 SAFE_BYTE_GET_AND_INC (names.pn_length, data, 8, end);
4210 offset_size = 8;
4211 initial_length_size = 12;
4212 }
4213 else
4214 {
4215 offset_size = 4;
4216 initial_length_size = 4;
4217 }
4218
4219 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, end);
4220 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, end);
4221
4222 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
4223 && num_debug_info_entries > 0
4224 && find_debug_info_for_offset (names.pn_offset) == NULL)
4225 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
4226 (unsigned long) names.pn_offset, section->name);
4227
4228 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, end);
4229
4230 adr = start + names.pn_length + initial_length_size;
4231 /* PR 17531: file: 7615b6b2. */
4232 if ((dwarf_signed_vma) names.pn_length < 0
4233 /* PR 17531: file: a5dbeaa7. */
4234 || adr < start)
4235 {
4236 warn (_("Negative length for public name: 0x%lx\n"), (long) names.pn_length);
4237 start = end;
4238 }
4239 else
4240 start = adr;
4241
4242 printf (_(" Length: %ld\n"),
4243 (long) names.pn_length);
4244 printf (_(" Version: %d\n"),
4245 names.pn_version);
4246 printf (_(" Offset into .debug_info section: 0x%lx\n"),
4247 (unsigned long) names.pn_offset);
4248 printf (_(" Size of area in .debug_info section: %ld\n"),
4249 (long) names.pn_size);
4250
4251 if (names.pn_version != 2 && names.pn_version != 3)
4252 {
4253 static int warned = 0;
4254
4255 if (! warned)
4256 {
4257 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
4258 warned = 1;
4259 }
4260
4261 continue;
4262 }
4263
4264 if (is_gnu)
4265 printf (_("\n Offset Kind Name\n"));
4266 else
4267 printf (_("\n Offset\tName\n"));
4268
4269 do
4270 {
4271 bfd_size_type maxprint;
4272
4273 SAFE_BYTE_GET (offset, data, offset_size, end);
4274
4275 if (offset != 0)
4276 {
4277 data += offset_size;
4278 if (data >= end)
4279 break;
4280 maxprint = (end - data) - 1;
4281
4282 if (is_gnu)
4283 {
4284 unsigned int kind_data;
4285 gdb_index_symbol_kind kind;
4286 const char *kind_name;
4287 int is_static;
4288
4289 SAFE_BYTE_GET (kind_data, data, 1, end);
4290 data++;
4291 maxprint --;
4292 /* GCC computes the kind as the upper byte in the CU index
4293 word, and then right shifts it by the CU index size.
4294 Left shift KIND to where the gdb-index.h accessor macros
4295 can use it. */
4296 kind_data <<= GDB_INDEX_CU_BITSIZE;
4297 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
4298 kind_name = get_gdb_index_symbol_kind_name (kind);
4299 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
4300 printf (" %-6lx %s,%-10s %.*s\n",
4301 (unsigned long) offset, is_static ? _("s") : _("g"),
4302 kind_name, (int) maxprint, data);
4303 }
4304 else
4305 printf (" %-6lx\t%.*s\n",
4306 (unsigned long) offset, (int) maxprint, data);
4307
4308 data += strnlen ((char *) data, maxprint) + 1;
4309 if (data >= end)
4310 break;
4311 }
4312 }
4313 while (offset != 0);
4314 }
4315
4316 printf ("\n");
4317 return 1;
4318 }
4319
4320 static int
4321 display_debug_pubnames (struct dwarf_section *section, void *file)
4322 {
4323 return display_debug_pubnames_worker (section, file, 0);
4324 }
4325
4326 static int
4327 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
4328 {
4329 return display_debug_pubnames_worker (section, file, 1);
4330 }
4331
4332 static int
4333 display_debug_macinfo (struct dwarf_section *section,
4334 void *file ATTRIBUTE_UNUSED)
4335 {
4336 unsigned char *start = section->start;
4337 unsigned char *end = start + section->size;
4338 unsigned char *curr = start;
4339 unsigned int bytes_read;
4340 enum dwarf_macinfo_record_type op;
4341
4342 printf (_("Contents of the %s section:\n\n"), section->name);
4343
4344 while (curr < end)
4345 {
4346 unsigned int lineno;
4347 const unsigned char *string;
4348
4349 op = (enum dwarf_macinfo_record_type) *curr;
4350 curr++;
4351
4352 switch (op)
4353 {
4354 case DW_MACINFO_start_file:
4355 {
4356 unsigned int filenum;
4357
4358 lineno = read_uleb128 (curr, & bytes_read, end);
4359 curr += bytes_read;
4360 filenum = read_uleb128 (curr, & bytes_read, end);
4361 curr += bytes_read;
4362
4363 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
4364 lineno, filenum);
4365 }
4366 break;
4367
4368 case DW_MACINFO_end_file:
4369 printf (_(" DW_MACINFO_end_file\n"));
4370 break;
4371
4372 case DW_MACINFO_define:
4373 lineno = read_uleb128 (curr, & bytes_read, end);
4374 curr += bytes_read;
4375 string = curr;
4376 curr += strnlen ((char *) string, end - string) + 1;
4377 printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
4378 lineno, string);
4379 break;
4380
4381 case DW_MACINFO_undef:
4382 lineno = read_uleb128 (curr, & bytes_read, end);
4383 curr += bytes_read;
4384 string = curr;
4385 curr += strnlen ((char *) string, end - string) + 1;
4386 printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
4387 lineno, string);
4388 break;
4389
4390 case DW_MACINFO_vendor_ext:
4391 {
4392 unsigned int constant;
4393
4394 constant = read_uleb128 (curr, & bytes_read, end);
4395 curr += bytes_read;
4396 string = curr;
4397 curr += strnlen ((char *) string, end - string) + 1;
4398 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
4399 constant, string);
4400 }
4401 break;
4402 }
4403 }
4404
4405 return 1;
4406 }
4407
4408 /* Given LINE_OFFSET into the .debug_line section, attempt to return
4409 filename and dirname corresponding to file name table entry with index
4410 FILEIDX. Return NULL on failure. */
4411
4412 static unsigned char *
4413 get_line_filename_and_dirname (dwarf_vma line_offset,
4414 dwarf_vma fileidx,
4415 unsigned char **dir_name)
4416 {
4417 struct dwarf_section *section = &debug_displays [line].section;
4418 unsigned char *hdrptr, *dirtable, *file_name;
4419 unsigned int offset_size, initial_length_size;
4420 unsigned int version, opcode_base, bytes_read;
4421 dwarf_vma length, diridx;
4422 const unsigned char * end;
4423
4424 *dir_name = NULL;
4425 if (section->start == NULL
4426 || line_offset >= section->size
4427 || fileidx == 0)
4428 return NULL;
4429
4430 hdrptr = section->start + line_offset;
4431 end = section->start + section->size;
4432
4433 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
4434 if (length == 0xffffffff)
4435 {
4436 /* This section is 64-bit DWARF 3. */
4437 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
4438 offset_size = 8;
4439 initial_length_size = 12;
4440 }
4441 else
4442 {
4443 offset_size = 4;
4444 initial_length_size = 4;
4445 }
4446 if (length + initial_length_size > section->size)
4447 return NULL;
4448
4449 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
4450 if (version != 2 && version != 3 && version != 4)
4451 return NULL;
4452 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
4453 if (version >= 4)
4454 hdrptr++; /* Skip max_ops_per_insn. */
4455 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
4456
4457 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
4458 if (opcode_base == 0)
4459 return NULL;
4460
4461 hdrptr += opcode_base - 1;
4462 dirtable = hdrptr;
4463 /* Skip over dirname table. */
4464 while (*hdrptr != '\0')
4465 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
4466 hdrptr++; /* Skip the NUL at the end of the table. */
4467 /* Now skip over preceding filename table entries. */
4468 for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
4469 {
4470 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
4471 read_uleb128 (hdrptr, &bytes_read, end);
4472 hdrptr += bytes_read;
4473 read_uleb128 (hdrptr, &bytes_read, end);
4474 hdrptr += bytes_read;
4475 read_uleb128 (hdrptr, &bytes_read, end);
4476 hdrptr += bytes_read;
4477 }
4478 if (hdrptr == end || *hdrptr == '\0')
4479 return NULL;
4480 file_name = hdrptr;
4481 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
4482 diridx = read_uleb128 (hdrptr, &bytes_read, end);
4483 if (diridx == 0)
4484 return file_name;
4485 for (; *dirtable != '\0' && diridx > 1; diridx--)
4486 dirtable += strnlen ((char *) dirtable, end - dirtable) + 1;
4487 if (*dirtable == '\0')
4488 return NULL;
4489 *dir_name = dirtable;
4490 return file_name;
4491 }
4492
4493 static int
4494 display_debug_macro (struct dwarf_section *section,
4495 void *file)
4496 {
4497 unsigned char *start = section->start;
4498 unsigned char *end = start + section->size;
4499 unsigned char *curr = start;
4500 unsigned char *extended_op_buf[256];
4501 unsigned int bytes_read;
4502
4503 load_debug_section (str, file);
4504 load_debug_section (line, file);
4505
4506 printf (_("Contents of the %s section:\n\n"), section->name);
4507
4508 while (curr < end)
4509 {
4510 unsigned int lineno, version, flags;
4511 unsigned int offset_size = 4;
4512 const unsigned char *string;
4513 dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
4514 unsigned char **extended_ops = NULL;
4515
4516 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
4517 if (version != 4)
4518 {
4519 error (_("Only GNU extension to DWARF 4 of %s is currently supported.\n"),
4520 section->name);
4521 return 0;
4522 }
4523
4524 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
4525 if (flags & 1)
4526 offset_size = 8;
4527 printf (_(" Offset: 0x%lx\n"),
4528 (unsigned long) sec_offset);
4529 printf (_(" Version: %d\n"), version);
4530 printf (_(" Offset size: %d\n"), offset_size);
4531 if (flags & 2)
4532 {
4533 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
4534 printf (_(" Offset into .debug_line: 0x%lx\n"),
4535 (unsigned long) line_offset);
4536 }
4537 if (flags & 4)
4538 {
4539 unsigned int i, count, op;
4540 dwarf_vma nargs, n;
4541
4542 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
4543
4544 memset (extended_op_buf, 0, sizeof (extended_op_buf));
4545 extended_ops = extended_op_buf;
4546 if (count)
4547 {
4548 printf (_(" Extension opcode arguments:\n"));
4549 for (i = 0; i < count; i++)
4550 {
4551 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4552 extended_ops[op] = curr;
4553 nargs = read_uleb128 (curr, &bytes_read, end);
4554 curr += bytes_read;
4555 if (nargs == 0)
4556 printf (_(" DW_MACRO_GNU_%02x has no arguments\n"), op);
4557 else
4558 {
4559 printf (_(" DW_MACRO_GNU_%02x arguments: "), op);
4560 for (n = 0; n < nargs; n++)
4561 {
4562 unsigned int form;
4563
4564 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
4565 printf ("%s%s", get_FORM_name (form),
4566 n == nargs - 1 ? "\n" : ", ");
4567 switch (form)
4568 {
4569 case DW_FORM_data1:
4570 case DW_FORM_data2:
4571 case DW_FORM_data4:
4572 case DW_FORM_data8:
4573 case DW_FORM_sdata:
4574 case DW_FORM_udata:
4575 case DW_FORM_block:
4576 case DW_FORM_block1:
4577 case DW_FORM_block2:
4578 case DW_FORM_block4:
4579 case DW_FORM_flag:
4580 case DW_FORM_string:
4581 case DW_FORM_strp:
4582 case DW_FORM_sec_offset:
4583 break;
4584 default:
4585 error (_("Invalid extension opcode form %s\n"),
4586 get_FORM_name (form));
4587 return 0;
4588 }
4589 }
4590 }
4591 }
4592 }
4593 }
4594 printf ("\n");
4595
4596 while (1)
4597 {
4598 unsigned int op;
4599
4600 if (curr >= end)
4601 {
4602 error (_(".debug_macro section not zero terminated\n"));
4603 return 0;
4604 }
4605
4606 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4607 if (op == 0)
4608 break;
4609
4610 switch (op)
4611 {
4612 case DW_MACRO_GNU_start_file:
4613 {
4614 unsigned int filenum;
4615 unsigned char *file_name = NULL, *dir_name = NULL;
4616
4617 lineno = read_uleb128 (curr, &bytes_read, end);
4618 curr += bytes_read;
4619 filenum = read_uleb128 (curr, &bytes_read, end);
4620 curr += bytes_read;
4621
4622 if ((flags & 2) == 0)
4623 error (_("DW_MACRO_GNU_start_file used, but no .debug_line offset provided.\n"));
4624 else
4625 file_name
4626 = get_line_filename_and_dirname (line_offset, filenum,
4627 &dir_name);
4628 if (file_name == NULL)
4629 printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d\n"),
4630 lineno, filenum);
4631 else
4632 printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
4633 lineno, filenum,
4634 dir_name != NULL ? (const char *) dir_name : "",
4635 dir_name != NULL ? "/" : "", file_name);
4636 }
4637 break;
4638
4639 case DW_MACRO_GNU_end_file:
4640 printf (_(" DW_MACRO_GNU_end_file\n"));
4641 break;
4642
4643 case DW_MACRO_GNU_define:
4644 lineno = read_uleb128 (curr, &bytes_read, end);
4645 curr += bytes_read;
4646 string = curr;
4647 curr += strnlen ((char *) string, end - string) + 1;
4648 printf (_(" DW_MACRO_GNU_define - lineno : %d macro : %s\n"),
4649 lineno, string);
4650 break;
4651
4652 case DW_MACRO_GNU_undef:
4653 lineno = read_uleb128 (curr, &bytes_read, end);
4654 curr += bytes_read;
4655 string = curr;
4656 curr += strnlen ((char *) string, end - string) + 1;
4657 printf (_(" DW_MACRO_GNU_undef - lineno : %d macro : %s\n"),
4658 lineno, string);
4659 break;
4660
4661 case DW_MACRO_GNU_define_indirect:
4662 lineno = read_uleb128 (curr, &bytes_read, end);
4663 curr += bytes_read;
4664 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4665 string = fetch_indirect_string (offset);
4666 printf (_(" DW_MACRO_GNU_define_indirect - lineno : %d macro : %s\n"),
4667 lineno, string);
4668 break;
4669
4670 case DW_MACRO_GNU_undef_indirect:
4671 lineno = read_uleb128 (curr, &bytes_read, end);
4672 curr += bytes_read;
4673 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4674 string = fetch_indirect_string (offset);
4675 printf (_(" DW_MACRO_GNU_undef_indirect - lineno : %d macro : %s\n"),
4676 lineno, string);
4677 break;
4678
4679 case DW_MACRO_GNU_transparent_include:
4680 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4681 printf (_(" DW_MACRO_GNU_transparent_include - offset : 0x%lx\n"),
4682 (unsigned long) offset);
4683 break;
4684
4685 case DW_MACRO_GNU_define_indirect_alt:
4686 lineno = read_uleb128 (curr, &bytes_read, end);
4687 curr += bytes_read;
4688 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4689 printf (_(" DW_MACRO_GNU_define_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
4690 lineno, (unsigned long) offset);
4691 break;
4692
4693 case DW_MACRO_GNU_undef_indirect_alt:
4694 lineno = read_uleb128 (curr, &bytes_read, end);
4695 curr += bytes_read;
4696 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4697 printf (_(" DW_MACRO_GNU_undef_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
4698 lineno, (unsigned long) offset);
4699 break;
4700
4701 case DW_MACRO_GNU_transparent_include_alt:
4702 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4703 printf (_(" DW_MACRO_GNU_transparent_include_alt - offset : 0x%lx\n"),
4704 (unsigned long) offset);
4705 break;
4706
4707 default:
4708 if (extended_ops == NULL || extended_ops[op] == NULL)
4709 {
4710 error (_(" Unknown macro opcode %02x seen\n"), op);
4711 return 0;
4712 }
4713 else
4714 {
4715 /* Skip over unhandled opcodes. */
4716 dwarf_vma nargs, n;
4717 unsigned char *desc = extended_ops[op];
4718 nargs = read_uleb128 (desc, &bytes_read, end);
4719 desc += bytes_read;
4720 if (nargs == 0)
4721 {
4722 printf (_(" DW_MACRO_GNU_%02x\n"), op);
4723 break;
4724 }
4725 printf (_(" DW_MACRO_GNU_%02x -"), op);
4726 for (n = 0; n < nargs; n++)
4727 {
4728 int val;
4729
4730 /* DW_FORM_implicit_const is not expected here. */
4731 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
4732 curr
4733 = read_and_display_attr_value (0, val, 0,
4734 curr, end, 0, 0, offset_size,
4735 version, NULL, 0, NULL,
4736 NULL, ' ');
4737 if (n != nargs - 1)
4738 printf (",");
4739 }
4740 printf ("\n");
4741 }
4742 break;
4743 }
4744 }
4745
4746 printf ("\n");
4747 }
4748
4749 return 1;
4750 }
4751
4752 static int
4753 display_debug_abbrev (struct dwarf_section *section,
4754 void *file ATTRIBUTE_UNUSED)
4755 {
4756 abbrev_entry *entry;
4757 unsigned char *start = section->start;
4758 unsigned char *end = start + section->size;
4759
4760 printf (_("Contents of the %s section:\n\n"), section->name);
4761
4762 do
4763 {
4764 unsigned char *last;
4765
4766 free_abbrevs ();
4767
4768 last = start;
4769 start = process_abbrev_section (start, end);
4770
4771 if (first_abbrev == NULL)
4772 continue;
4773
4774 printf (_(" Number TAG (0x%lx)\n"), (long) (last - section->start));
4775
4776 for (entry = first_abbrev; entry; entry = entry->next)
4777 {
4778 abbrev_attr *attr;
4779
4780 printf (" %ld %s [%s]\n",
4781 entry->entry,
4782 get_TAG_name (entry->tag),
4783 entry->children ? _("has children") : _("no children"));
4784
4785 for (attr = entry->first_attr; attr; attr = attr->next)
4786 {
4787 printf (" %-18s %s",
4788 get_AT_name (attr->attribute),
4789 get_FORM_name (attr->form));
4790 if (attr->form == DW_FORM_implicit_const)
4791 printf (": %" BFD_VMA_FMT "d", attr->implicit_const);
4792 putchar ('\n');
4793 }
4794 }
4795 }
4796 while (start);
4797
4798 printf ("\n");
4799
4800 return 1;
4801 }
4802
4803 /* Return true when ADDR is the maximum address, when addresses are
4804 POINTER_SIZE bytes long. */
4805
4806 static bfd_boolean
4807 is_max_address (dwarf_vma addr, unsigned int pointer_size)
4808 {
4809 dwarf_vma mask = ~(~(dwarf_vma) 1 << (pointer_size * 8 - 1));
4810 return ((addr & mask) == mask);
4811 }
4812
4813 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
4814
4815 static void
4816 display_loc_list (struct dwarf_section *section,
4817 unsigned char **start_ptr,
4818 unsigned int debug_info_entry,
4819 dwarf_vma offset,
4820 dwarf_vma base_address,
4821 int has_frame_base)
4822 {
4823 unsigned char *start = *start_ptr;
4824 unsigned char *section_end = section->start + section->size;
4825 unsigned long cu_offset;
4826 unsigned int pointer_size;
4827 unsigned int offset_size;
4828 int dwarf_version;
4829
4830 dwarf_vma begin;
4831 dwarf_vma end;
4832 unsigned short length;
4833 int need_frame_base;
4834
4835 if (debug_info_entry >= num_debug_info_entries)
4836 {
4837 warn (_("No debug information available for loc lists of entry: %u\n"),
4838 debug_info_entry);
4839 return;
4840 }
4841
4842 cu_offset = debug_information [debug_info_entry].cu_offset;
4843 pointer_size = debug_information [debug_info_entry].pointer_size;
4844 offset_size = debug_information [debug_info_entry].offset_size;
4845 dwarf_version = debug_information [debug_info_entry].dwarf_version;
4846
4847 if (pointer_size < 2 || pointer_size > 8)
4848 {
4849 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
4850 pointer_size, debug_info_entry);
4851 return;
4852 }
4853
4854 while (1)
4855 {
4856 dwarf_vma off = offset + (start - *start_ptr);
4857
4858 if (start + 2 * pointer_size > section_end)
4859 {
4860 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4861 (unsigned long) offset);
4862 break;
4863 }
4864
4865 printf (" %8.8lx ", (unsigned long) off);
4866
4867 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
4868 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
4869
4870 if (begin == 0 && end == 0)
4871 {
4872 /* PR 18374: In a object file we can have a location list that
4873 starts with a begin and end of 0 because there are relocations
4874 that need to be applied to the addresses. Actually applying
4875 the relocations now does not help as they will probably resolve
4876 to 0, since the object file has not been fully linked. Real
4877 end of list markers will not have any relocations against them. */
4878 if (! reloc_at (section, off)
4879 && ! reloc_at (section, off + pointer_size))
4880 {
4881 printf (_("<End of list>\n"));
4882 break;
4883 }
4884 }
4885
4886 /* Check base address specifiers. */
4887 if (is_max_address (begin, pointer_size)
4888 && !is_max_address (end, pointer_size))
4889 {
4890 base_address = end;
4891 print_dwarf_vma (begin, pointer_size);
4892 print_dwarf_vma (end, pointer_size);
4893 printf (_("(base address)\n"));
4894 continue;
4895 }
4896
4897 if (start + 2 > section_end)
4898 {
4899 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4900 (unsigned long) offset);
4901 break;
4902 }
4903
4904 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4905
4906 if (start + length > section_end)
4907 {
4908 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4909 (unsigned long) offset);
4910 break;
4911 }
4912
4913 print_dwarf_vma (begin + base_address, pointer_size);
4914 print_dwarf_vma (end + base_address, pointer_size);
4915
4916 putchar ('(');
4917 need_frame_base = decode_location_expression (start,
4918 pointer_size,
4919 offset_size,
4920 dwarf_version,
4921 length,
4922 cu_offset, section);
4923 putchar (')');
4924
4925 if (need_frame_base && !has_frame_base)
4926 printf (_(" [without DW_AT_frame_base]"));
4927
4928 if (begin == end)
4929 fputs (_(" (start == end)"), stdout);
4930 else if (begin > end)
4931 fputs (_(" (start > end)"), stdout);
4932
4933 putchar ('\n');
4934
4935 start += length;
4936 }
4937
4938 *start_ptr = start;
4939 }
4940
4941 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
4942
4943 static void
4944 display_loclists_list (struct dwarf_section *section,
4945 unsigned char **start_ptr,
4946 unsigned int debug_info_entry,
4947 dwarf_vma offset,
4948 dwarf_vma base_address,
4949 int has_frame_base)
4950 {
4951 unsigned char *start = *start_ptr;
4952 unsigned char *section_end = section->start + section->size;
4953 unsigned long cu_offset;
4954 unsigned int pointer_size;
4955 unsigned int offset_size;
4956 int dwarf_version;
4957 unsigned int bytes_read;
4958
4959 dwarf_vma begin;
4960 dwarf_vma end;
4961 dwarf_vma length;
4962 int need_frame_base;
4963
4964 if (debug_info_entry >= num_debug_info_entries)
4965 {
4966 warn (_("No debug information available for "
4967 "loclists lists of entry: %u\n"),
4968 debug_info_entry);
4969 return;
4970 }
4971
4972 cu_offset = debug_information [debug_info_entry].cu_offset;
4973 pointer_size = debug_information [debug_info_entry].pointer_size;
4974 offset_size = debug_information [debug_info_entry].offset_size;
4975 dwarf_version = debug_information [debug_info_entry].dwarf_version;
4976
4977 if (pointer_size < 2 || pointer_size > 8)
4978 {
4979 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
4980 pointer_size, debug_info_entry);
4981 return;
4982 }
4983
4984 while (1)
4985 {
4986 dwarf_vma off = offset + (start - *start_ptr);
4987 enum dwarf_location_list_entry_type llet;
4988
4989 if (start + 1 > section_end)
4990 {
4991 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4992 (unsigned long) offset);
4993 break;
4994 }
4995
4996 printf (" %8.8lx ", (unsigned long) off);
4997
4998 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
4999
5000 switch (llet)
5001 {
5002 case DW_LLE_end_of_list:
5003 printf (_("<End of list>\n"));
5004 break;
5005 case DW_LLE_offset_pair:
5006 begin = read_uleb128 (start, &bytes_read, section_end);
5007 start += bytes_read;
5008 end = read_uleb128 (start, &bytes_read, section_end);
5009 start += bytes_read;
5010 break;
5011 case DW_LLE_base_address:
5012 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
5013 section_end);
5014 print_dwarf_vma (base_address, pointer_size);
5015 printf (_("(base address)\n"));
5016 break;
5017 default:
5018 error (_("Invalid location list entry type %d\n"), llet);
5019 return;
5020 }
5021 if (llet == DW_LLE_end_of_list)
5022 break;
5023 if (llet != DW_LLE_offset_pair)
5024 continue;
5025
5026 if (start + 2 > section_end)
5027 {
5028 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5029 (unsigned long) offset);
5030 break;
5031 }
5032
5033 length = read_uleb128 (start, &bytes_read, section_end);
5034 start += bytes_read;
5035
5036 print_dwarf_vma (begin + base_address, pointer_size);
5037 print_dwarf_vma (end + base_address, pointer_size);
5038
5039 putchar ('(');
5040 need_frame_base = decode_location_expression (start,
5041 pointer_size,
5042 offset_size,
5043 dwarf_version,
5044 length,
5045 cu_offset, section);
5046 putchar (')');
5047
5048 if (need_frame_base && !has_frame_base)
5049 printf (_(" [without DW_AT_frame_base]"));
5050
5051 if (begin == end)
5052 fputs (_(" (start == end)"), stdout);
5053 else if (begin > end)
5054 fputs (_(" (start > end)"), stdout);
5055
5056 putchar ('\n');
5057
5058 start += length;
5059 }
5060
5061 *start_ptr = start;
5062 }
5063
5064 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
5065 right-adjusted in a field of length LEN, and followed by a space. */
5066
5067 static void
5068 print_addr_index (unsigned int idx, unsigned int len)
5069 {
5070 static char buf[15];
5071 snprintf (buf, sizeof (buf), "[%d]", idx);
5072 printf ("%*s ", len, buf);
5073 }
5074
5075 /* Display a location list from a .dwo section. It uses address indexes rather
5076 than embedded addresses. This code closely follows display_loc_list, but the
5077 two are sufficiently different that combining things is very ugly. */
5078
5079 static void
5080 display_loc_list_dwo (struct dwarf_section *section,
5081 unsigned char **start_ptr,
5082 unsigned int debug_info_entry,
5083 dwarf_vma offset,
5084 int has_frame_base)
5085 {
5086 unsigned char *start = *start_ptr;
5087 unsigned char *section_end = section->start + section->size;
5088 unsigned long cu_offset;
5089 unsigned int pointer_size;
5090 unsigned int offset_size;
5091 int dwarf_version;
5092 int entry_type;
5093 unsigned short length;
5094 int need_frame_base;
5095 unsigned int idx;
5096 unsigned int bytes_read;
5097
5098 if (debug_info_entry >= num_debug_info_entries)
5099 {
5100 warn (_("No debug information for loc lists of entry: %u\n"),
5101 debug_info_entry);
5102 return;
5103 }
5104
5105 cu_offset = debug_information [debug_info_entry].cu_offset;
5106 pointer_size = debug_information [debug_info_entry].pointer_size;
5107 offset_size = debug_information [debug_info_entry].offset_size;
5108 dwarf_version = debug_information [debug_info_entry].dwarf_version;
5109
5110 if (pointer_size < 2 || pointer_size > 8)
5111 {
5112 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
5113 pointer_size, debug_info_entry);
5114 return;
5115 }
5116
5117 while (1)
5118 {
5119 printf (" %8.8lx ", (unsigned long) (offset + (start - *start_ptr)));
5120
5121 if (start >= section_end)
5122 {
5123 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5124 (unsigned long) offset);
5125 break;
5126 }
5127
5128 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
5129 switch (entry_type)
5130 {
5131 case 0: /* A terminating entry. */
5132 *start_ptr = start;
5133 printf (_("<End of list>\n"));
5134 return;
5135 case 1: /* A base-address entry. */
5136 idx = read_uleb128 (start, &bytes_read, section_end);
5137 start += bytes_read;
5138 print_addr_index (idx, 8);
5139 printf (" ");
5140 printf (_("(base address selection entry)\n"));
5141 continue;
5142 case 2: /* A start/end entry. */
5143 idx = read_uleb128 (start, &bytes_read, section_end);
5144 start += bytes_read;
5145 print_addr_index (idx, 8);
5146 idx = read_uleb128 (start, &bytes_read, section_end);
5147 start += bytes_read;
5148 print_addr_index (idx, 8);
5149 break;
5150 case 3: /* A start/length entry. */
5151 idx = read_uleb128 (start, &bytes_read, section_end);
5152 start += bytes_read;
5153 print_addr_index (idx, 8);
5154 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
5155 printf ("%08x ", idx);
5156 break;
5157 case 4: /* An offset pair entry. */
5158 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
5159 printf ("%08x ", idx);
5160 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
5161 printf ("%08x ", idx);
5162 break;
5163 default:
5164 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
5165 *start_ptr = start;
5166 return;
5167 }
5168
5169 if (start + 2 > section_end)
5170 {
5171 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5172 (unsigned long) offset);
5173 break;
5174 }
5175
5176 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
5177 if (start + length > section_end)
5178 {
5179 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5180 (unsigned long) offset);
5181 break;
5182 }
5183
5184 putchar ('(');
5185 need_frame_base = decode_location_expression (start,
5186 pointer_size,
5187 offset_size,
5188 dwarf_version,
5189 length,
5190 cu_offset, section);
5191 putchar (')');
5192
5193 if (need_frame_base && !has_frame_base)
5194 printf (_(" [without DW_AT_frame_base]"));
5195
5196 putchar ('\n');
5197
5198 start += length;
5199 }
5200
5201 *start_ptr = start;
5202 }
5203
5204 /* Sort array of indexes in ascending order of loc_offsets[idx]. */
5205
5206 static dwarf_vma *loc_offsets;
5207
5208 static int
5209 loc_offsets_compar (const void *ap, const void *bp)
5210 {
5211 dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
5212 dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
5213
5214 return (a > b) - (b > a);
5215 }
5216
5217 static int
5218 display_debug_loc (struct dwarf_section *section, void *file)
5219 {
5220 unsigned char *start = section->start;
5221 unsigned long bytes;
5222 unsigned char *section_begin = start;
5223 unsigned int num_loc_list = 0;
5224 unsigned long last_offset = 0;
5225 unsigned int first = 0;
5226 unsigned int i;
5227 unsigned int j;
5228 int seen_first_offset = 0;
5229 int locs_sorted = 1;
5230 unsigned char *next;
5231 unsigned int *array = NULL;
5232 const char *suffix = strrchr (section->name, '.');
5233 int is_dwo = 0;
5234 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
5235 dwarf_vma expected_start = 0;
5236
5237 if (suffix && strcmp (suffix, ".dwo") == 0)
5238 is_dwo = 1;
5239
5240 bytes = section->size;
5241
5242 if (bytes == 0)
5243 {
5244 printf (_("\nThe %s section is empty.\n"), section->name);
5245 return 0;
5246 }
5247
5248 if (is_loclists)
5249 {
5250 unsigned char *hdrptr = section_begin;
5251 dwarf_vma ll_length;
5252 unsigned short ll_version;
5253 unsigned char *end = section_begin + section->size;
5254 unsigned char address_size, segment_selector_size;
5255 uint32_t offset_entry_count;
5256
5257 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
5258 if (ll_length == 0xffffffff)
5259 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
5260
5261 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
5262 if (ll_version != 5)
5263 {
5264 warn (_("The %s section contains corrupt or "
5265 "unsupported version number: %d.\n"),
5266 section->name, ll_version);
5267 return 0;
5268 }
5269
5270 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
5271
5272 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
5273 if (segment_selector_size != 0)
5274 {
5275 warn (_("The %s section contains "
5276 "unsupported segment selector size: %d.\n"),
5277 section->name, segment_selector_size);
5278 return 0;
5279 }
5280
5281 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
5282 if (offset_entry_count != 0)
5283 {
5284 warn (_("The %s section contains "
5285 "unsupported offset entry count: %d.\n"),
5286 section->name, offset_entry_count);
5287 return 0;
5288 }
5289
5290 expected_start = hdrptr - section_begin;
5291 }
5292
5293 if (load_debug_info (file) == 0)
5294 {
5295 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
5296 section->name);
5297 return 0;
5298 }
5299
5300 /* Check the order of location list in .debug_info section. If
5301 offsets of location lists are in the ascending order, we can
5302 use `debug_information' directly. */
5303 for (i = 0; i < num_debug_info_entries; i++)
5304 {
5305 unsigned int num;
5306
5307 num = debug_information [i].num_loc_offsets;
5308 if (num > num_loc_list)
5309 num_loc_list = num;
5310
5311 /* Check if we can use `debug_information' directly. */
5312 if (locs_sorted && num != 0)
5313 {
5314 if (!seen_first_offset)
5315 {
5316 /* This is the first location list. */
5317 last_offset = debug_information [i].loc_offsets [0];
5318 first = i;
5319 seen_first_offset = 1;
5320 j = 1;
5321 }
5322 else
5323 j = 0;
5324
5325 for (; j < num; j++)
5326 {
5327 if (last_offset >
5328 debug_information [i].loc_offsets [j])
5329 {
5330 locs_sorted = 0;
5331 break;
5332 }
5333 last_offset = debug_information [i].loc_offsets [j];
5334 }
5335 }
5336 }
5337
5338 if (!seen_first_offset)
5339 error (_("No location lists in .debug_info section!\n"));
5340
5341 if (debug_information [first].num_loc_offsets > 0
5342 && debug_information [first].loc_offsets [0] != expected_start)
5343 warn (_("Location lists in %s section start at 0x%s\n"),
5344 section->name,
5345 dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
5346
5347 if (!locs_sorted)
5348 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
5349 printf (_("Contents of the %s section:\n\n"), section->name);
5350 if (reloc_at (section, 0))
5351 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
5352 printf (_(" Offset Begin End Expression\n"));
5353
5354 seen_first_offset = 0;
5355 for (i = first; i < num_debug_info_entries; i++)
5356 {
5357 dwarf_vma offset;
5358 dwarf_vma base_address;
5359 unsigned int k;
5360 int has_frame_base;
5361
5362 if (!locs_sorted)
5363 {
5364 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
5365 array[k] = k;
5366 loc_offsets = debug_information [i].loc_offsets;
5367 qsort (array, debug_information [i].num_loc_offsets,
5368 sizeof (*array), loc_offsets_compar);
5369 }
5370
5371 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
5372 {
5373 j = locs_sorted ? k : array[k];
5374 if (k
5375 && debug_information [i].loc_offsets [locs_sorted
5376 ? k - 1 : array [k - 1]]
5377 == debug_information [i].loc_offsets [j])
5378 continue;
5379 has_frame_base = debug_information [i].have_frame_base [j];
5380 offset = debug_information [i].loc_offsets [j];
5381 next = section_begin + offset;
5382 base_address = debug_information [i].base_address;
5383
5384 if (!seen_first_offset)
5385 seen_first_offset = 1;
5386 else
5387 {
5388 if (start < next)
5389 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
5390 (unsigned long) (start - section_begin),
5391 (unsigned long) offset);
5392 else if (start > next)
5393 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
5394 (unsigned long) (start - section_begin),
5395 (unsigned long) offset);
5396 }
5397 start = next;
5398
5399 if (offset >= bytes)
5400 {
5401 warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
5402 (unsigned long) offset);
5403 continue;
5404 }
5405
5406 if (!is_loclists)
5407 {
5408 if (is_dwo)
5409 display_loc_list_dwo (section, &start, i, offset,
5410 has_frame_base);
5411 else
5412 display_loc_list (section, &start, i, offset, base_address,
5413 has_frame_base);
5414 }
5415 else
5416 {
5417 if (is_dwo)
5418 warn (_("DWO is not yet supported.\n"));
5419 else
5420 display_loclists_list (section, &start, i, offset, base_address,
5421 has_frame_base);
5422 }
5423 }
5424 }
5425
5426 if (start < section->start + section->size)
5427 warn (_("There are %ld unused bytes at the end of section %s\n"),
5428 (long) (section->start + section->size - start), section->name);
5429 putchar ('\n');
5430 free (array);
5431 return 1;
5432 }
5433
5434 static int
5435 display_debug_str (struct dwarf_section *section,
5436 void *file ATTRIBUTE_UNUSED)
5437 {
5438 unsigned char *start = section->start;
5439 unsigned long bytes = section->size;
5440 dwarf_vma addr = section->address;
5441
5442 if (bytes == 0)
5443 {
5444 printf (_("\nThe %s section is empty.\n"), section->name);
5445 return 0;
5446 }
5447
5448 printf (_("Contents of the %s section:\n\n"), section->name);
5449
5450 while (bytes)
5451 {
5452 int j;
5453 int k;
5454 int lbytes;
5455
5456 lbytes = (bytes > 16 ? 16 : bytes);
5457
5458 printf (" 0x%8.8lx ", (unsigned long) addr);
5459
5460 for (j = 0; j < 16; j++)
5461 {
5462 if (j < lbytes)
5463 printf ("%2.2x", start[j]);
5464 else
5465 printf (" ");
5466
5467 if ((j & 3) == 3)
5468 printf (" ");
5469 }
5470
5471 for (j = 0; j < lbytes; j++)
5472 {
5473 k = start[j];
5474 if (k >= ' ' && k < 0x80)
5475 printf ("%c", k);
5476 else
5477 printf (".");
5478 }
5479
5480 putchar ('\n');
5481
5482 start += lbytes;
5483 addr += lbytes;
5484 bytes -= lbytes;
5485 }
5486
5487 putchar ('\n');
5488
5489 return 1;
5490 }
5491
5492 static int
5493 display_debug_info (struct dwarf_section *section, void *file)
5494 {
5495 return process_debug_info (section, file, section->abbrev_sec, 0, 0);
5496 }
5497
5498 static int
5499 display_debug_types (struct dwarf_section *section, void *file)
5500 {
5501 return process_debug_info (section, file, section->abbrev_sec, 0, 1);
5502 }
5503
5504 static int
5505 display_trace_info (struct dwarf_section *section, void *file)
5506 {
5507 return process_debug_info (section, file, section->abbrev_sec, 0, 0);
5508 }
5509
5510 static int
5511 display_debug_aranges (struct dwarf_section *section,
5512 void *file ATTRIBUTE_UNUSED)
5513 {
5514 unsigned char *start = section->start;
5515 unsigned char *end = start + section->size;
5516
5517 printf (_("Contents of the %s section:\n\n"), section->name);
5518
5519 /* It does not matter if this load fails,
5520 we test for that later on. */
5521 load_debug_info (file);
5522
5523 while (start < end)
5524 {
5525 unsigned char *hdrptr;
5526 DWARF2_Internal_ARange arange;
5527 unsigned char *addr_ranges;
5528 dwarf_vma length;
5529 dwarf_vma address;
5530 unsigned char address_size;
5531 int excess;
5532 unsigned int offset_size;
5533 unsigned int initial_length_size;
5534
5535 hdrptr = start;
5536
5537 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
5538 if (arange.ar_length == 0xffffffff)
5539 {
5540 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
5541 offset_size = 8;
5542 initial_length_size = 12;
5543 }
5544 else
5545 {
5546 offset_size = 4;
5547 initial_length_size = 4;
5548 }
5549
5550 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end);
5551 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size, end);
5552
5553 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5554 && num_debug_info_entries > 0
5555 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
5556 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
5557 (unsigned long) arange.ar_info_offset, section->name);
5558
5559 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end);
5560 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end);
5561
5562 if (arange.ar_version != 2 && arange.ar_version != 3)
5563 {
5564 /* PR 19872: A version number of 0 probably means that there is
5565 padding at the end of the .debug_aranges section. Gold puts
5566 it there when performing an incremental link, for example.
5567 So do not generate a warning in this case. */
5568 if (arange.ar_version)
5569 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
5570 break;
5571 }
5572
5573 printf (_(" Length: %ld\n"),
5574 (long) arange.ar_length);
5575 printf (_(" Version: %d\n"), arange.ar_version);
5576 printf (_(" Offset into .debug_info: 0x%lx\n"),
5577 (unsigned long) arange.ar_info_offset);
5578 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
5579 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
5580
5581 address_size = arange.ar_pointer_size + arange.ar_segment_size;
5582
5583 /* PR 17512: file: 001-108546-0.001:0.1. */
5584 if (address_size == 0 || address_size > 8)
5585 {
5586 error (_("Invalid address size in %s section!\n"),
5587 section->name);
5588 break;
5589 }
5590
5591 /* The DWARF spec does not require that the address size be a power
5592 of two, but we do. This will have to change if we ever encounter
5593 an uneven architecture. */
5594 if ((address_size & (address_size - 1)) != 0)
5595 {
5596 warn (_("Pointer size + Segment size is not a power of two.\n"));
5597 break;
5598 }
5599
5600 if (address_size > 4)
5601 printf (_("\n Address Length\n"));
5602 else
5603 printf (_("\n Address Length\n"));
5604
5605 addr_ranges = hdrptr;
5606
5607 /* Must pad to an alignment boundary that is twice the address size. */
5608 excess = (hdrptr - start) % (2 * address_size);
5609 if (excess)
5610 addr_ranges += (2 * address_size) - excess;
5611
5612 hdrptr = start + arange.ar_length + initial_length_size;
5613 if (hdrptr < start || hdrptr > end)
5614 {
5615 error (_("Excessive header length: %lx\n"), (long) arange.ar_length);
5616 break;
5617 }
5618 start = hdrptr;
5619
5620 while (addr_ranges + 2 * address_size <= start)
5621 {
5622 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size, end);
5623 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size, end);
5624
5625 printf (" ");
5626 print_dwarf_vma (address, address_size);
5627 print_dwarf_vma (length, address_size);
5628 putchar ('\n');
5629 }
5630 }
5631
5632 printf ("\n");
5633
5634 return 1;
5635 }
5636
5637 /* Comparison function for qsort. */
5638 static int
5639 comp_addr_base (const void * v0, const void * v1)
5640 {
5641 debug_info * info0 = (debug_info *) v0;
5642 debug_info * info1 = (debug_info *) v1;
5643 return info0->addr_base - info1->addr_base;
5644 }
5645
5646 /* Display the debug_addr section. */
5647 static int
5648 display_debug_addr (struct dwarf_section *section,
5649 void *file)
5650 {
5651 debug_info **debug_addr_info;
5652 unsigned char *entry;
5653 unsigned char *end;
5654 unsigned int i;
5655 unsigned int count;
5656
5657 if (section->size == 0)
5658 {
5659 printf (_("\nThe %s section is empty.\n"), section->name);
5660 return 0;
5661 }
5662
5663 if (load_debug_info (file) == 0)
5664 {
5665 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
5666 section->name);
5667 return 0;
5668 }
5669
5670 printf (_("Contents of the %s section:\n\n"), section->name);
5671
5672 /* PR 17531: file: cf38d01b.
5673 We use xcalloc because a corrupt file may not have initialised all of the
5674 fields in the debug_info structure, which means that the sort below might
5675 try to move uninitialised data. */
5676 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
5677 sizeof (debug_info *));
5678
5679 count = 0;
5680 for (i = 0; i < num_debug_info_entries; i++)
5681 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
5682 {
5683 /* PR 17531: file: cf38d01b. */
5684 if (debug_information[i].addr_base >= section->size)
5685 warn (_("Corrupt address base (%lx) found in debug section %u\n"),
5686 (unsigned long) debug_information[i].addr_base, i);
5687 else
5688 debug_addr_info [count++] = debug_information + i;
5689 }
5690
5691 /* Add a sentinel to make iteration convenient. */
5692 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
5693 debug_addr_info [count]->addr_base = section->size;
5694 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
5695
5696 for (i = 0; i < count; i++)
5697 {
5698 unsigned int idx;
5699 unsigned int address_size = debug_addr_info [i]->pointer_size;
5700
5701 printf (_(" For compilation unit at offset 0x%s:\n"),
5702 dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
5703
5704 printf (_("\tIndex\tAddress\n"));
5705 entry = section->start + debug_addr_info [i]->addr_base;
5706 end = section->start + debug_addr_info [i + 1]->addr_base;
5707 idx = 0;
5708 while (entry < end)
5709 {
5710 dwarf_vma base = byte_get (entry, address_size);
5711 printf (_("\t%d:\t"), idx);
5712 print_dwarf_vma (base, address_size);
5713 printf ("\n");
5714 entry += address_size;
5715 idx++;
5716 }
5717 }
5718 printf ("\n");
5719
5720 free (debug_addr_info);
5721 return 1;
5722 }
5723
5724 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
5725 static int
5726 display_debug_str_offsets (struct dwarf_section *section,
5727 void *file ATTRIBUTE_UNUSED)
5728 {
5729 if (section->size == 0)
5730 {
5731 printf (_("\nThe %s section is empty.\n"), section->name);
5732 return 0;
5733 }
5734 /* TODO: Dump the contents. This is made somewhat difficult by not knowing
5735 what the offset size is for this section. */
5736 return 1;
5737 }
5738
5739 /* Each debug_information[x].range_lists[y] gets this representation for
5740 sorting purposes. */
5741
5742 struct range_entry
5743 {
5744 /* The debug_information[x].range_lists[y] value. */
5745 dwarf_vma ranges_offset;
5746
5747 /* Original debug_information to find parameters of the data. */
5748 debug_info *debug_info_p;
5749 };
5750
5751 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
5752
5753 static int
5754 range_entry_compar (const void *ap, const void *bp)
5755 {
5756 const struct range_entry *a_re = (const struct range_entry *) ap;
5757 const struct range_entry *b_re = (const struct range_entry *) bp;
5758 const dwarf_vma a = a_re->ranges_offset;
5759 const dwarf_vma b = b_re->ranges_offset;
5760
5761 return (a > b) - (b > a);
5762 }
5763
5764 static void
5765 display_debug_ranges_list (unsigned char *start, unsigned char *finish,
5766 unsigned int pointer_size, unsigned long offset,
5767 unsigned long base_address)
5768 {
5769 while (start < finish)
5770 {
5771 dwarf_vma begin;
5772 dwarf_vma end;
5773
5774 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
5775 if (start >= finish)
5776 break;
5777 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
5778
5779 printf (" %8.8lx ", offset);
5780
5781 if (begin == 0 && end == 0)
5782 {
5783 printf (_("<End of list>\n"));
5784 break;
5785 }
5786
5787 /* Check base address specifiers. */
5788 if (is_max_address (begin, pointer_size)
5789 && !is_max_address (end, pointer_size))
5790 {
5791 base_address = end;
5792 print_dwarf_vma (begin, pointer_size);
5793 print_dwarf_vma (end, pointer_size);
5794 printf ("(base address)\n");
5795 continue;
5796 }
5797
5798 print_dwarf_vma (begin + base_address, pointer_size);
5799 print_dwarf_vma (end + base_address, pointer_size);
5800
5801 if (begin == end)
5802 fputs (_("(start == end)"), stdout);
5803 else if (begin > end)
5804 fputs (_("(start > end)"), stdout);
5805
5806 putchar ('\n');
5807 }
5808 }
5809
5810 static void
5811 display_debug_rnglists_list (unsigned char *start, unsigned char *finish,
5812 unsigned int pointer_size, unsigned long offset,
5813 unsigned long base_address)
5814 {
5815 unsigned char *next = start;
5816
5817 while (1)
5818 {
5819 unsigned long off = offset + (start - next);
5820 enum dwarf_range_list_entry rlet;
5821 dwarf_vma begin, length, end;
5822 unsigned int bytes_read;
5823
5824 if (start + 1 > finish)
5825 {
5826 warn (_("Range list starting at offset 0x%lx is not terminated.\n"),
5827 offset);
5828 break;
5829 }
5830
5831 printf (" %8.8lx ", off);
5832
5833 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
5834
5835 switch (rlet)
5836 {
5837 case DW_RLE_end_of_list:
5838 printf (_("<End of list>\n"));
5839 break;
5840 case DW_RLE_base_address:
5841 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
5842 print_dwarf_vma (base_address, pointer_size);
5843 printf (_("(base address)\n"));
5844 break;
5845 case DW_RLE_start_length:
5846 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
5847 length = read_uleb128 (start, &bytes_read, finish);
5848 start += bytes_read;
5849 end = begin + length;
5850 break;
5851 case DW_RLE_offset_pair:
5852 begin = read_uleb128 (start, &bytes_read, finish);
5853 start += bytes_read;
5854 end = read_uleb128 (start, &bytes_read, finish);
5855 start += bytes_read;
5856 break;
5857 case DW_RLE_start_end:
5858 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
5859 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
5860 break;
5861 default:
5862 error (_("Invalid range list entry type %d\n"), rlet);
5863 rlet = DW_RLE_end_of_list;
5864 break;
5865 }
5866 if (rlet == DW_RLE_end_of_list)
5867 break;
5868 if (rlet == DW_RLE_base_address)
5869 continue;
5870
5871 print_dwarf_vma (begin + base_address, pointer_size);
5872 print_dwarf_vma (end + base_address, pointer_size);
5873
5874 if (begin == end)
5875 fputs (_("(start == end)"), stdout);
5876 else if (begin > end)
5877 fputs (_("(start > end)"), stdout);
5878
5879 putchar ('\n');
5880 }
5881 }
5882
5883 static int
5884 display_debug_ranges (struct dwarf_section *section,
5885 void *file ATTRIBUTE_UNUSED)
5886 {
5887 unsigned char *start = section->start;
5888 unsigned char *last_start = start;
5889 unsigned long bytes = section->size;
5890 unsigned char *section_begin = start;
5891 unsigned char *finish = start + bytes;
5892 unsigned int num_range_list, i;
5893 struct range_entry *range_entries, *range_entry_fill;
5894 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
5895 /* Initialize it due to a false compiler warning. */
5896 unsigned char address_size = 0;
5897
5898 if (bytes == 0)
5899 {
5900 printf (_("\nThe %s section is empty.\n"), section->name);
5901 return 0;
5902 }
5903
5904 if (is_rnglists)
5905 {
5906 dwarf_vma initial_length;
5907 unsigned int initial_length_size;
5908 unsigned char segment_selector_size;
5909 unsigned int offset_size, offset_entry_count;
5910 unsigned short version;
5911
5912 /* Get and check the length of the block. */
5913 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
5914
5915 if (initial_length == 0xffffffff)
5916 {
5917 /* This section is 64-bit DWARF 3. */
5918 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
5919 offset_size = 8;
5920 initial_length_size = 12;
5921 }
5922 else
5923 {
5924 offset_size = 4;
5925 initial_length_size = 4;
5926 }
5927
5928 if (initial_length + initial_length_size > section->size)
5929 {
5930 /* If the length field has a relocation against it, then we should
5931 not complain if it is inaccurate (and probably negative).
5932 It is copied from .debug_line handling code. */
5933 if (reloc_at (section, (start - section->start) - offset_size))
5934 {
5935 initial_length = (finish - start) - initial_length_size;
5936 }
5937 else
5938 {
5939 warn (_("The length field (0x%lx) in the debug_rnglists header is wrong - the section is too small\n"),
5940 (long) initial_length);
5941 return 0;
5942 }
5943 }
5944
5945 /* Get and check the version number. */
5946 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
5947
5948 if (version != 5)
5949 {
5950 warn (_("Only DWARF version 5 debug_rnglists info "
5951 "is currently supported.\n"));
5952 return 0;
5953 }
5954
5955 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
5956
5957 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
5958 if (segment_selector_size != 0)
5959 {
5960 warn (_("The %s section contains "
5961 "unsupported segment selector size: %d.\n"),
5962 section->name, segment_selector_size);
5963 return 0;
5964 }
5965
5966 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
5967 if (offset_entry_count != 0)
5968 {
5969 warn (_("The %s section contains "
5970 "unsupported offset entry count: %u.\n"),
5971 section->name, offset_entry_count);
5972 return 0;
5973 }
5974 }
5975
5976 if (load_debug_info (file) == 0)
5977 {
5978 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
5979 section->name);
5980 return 0;
5981 }
5982
5983 num_range_list = 0;
5984 for (i = 0; i < num_debug_info_entries; i++)
5985 num_range_list += debug_information [i].num_range_lists;
5986
5987 if (num_range_list == 0)
5988 {
5989 /* This can happen when the file was compiled with -gsplit-debug
5990 which removes references to range lists from the primary .o file. */
5991 printf (_("No range lists in .debug_info section.\n"));
5992 return 1;
5993 }
5994
5995 range_entries = (struct range_entry *)
5996 xmalloc (sizeof (*range_entries) * num_range_list);
5997 range_entry_fill = range_entries;
5998
5999 for (i = 0; i < num_debug_info_entries; i++)
6000 {
6001 debug_info *debug_info_p = &debug_information[i];
6002 unsigned int j;
6003
6004 for (j = 0; j < debug_info_p->num_range_lists; j++)
6005 {
6006 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
6007 range_entry_fill->debug_info_p = debug_info_p;
6008 range_entry_fill++;
6009 }
6010 }
6011
6012 qsort (range_entries, num_range_list, sizeof (*range_entries),
6013 range_entry_compar);
6014
6015 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
6016 warn (_("Range lists in %s section start at 0x%lx\n"),
6017 section->name, (unsigned long) range_entries[0].ranges_offset);
6018
6019 printf (_("Contents of the %s section:\n\n"), section->name);
6020 printf (_(" Offset Begin End\n"));
6021
6022 for (i = 0; i < num_range_list; i++)
6023 {
6024 struct range_entry *range_entry = &range_entries[i];
6025 debug_info *debug_info_p = range_entry->debug_info_p;
6026 unsigned int pointer_size;
6027 dwarf_vma offset;
6028 unsigned char *next;
6029 dwarf_vma base_address;
6030
6031 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
6032 offset = range_entry->ranges_offset;
6033 next = section_begin + offset;
6034 base_address = debug_info_p->base_address;
6035
6036 /* PR 17512: file: 001-101485-0.001:0.1. */
6037 if (pointer_size < 2 || pointer_size > 8)
6038 {
6039 warn (_("Corrupt pointer size (%d) in debug entry at offset %8.8lx\n"),
6040 pointer_size, (unsigned long) offset);
6041 continue;
6042 }
6043
6044 if (dwarf_check != 0 && i > 0)
6045 {
6046 if (start < next)
6047 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
6048 (unsigned long) (start - section_begin),
6049 (unsigned long) (next - section_begin), section->name);
6050 else if (start > next)
6051 {
6052 if (next == last_start)
6053 continue;
6054 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
6055 (unsigned long) (start - section_begin),
6056 (unsigned long) (next - section_begin), section->name);
6057 }
6058 }
6059 start = next;
6060 last_start = next;
6061
6062 (is_rnglists ? display_debug_rnglists_list : display_debug_ranges_list)
6063 (start, finish, pointer_size, offset, base_address);
6064 }
6065 putchar ('\n');
6066
6067 free (range_entries);
6068
6069 return 1;
6070 }
6071
6072 typedef struct Frame_Chunk
6073 {
6074 struct Frame_Chunk *next;
6075 unsigned char *chunk_start;
6076 unsigned int ncols;
6077 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
6078 short int *col_type;
6079 int *col_offset;
6080 char *augmentation;
6081 unsigned int code_factor;
6082 int data_factor;
6083 dwarf_vma pc_begin;
6084 dwarf_vma pc_range;
6085 int cfa_reg;
6086 dwarf_vma cfa_offset;
6087 unsigned int ra;
6088 unsigned char fde_encoding;
6089 unsigned char cfa_exp;
6090 unsigned char ptr_size;
6091 unsigned char segment_size;
6092 }
6093 Frame_Chunk;
6094
6095 static const char *const *dwarf_regnames;
6096 static unsigned int dwarf_regnames_count;
6097
6098 /* A marker for a col_type that means this column was never referenced
6099 in the frame info. */
6100 #define DW_CFA_unreferenced (-1)
6101
6102 /* Return 0 if no more space is needed, 1 if more space is needed,
6103 -1 for invalid reg. */
6104
6105 static int
6106 frame_need_space (Frame_Chunk *fc, unsigned int reg)
6107 {
6108 unsigned int prev = fc->ncols;
6109
6110 if (reg < (unsigned int) fc->ncols)
6111 return 0;
6112
6113 if (dwarf_regnames_count
6114 && reg > dwarf_regnames_count)
6115 return -1;
6116
6117 fc->ncols = reg + 1;
6118 /* PR 17512: file: 10450-2643-0.004.
6119 If reg == -1 then this can happen... */
6120 if (fc->ncols == 0)
6121 return -1;
6122
6123 /* PR 17512: file: 2844a11d. */
6124 if (fc->ncols > 1024)
6125 {
6126 error (_("Unfeasibly large register number: %u\n"), reg);
6127 fc->ncols = 0;
6128 /* FIXME: 1024 is an arbitrary limit. Increase it if
6129 we ever encounter a valid binary that exceeds it. */
6130 return -1;
6131 }
6132
6133 fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
6134 sizeof (short int));
6135 fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
6136 /* PR 17512: file:002-10025-0.005. */
6137 if (fc->col_type == NULL || fc->col_offset == NULL)
6138 {
6139 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
6140 fc->ncols);
6141 fc->ncols = 0;
6142 return -1;
6143 }
6144
6145 while (prev < fc->ncols)
6146 {
6147 fc->col_type[prev] = DW_CFA_unreferenced;
6148 fc->col_offset[prev] = 0;
6149 prev++;
6150 }
6151 return 1;
6152 }
6153
6154 static const char *const dwarf_regnames_i386[] =
6155 {
6156 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
6157 "esp", "ebp", "esi", "edi", /* 4 - 7 */
6158 "eip", "eflags", NULL, /* 8 - 10 */
6159 "st0", "st1", "st2", "st3", /* 11 - 14 */
6160 "st4", "st5", "st6", "st7", /* 15 - 18 */
6161 NULL, NULL, /* 19 - 20 */
6162 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
6163 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
6164 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
6165 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
6166 "fcw", "fsw", "mxcsr", /* 37 - 39 */
6167 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
6168 "tr", "ldtr", /* 48 - 49 */
6169 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
6170 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
6171 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
6172 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
6173 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
6174 NULL, NULL, NULL, /* 90 - 92 */
6175 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
6176 };
6177
6178 static const char *const dwarf_regnames_iamcu[] =
6179 {
6180 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
6181 "esp", "ebp", "esi", "edi", /* 4 - 7 */
6182 "eip", "eflags", NULL, /* 8 - 10 */
6183 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
6184 NULL, NULL, /* 19 - 20 */
6185 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
6186 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
6187 NULL, NULL, NULL, /* 37 - 39 */
6188 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
6189 "tr", "ldtr", /* 48 - 49 */
6190 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
6191 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
6192 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
6193 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
6194 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
6195 NULL, NULL, NULL, /* 90 - 92 */
6196 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
6197 };
6198
6199 void
6200 init_dwarf_regnames_i386 (void)
6201 {
6202 dwarf_regnames = dwarf_regnames_i386;
6203 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
6204 }
6205
6206 void
6207 init_dwarf_regnames_iamcu (void)
6208 {
6209 dwarf_regnames = dwarf_regnames_iamcu;
6210 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
6211 }
6212
6213 static const char *const dwarf_regnames_x86_64[] =
6214 {
6215 "rax", "rdx", "rcx", "rbx",
6216 "rsi", "rdi", "rbp", "rsp",
6217 "r8", "r9", "r10", "r11",
6218 "r12", "r13", "r14", "r15",
6219 "rip",
6220 "xmm0", "xmm1", "xmm2", "xmm3",
6221 "xmm4", "xmm5", "xmm6", "xmm7",
6222 "xmm8", "xmm9", "xmm10", "xmm11",
6223 "xmm12", "xmm13", "xmm14", "xmm15",
6224 "st0", "st1", "st2", "st3",
6225 "st4", "st5", "st6", "st7",
6226 "mm0", "mm1", "mm2", "mm3",
6227 "mm4", "mm5", "mm6", "mm7",
6228 "rflags",
6229 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
6230 "fs.base", "gs.base", NULL, NULL,
6231 "tr", "ldtr",
6232 "mxcsr", "fcw", "fsw",
6233 "xmm16", "xmm17", "xmm18", "xmm19",
6234 "xmm20", "xmm21", "xmm22", "xmm23",
6235 "xmm24", "xmm25", "xmm26", "xmm27",
6236 "xmm28", "xmm29", "xmm30", "xmm31",
6237 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
6238 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
6239 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
6240 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
6241 NULL, NULL, NULL, /* 115 - 117 */
6242 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
6243 };
6244
6245 void
6246 init_dwarf_regnames_x86_64 (void)
6247 {
6248 dwarf_regnames = dwarf_regnames_x86_64;
6249 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
6250 }
6251
6252 static const char *const dwarf_regnames_aarch64[] =
6253 {
6254 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
6255 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
6256 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
6257 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
6258 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
6259 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
6260 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
6261 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
6262 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
6263 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
6264 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
6265 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
6266 };
6267
6268 void
6269 init_dwarf_regnames_aarch64 (void)
6270 {
6271 dwarf_regnames = dwarf_regnames_aarch64;
6272 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
6273 }
6274
6275 static const char *const dwarf_regnames_s390[] =
6276 {
6277 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
6278 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
6279 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
6280 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
6281 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
6282 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
6283 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
6284 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
6285 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
6286 "pswm", "pswa",
6287 NULL, NULL,
6288 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
6289 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
6290 };
6291
6292 void
6293 init_dwarf_regnames_s390 (void)
6294 {
6295 dwarf_regnames = dwarf_regnames_s390;
6296 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
6297 }
6298
6299 void
6300 init_dwarf_regnames (unsigned int e_machine)
6301 {
6302 switch (e_machine)
6303 {
6304 case EM_386:
6305 init_dwarf_regnames_i386 ();
6306 break;
6307
6308 case EM_IAMCU:
6309 init_dwarf_regnames_iamcu ();
6310 break;
6311
6312 case EM_X86_64:
6313 case EM_L1OM:
6314 case EM_K1OM:
6315 init_dwarf_regnames_x86_64 ();
6316 break;
6317
6318 case EM_AARCH64:
6319 init_dwarf_regnames_aarch64 ();
6320 break;
6321
6322 case EM_S390:
6323 init_dwarf_regnames_s390 ();
6324 break;
6325
6326 default:
6327 break;
6328 }
6329 }
6330
6331 static const char *
6332 regname (unsigned int regno, int row)
6333 {
6334 static char reg[64];
6335 if (dwarf_regnames
6336 && regno < dwarf_regnames_count
6337 && dwarf_regnames [regno] != NULL)
6338 {
6339 if (row)
6340 return dwarf_regnames [regno];
6341 snprintf (reg, sizeof (reg), "r%d (%s)", regno,
6342 dwarf_regnames [regno]);
6343 }
6344 else
6345 snprintf (reg, sizeof (reg), "r%d", regno);
6346 return reg;
6347 }
6348
6349 static void
6350 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
6351 {
6352 unsigned int r;
6353 char tmp[100];
6354
6355 if (*max_regs != fc->ncols)
6356 *max_regs = fc->ncols;
6357
6358 if (*need_col_headers)
6359 {
6360 static const char *sloc = " LOC";
6361
6362 *need_col_headers = 0;
6363
6364 printf ("%-*s CFA ", eh_addr_size * 2, sloc);
6365
6366 for (r = 0; r < *max_regs; r++)
6367 if (fc->col_type[r] != DW_CFA_unreferenced)
6368 {
6369 if (r == fc->ra)
6370 printf ("ra ");
6371 else
6372 printf ("%-5s ", regname (r, 1));
6373 }
6374
6375 printf ("\n");
6376 }
6377
6378 print_dwarf_vma (fc->pc_begin, eh_addr_size);
6379 if (fc->cfa_exp)
6380 strcpy (tmp, "exp");
6381 else
6382 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
6383 printf ("%-8s ", tmp);
6384
6385 for (r = 0; r < fc->ncols; r++)
6386 {
6387 if (fc->col_type[r] != DW_CFA_unreferenced)
6388 {
6389 switch (fc->col_type[r])
6390 {
6391 case DW_CFA_undefined:
6392 strcpy (tmp, "u");
6393 break;
6394 case DW_CFA_same_value:
6395 strcpy (tmp, "s");
6396 break;
6397 case DW_CFA_offset:
6398 sprintf (tmp, "c%+d", fc->col_offset[r]);
6399 break;
6400 case DW_CFA_val_offset:
6401 sprintf (tmp, "v%+d", fc->col_offset[r]);
6402 break;
6403 case DW_CFA_register:
6404 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
6405 break;
6406 case DW_CFA_expression:
6407 strcpy (tmp, "exp");
6408 break;
6409 case DW_CFA_val_expression:
6410 strcpy (tmp, "vexp");
6411 break;
6412 default:
6413 strcpy (tmp, "n/a");
6414 break;
6415 }
6416 printf ("%-5s ", tmp);
6417 }
6418 }
6419 printf ("\n");
6420 }
6421
6422 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
6423 #define LEB() read_uleb128 (start, & length_return, end); start += length_return
6424 #define SLEB() read_sleb128 (start, & length_return, end); start += length_return
6425
6426 static unsigned char *
6427 read_cie (unsigned char *start, unsigned char *end,
6428 Frame_Chunk **p_cie, int *p_version,
6429 unsigned long *p_aug_len, unsigned char **p_aug)
6430 {
6431 int version;
6432 Frame_Chunk *fc;
6433 unsigned int length_return;
6434 unsigned char *augmentation_data = NULL;
6435 unsigned long augmentation_data_len = 0;
6436
6437 * p_cie = NULL;
6438 /* PR 17512: file: 001-228113-0.004. */
6439 if (start >= end)
6440 return end;
6441
6442 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
6443 memset (fc, 0, sizeof (Frame_Chunk));
6444
6445 fc->col_type = (short int *) xmalloc (sizeof (short int));
6446 fc->col_offset = (int *) xmalloc (sizeof (int));
6447
6448 version = *start++;
6449
6450 fc->augmentation = (char *) start;
6451 /* PR 17512: file: 001-228113-0.004.
6452 Skip past augmentation name, but avoid running off the end of the data. */
6453 while (start < end)
6454 if (* start ++ == '\0')
6455 break;
6456 if (start == end)
6457 {
6458 warn (_("No terminator for augmentation name\n"));
6459 return start;
6460 }
6461
6462 if (strcmp (fc->augmentation, "eh") == 0)
6463 start += eh_addr_size;
6464
6465 if (version >= 4)
6466 {
6467 GET (fc->ptr_size, 1);
6468 if (fc->ptr_size < 1 || fc->ptr_size > 8)
6469 {
6470 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
6471 return end;
6472 }
6473
6474 GET (fc->segment_size, 1);
6475 /* PR 17512: file: e99d2804. */
6476 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
6477 {
6478 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
6479 return end;
6480 }
6481
6482 eh_addr_size = fc->ptr_size;
6483 }
6484 else
6485 {
6486 fc->ptr_size = eh_addr_size;
6487 fc->segment_size = 0;
6488 }
6489 fc->code_factor = LEB ();
6490 fc->data_factor = SLEB ();
6491 if (version == 1)
6492 {
6493 GET (fc->ra, 1);
6494 }
6495 else
6496 {
6497 fc->ra = LEB ();
6498 }
6499
6500 if (fc->augmentation[0] == 'z')
6501 {
6502 augmentation_data_len = LEB ();
6503 augmentation_data = start;
6504 start += augmentation_data_len;
6505 /* PR 17512: file: 11042-2589-0.004. */
6506 if (start > end)
6507 {
6508 warn (_("Augmentation data too long: 0x%lx\n"), augmentation_data_len);
6509 return end;
6510 }
6511 }
6512
6513 if (augmentation_data_len)
6514 {
6515 unsigned char *p;
6516 unsigned char *q;
6517 unsigned char *qend;
6518
6519 p = (unsigned char *) fc->augmentation + 1;
6520 q = augmentation_data;
6521 qend = q + augmentation_data_len;
6522
6523 /* PR 17531: file: 015adfaa. */
6524 if (qend < q)
6525 {
6526 warn (_("Negative augmentation data length: 0x%lx"), augmentation_data_len);
6527 augmentation_data_len = 0;
6528 }
6529
6530 while (p < end && q < augmentation_data + augmentation_data_len)
6531 {
6532 if (*p == 'L')
6533 q++;
6534 else if (*p == 'P')
6535 q += 1 + size_of_encoded_value (*q);
6536 else if (*p == 'R')
6537 fc->fde_encoding = *q++;
6538 else if (*p == 'S')
6539 ;
6540 else
6541 break;
6542 p++;
6543 }
6544 /* Note - it is OK if this loop terminates with q < qend.
6545 Padding may have been inserted to align the end of the CIE. */
6546 }
6547
6548 *p_cie = fc;
6549 if (p_version)
6550 *p_version = version;
6551 if (p_aug_len)
6552 {
6553 *p_aug_len = augmentation_data_len;
6554 *p_aug = augmentation_data;
6555 }
6556 return start;
6557 }
6558
6559 static int
6560 display_debug_frames (struct dwarf_section *section,
6561 void *file ATTRIBUTE_UNUSED)
6562 {
6563 unsigned char *start = section->start;
6564 unsigned char *end = start + section->size;
6565 unsigned char *section_start = start;
6566 Frame_Chunk *chunks = 0, *forward_refs = 0;
6567 Frame_Chunk *remembered_state = 0;
6568 Frame_Chunk *rs;
6569 int is_eh = strcmp (section->name, ".eh_frame") == 0;
6570 unsigned int length_return;
6571 unsigned int max_regs = 0;
6572 const char *bad_reg = _("bad register: ");
6573 unsigned int saved_eh_addr_size = eh_addr_size;
6574
6575 printf (_("Contents of the %s section:\n"), section->name);
6576
6577 while (start < end)
6578 {
6579 unsigned char *saved_start;
6580 unsigned char *block_end;
6581 dwarf_vma length;
6582 dwarf_vma cie_id;
6583 Frame_Chunk *fc;
6584 Frame_Chunk *cie;
6585 int need_col_headers = 1;
6586 unsigned char *augmentation_data = NULL;
6587 unsigned long augmentation_data_len = 0;
6588 unsigned int encoded_ptr_size = saved_eh_addr_size;
6589 unsigned int offset_size;
6590 unsigned int initial_length_size;
6591 bfd_boolean all_nops;
6592
6593 saved_start = start;
6594
6595 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
6596
6597 if (length == 0)
6598 {
6599 printf ("\n%08lx ZERO terminator\n\n",
6600 (unsigned long)(saved_start - section_start));
6601 /* Skip any zero terminators that directly follow.
6602 A corrupt section size could have loaded a whole
6603 slew of zero filled memory bytes. eg
6604 PR 17512: file: 070-19381-0.004. */
6605 while (start < end && * start == 0)
6606 ++ start;
6607 continue;
6608 }
6609
6610 if (length == 0xffffffff)
6611 {
6612 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
6613 offset_size = 8;
6614 initial_length_size = 12;
6615 }
6616 else
6617 {
6618 offset_size = 4;
6619 initial_length_size = 4;
6620 }
6621
6622 block_end = saved_start + length + initial_length_size;
6623 if (block_end > end || block_end < start)
6624 {
6625 warn ("Invalid length 0x%s in FDE at %#08lx\n",
6626 dwarf_vmatoa_1 (NULL, length, offset_size),
6627 (unsigned long) (saved_start - section_start));
6628 block_end = end;
6629 }
6630
6631 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, end);
6632
6633 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
6634 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
6635 {
6636 int version;
6637 unsigned int mreg;
6638
6639 start = read_cie (start, end, &cie, &version,
6640 &augmentation_data_len, &augmentation_data);
6641 /* PR 17512: file: 027-135133-0.005. */
6642 if (cie == NULL)
6643 break;
6644
6645 fc = cie;
6646 fc->next = chunks;
6647 chunks = fc;
6648 fc->chunk_start = saved_start;
6649 mreg = max_regs > 0 ? max_regs - 1 : 0;
6650 if (mreg < fc->ra)
6651 mreg = fc->ra;
6652 if (frame_need_space (fc, mreg) < 0)
6653 break;
6654 if (fc->fde_encoding)
6655 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
6656
6657 printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
6658 print_dwarf_vma (length, fc->ptr_size);
6659 print_dwarf_vma (cie_id, offset_size);
6660
6661 if (do_debug_frames_interp)
6662 {
6663 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
6664 fc->code_factor, fc->data_factor, fc->ra);
6665 }
6666 else
6667 {
6668 printf ("CIE\n");
6669 printf (" Version: %d\n", version);
6670 printf (" Augmentation: \"%s\"\n", fc->augmentation);
6671 if (version >= 4)
6672 {
6673 printf (" Pointer Size: %u\n", fc->ptr_size);
6674 printf (" Segment Size: %u\n", fc->segment_size);
6675 }
6676 printf (" Code alignment factor: %u\n", fc->code_factor);
6677 printf (" Data alignment factor: %d\n", fc->data_factor);
6678 printf (" Return address column: %d\n", fc->ra);
6679
6680 if (augmentation_data_len)
6681 {
6682 unsigned long i;
6683
6684 printf (" Augmentation data: ");
6685 for (i = 0; i < augmentation_data_len; ++i)
6686 /* FIXME: If do_wide is FALSE, then we should
6687 add carriage returns at 80 columns... */
6688 printf (" %02x", augmentation_data[i]);
6689 putchar ('\n');
6690 }
6691 putchar ('\n');
6692 }
6693 }
6694 else
6695 {
6696 unsigned char *look_for;
6697 static Frame_Chunk fde_fc;
6698 unsigned long segment_selector;
6699
6700 if (is_eh)
6701 {
6702 dwarf_vma sign = (dwarf_vma) 1 << (offset_size * 8 - 1);
6703 look_for = start - 4 - ((cie_id ^ sign) - sign);
6704 }
6705 else
6706 look_for = section_start + cie_id;
6707
6708 if (look_for <= saved_start)
6709 {
6710 for (cie = chunks; cie ; cie = cie->next)
6711 if (cie->chunk_start == look_for)
6712 break;
6713 }
6714 else
6715 {
6716 for (cie = forward_refs; cie ; cie = cie->next)
6717 if (cie->chunk_start == look_for)
6718 break;
6719 if (!cie)
6720 {
6721 unsigned int off_size;
6722 unsigned char *cie_scan;
6723
6724 cie_scan = look_for;
6725 off_size = 4;
6726 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
6727 if (length == 0xffffffff)
6728 {
6729 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
6730 off_size = 8;
6731 }
6732 if (length != 0)
6733 {
6734 dwarf_vma c_id;
6735
6736 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size, end);
6737 if (is_eh
6738 ? c_id == 0
6739 : ((off_size == 4 && c_id == DW_CIE_ID)
6740 || (off_size == 8 && c_id == DW64_CIE_ID)))
6741 {
6742 int version;
6743 unsigned int mreg;
6744
6745 read_cie (cie_scan, end, &cie, &version,
6746 &augmentation_data_len, &augmentation_data);
6747 /* PR 17512: file: 3450-2098-0.004. */
6748 if (cie == NULL)
6749 {
6750 warn (_("Failed to read CIE information\n"));
6751 break;
6752 }
6753 cie->next = forward_refs;
6754 forward_refs = cie;
6755 cie->chunk_start = look_for;
6756 mreg = max_regs > 0 ? max_regs - 1 : 0;
6757 if (mreg < cie->ra)
6758 mreg = cie->ra;
6759 if (frame_need_space (cie, mreg) < 0)
6760 {
6761 warn (_("Invalid max register\n"));
6762 break;
6763 }
6764 if (cie->fde_encoding)
6765 encoded_ptr_size
6766 = size_of_encoded_value (cie->fde_encoding);
6767 }
6768 }
6769 }
6770 }
6771
6772 fc = &fde_fc;
6773 memset (fc, 0, sizeof (Frame_Chunk));
6774
6775 if (!cie)
6776 {
6777 warn ("Invalid CIE pointer 0x%s in FDE at %#08lx\n",
6778 dwarf_vmatoa_1 (NULL, cie_id, offset_size),
6779 (unsigned long) (saved_start - section_start));
6780 fc->ncols = 0;
6781 fc->col_type = (short int *) xmalloc (sizeof (short int));
6782 fc->col_offset = (int *) xmalloc (sizeof (int));
6783 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
6784 {
6785 warn (_("Invalid max register\n"));
6786 break;
6787 }
6788 cie = fc;
6789 fc->augmentation = "";
6790 fc->fde_encoding = 0;
6791 fc->ptr_size = eh_addr_size;
6792 fc->segment_size = 0;
6793 }
6794 else
6795 {
6796 fc->ncols = cie->ncols;
6797 fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
6798 fc->col_offset = (int *) xcmalloc (fc->ncols, sizeof (int));
6799 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
6800 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
6801 fc->augmentation = cie->augmentation;
6802 fc->ptr_size = cie->ptr_size;
6803 eh_addr_size = cie->ptr_size;
6804 fc->segment_size = cie->segment_size;
6805 fc->code_factor = cie->code_factor;
6806 fc->data_factor = cie->data_factor;
6807 fc->cfa_reg = cie->cfa_reg;
6808 fc->cfa_offset = cie->cfa_offset;
6809 fc->ra = cie->ra;
6810 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
6811 {
6812 warn (_("Invalid max register\n"));
6813 break;
6814 }
6815 fc->fde_encoding = cie->fde_encoding;
6816 }
6817
6818 if (fc->fde_encoding)
6819 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
6820
6821 segment_selector = 0;
6822 if (fc->segment_size)
6823 {
6824 if (fc->segment_size > sizeof (segment_selector))
6825 {
6826 /* PR 17512: file: 9e196b3e. */
6827 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
6828 fc->segment_size = 4;
6829 }
6830 SAFE_BYTE_GET_AND_INC (segment_selector, start, fc->segment_size, end);
6831 }
6832
6833 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section, end);
6834
6835 /* FIXME: It appears that sometimes the final pc_range value is
6836 encoded in less than encoded_ptr_size bytes. See the x86_64
6837 run of the "objcopy on compressed debug sections" test for an
6838 example of this. */
6839 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size, end);
6840
6841 if (cie->augmentation[0] == 'z')
6842 {
6843 augmentation_data_len = LEB ();
6844 augmentation_data = start;
6845 start += augmentation_data_len;
6846 /* PR 17512: file: 722-8446-0.004. */
6847 if (start >= end || ((signed long) augmentation_data_len) < 0)
6848 {
6849 warn (_("Corrupt augmentation data length: %lx\n"),
6850 augmentation_data_len);
6851 start = end;
6852 augmentation_data = NULL;
6853 augmentation_data_len = 0;
6854 }
6855 }
6856
6857 printf ("\n%08lx %s %s FDE cie=%08lx pc=",
6858 (unsigned long)(saved_start - section_start),
6859 dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
6860 dwarf_vmatoa_1 (NULL, cie_id, offset_size),
6861 (unsigned long)(cie->chunk_start - section_start));
6862
6863 if (fc->segment_size)
6864 printf ("%04lx:", segment_selector);
6865
6866 printf ("%s..%s\n",
6867 dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
6868 dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));
6869
6870 if (! do_debug_frames_interp && augmentation_data_len)
6871 {
6872 unsigned long i;
6873
6874 printf (" Augmentation data: ");
6875 for (i = 0; i < augmentation_data_len; ++i)
6876 printf (" %02x", augmentation_data[i]);
6877 putchar ('\n');
6878 putchar ('\n');
6879 }
6880 }
6881
6882 /* At this point, fc is the current chunk, cie (if any) is set, and
6883 we're about to interpret instructions for the chunk. */
6884 /* ??? At present we need to do this always, since this sizes the
6885 fc->col_type and fc->col_offset arrays, which we write into always.
6886 We should probably split the interpreted and non-interpreted bits
6887 into two different routines, since there's so much that doesn't
6888 really overlap between them. */
6889 if (1 || do_debug_frames_interp)
6890 {
6891 /* Start by making a pass over the chunk, allocating storage
6892 and taking note of what registers are used. */
6893 unsigned char *tmp = start;
6894
6895 while (start < block_end)
6896 {
6897 unsigned int reg, op, opa;
6898 unsigned long temp;
6899 unsigned char * new_start;
6900
6901 op = *start++;
6902 opa = op & 0x3f;
6903 if (op & 0xc0)
6904 op &= 0xc0;
6905
6906 /* Warning: if you add any more cases to this switch, be
6907 sure to add them to the corresponding switch below. */
6908 switch (op)
6909 {
6910 case DW_CFA_advance_loc:
6911 break;
6912 case DW_CFA_offset:
6913 LEB ();
6914 if (frame_need_space (fc, opa) >= 0)
6915 fc->col_type[opa] = DW_CFA_undefined;
6916 break;
6917 case DW_CFA_restore:
6918 if (frame_need_space (fc, opa) >= 0)
6919 fc->col_type[opa] = DW_CFA_undefined;
6920 break;
6921 case DW_CFA_set_loc:
6922 start += encoded_ptr_size;
6923 break;
6924 case DW_CFA_advance_loc1:
6925 start += 1;
6926 break;
6927 case DW_CFA_advance_loc2:
6928 start += 2;
6929 break;
6930 case DW_CFA_advance_loc4:
6931 start += 4;
6932 break;
6933 case DW_CFA_offset_extended:
6934 case DW_CFA_val_offset:
6935 reg = LEB (); LEB ();
6936 if (frame_need_space (fc, reg) >= 0)
6937 fc->col_type[reg] = DW_CFA_undefined;
6938 break;
6939 case DW_CFA_restore_extended:
6940 reg = LEB ();
6941 if (frame_need_space (fc, reg) >= 0)
6942 fc->col_type[reg] = DW_CFA_undefined;
6943 break;
6944 case DW_CFA_undefined:
6945 reg = LEB ();
6946 if (frame_need_space (fc, reg) >= 0)
6947 fc->col_type[reg] = DW_CFA_undefined;
6948 break;
6949 case DW_CFA_same_value:
6950 reg = LEB ();
6951 if (frame_need_space (fc, reg) >= 0)
6952 fc->col_type[reg] = DW_CFA_undefined;
6953 break;
6954 case DW_CFA_register:
6955 reg = LEB (); LEB ();
6956 if (frame_need_space (fc, reg) >= 0)
6957 fc->col_type[reg] = DW_CFA_undefined;
6958 break;
6959 case DW_CFA_def_cfa:
6960 LEB (); LEB ();
6961 break;
6962 case DW_CFA_def_cfa_register:
6963 LEB ();
6964 break;
6965 case DW_CFA_def_cfa_offset:
6966 LEB ();
6967 break;
6968 case DW_CFA_def_cfa_expression:
6969 temp = LEB ();
6970 new_start = start + temp;
6971 if (new_start < start)
6972 {
6973 warn (_("Corrupt CFA_def expression value: %lu\n"), temp);
6974 start = block_end;
6975 }
6976 else
6977 start = new_start;
6978 break;
6979 case DW_CFA_expression:
6980 case DW_CFA_val_expression:
6981 reg = LEB ();
6982 temp = LEB ();
6983 new_start = start + temp;
6984 if (new_start < start)
6985 {
6986 /* PR 17512: file:306-192417-0.005. */
6987 warn (_("Corrupt CFA expression value: %lu\n"), temp);
6988 start = block_end;
6989 }
6990 else
6991 start = new_start;
6992 if (frame_need_space (fc, reg) >= 0)
6993 fc->col_type[reg] = DW_CFA_undefined;
6994 break;
6995 case DW_CFA_offset_extended_sf:
6996 case DW_CFA_val_offset_sf:
6997 reg = LEB (); SLEB ();
6998 if (frame_need_space (fc, reg) >= 0)
6999 fc->col_type[reg] = DW_CFA_undefined;
7000 break;
7001 case DW_CFA_def_cfa_sf:
7002 LEB (); SLEB ();
7003 break;
7004 case DW_CFA_def_cfa_offset_sf:
7005 SLEB ();
7006 break;
7007 case DW_CFA_MIPS_advance_loc8:
7008 start += 8;
7009 break;
7010 case DW_CFA_GNU_args_size:
7011 LEB ();
7012 break;
7013 case DW_CFA_GNU_negative_offset_extended:
7014 reg = LEB (); LEB ();
7015 if (frame_need_space (fc, reg) >= 0)
7016 fc->col_type[reg] = DW_CFA_undefined;
7017 break;
7018 default:
7019 break;
7020 }
7021 }
7022 start = tmp;
7023 }
7024
7025 all_nops = TRUE;
7026
7027 /* Now we know what registers are used, make a second pass over
7028 the chunk, this time actually printing out the info. */
7029
7030 while (start < block_end)
7031 {
7032 unsigned char * tmp;
7033 unsigned op, opa;
7034 unsigned long ul, reg, roffs;
7035 dwarf_vma l;
7036 dwarf_vma ofs;
7037 dwarf_vma vma;
7038 const char *reg_prefix = "";
7039
7040 op = *start++;
7041 opa = op & 0x3f;
7042 if (op & 0xc0)
7043 op &= 0xc0;
7044
7045 /* Make a note if something other than DW_CFA_nop happens. */
7046 if (op != DW_CFA_nop)
7047 all_nops = FALSE;
7048
7049 /* Warning: if you add any more cases to this switch, be
7050 sure to add them to the corresponding switch above. */
7051 switch (op)
7052 {
7053 case DW_CFA_advance_loc:
7054 if (do_debug_frames_interp)
7055 frame_display_row (fc, &need_col_headers, &max_regs);
7056 else
7057 printf (" DW_CFA_advance_loc: %d to %s\n",
7058 opa * fc->code_factor,
7059 dwarf_vmatoa_1 (NULL,
7060 fc->pc_begin + opa * fc->code_factor,
7061 fc->ptr_size));
7062 fc->pc_begin += opa * fc->code_factor;
7063 break;
7064
7065 case DW_CFA_offset:
7066 roffs = LEB ();
7067 if (opa >= (unsigned int) fc->ncols)
7068 reg_prefix = bad_reg;
7069 if (! do_debug_frames_interp || *reg_prefix != '\0')
7070 printf (" DW_CFA_offset: %s%s at cfa%+ld\n",
7071 reg_prefix, regname (opa, 0),
7072 roffs * fc->data_factor);
7073 if (*reg_prefix == '\0')
7074 {
7075 fc->col_type[opa] = DW_CFA_offset;
7076 fc->col_offset[opa] = roffs * fc->data_factor;
7077 }
7078 break;
7079
7080 case DW_CFA_restore:
7081 if (opa >= (unsigned int) fc->ncols)
7082 reg_prefix = bad_reg;
7083 if (! do_debug_frames_interp || *reg_prefix != '\0')
7084 printf (" DW_CFA_restore: %s%s\n",
7085 reg_prefix, regname (opa, 0));
7086 if (*reg_prefix != '\0')
7087 break;
7088
7089 if (opa >= (unsigned int) cie->ncols
7090 || (do_debug_frames_interp
7091 && cie->col_type[opa] == DW_CFA_unreferenced))
7092 {
7093 fc->col_type[opa] = DW_CFA_undefined;
7094 fc->col_offset[opa] = 0;
7095 }
7096 else
7097 {
7098 fc->col_type[opa] = cie->col_type[opa];
7099 fc->col_offset[opa] = cie->col_offset[opa];
7100 }
7101 break;
7102
7103 case DW_CFA_set_loc:
7104 vma = get_encoded_value (&start, fc->fde_encoding, section, block_end);
7105 if (do_debug_frames_interp)
7106 frame_display_row (fc, &need_col_headers, &max_regs);
7107 else
7108 printf (" DW_CFA_set_loc: %s\n",
7109 dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
7110 fc->pc_begin = vma;
7111 break;
7112
7113 case DW_CFA_advance_loc1:
7114 SAFE_BYTE_GET_AND_INC (ofs, start, 1, end);
7115 if (do_debug_frames_interp)
7116 frame_display_row (fc, &need_col_headers, &max_regs);
7117 else
7118 printf (" DW_CFA_advance_loc1: %ld to %s\n",
7119 (unsigned long) (ofs * fc->code_factor),
7120 dwarf_vmatoa_1 (NULL,
7121 fc->pc_begin + ofs * fc->code_factor,
7122 fc->ptr_size));
7123 fc->pc_begin += ofs * fc->code_factor;
7124 break;
7125
7126 case DW_CFA_advance_loc2:
7127 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
7128 if (do_debug_frames_interp)
7129 frame_display_row (fc, &need_col_headers, &max_regs);
7130 else
7131 printf (" DW_CFA_advance_loc2: %ld to %s\n",
7132 (unsigned long) (ofs * fc->code_factor),
7133 dwarf_vmatoa_1 (NULL,
7134 fc->pc_begin + ofs * fc->code_factor,
7135 fc->ptr_size));
7136 fc->pc_begin += ofs * fc->code_factor;
7137 break;
7138
7139 case DW_CFA_advance_loc4:
7140 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
7141 if (do_debug_frames_interp)
7142 frame_display_row (fc, &need_col_headers, &max_regs);
7143 else
7144 printf (" DW_CFA_advance_loc4: %ld to %s\n",
7145 (unsigned long) (ofs * fc->code_factor),
7146 dwarf_vmatoa_1 (NULL,
7147 fc->pc_begin + ofs * fc->code_factor,
7148 fc->ptr_size));
7149 fc->pc_begin += ofs * fc->code_factor;
7150 break;
7151
7152 case DW_CFA_offset_extended:
7153 reg = LEB ();
7154 roffs = LEB ();
7155 if (reg >= (unsigned int) fc->ncols)
7156 reg_prefix = bad_reg;
7157 if (! do_debug_frames_interp || *reg_prefix != '\0')
7158 printf (" DW_CFA_offset_extended: %s%s at cfa%+ld\n",
7159 reg_prefix, regname (reg, 0),
7160 roffs * fc->data_factor);
7161 if (*reg_prefix == '\0')
7162 {
7163 fc->col_type[reg] = DW_CFA_offset;
7164 fc->col_offset[reg] = roffs * fc->data_factor;
7165 }
7166 break;
7167
7168 case DW_CFA_val_offset:
7169 reg = LEB ();
7170 roffs = LEB ();
7171 if (reg >= (unsigned int) fc->ncols)
7172 reg_prefix = bad_reg;
7173 if (! do_debug_frames_interp || *reg_prefix != '\0')
7174 printf (" DW_CFA_val_offset: %s%s is cfa%+ld\n",
7175 reg_prefix, regname (reg, 0),
7176 roffs * fc->data_factor);
7177 if (*reg_prefix == '\0')
7178 {
7179 fc->col_type[reg] = DW_CFA_val_offset;
7180 fc->col_offset[reg] = roffs * fc->data_factor;
7181 }
7182 break;
7183
7184 case DW_CFA_restore_extended:
7185 reg = LEB ();
7186 if (reg >= (unsigned int) fc->ncols)
7187 reg_prefix = bad_reg;
7188 if (! do_debug_frames_interp || *reg_prefix != '\0')
7189 printf (" DW_CFA_restore_extended: %s%s\n",
7190 reg_prefix, regname (reg, 0));
7191 if (*reg_prefix != '\0')
7192 break;
7193
7194 if (reg >= (unsigned int) cie->ncols)
7195 {
7196 fc->col_type[reg] = DW_CFA_undefined;
7197 fc->col_offset[reg] = 0;
7198 }
7199 else
7200 {
7201 fc->col_type[reg] = cie->col_type[reg];
7202 fc->col_offset[reg] = cie->col_offset[reg];
7203 }
7204 break;
7205
7206 case DW_CFA_undefined:
7207 reg = LEB ();
7208 if (reg >= (unsigned int) fc->ncols)
7209 reg_prefix = bad_reg;
7210 if (! do_debug_frames_interp || *reg_prefix != '\0')
7211 printf (" DW_CFA_undefined: %s%s\n",
7212 reg_prefix, regname (reg, 0));
7213 if (*reg_prefix == '\0')
7214 {
7215 fc->col_type[reg] = DW_CFA_undefined;
7216 fc->col_offset[reg] = 0;
7217 }
7218 break;
7219
7220 case DW_CFA_same_value:
7221 reg = LEB ();
7222 if (reg >= (unsigned int) fc->ncols)
7223 reg_prefix = bad_reg;
7224 if (! do_debug_frames_interp || *reg_prefix != '\0')
7225 printf (" DW_CFA_same_value: %s%s\n",
7226 reg_prefix, regname (reg, 0));
7227 if (*reg_prefix == '\0')
7228 {
7229 fc->col_type[reg] = DW_CFA_same_value;
7230 fc->col_offset[reg] = 0;
7231 }
7232 break;
7233
7234 case DW_CFA_register:
7235 reg = LEB ();
7236 roffs = LEB ();
7237 if (reg >= (unsigned int) fc->ncols)
7238 reg_prefix = bad_reg;
7239 if (! do_debug_frames_interp || *reg_prefix != '\0')
7240 {
7241 printf (" DW_CFA_register: %s%s in ",
7242 reg_prefix, regname (reg, 0));
7243 puts (regname (roffs, 0));
7244 }
7245 if (*reg_prefix == '\0')
7246 {
7247 fc->col_type[reg] = DW_CFA_register;
7248 fc->col_offset[reg] = roffs;
7249 }
7250 break;
7251
7252 case DW_CFA_remember_state:
7253 if (! do_debug_frames_interp)
7254 printf (" DW_CFA_remember_state\n");
7255 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
7256 rs->cfa_offset = fc->cfa_offset;
7257 rs->cfa_reg = fc->cfa_reg;
7258 rs->ra = fc->ra;
7259 rs->cfa_exp = fc->cfa_exp;
7260 rs->ncols = fc->ncols;
7261 rs->col_type = (short int *) xcmalloc (rs->ncols,
7262 sizeof (* rs->col_type));
7263 rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
7264 memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
7265 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
7266 rs->next = remembered_state;
7267 remembered_state = rs;
7268 break;
7269
7270 case DW_CFA_restore_state:
7271 if (! do_debug_frames_interp)
7272 printf (" DW_CFA_restore_state\n");
7273 rs = remembered_state;
7274 if (rs)
7275 {
7276 remembered_state = rs->next;
7277 fc->cfa_offset = rs->cfa_offset;
7278 fc->cfa_reg = rs->cfa_reg;
7279 fc->ra = rs->ra;
7280 fc->cfa_exp = rs->cfa_exp;
7281 if (frame_need_space (fc, rs->ncols - 1) < 0)
7282 {
7283 warn (_("Invalid column number in saved frame state\n"));
7284 fc->ncols = 0;
7285 break;
7286 }
7287 memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
7288 memcpy (fc->col_offset, rs->col_offset,
7289 rs->ncols * sizeof (* rs->col_offset));
7290 free (rs->col_type);
7291 free (rs->col_offset);
7292 free (rs);
7293 }
7294 else if (do_debug_frames_interp)
7295 printf ("Mismatched DW_CFA_restore_state\n");
7296 break;
7297
7298 case DW_CFA_def_cfa:
7299 fc->cfa_reg = LEB ();
7300 fc->cfa_offset = LEB ();
7301 fc->cfa_exp = 0;
7302 if (! do_debug_frames_interp)
7303 printf (" DW_CFA_def_cfa: %s ofs %d\n",
7304 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
7305 break;
7306
7307 case DW_CFA_def_cfa_register:
7308 fc->cfa_reg = LEB ();
7309 fc->cfa_exp = 0;
7310 if (! do_debug_frames_interp)
7311 printf (" DW_CFA_def_cfa_register: %s\n",
7312 regname (fc->cfa_reg, 0));
7313 break;
7314
7315 case DW_CFA_def_cfa_offset:
7316 fc->cfa_offset = LEB ();
7317 if (! do_debug_frames_interp)
7318 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
7319 break;
7320
7321 case DW_CFA_nop:
7322 if (! do_debug_frames_interp)
7323 printf (" DW_CFA_nop\n");
7324 break;
7325
7326 case DW_CFA_def_cfa_expression:
7327 ul = LEB ();
7328 if (start >= block_end || ul > (unsigned long) (block_end - start))
7329 {
7330 printf (_(" DW_CFA_def_cfa_expression: <corrupt len %lu>\n"), ul);
7331 break;
7332 }
7333 if (! do_debug_frames_interp)
7334 {
7335 printf (" DW_CFA_def_cfa_expression (");
7336 decode_location_expression (start, eh_addr_size, 0, -1,
7337 ul, 0, section);
7338 printf (")\n");
7339 }
7340 fc->cfa_exp = 1;
7341 start += ul;
7342 break;
7343
7344 case DW_CFA_expression:
7345 reg = LEB ();
7346 ul = LEB ();
7347 if (reg >= (unsigned int) fc->ncols)
7348 reg_prefix = bad_reg;
7349 /* PR 17512: file: 069-133014-0.006. */
7350 /* PR 17512: file: 98c02eb4. */
7351 tmp = start + ul;
7352 if (start >= block_end || tmp > block_end || tmp < start)
7353 {
7354 printf (_(" DW_CFA_expression: <corrupt len %lu>\n"), ul);
7355 break;
7356 }
7357 if (! do_debug_frames_interp || *reg_prefix != '\0')
7358 {
7359 printf (" DW_CFA_expression: %s%s (",
7360 reg_prefix, regname (reg, 0));
7361 decode_location_expression (start, eh_addr_size, 0, -1,
7362 ul, 0, section);
7363 printf (")\n");
7364 }
7365 if (*reg_prefix == '\0')
7366 fc->col_type[reg] = DW_CFA_expression;
7367 start = tmp;
7368 break;
7369
7370 case DW_CFA_val_expression:
7371 reg = LEB ();
7372 ul = LEB ();
7373 if (reg >= (unsigned int) fc->ncols)
7374 reg_prefix = bad_reg;
7375 tmp = start + ul;
7376 if (start >= block_end || tmp > block_end || tmp < start)
7377 {
7378 printf (" DW_CFA_val_expression: <corrupt len %lu>\n", ul);
7379 break;
7380 }
7381 if (! do_debug_frames_interp || *reg_prefix != '\0')
7382 {
7383 printf (" DW_CFA_val_expression: %s%s (",
7384 reg_prefix, regname (reg, 0));
7385 decode_location_expression (start, eh_addr_size, 0, -1,
7386 ul, 0, section);
7387 printf (")\n");
7388 }
7389 if (*reg_prefix == '\0')
7390 fc->col_type[reg] = DW_CFA_val_expression;
7391 start = tmp;
7392 break;
7393
7394 case DW_CFA_offset_extended_sf:
7395 reg = LEB ();
7396 l = SLEB ();
7397 if (frame_need_space (fc, reg) < 0)
7398 reg_prefix = bad_reg;
7399 if (! do_debug_frames_interp || *reg_prefix != '\0')
7400 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
7401 reg_prefix, regname (reg, 0),
7402 (long)(l * fc->data_factor));
7403 if (*reg_prefix == '\0')
7404 {
7405 fc->col_type[reg] = DW_CFA_offset;
7406 fc->col_offset[reg] = l * fc->data_factor;
7407 }
7408 break;
7409
7410 case DW_CFA_val_offset_sf:
7411 reg = LEB ();
7412 l = SLEB ();
7413 if (frame_need_space (fc, reg) < 0)
7414 reg_prefix = bad_reg;
7415 if (! do_debug_frames_interp || *reg_prefix != '\0')
7416 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+ld\n",
7417 reg_prefix, regname (reg, 0),
7418 (long)(l * fc->data_factor));
7419 if (*reg_prefix == '\0')
7420 {
7421 fc->col_type[reg] = DW_CFA_val_offset;
7422 fc->col_offset[reg] = l * fc->data_factor;
7423 }
7424 break;
7425
7426 case DW_CFA_def_cfa_sf:
7427 fc->cfa_reg = LEB ();
7428 fc->cfa_offset = SLEB ();
7429 fc->cfa_offset = fc->cfa_offset * fc->data_factor;
7430 fc->cfa_exp = 0;
7431 if (! do_debug_frames_interp)
7432 printf (" DW_CFA_def_cfa_sf: %s ofs %d\n",
7433 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
7434 break;
7435
7436 case DW_CFA_def_cfa_offset_sf:
7437 fc->cfa_offset = SLEB ();
7438 fc->cfa_offset *= fc->data_factor;
7439 if (! do_debug_frames_interp)
7440 printf (" DW_CFA_def_cfa_offset_sf: %d\n", (int) fc->cfa_offset);
7441 break;
7442
7443 case DW_CFA_MIPS_advance_loc8:
7444 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
7445 if (do_debug_frames_interp)
7446 frame_display_row (fc, &need_col_headers, &max_regs);
7447 else
7448 printf (" DW_CFA_MIPS_advance_loc8: %ld to %s\n",
7449 (unsigned long) (ofs * fc->code_factor),
7450 dwarf_vmatoa_1 (NULL,
7451 fc->pc_begin + ofs * fc->code_factor,
7452 fc->ptr_size));
7453 fc->pc_begin += ofs * fc->code_factor;
7454 break;
7455
7456 case DW_CFA_GNU_window_save:
7457 if (! do_debug_frames_interp)
7458 printf (" DW_CFA_GNU_window_save\n");
7459 break;
7460
7461 case DW_CFA_GNU_args_size:
7462 ul = LEB ();
7463 if (! do_debug_frames_interp)
7464 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
7465 break;
7466
7467 case DW_CFA_GNU_negative_offset_extended:
7468 reg = LEB ();
7469 l = - LEB ();
7470 if (frame_need_space (fc, reg) < 0)
7471 reg_prefix = bad_reg;
7472 if (! do_debug_frames_interp || *reg_prefix != '\0')
7473 printf (" DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
7474 reg_prefix, regname (reg, 0),
7475 (long)(l * fc->data_factor));
7476 if (*reg_prefix == '\0')
7477 {
7478 fc->col_type[reg] = DW_CFA_offset;
7479 fc->col_offset[reg] = l * fc->data_factor;
7480 }
7481 break;
7482
7483 default:
7484 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
7485 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
7486 else
7487 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
7488 start = block_end;
7489 }
7490 }
7491
7492 /* Interpret the CFA - as long as it is not completely full of NOPs. */
7493 if (do_debug_frames_interp && ! all_nops)
7494 frame_display_row (fc, &need_col_headers, &max_regs);
7495
7496 start = block_end;
7497 eh_addr_size = saved_eh_addr_size;
7498 }
7499
7500 printf ("\n");
7501
7502 return 1;
7503 }
7504
7505 #undef GET
7506 #undef LEB
7507 #undef SLEB
7508
7509 static int
7510 display_gdb_index (struct dwarf_section *section,
7511 void *file ATTRIBUTE_UNUSED)
7512 {
7513 unsigned char *start = section->start;
7514 uint32_t version;
7515 uint32_t cu_list_offset, tu_list_offset;
7516 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
7517 unsigned int cu_list_elements, tu_list_elements;
7518 unsigned int address_table_size, symbol_table_slots;
7519 unsigned char *cu_list, *tu_list;
7520 unsigned char *address_table, *symbol_table, *constant_pool;
7521 unsigned int i;
7522
7523 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
7524
7525 printf (_("Contents of the %s section:\n"), section->name);
7526
7527 if (section->size < 6 * sizeof (uint32_t))
7528 {
7529 warn (_("Truncated header in the %s section.\n"), section->name);
7530 return 0;
7531 }
7532
7533 version = byte_get_little_endian (start, 4);
7534 printf (_("Version %ld\n"), (long) version);
7535
7536 /* Prior versions are obsolete, and future versions may not be
7537 backwards compatible. */
7538 if (version < 3 || version > 8)
7539 {
7540 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
7541 return 0;
7542 }
7543 if (version < 4)
7544 warn (_("The address table data in version 3 may be wrong.\n"));
7545 if (version < 5)
7546 warn (_("Version 4 does not support case insensitive lookups.\n"));
7547 if (version < 6)
7548 warn (_("Version 5 does not include inlined functions.\n"));
7549 if (version < 7)
7550 warn (_("Version 6 does not include symbol attributes.\n"));
7551 /* Version 7 indices generated by Gold have bad type unit references,
7552 PR binutils/15021. But we don't know if the index was generated by
7553 Gold or not, so to avoid worrying users with gdb-generated indices
7554 we say nothing for version 7 here. */
7555
7556 cu_list_offset = byte_get_little_endian (start + 4, 4);
7557 tu_list_offset = byte_get_little_endian (start + 8, 4);
7558 address_table_offset = byte_get_little_endian (start + 12, 4);
7559 symbol_table_offset = byte_get_little_endian (start + 16, 4);
7560 constant_pool_offset = byte_get_little_endian (start + 20, 4);
7561
7562 if (cu_list_offset > section->size
7563 || tu_list_offset > section->size
7564 || address_table_offset > section->size
7565 || symbol_table_offset > section->size
7566 || constant_pool_offset > section->size)
7567 {
7568 warn (_("Corrupt header in the %s section.\n"), section->name);
7569 return 0;
7570 }
7571
7572 /* PR 17531: file: 418d0a8a. */
7573 if (tu_list_offset < cu_list_offset)
7574 {
7575 warn (_("TU offset (%x) is less than CU offset (%x)\n"),
7576 tu_list_offset, cu_list_offset);
7577 return 0;
7578 }
7579
7580 cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
7581
7582 if (address_table_offset < tu_list_offset)
7583 {
7584 warn (_("Address table offset (%x) is less than TU offset (%x)\n"),
7585 address_table_offset, tu_list_offset);
7586 return 0;
7587 }
7588
7589 tu_list_elements = (address_table_offset - tu_list_offset) / 8;
7590
7591 /* PR 17531: file: 18a47d3d. */
7592 if (symbol_table_offset < address_table_offset)
7593 {
7594 warn (_("Symbol table offset (%xl) is less then Address table offset (%x)\n"),
7595 symbol_table_offset, address_table_offset);
7596 return 0;
7597 }
7598
7599 address_table_size = symbol_table_offset - address_table_offset;
7600
7601 if (constant_pool_offset < symbol_table_offset)
7602 {
7603 warn (_("Constant pool offset (%x) is less than symbol table offset (%x)\n"),
7604 constant_pool_offset, symbol_table_offset);
7605 return 0;
7606 }
7607
7608 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
7609
7610 cu_list = start + cu_list_offset;
7611 tu_list = start + tu_list_offset;
7612 address_table = start + address_table_offset;
7613 symbol_table = start + symbol_table_offset;
7614 constant_pool = start + constant_pool_offset;
7615
7616 if (address_table + address_table_size * (2 + 8 + 4) > section->start + section->size)
7617 {
7618 warn (_("Address table extends beyond end of section.\n"));
7619 return 0;
7620 }
7621
7622 printf (_("\nCU table:\n"));
7623 for (i = 0; i < cu_list_elements; i += 2)
7624 {
7625 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
7626 uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
7627
7628 printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
7629 (unsigned long) cu_offset,
7630 (unsigned long) (cu_offset + cu_length - 1));
7631 }
7632
7633 printf (_("\nTU table:\n"));
7634 for (i = 0; i < tu_list_elements; i += 3)
7635 {
7636 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
7637 uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
7638 uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
7639
7640 printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
7641 (unsigned long) tu_offset,
7642 (unsigned long) type_offset);
7643 print_dwarf_vma (signature, 8);
7644 printf ("\n");
7645 }
7646
7647 printf (_("\nAddress table:\n"));
7648 for (i = 0; i < address_table_size && i <= address_table_size - (2 * 8 + 4);
7649 i += 2 * 8 + 4)
7650 {
7651 uint64_t low = byte_get_little_endian (address_table + i, 8);
7652 uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
7653 uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
7654
7655 print_dwarf_vma (low, 8);
7656 print_dwarf_vma (high, 8);
7657 printf (_("%lu\n"), (unsigned long) cu_index);
7658 }
7659
7660 printf (_("\nSymbol table:\n"));
7661 for (i = 0; i < symbol_table_slots; ++i)
7662 {
7663 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
7664 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
7665 uint32_t num_cus, cu;
7666
7667 if (name_offset != 0
7668 || cu_vector_offset != 0)
7669 {
7670 unsigned int j;
7671 unsigned char * adr;
7672
7673 adr = constant_pool + name_offset;
7674 /* PR 17531: file: 5b7b07ad. */
7675 if (adr < constant_pool || adr >= section->start + section->size)
7676 {
7677 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
7678 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
7679 name_offset, i);
7680 }
7681 else
7682 printf ("[%3u] %.*s:", i,
7683 (int) (section->size - (constant_pool_offset + name_offset)),
7684 constant_pool + name_offset);
7685
7686 adr = constant_pool + cu_vector_offset;
7687 if (adr < constant_pool || adr >= section->start + section->size - 3)
7688 {
7689 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
7690 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
7691 cu_vector_offset, i);
7692 continue;
7693 }
7694
7695 num_cus = byte_get_little_endian (adr, 4);
7696
7697 adr = constant_pool + cu_vector_offset + 4 + num_cus * 4;
7698 if (num_cus * 4 < num_cus
7699 || adr >= section->start + section->size
7700 || adr < constant_pool)
7701 {
7702 printf ("<invalid number of CUs: %d>\n", num_cus);
7703 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
7704 num_cus, i);
7705 continue;
7706 }
7707
7708 if (num_cus > 1)
7709 printf ("\n");
7710
7711 for (j = 0; j < num_cus; ++j)
7712 {
7713 int is_static;
7714 gdb_index_symbol_kind kind;
7715
7716 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
7717 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
7718 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
7719 cu = GDB_INDEX_CU_VALUE (cu);
7720 /* Convert to TU number if it's for a type unit. */
7721 if (cu >= cu_list_elements / 2)
7722 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
7723 (unsigned long) (cu - cu_list_elements / 2));
7724 else
7725 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
7726
7727 printf (" [%s, %s]",
7728 is_static ? _("static") : _("global"),
7729 get_gdb_index_symbol_kind_name (kind));
7730 if (num_cus > 1)
7731 printf ("\n");
7732 }
7733 if (num_cus <= 1)
7734 printf ("\n");
7735 }
7736 }
7737
7738 return 1;
7739 }
7740
7741 /* Pre-allocate enough space for the CU/TU sets needed. */
7742
7743 static void
7744 prealloc_cu_tu_list (unsigned int nshndx)
7745 {
7746 if (shndx_pool == NULL)
7747 {
7748 shndx_pool_size = nshndx;
7749 shndx_pool_used = 0;
7750 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
7751 sizeof (unsigned int));
7752 }
7753 else
7754 {
7755 shndx_pool_size = shndx_pool_used + nshndx;
7756 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
7757 sizeof (unsigned int));
7758 }
7759 }
7760
7761 static void
7762 add_shndx_to_cu_tu_entry (unsigned int shndx)
7763 {
7764 if (shndx_pool_used >= shndx_pool_size)
7765 {
7766 error (_("Internal error: out of space in the shndx pool.\n"));
7767 return;
7768 }
7769 shndx_pool [shndx_pool_used++] = shndx;
7770 }
7771
7772 static void
7773 end_cu_tu_entry (void)
7774 {
7775 if (shndx_pool_used >= shndx_pool_size)
7776 {
7777 error (_("Internal error: out of space in the shndx pool.\n"));
7778 return;
7779 }
7780 shndx_pool [shndx_pool_used++] = 0;
7781 }
7782
7783 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
7784
7785 static const char *
7786 get_DW_SECT_short_name (unsigned int dw_sect)
7787 {
7788 static char buf[16];
7789
7790 switch (dw_sect)
7791 {
7792 case DW_SECT_INFO:
7793 return "info";
7794 case DW_SECT_TYPES:
7795 return "types";
7796 case DW_SECT_ABBREV:
7797 return "abbrev";
7798 case DW_SECT_LINE:
7799 return "line";
7800 case DW_SECT_LOC:
7801 return "loc";
7802 case DW_SECT_STR_OFFSETS:
7803 return "str_off";
7804 case DW_SECT_MACINFO:
7805 return "macinfo";
7806 case DW_SECT_MACRO:
7807 return "macro";
7808 default:
7809 break;
7810 }
7811
7812 snprintf (buf, sizeof (buf), "%d", dw_sect);
7813 return buf;
7814 }
7815
7816 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
7817 These sections are extensions for Fission.
7818 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
7819
7820 static int
7821 process_cu_tu_index (struct dwarf_section *section, int do_display)
7822 {
7823 unsigned char *phdr = section->start;
7824 unsigned char *limit = phdr + section->size;
7825 unsigned char *phash;
7826 unsigned char *pindex;
7827 unsigned char *ppool;
7828 unsigned int version;
7829 unsigned int ncols = 0;
7830 unsigned int nused;
7831 unsigned int nslots;
7832 unsigned int i;
7833 unsigned int j;
7834 dwarf_vma signature_high;
7835 dwarf_vma signature_low;
7836 char buf[64];
7837
7838 /* PR 17512: file: 002-168123-0.004. */
7839 if (phdr == NULL)
7840 {
7841 warn (_("Section %s is empty\n"), section->name);
7842 return 0;
7843 }
7844 /* PR 17512: file: 002-376-0.004. */
7845 if (section->size < 24)
7846 {
7847 warn (_("Section %s is too small to contain a CU/TU header\n"),
7848 section->name);
7849 return 0;
7850 }
7851
7852 SAFE_BYTE_GET (version, phdr, 4, limit);
7853 if (version >= 2)
7854 SAFE_BYTE_GET (ncols, phdr + 4, 4, limit);
7855 SAFE_BYTE_GET (nused, phdr + 8, 4, limit);
7856 SAFE_BYTE_GET (nslots, phdr + 12, 4, limit);
7857
7858 phash = phdr + 16;
7859 pindex = phash + nslots * 8;
7860 ppool = pindex + nslots * 4;
7861
7862 /* PR 17531: file: 45d69832. */
7863 if (pindex < phash || ppool < phdr || (pindex == phash && nslots != 0))
7864 {
7865 warn (_("Section %s is too small for %d slots\n"),
7866 section->name, nslots);
7867 return 0;
7868 }
7869
7870 if (do_display)
7871 {
7872 printf (_("Contents of the %s section:\n\n"), section->name);
7873 printf (_(" Version: %d\n"), version);
7874 if (version >= 2)
7875 printf (_(" Number of columns: %d\n"), ncols);
7876 printf (_(" Number of used entries: %d\n"), nused);
7877 printf (_(" Number of slots: %d\n\n"), nslots);
7878 }
7879
7880 if (ppool > limit || ppool < phdr)
7881 {
7882 warn (_("Section %s too small for %d hash table entries\n"),
7883 section->name, nslots);
7884 return 0;
7885 }
7886
7887 if (version == 1)
7888 {
7889 if (!do_display)
7890 prealloc_cu_tu_list ((limit - ppool) / 4);
7891 for (i = 0; i < nslots; i++)
7892 {
7893 unsigned char *shndx_list;
7894 unsigned int shndx;
7895
7896 SAFE_BYTE_GET64 (phash, &signature_high, &signature_low, limit);
7897 if (signature_high != 0 || signature_low != 0)
7898 {
7899 SAFE_BYTE_GET (j, pindex, 4, limit);
7900 shndx_list = ppool + j * 4;
7901 /* PR 17531: file: 705e010d. */
7902 if (shndx_list < ppool)
7903 {
7904 warn (_("Section index pool located before start of section\n"));
7905 return 0;
7906 }
7907
7908 if (do_display)
7909 printf (_(" [%3d] Signature: 0x%s Sections: "),
7910 i, dwarf_vmatoa64 (signature_high, signature_low,
7911 buf, sizeof (buf)));
7912 for (;;)
7913 {
7914 if (shndx_list >= limit)
7915 {
7916 warn (_("Section %s too small for shndx pool\n"),
7917 section->name);
7918 return 0;
7919 }
7920 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
7921 if (shndx == 0)
7922 break;
7923 if (do_display)
7924 printf (" %d", shndx);
7925 else
7926 add_shndx_to_cu_tu_entry (shndx);
7927 shndx_list += 4;
7928 }
7929 if (do_display)
7930 printf ("\n");
7931 else
7932 end_cu_tu_entry ();
7933 }
7934 phash += 8;
7935 pindex += 4;
7936 }
7937 }
7938 else if (version == 2)
7939 {
7940 unsigned int val;
7941 unsigned int dw_sect;
7942 unsigned char *ph = phash;
7943 unsigned char *pi = pindex;
7944 unsigned char *poffsets = ppool + ncols * 4;
7945 unsigned char *psizes = poffsets + nused * ncols * 4;
7946 unsigned char *pend = psizes + nused * ncols * 4;
7947 bfd_boolean is_tu_index;
7948 struct cu_tu_set *this_set = NULL;
7949 unsigned int row;
7950 unsigned char *prow;
7951
7952 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
7953
7954 /* PR 17531: file: 0dd159bf.
7955 Check for wraparound with an overlarge ncols value. */
7956 if (poffsets < ppool || (unsigned int) ((poffsets - ppool) / 4) != ncols)
7957 {
7958 warn (_("Overlarge number of columns: %x\n"), ncols);
7959 return 0;
7960 }
7961
7962 if (pend > limit)
7963 {
7964 warn (_("Section %s too small for offset and size tables\n"),
7965 section->name);
7966 return 0;
7967 }
7968
7969 if (do_display)
7970 {
7971 printf (_(" Offset table\n"));
7972 printf (" slot %-16s ",
7973 is_tu_index ? _("signature") : _("dwo_id"));
7974 }
7975 else
7976 {
7977 if (is_tu_index)
7978 {
7979 tu_count = nused;
7980 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
7981 this_set = tu_sets;
7982 }
7983 else
7984 {
7985 cu_count = nused;
7986 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
7987 this_set = cu_sets;
7988 }
7989 }
7990
7991 if (do_display)
7992 {
7993 for (j = 0; j < ncols; j++)
7994 {
7995 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
7996 printf (" %8s", get_DW_SECT_short_name (dw_sect));
7997 }
7998 printf ("\n");
7999 }
8000
8001 for (i = 0; i < nslots; i++)
8002 {
8003 SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
8004
8005 SAFE_BYTE_GET (row, pi, 4, limit);
8006 if (row != 0)
8007 {
8008 /* PR 17531: file: a05f6ab3. */
8009 if (row > nused)
8010 {
8011 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
8012 row, nused);
8013 return 0;
8014 }
8015
8016 if (!do_display)
8017 memcpy (&this_set[row - 1].signature, ph, sizeof (uint64_t));
8018
8019 prow = poffsets + (row - 1) * ncols * 4;
8020 /* PR 17531: file: b8ce60a8. */
8021 if (prow < poffsets || prow > limit)
8022 {
8023 warn (_("Row index (%u) * num columns (%u) > space remaining in section\n"),
8024 row, ncols);
8025 return 0;
8026 }
8027
8028 if (do_display)
8029 printf (_(" [%3d] 0x%s"),
8030 i, dwarf_vmatoa64 (signature_high, signature_low,
8031 buf, sizeof (buf)));
8032 for (j = 0; j < ncols; j++)
8033 {
8034 SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
8035 if (do_display)
8036 printf (" %8d", val);
8037 else
8038 {
8039 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
8040
8041 /* PR 17531: file: 10796eb3. */
8042 if (dw_sect >= DW_SECT_MAX)
8043 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
8044 else
8045 this_set [row - 1].section_offsets [dw_sect] = val;
8046 }
8047 }
8048
8049 if (do_display)
8050 printf ("\n");
8051 }
8052 ph += 8;
8053 pi += 4;
8054 }
8055
8056 ph = phash;
8057 pi = pindex;
8058 if (do_display)
8059 {
8060 printf ("\n");
8061 printf (_(" Size table\n"));
8062 printf (" slot %-16s ",
8063 is_tu_index ? _("signature") : _("dwo_id"));
8064 }
8065
8066 for (j = 0; j < ncols; j++)
8067 {
8068 SAFE_BYTE_GET (val, ppool + j * 4, 4, limit);
8069 if (do_display)
8070 printf (" %8s", get_DW_SECT_short_name (val));
8071 }
8072
8073 if (do_display)
8074 printf ("\n");
8075
8076 for (i = 0; i < nslots; i++)
8077 {
8078 SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
8079
8080 SAFE_BYTE_GET (row, pi, 4, limit);
8081 if (row != 0)
8082 {
8083 prow = psizes + (row - 1) * ncols * 4;
8084
8085 if (do_display)
8086 printf (_(" [%3d] 0x%s"),
8087 i, dwarf_vmatoa64 (signature_high, signature_low,
8088 buf, sizeof (buf)));
8089
8090 for (j = 0; j < ncols; j++)
8091 {
8092 SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
8093 if (do_display)
8094 printf (" %8d", val);
8095 else
8096 {
8097 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
8098 if (dw_sect >= DW_SECT_MAX)
8099 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
8100 else
8101 this_set [row - 1].section_sizes [dw_sect] = val;
8102 }
8103 }
8104
8105 if (do_display)
8106 printf ("\n");
8107 }
8108
8109 ph += 8;
8110 pi += 4;
8111 }
8112 }
8113 else if (do_display)
8114 printf (_(" Unsupported version (%d)\n"), version);
8115
8116 if (do_display)
8117 printf ("\n");
8118
8119 return 1;
8120 }
8121
8122 /* Load the CU and TU indexes if present. This will build a list of
8123 section sets that we can use to associate a .debug_info.dwo section
8124 with its associated .debug_abbrev.dwo section in a .dwp file. */
8125
8126 static bfd_boolean
8127 load_cu_tu_indexes (void *file)
8128 {
8129 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
8130
8131 /* If we have already loaded (or tried to load) the CU and TU indexes
8132 then do not bother to repeat the task. */
8133 if (cu_tu_indexes_read == -1)
8134 {
8135 cu_tu_indexes_read = TRUE;
8136
8137 if (load_debug_section (dwp_cu_index, file))
8138 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
8139 cu_tu_indexes_read = FALSE;
8140
8141 if (load_debug_section (dwp_tu_index, file))
8142 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
8143 cu_tu_indexes_read = FALSE;
8144 }
8145
8146 return (bfd_boolean) cu_tu_indexes_read;
8147 }
8148
8149 /* Find the set of sections that includes section SHNDX. */
8150
8151 unsigned int *
8152 find_cu_tu_set (void *file, unsigned int shndx)
8153 {
8154 unsigned int i;
8155
8156 if (! load_cu_tu_indexes (file))
8157 return NULL;
8158
8159 /* Find SHNDX in the shndx pool. */
8160 for (i = 0; i < shndx_pool_used; i++)
8161 if (shndx_pool [i] == shndx)
8162 break;
8163
8164 if (i >= shndx_pool_used)
8165 return NULL;
8166
8167 /* Now backup to find the first entry in the set. */
8168 while (i > 0 && shndx_pool [i - 1] != 0)
8169 i--;
8170
8171 return shndx_pool + i;
8172 }
8173
8174 /* Display a .debug_cu_index or .debug_tu_index section. */
8175
8176 static int
8177 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
8178 {
8179 return process_cu_tu_index (section, 1);
8180 }
8181
8182 static int
8183 display_debug_not_supported (struct dwarf_section *section,
8184 void *file ATTRIBUTE_UNUSED)
8185 {
8186 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
8187 section->name);
8188
8189 return 1;
8190 }
8191
8192 /* Like malloc, but takes two parameters like calloc.
8193 Verifies that the first parameter is not too large.
8194 Note: does *not* initialise the allocated memory to zero. */
8195 void *
8196 cmalloc (size_t nmemb, size_t size)
8197 {
8198 /* Check for overflow. */
8199 if (nmemb >= ~(size_t) 0 / size)
8200 return NULL;
8201
8202 return xmalloc (nmemb * size);
8203 }
8204
8205 /* Like xmalloc, but takes two parameters like calloc.
8206 Verifies that the first parameter is not too large.
8207 Note: does *not* initialise the allocated memory to zero. */
8208 void *
8209 xcmalloc (size_t nmemb, size_t size)
8210 {
8211 /* Check for overflow. */
8212 if (nmemb >= ~(size_t) 0 / size)
8213 {
8214 fprintf (stderr,
8215 _("Attempt to allocate an array with an excessive number of elements: 0x%lx\n"),
8216 (long) nmemb);
8217 xexit (1);
8218 }
8219
8220 return xmalloc (nmemb * size);
8221 }
8222
8223 /* Like xrealloc, but takes three parameters.
8224 Verifies that the second parameter is not too large.
8225 Note: does *not* initialise any new memory to zero. */
8226 void *
8227 xcrealloc (void *ptr, size_t nmemb, size_t size)
8228 {
8229 /* Check for overflow. */
8230 if (nmemb >= ~(size_t) 0 / size)
8231 {
8232 fprintf (stderr,
8233 _("Attempt to re-allocate an array with an excessive number of elements: 0x%lx\n"),
8234 (long) nmemb);
8235 xexit (1);
8236 }
8237
8238 return xrealloc (ptr, nmemb * size);
8239 }
8240
8241 /* Like xcalloc, but verifies that the first parameter is not too large. */
8242 void *
8243 xcalloc2 (size_t nmemb, size_t size)
8244 {
8245 /* Check for overflow. */
8246 if (nmemb >= ~(size_t) 0 / size)
8247 {
8248 fprintf (stderr,
8249 _("Attempt to allocate a zero'ed array with an excessive number of elements: 0x%lx\n"),
8250 (long) nmemb);
8251 xexit (1);
8252 }
8253
8254 return xcalloc (nmemb, size);
8255 }
8256
8257 void
8258 free_debug_memory (void)
8259 {
8260 unsigned int i;
8261
8262 free_abbrevs ();
8263
8264 for (i = 0; i < max; i++)
8265 free_debug_section ((enum dwarf_section_display_enum) i);
8266
8267 if (debug_information != NULL)
8268 {
8269 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
8270 {
8271 for (i = 0; i < num_debug_info_entries; i++)
8272 {
8273 if (!debug_information [i].max_loc_offsets)
8274 {
8275 free (debug_information [i].loc_offsets);
8276 free (debug_information [i].have_frame_base);
8277 }
8278 if (!debug_information [i].max_range_lists)
8279 free (debug_information [i].range_lists);
8280 }
8281 }
8282 free (debug_information);
8283 debug_information = NULL;
8284 alloc_num_debug_info_entries = num_debug_info_entries = 0;
8285 }
8286 }
8287
8288 void
8289 dwarf_select_sections_by_names (const char *names)
8290 {
8291 typedef struct
8292 {
8293 const char * option;
8294 int * variable;
8295 int val;
8296 }
8297 debug_dump_long_opts;
8298
8299 static const debug_dump_long_opts opts_table [] =
8300 {
8301 /* Please keep this table alpha- sorted. */
8302 { "Ranges", & do_debug_ranges, 1 },
8303 { "abbrev", & do_debug_abbrevs, 1 },
8304 { "addr", & do_debug_addr, 1 },
8305 { "aranges", & do_debug_aranges, 1 },
8306 { "cu_index", & do_debug_cu_index, 1 },
8307 { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
8308 { "frames", & do_debug_frames, 1 },
8309 { "frames-interp", & do_debug_frames_interp, 1 },
8310 /* The special .gdb_index section. */
8311 { "gdb_index", & do_gdb_index, 1 },
8312 { "info", & do_debug_info, 1 },
8313 { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility. */
8314 { "loc", & do_debug_loc, 1 },
8315 { "macro", & do_debug_macinfo, 1 },
8316 { "pubnames", & do_debug_pubnames, 1 },
8317 { "pubtypes", & do_debug_pubtypes, 1 },
8318 /* This entry is for compatibility
8319 with earlier versions of readelf. */
8320 { "ranges", & do_debug_aranges, 1 },
8321 { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
8322 { "str", & do_debug_str, 1 },
8323 /* These trace_* sections are used by Itanium VMS. */
8324 { "trace_abbrev", & do_trace_abbrevs, 1 },
8325 { "trace_aranges", & do_trace_aranges, 1 },
8326 { "trace_info", & do_trace_info, 1 },
8327 { NULL, NULL, 0 }
8328 };
8329
8330 const char *p;
8331
8332 p = names;
8333 while (*p)
8334 {
8335 const debug_dump_long_opts * entry;
8336
8337 for (entry = opts_table; entry->option; entry++)
8338 {
8339 size_t len = strlen (entry->option);
8340
8341 if (strncmp (p, entry->option, len) == 0
8342 && (p[len] == ',' || p[len] == '\0'))
8343 {
8344 * entry->variable |= entry->val;
8345
8346 /* The --debug-dump=frames-interp option also
8347 enables the --debug-dump=frames option. */
8348 if (do_debug_frames_interp)
8349 do_debug_frames = 1;
8350
8351 p += len;
8352 break;
8353 }
8354 }
8355
8356 if (entry->option == NULL)
8357 {
8358 warn (_("Unrecognized debug option '%s'\n"), p);
8359 p = strchr (p, ',');
8360 if (p == NULL)
8361 break;
8362 }
8363
8364 if (*p == ',')
8365 p++;
8366 }
8367 }
8368
8369 void
8370 dwarf_select_sections_by_letters (const char *letters)
8371 {
8372 unsigned int lindex = 0;
8373
8374 while (letters[lindex])
8375 switch (letters[lindex++])
8376 {
8377 case 'i':
8378 do_debug_info = 1;
8379 break;
8380
8381 case 'a':
8382 do_debug_abbrevs = 1;
8383 break;
8384
8385 case 'l':
8386 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
8387 break;
8388
8389 case 'L':
8390 do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
8391 break;
8392
8393 case 'p':
8394 do_debug_pubnames = 1;
8395 break;
8396
8397 case 't':
8398 do_debug_pubtypes = 1;
8399 break;
8400
8401 case 'r':
8402 do_debug_aranges = 1;
8403 break;
8404
8405 case 'R':
8406 do_debug_ranges = 1;
8407 break;
8408
8409 case 'F':
8410 do_debug_frames_interp = 1;
8411 /* Fall through. */
8412 case 'f':
8413 do_debug_frames = 1;
8414 break;
8415
8416 case 'm':
8417 do_debug_macinfo = 1;
8418 break;
8419
8420 case 's':
8421 do_debug_str = 1;
8422 break;
8423
8424 case 'o':
8425 do_debug_loc = 1;
8426 break;
8427
8428 default:
8429 warn (_("Unrecognized debug option '%s'\n"), letters);
8430 break;
8431 }
8432 }
8433
8434 void
8435 dwarf_select_sections_all (void)
8436 {
8437 do_debug_info = 1;
8438 do_debug_abbrevs = 1;
8439 do_debug_lines = FLAG_DEBUG_LINES_RAW;
8440 do_debug_pubnames = 1;
8441 do_debug_pubtypes = 1;
8442 do_debug_aranges = 1;
8443 do_debug_ranges = 1;
8444 do_debug_frames = 1;
8445 do_debug_macinfo = 1;
8446 do_debug_str = 1;
8447 do_debug_loc = 1;
8448 do_gdb_index = 1;
8449 do_trace_info = 1;
8450 do_trace_abbrevs = 1;
8451 do_trace_aranges = 1;
8452 do_debug_addr = 1;
8453 do_debug_cu_index = 1;
8454 }
8455
8456 struct dwarf_section_display debug_displays[] =
8457 {
8458 { { ".debug_abbrev", ".zdebug_abbrev", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8459 display_debug_abbrev, &do_debug_abbrevs, FALSE },
8460 { { ".debug_aranges", ".zdebug_aranges", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8461 display_debug_aranges, &do_debug_aranges, TRUE },
8462 { { ".debug_frame", ".zdebug_frame", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8463 display_debug_frames, &do_debug_frames, TRUE },
8464 { { ".debug_info", ".zdebug_info", NULL, NULL, 0, 0, abbrev, NULL, 0, NULL },
8465 display_debug_info, &do_debug_info, TRUE },
8466 { { ".debug_line", ".zdebug_line", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8467 display_debug_lines, &do_debug_lines, TRUE },
8468 { { ".debug_pubnames", ".zdebug_pubnames", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8469 display_debug_pubnames, &do_debug_pubnames, FALSE },
8470 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8471 display_debug_gnu_pubnames, &do_debug_pubnames, FALSE },
8472 { { ".eh_frame", "", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8473 display_debug_frames, &do_debug_frames, TRUE },
8474 { { ".debug_macinfo", ".zdebug_macinfo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8475 display_debug_macinfo, &do_debug_macinfo, FALSE },
8476 { { ".debug_macro", ".zdebug_macro", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8477 display_debug_macro, &do_debug_macinfo, TRUE },
8478 { { ".debug_str", ".zdebug_str", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8479 display_debug_str, &do_debug_str, FALSE },
8480 { { ".debug_line_str", ".zdebug_line_str", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8481 display_debug_str, &do_debug_str, FALSE },
8482 { { ".debug_loc", ".zdebug_loc", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8483 display_debug_loc, &do_debug_loc, TRUE },
8484 { { ".debug_loclists", ".zdebug_loclists", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8485 display_debug_loc, &do_debug_loc, TRUE },
8486 { { ".debug_pubtypes", ".zdebug_pubtypes", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8487 display_debug_pubnames, &do_debug_pubtypes, FALSE },
8488 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8489 display_debug_gnu_pubnames, &do_debug_pubtypes, FALSE },
8490 { { ".debug_ranges", ".zdebug_ranges", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8491 display_debug_ranges, &do_debug_ranges, TRUE },
8492 { { ".debug_rnglists", ".zdebug_rnglists", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8493 display_debug_ranges, &do_debug_ranges, TRUE },
8494 { { ".debug_static_func", ".zdebug_static_func", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8495 display_debug_not_supported, NULL, FALSE },
8496 { { ".debug_static_vars", ".zdebug_static_vars", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8497 display_debug_not_supported, NULL, FALSE },
8498 { { ".debug_types", ".zdebug_types", NULL, NULL, 0, 0, abbrev, NULL, 0, NULL },
8499 display_debug_types, &do_debug_info, TRUE },
8500 { { ".debug_weaknames", ".zdebug_weaknames", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8501 display_debug_not_supported, NULL, FALSE },
8502 { { ".gdb_index", "", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8503 display_gdb_index, &do_gdb_index, FALSE },
8504 { { ".trace_info", "", NULL, NULL, 0, 0, trace_abbrev, NULL, 0, NULL },
8505 display_trace_info, &do_trace_info, TRUE },
8506 { { ".trace_abbrev", "", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8507 display_debug_abbrev, &do_trace_abbrevs, FALSE },
8508 { { ".trace_aranges", "", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8509 display_debug_aranges, &do_trace_aranges, FALSE },
8510 { { ".debug_info.dwo", ".zdebug_info.dwo", NULL, NULL, 0, 0, abbrev_dwo, NULL, 0, NULL },
8511 display_debug_info, &do_debug_info, TRUE },
8512 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8513 display_debug_abbrev, &do_debug_abbrevs, FALSE },
8514 { { ".debug_types.dwo", ".zdebug_types.dwo", NULL, NULL, 0, 0, abbrev_dwo, NULL, 0, NULL },
8515 display_debug_types, &do_debug_info, TRUE },
8516 { { ".debug_line.dwo", ".zdebug_line.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8517 display_debug_lines, &do_debug_lines, TRUE },
8518 { { ".debug_loc.dwo", ".zdebug_loc.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8519 display_debug_loc, &do_debug_loc, TRUE },
8520 { { ".debug_macro.dwo", ".zdebug_macro.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8521 display_debug_macro, &do_debug_macinfo, TRUE },
8522 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8523 display_debug_macinfo, &do_debug_macinfo, FALSE },
8524 { { ".debug_str.dwo", ".zdebug_str.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8525 display_debug_str, &do_debug_str, TRUE },
8526 { { ".debug_str_offsets", ".zdebug_str_offsets", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8527 display_debug_str_offsets, NULL, FALSE },
8528 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8529 display_debug_str_offsets, NULL, FALSE },
8530 { { ".debug_addr", ".zdebug_addr", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8531 display_debug_addr, &do_debug_addr, TRUE },
8532 { { ".debug_cu_index", "", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8533 display_cu_index, &do_debug_cu_index, FALSE },
8534 { { ".debug_tu_index", "", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8535 display_cu_index, &do_debug_cu_index, FALSE },
8536 };
This page took 0.218865 seconds and 4 git commands to generate.