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