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