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