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