xref: /netbsd-src/external/gpl3/binutils.old/dist/binutils/dwarf.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2    Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3    Free Software Foundation, Inc.
4 
5    This file is part of GNU Binutils.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21 
22 #include "sysdep.h"
23 #include "libiberty.h"
24 #include "bfd.h"
25 #include "bfd_stdint.h"
26 #include "bucomm.h"
27 #include "elfcomm.h"
28 #include "elf/common.h"
29 #include "dwarf2.h"
30 #include "dwarf.h"
31 #include "gdb/gdb-index.h"
32 
33 static const char *regname (unsigned int regno, int row);
34 
35 static int have_frame_base;
36 static int need_base_address;
37 
38 static unsigned int last_pointer_size = 0;
39 static int warned_about_missing_comp_units = FALSE;
40 
41 static unsigned int 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 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_wide;
66 
67 int dwarf_cutoff_level = -1;
68 unsigned long dwarf_start_die;
69 
70 int dwarf_check = 0;
71 
72 /* Values for do_debug_lines.  */
73 #define FLAG_DEBUG_LINES_RAW	 1
74 #define FLAG_DEBUG_LINES_DECODED 2
75 
76 static int
77 size_of_encoded_value (int encoding)
78 {
79   switch (encoding & 0x7)
80     {
81     default:	/* ??? */
82     case 0:	return eh_addr_size;
83     case 2:	return 2;
84     case 3:	return 4;
85     case 4:	return 8;
86     }
87 }
88 
89 static dwarf_vma
90 get_encoded_value (unsigned char *data,
91 		   int encoding,
92 		   struct dwarf_section *section)
93 {
94   int size = size_of_encoded_value (encoding);
95   dwarf_vma val;
96 
97   if (encoding & DW_EH_PE_signed)
98     val = byte_get_signed (data, size);
99   else
100     val = byte_get (data, size);
101 
102   if ((encoding & 0x70) == DW_EH_PE_pcrel)
103     val += section->address + (data - section->start);
104   return val;
105 }
106 
107 /* Print a dwarf_vma value (typically an address, offset or length) in
108    hexadecimal format, followed by a space.  The length of the value (and
109    hence the precision displayed) is determined by the byte_size parameter.  */
110 
111 static void
112 print_dwarf_vma (dwarf_vma val, unsigned byte_size)
113 {
114   static char buff[18];
115   int offset = 0;
116 
117   /* Printf does not have a way of specifiying a maximum field width for an
118      integer value, so we print the full value into a buffer and then select
119      the precision we need.  */
120 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
121 #ifndef __MINGW32__
122   snprintf (buff, sizeof (buff), "%16.16llx ", val);
123 #else
124   snprintf (buff, sizeof (buff), "%016I64x ", val);
125 #endif
126 #else
127   snprintf (buff, sizeof (buff), "%16.16lx ", val);
128 #endif
129 
130   if (byte_size != 0)
131     {
132       if (byte_size > 0 && byte_size <= 8)
133 	offset = 16 - 2 * byte_size;
134       else
135 	error (_("Wrong size in print_dwarf_vma"));
136     }
137 
138   fputs (buff + offset, stdout);
139 }
140 
141 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
142 #ifndef __MINGW32__
143 #define  DWARF_VMA_FMT "ll"
144 #else
145 #define  DWARF_VMA_FMT "I64"
146 #endif
147 #else
148 #define  DWARF_VMA_FMT "l"
149 #endif
150 
151 static const char *
152 dwarf_vmatoa (const char *fmtch, dwarf_vma value)
153 {
154   /* As dwarf_vmatoa is used more then once in a printf call
155      for output, we are cycling through an fixed array of pointers
156      for return address.  */
157   static int buf_pos = 0;
158   static struct dwarf_vmatoa_buf
159   {
160     char place[64];
161   } buf[16];
162   char fmt[32];
163   char *ret;
164 
165   sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
166 
167   ret = buf[buf_pos++].place;
168   buf_pos %= ARRAY_SIZE (buf);
169 
170   snprintf (ret, sizeof (buf[0].place), fmt, value);
171 
172   return ret;
173 }
174 
175 /* Format a 64-bit value, given as two 32-bit values, in hex.
176    For reentrancy, this uses a buffer provided by the caller.  */
177 
178 static const char *
179 dwarf_vmatoa64 (dwarf_vma hvalue, dwarf_vma lvalue, char *buf,
180 		unsigned int buf_len)
181 {
182   int len = 0;
183 
184   if (hvalue == 0)
185     snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", lvalue);
186   else
187     {
188       len = snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", hvalue);
189       snprintf (buf + len, buf_len - len,
190 		"%08" DWARF_VMA_FMT "x", lvalue);
191     }
192 
193   return buf;
194 }
195 
196 dwarf_vma
197 read_leb128 (unsigned char *data, unsigned int *length_return, int sign)
198 {
199   dwarf_vma result = 0;
200   unsigned int num_read = 0;
201   unsigned int shift = 0;
202   unsigned char byte;
203 
204   do
205     {
206       byte = *data++;
207       num_read++;
208 
209       result |= ((dwarf_vma) (byte & 0x7f)) << shift;
210 
211       shift += 7;
212 
213     }
214   while (byte & 0x80);
215 
216   if (length_return != NULL)
217     *length_return = num_read;
218 
219   if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
220     result |= -1L << shift;
221 
222   return result;
223 }
224 
225 /* Create a signed version to avoid painful typecasts.  */
226 static dwarf_signed_vma
227 read_sleb128 (unsigned char *data, unsigned int *length_return)
228 {
229   return (dwarf_signed_vma) read_leb128 (data, length_return, 1);
230 }
231 
232 typedef struct State_Machine_Registers
233 {
234   dwarf_vma address;
235   unsigned int file;
236   unsigned int line;
237   unsigned int column;
238   int is_stmt;
239   int basic_block;
240   unsigned char op_index;
241   unsigned char end_sequence;
242 /* This variable hold the number of the last entry seen
243    in the File Table.  */
244   unsigned int last_file_entry;
245 } SMR;
246 
247 static SMR state_machine_regs;
248 
249 static void
250 reset_state_machine (int is_stmt)
251 {
252   state_machine_regs.address = 0;
253   state_machine_regs.op_index = 0;
254   state_machine_regs.file = 1;
255   state_machine_regs.line = 1;
256   state_machine_regs.column = 0;
257   state_machine_regs.is_stmt = is_stmt;
258   state_machine_regs.basic_block = 0;
259   state_machine_regs.end_sequence = 0;
260   state_machine_regs.last_file_entry = 0;
261 }
262 
263 /* Handled an extend line op.
264    Returns the number of bytes read.  */
265 
266 static int
267 process_extended_line_op (unsigned char *data, int is_stmt)
268 {
269   unsigned char op_code;
270   unsigned int bytes_read;
271   unsigned int len;
272   unsigned char *name;
273   dwarf_vma adr;
274   unsigned char *orig_data = data;
275 
276   len = read_leb128 (data, & bytes_read, 0);
277   data += bytes_read;
278 
279   if (len == 0)
280     {
281       warn (_("badly formed extended line op encountered!\n"));
282       return bytes_read;
283     }
284 
285   len += bytes_read;
286   op_code = *data++;
287 
288   printf (_("  Extended opcode %d: "), op_code);
289 
290   switch (op_code)
291     {
292     case DW_LNE_end_sequence:
293       printf (_("End of Sequence\n\n"));
294       reset_state_machine (is_stmt);
295       break;
296 
297     case DW_LNE_set_address:
298       adr = byte_get (data, len - bytes_read - 1);
299       printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
300       state_machine_regs.address = adr;
301       state_machine_regs.op_index = 0;
302       break;
303 
304     case DW_LNE_define_file:
305       printf (_("define new File Table entry\n"));
306       printf (_("  Entry\tDir\tTime\tSize\tName\n"));
307 
308       printf ("   %d\t", ++state_machine_regs.last_file_entry);
309       name = data;
310       data += strlen ((char *) data) + 1;
311       printf ("%s\t", dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
312       data += bytes_read;
313       printf ("%s\t", dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
314       data += bytes_read;
315       printf ("%s\t", dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
316       data += bytes_read;
317       printf ("%s", name);
318       if ((unsigned int) (data - orig_data) != len)
319         printf (_(" [Bad opcode length]"));
320       printf ("\n\n");
321       break;
322 
323     case DW_LNE_set_discriminator:
324       printf (_("set Discriminator to %s\n"),
325 	      dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
326       break;
327 
328     /* HP extensions.  */
329     case DW_LNE_HP_negate_is_UV_update:
330       printf ("DW_LNE_HP_negate_is_UV_update\n");
331       break;
332     case DW_LNE_HP_push_context:
333       printf ("DW_LNE_HP_push_context\n");
334       break;
335     case DW_LNE_HP_pop_context:
336       printf ("DW_LNE_HP_pop_context\n");
337       break;
338     case DW_LNE_HP_set_file_line_column:
339       printf ("DW_LNE_HP_set_file_line_column\n");
340       break;
341     case DW_LNE_HP_set_routine_name:
342       printf ("DW_LNE_HP_set_routine_name\n");
343       break;
344     case DW_LNE_HP_set_sequence:
345       printf ("DW_LNE_HP_set_sequence\n");
346       break;
347     case DW_LNE_HP_negate_post_semantics:
348       printf ("DW_LNE_HP_negate_post_semantics\n");
349       break;
350     case DW_LNE_HP_negate_function_exit:
351       printf ("DW_LNE_HP_negate_function_exit\n");
352       break;
353     case DW_LNE_HP_negate_front_end_logical:
354       printf ("DW_LNE_HP_negate_front_end_logical\n");
355       break;
356     case DW_LNE_HP_define_proc:
357       printf ("DW_LNE_HP_define_proc\n");
358       break;
359     case DW_LNE_HP_source_file_correlation:
360       {
361         unsigned char *edata = data + len - bytes_read - 1;
362 
363         printf ("DW_LNE_HP_source_file_correlation\n");
364 
365         while (data < edata)
366           {
367             unsigned int opc;
368 
369             opc = read_leb128 (data, & bytes_read, 0);
370             data += bytes_read;
371 
372             switch (opc)
373               {
374               case DW_LNE_HP_SFC_formfeed:
375                 printf ("    DW_LNE_HP_SFC_formfeed\n");
376                 break;
377               case DW_LNE_HP_SFC_set_listing_line:
378                 printf ("    DW_LNE_HP_SFC_set_listing_line (%s)\n",
379                         dwarf_vmatoa ("u",
380                                       read_leb128 (data, & bytes_read, 0)));
381                 data += bytes_read;
382                 break;
383               case DW_LNE_HP_SFC_associate:
384                 printf ("    DW_LNE_HP_SFC_associate ");
385                 printf ("(%s",
386                         dwarf_vmatoa ("u",
387                                       read_leb128 (data, & bytes_read, 0)));
388                 data += bytes_read;
389                 printf (",%s",
390                         dwarf_vmatoa ("u",
391                                       read_leb128 (data, & bytes_read, 0)));
392                 data += bytes_read;
393                 printf (",%s)\n",
394                         dwarf_vmatoa ("u",
395                                       read_leb128 (data, & bytes_read, 0)));
396                 data += bytes_read;
397                 break;
398               default:
399                 printf (_("    UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
400                 data = edata;
401                 break;
402               }
403           }
404       }
405       break;
406 
407     default:
408       {
409         unsigned int rlen = len - bytes_read - 1;
410 
411         if (op_code >= DW_LNE_lo_user
412             /* The test against DW_LNW_hi_user is redundant due to
413                the limited range of the unsigned char data type used
414                for op_code.  */
415             /*&& op_code <= DW_LNE_hi_user*/)
416           printf (_("user defined: "));
417         else
418           printf (_("UNKNOWN: "));
419         printf (_("length %d ["), rlen);
420         for (; rlen; rlen--)
421           printf (" %02x", *data++);
422         printf ("]\n");
423       }
424       break;
425     }
426 
427   return len;
428 }
429 
430 static const char *
431 fetch_indirect_string (dwarf_vma offset)
432 {
433   struct dwarf_section *section = &debug_displays [str].section;
434 
435   if (section->start == NULL)
436     return _("<no .debug_str section>");
437 
438   /* DWARF sections under Mach-O have non-zero addresses.  */
439   offset -= section->address;
440   if (offset > section->size)
441     {
442       warn (_("DW_FORM_strp offset too big: %s\n"),
443 	    dwarf_vmatoa ("x", offset));
444       return _("<offset is too big>");
445     }
446 
447   return (const char *) section->start + offset;
448 }
449 
450 static const char *
451 fetch_indexed_string (dwarf_vma idx, dwarf_vma offset_size, int dwo)
452 {
453   enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
454   enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
455   struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
456   struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
457   dwarf_vma index_offset = idx * offset_size;
458   dwarf_vma str_offset;
459 
460   if (index_section->start == NULL)
461     return (dwo ? _("<no .debug_str_offsets.dwo section>")
462 		: _("<no .debug_str_offsets section>"));
463 
464   /* DWARF sections under Mach-O have non-zero addresses.  */
465   index_offset -= index_section->address;
466   if (index_offset > index_section->size)
467     {
468       warn (_("DW_FORM_GNU_str_index offset too big: %s\n"),
469 	    dwarf_vmatoa ("x", index_offset));
470       return _("<index offset is too big>");
471     }
472 
473   if (str_section->start == NULL)
474     return (dwo ? _("<no .debug_str.dwo section>")
475 		: _("<no .debug_str section>"));
476 
477   str_offset = byte_get (index_section->start + index_offset, offset_size);
478   str_offset -= str_section->address;
479   if (str_offset > str_section->size)
480     {
481       warn (_("DW_FORM_GNU_str_index indirect offset too big: %s\n"),
482 	    dwarf_vmatoa ("x", str_offset));
483       return _("<indirect index offset is too big>");
484     }
485 
486   return (const char *) str_section->start + str_offset;
487 }
488 
489 static const char *
490 fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
491 {
492   struct dwarf_section *section = &debug_displays [debug_addr].section;
493 
494   if (section->start == NULL)
495     return (_("<no .debug_addr section>"));
496 
497   if (offset + bytes > section->size)
498     {
499       warn (_("Offset into section %s too big: %s\n"),
500             section->name, dwarf_vmatoa ("x", offset));
501       return "<offset too big>";
502     }
503 
504   return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
505 }
506 
507 
508 /* FIXME:  There are better and more efficient ways to handle
509    these structures.  For now though, I just want something that
510    is simple to implement.  */
511 typedef struct abbrev_attr
512 {
513   unsigned long attribute;
514   unsigned long form;
515   struct abbrev_attr *next;
516 }
517 abbrev_attr;
518 
519 typedef struct abbrev_entry
520 {
521   unsigned long entry;
522   unsigned long tag;
523   int children;
524   struct abbrev_attr *first_attr;
525   struct abbrev_attr *last_attr;
526   struct abbrev_entry *next;
527 }
528 abbrev_entry;
529 
530 static abbrev_entry *first_abbrev = NULL;
531 static abbrev_entry *last_abbrev = NULL;
532 
533 static void
534 free_abbrevs (void)
535 {
536   abbrev_entry *abbrv;
537 
538   for (abbrv = first_abbrev; abbrv;)
539     {
540       abbrev_entry *next_abbrev = abbrv->next;
541       abbrev_attr *attr;
542 
543       for (attr = abbrv->first_attr; attr;)
544 	{
545 	  abbrev_attr *next_attr = attr->next;
546 
547 	  free (attr);
548 	  attr = next_attr;
549 	}
550 
551       free (abbrv);
552       abbrv = next_abbrev;
553     }
554 
555   last_abbrev = first_abbrev = NULL;
556 }
557 
558 static void
559 add_abbrev (unsigned long number, unsigned long tag, int children)
560 {
561   abbrev_entry *entry;
562 
563   entry = (abbrev_entry *) malloc (sizeof (*entry));
564   if (entry == NULL)
565     /* ugg */
566     return;
567 
568   entry->entry      = number;
569   entry->tag        = tag;
570   entry->children   = children;
571   entry->first_attr = NULL;
572   entry->last_attr  = NULL;
573   entry->next       = NULL;
574 
575   if (first_abbrev == NULL)
576     first_abbrev = entry;
577   else
578     last_abbrev->next = entry;
579 
580   last_abbrev = entry;
581 }
582 
583 static void
584 add_abbrev_attr (unsigned long attribute, unsigned long form)
585 {
586   abbrev_attr *attr;
587 
588   attr = (abbrev_attr *) malloc (sizeof (*attr));
589   if (attr == NULL)
590     /* ugg */
591     return;
592 
593   attr->attribute = attribute;
594   attr->form      = form;
595   attr->next      = NULL;
596 
597   if (last_abbrev->first_attr == NULL)
598     last_abbrev->first_attr = attr;
599   else
600     last_abbrev->last_attr->next = attr;
601 
602   last_abbrev->last_attr = attr;
603 }
604 
605 /* Processes the (partial) contents of a .debug_abbrev section.
606    Returns NULL if the end of the section was encountered.
607    Returns the address after the last byte read if the end of
608    an abbreviation set was found.  */
609 
610 static unsigned char *
611 process_abbrev_section (unsigned char *start, unsigned char *end)
612 {
613   if (first_abbrev != NULL)
614     return NULL;
615 
616   while (start < end)
617     {
618       unsigned int bytes_read;
619       unsigned long entry;
620       unsigned long tag;
621       unsigned long attribute;
622       int children;
623 
624       entry = read_leb128 (start, & bytes_read, 0);
625       start += bytes_read;
626 
627       /* A single zero is supposed to end the section according
628 	 to the standard.  If there's more, then signal that to
629 	 the caller.  */
630       if (entry == 0)
631 	return start == end ? NULL : start;
632 
633       tag = read_leb128 (start, & bytes_read, 0);
634       start += bytes_read;
635 
636       children = *start++;
637 
638       add_abbrev (entry, tag, children);
639 
640       do
641 	{
642 	  unsigned long form;
643 
644 	  attribute = read_leb128 (start, & bytes_read, 0);
645 	  start += bytes_read;
646 
647 	  form = read_leb128 (start, & bytes_read, 0);
648 	  start += bytes_read;
649 
650 	  if (attribute != 0)
651 	    add_abbrev_attr (attribute, form);
652 	}
653       while (attribute != 0);
654     }
655 
656   return NULL;
657 }
658 
659 static const char *
660 get_TAG_name (unsigned long tag)
661 {
662   const char *name = get_DW_TAG_name ((unsigned int)tag);
663 
664   if (name == NULL)
665     {
666       static char buffer[100];
667 
668       snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
669       return buffer;
670     }
671 
672   return name;
673 }
674 
675 static const char *
676 get_FORM_name (unsigned long form)
677 {
678   const char *name = get_DW_FORM_name (form);
679 
680   if (name == NULL)
681     {
682       static char buffer[100];
683 
684       snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
685       return buffer;
686     }
687 
688   return name;
689 }
690 
691 static unsigned char *
692 display_block (unsigned char *data, dwarf_vma length)
693 {
694   printf (_(" %s byte block: "), dwarf_vmatoa ("u", length));
695 
696   while (length --)
697     printf ("%lx ", (unsigned long) byte_get (data++, 1));
698 
699   return data;
700 }
701 
702 static int
703 decode_location_expression (unsigned char * data,
704 			    unsigned int pointer_size,
705 			    unsigned int offset_size,
706 			    int dwarf_version,
707 			    dwarf_vma length,
708 			    dwarf_vma cu_offset,
709 			    struct dwarf_section * section)
710 {
711   unsigned op;
712   unsigned int bytes_read;
713   dwarf_vma uvalue;
714   unsigned char *end = data + length;
715   int need_frame_base = 0;
716 
717   while (data < end)
718     {
719       op = *data++;
720 
721       switch (op)
722 	{
723 	case DW_OP_addr:
724          printf ("DW_OP_addr: %s",
725                  dwarf_vmatoa ("x", byte_get (data, pointer_size)));
726 	  data += pointer_size;
727 	  break;
728 	case DW_OP_deref:
729 	  printf ("DW_OP_deref");
730 	  break;
731 	case DW_OP_const1u:
732 	  printf ("DW_OP_const1u: %lu", (unsigned long) byte_get (data++, 1));
733 	  break;
734 	case DW_OP_const1s:
735 	  printf ("DW_OP_const1s: %ld", (long) byte_get_signed (data++, 1));
736 	  break;
737 	case DW_OP_const2u:
738 	  printf ("DW_OP_const2u: %lu", (unsigned long) byte_get (data, 2));
739 	  data += 2;
740 	  break;
741 	case DW_OP_const2s:
742 	  printf ("DW_OP_const2s: %ld", (long) byte_get_signed (data, 2));
743 	  data += 2;
744 	  break;
745 	case DW_OP_const4u:
746 	  printf ("DW_OP_const4u: %lu", (unsigned long) byte_get (data, 4));
747 	  data += 4;
748 	  break;
749 	case DW_OP_const4s:
750 	  printf ("DW_OP_const4s: %ld", (long) byte_get_signed (data, 4));
751 	  data += 4;
752 	  break;
753 	case DW_OP_const8u:
754 	  printf ("DW_OP_const8u: %lu %lu", (unsigned long) byte_get (data, 4),
755 		  (unsigned long) byte_get (data + 4, 4));
756 	  data += 8;
757 	  break;
758 	case DW_OP_const8s:
759 	  printf ("DW_OP_const8s: %ld %ld", (long) byte_get (data, 4),
760 		  (long) byte_get (data + 4, 4));
761 	  data += 8;
762 	  break;
763 	case DW_OP_constu:
764 	  printf ("DW_OP_constu: %s",
765 		  dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
766 	  data += bytes_read;
767 	  break;
768 	case DW_OP_consts:
769 	  printf ("DW_OP_consts: %s",
770 		  dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read)));
771 	  data += bytes_read;
772 	  break;
773 	case DW_OP_dup:
774 	  printf ("DW_OP_dup");
775 	  break;
776 	case DW_OP_drop:
777 	  printf ("DW_OP_drop");
778 	  break;
779 	case DW_OP_over:
780 	  printf ("DW_OP_over");
781 	  break;
782 	case DW_OP_pick:
783 	  printf ("DW_OP_pick: %ld", (unsigned long) byte_get (data++, 1));
784 	  break;
785 	case DW_OP_swap:
786 	  printf ("DW_OP_swap");
787 	  break;
788 	case DW_OP_rot:
789 	  printf ("DW_OP_rot");
790 	  break;
791 	case DW_OP_xderef:
792 	  printf ("DW_OP_xderef");
793 	  break;
794 	case DW_OP_abs:
795 	  printf ("DW_OP_abs");
796 	  break;
797 	case DW_OP_and:
798 	  printf ("DW_OP_and");
799 	  break;
800 	case DW_OP_div:
801 	  printf ("DW_OP_div");
802 	  break;
803 	case DW_OP_minus:
804 	  printf ("DW_OP_minus");
805 	  break;
806 	case DW_OP_mod:
807 	  printf ("DW_OP_mod");
808 	  break;
809 	case DW_OP_mul:
810 	  printf ("DW_OP_mul");
811 	  break;
812 	case DW_OP_neg:
813 	  printf ("DW_OP_neg");
814 	  break;
815 	case DW_OP_not:
816 	  printf ("DW_OP_not");
817 	  break;
818 	case DW_OP_or:
819 	  printf ("DW_OP_or");
820 	  break;
821 	case DW_OP_plus:
822 	  printf ("DW_OP_plus");
823 	  break;
824 	case DW_OP_plus_uconst:
825 	  printf ("DW_OP_plus_uconst: %s",
826 		  dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
827 	  data += bytes_read;
828 	  break;
829 	case DW_OP_shl:
830 	  printf ("DW_OP_shl");
831 	  break;
832 	case DW_OP_shr:
833 	  printf ("DW_OP_shr");
834 	  break;
835 	case DW_OP_shra:
836 	  printf ("DW_OP_shra");
837 	  break;
838 	case DW_OP_xor:
839 	  printf ("DW_OP_xor");
840 	  break;
841 	case DW_OP_bra:
842 	  printf ("DW_OP_bra: %ld", (long) byte_get_signed (data, 2));
843 	  data += 2;
844 	  break;
845 	case DW_OP_eq:
846 	  printf ("DW_OP_eq");
847 	  break;
848 	case DW_OP_ge:
849 	  printf ("DW_OP_ge");
850 	  break;
851 	case DW_OP_gt:
852 	  printf ("DW_OP_gt");
853 	  break;
854 	case DW_OP_le:
855 	  printf ("DW_OP_le");
856 	  break;
857 	case DW_OP_lt:
858 	  printf ("DW_OP_lt");
859 	  break;
860 	case DW_OP_ne:
861 	  printf ("DW_OP_ne");
862 	  break;
863 	case DW_OP_skip:
864 	  printf ("DW_OP_skip: %ld", (long) byte_get_signed (data, 2));
865 	  data += 2;
866 	  break;
867 
868 	case DW_OP_lit0:
869 	case DW_OP_lit1:
870 	case DW_OP_lit2:
871 	case DW_OP_lit3:
872 	case DW_OP_lit4:
873 	case DW_OP_lit5:
874 	case DW_OP_lit6:
875 	case DW_OP_lit7:
876 	case DW_OP_lit8:
877 	case DW_OP_lit9:
878 	case DW_OP_lit10:
879 	case DW_OP_lit11:
880 	case DW_OP_lit12:
881 	case DW_OP_lit13:
882 	case DW_OP_lit14:
883 	case DW_OP_lit15:
884 	case DW_OP_lit16:
885 	case DW_OP_lit17:
886 	case DW_OP_lit18:
887 	case DW_OP_lit19:
888 	case DW_OP_lit20:
889 	case DW_OP_lit21:
890 	case DW_OP_lit22:
891 	case DW_OP_lit23:
892 	case DW_OP_lit24:
893 	case DW_OP_lit25:
894 	case DW_OP_lit26:
895 	case DW_OP_lit27:
896 	case DW_OP_lit28:
897 	case DW_OP_lit29:
898 	case DW_OP_lit30:
899 	case DW_OP_lit31:
900 	  printf ("DW_OP_lit%d", op - DW_OP_lit0);
901 	  break;
902 
903 	case DW_OP_reg0:
904 	case DW_OP_reg1:
905 	case DW_OP_reg2:
906 	case DW_OP_reg3:
907 	case DW_OP_reg4:
908 	case DW_OP_reg5:
909 	case DW_OP_reg6:
910 	case DW_OP_reg7:
911 	case DW_OP_reg8:
912 	case DW_OP_reg9:
913 	case DW_OP_reg10:
914 	case DW_OP_reg11:
915 	case DW_OP_reg12:
916 	case DW_OP_reg13:
917 	case DW_OP_reg14:
918 	case DW_OP_reg15:
919 	case DW_OP_reg16:
920 	case DW_OP_reg17:
921 	case DW_OP_reg18:
922 	case DW_OP_reg19:
923 	case DW_OP_reg20:
924 	case DW_OP_reg21:
925 	case DW_OP_reg22:
926 	case DW_OP_reg23:
927 	case DW_OP_reg24:
928 	case DW_OP_reg25:
929 	case DW_OP_reg26:
930 	case DW_OP_reg27:
931 	case DW_OP_reg28:
932 	case DW_OP_reg29:
933 	case DW_OP_reg30:
934 	case DW_OP_reg31:
935 	  printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
936 		  regname (op - DW_OP_reg0, 1));
937 	  break;
938 
939 	case DW_OP_breg0:
940 	case DW_OP_breg1:
941 	case DW_OP_breg2:
942 	case DW_OP_breg3:
943 	case DW_OP_breg4:
944 	case DW_OP_breg5:
945 	case DW_OP_breg6:
946 	case DW_OP_breg7:
947 	case DW_OP_breg8:
948 	case DW_OP_breg9:
949 	case DW_OP_breg10:
950 	case DW_OP_breg11:
951 	case DW_OP_breg12:
952 	case DW_OP_breg13:
953 	case DW_OP_breg14:
954 	case DW_OP_breg15:
955 	case DW_OP_breg16:
956 	case DW_OP_breg17:
957 	case DW_OP_breg18:
958 	case DW_OP_breg19:
959 	case DW_OP_breg20:
960 	case DW_OP_breg21:
961 	case DW_OP_breg22:
962 	case DW_OP_breg23:
963 	case DW_OP_breg24:
964 	case DW_OP_breg25:
965 	case DW_OP_breg26:
966 	case DW_OP_breg27:
967 	case DW_OP_breg28:
968 	case DW_OP_breg29:
969 	case DW_OP_breg30:
970 	case DW_OP_breg31:
971 	  printf ("DW_OP_breg%d (%s): %s",
972 		  op - DW_OP_breg0,
973 		  regname (op - DW_OP_breg0, 1),
974 		  dwarf_vmatoa ("d", (dwarf_signed_vma)
975 		    read_leb128 (data, &bytes_read, 1)));
976 	  data += bytes_read;
977 	  break;
978 
979 	case DW_OP_regx:
980 	  uvalue = read_leb128 (data, &bytes_read, 0);
981 	  data += bytes_read;
982 	  printf ("DW_OP_regx: %s (%s)",
983 		  dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
984 	  break;
985 	case DW_OP_fbreg:
986 	  need_frame_base = 1;
987 	  printf ("DW_OP_fbreg: %s",
988 		  dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read)));
989 	  data += bytes_read;
990 	  break;
991 	case DW_OP_bregx:
992 	  uvalue = read_leb128 (data, &bytes_read, 0);
993 	  data += bytes_read;
994 	  printf ("DW_OP_bregx: %s (%s) %s",
995 		  dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
996 		  dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read)));
997 	  data += bytes_read;
998 	  break;
999 	case DW_OP_piece:
1000 	  printf ("DW_OP_piece: %s",
1001 		  dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
1002 	  data += bytes_read;
1003 	  break;
1004 	case DW_OP_deref_size:
1005 	  printf ("DW_OP_deref_size: %ld", (long) byte_get (data++, 1));
1006 	  break;
1007 	case DW_OP_xderef_size:
1008 	  printf ("DW_OP_xderef_size: %ld", (long) byte_get (data++, 1));
1009 	  break;
1010 	case DW_OP_nop:
1011 	  printf ("DW_OP_nop");
1012 	  break;
1013 
1014 	  /* DWARF 3 extensions.  */
1015 	case DW_OP_push_object_address:
1016 	  printf ("DW_OP_push_object_address");
1017 	  break;
1018 	case DW_OP_call2:
1019 	  /* XXX: Strictly speaking for 64-bit DWARF3 files
1020 	     this ought to be an 8-byte wide computation.  */
1021 	  printf ("DW_OP_call2: <0x%s>",
1022 		  dwarf_vmatoa ("x", (dwarf_signed_vma) byte_get (data, 2)
1023 				     + cu_offset));
1024 	  data += 2;
1025 	  break;
1026 	case DW_OP_call4:
1027 	  /* XXX: Strictly speaking for 64-bit DWARF3 files
1028 	     this ought to be an 8-byte wide computation.  */
1029 	  printf ("DW_OP_call4: <0x%s>",
1030 		  dwarf_vmatoa ("x", (dwarf_signed_vma) byte_get (data, 4)
1031 				     + cu_offset));
1032 	  data += 4;
1033 	  break;
1034 	case DW_OP_call_ref:
1035 	  /* XXX: Strictly speaking for 64-bit DWARF3 files
1036 	     this ought to be an 8-byte wide computation.  */
1037 	  if (dwarf_version == -1)
1038 	    {
1039 	      printf (_("(DW_OP_call_ref in frame info)"));
1040 	      /* No way to tell where the next op is, so just bail.  */
1041 	      return need_frame_base;
1042 	    }
1043 	  if (dwarf_version == 2)
1044 	    {
1045 	      printf ("DW_OP_call_ref: <0x%s>",
1046 		      dwarf_vmatoa ("x", byte_get (data, pointer_size)));
1047 	      data += pointer_size;
1048 	    }
1049 	  else
1050 	    {
1051 	      printf ("DW_OP_call_ref: <0x%s>",
1052 		      dwarf_vmatoa ("x", byte_get (data, offset_size)));
1053 	      data += offset_size;
1054 	    }
1055 	  break;
1056 	case DW_OP_form_tls_address:
1057 	  printf ("DW_OP_form_tls_address");
1058 	  break;
1059 	case DW_OP_call_frame_cfa:
1060 	  printf ("DW_OP_call_frame_cfa");
1061 	  break;
1062 	case DW_OP_bit_piece:
1063 	  printf ("DW_OP_bit_piece: ");
1064 	  printf (_("size: %s "),
1065 		  dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
1066 	  data += bytes_read;
1067 	  printf (_("offset: %s "),
1068 		  dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
1069 	  data += bytes_read;
1070 	  break;
1071 
1072 	  /* DWARF 4 extensions.  */
1073 	case DW_OP_stack_value:
1074 	  printf ("DW_OP_stack_value");
1075 	  break;
1076 
1077 	case DW_OP_implicit_value:
1078 	  printf ("DW_OP_implicit_value");
1079 	  uvalue = read_leb128 (data, &bytes_read, 0);
1080 	  data += bytes_read;
1081 	  display_block (data, uvalue);
1082 	  data += uvalue;
1083 	  break;
1084 
1085 	  /* GNU extensions.  */
1086 	case DW_OP_GNU_push_tls_address:
1087 	  printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1088 	  break;
1089 	case DW_OP_GNU_uninit:
1090 	  printf ("DW_OP_GNU_uninit");
1091 	  /* FIXME: Is there data associated with this OP ?  */
1092 	  break;
1093 	case DW_OP_GNU_encoded_addr:
1094 	  {
1095 	    int encoding;
1096 	    dwarf_vma addr;
1097 
1098 	    encoding = *data++;
1099 	    addr = get_encoded_value (data, encoding, section);
1100 	    data += size_of_encoded_value (encoding);
1101 
1102 	    printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1103 	    print_dwarf_vma (addr, pointer_size);
1104 	  }
1105 	  break;
1106 	case DW_OP_GNU_implicit_pointer:
1107 	  /* XXX: Strictly speaking for 64-bit DWARF3 files
1108 	     this ought to be an 8-byte wide computation.  */
1109 	  if (dwarf_version == -1)
1110 	    {
1111 	      printf (_("(DW_OP_GNU_implicit_pointer in frame info)"));
1112 	      /* No way to tell where the next op is, so just bail.  */
1113 	      return need_frame_base;
1114 	    }
1115 	  if (dwarf_version == 2)
1116 	    {
1117 	      printf ("DW_OP_GNU_implicit_pointer: <0x%s> %s",
1118 		      dwarf_vmatoa ("x", byte_get (data, pointer_size)),
1119 		      dwarf_vmatoa ("d", read_sleb128 (data + pointer_size,
1120 				     &bytes_read)));
1121 	      data += pointer_size + bytes_read;
1122 	    }
1123 	  else
1124 	    {
1125 	      printf ("DW_OP_GNU_implicit_pointer: <0x%s> %s",
1126 		      dwarf_vmatoa ("x", byte_get (data, offset_size)),
1127 		      dwarf_vmatoa ("d", read_sleb128 (data + offset_size,
1128 				     &bytes_read)));
1129 	      data += offset_size + bytes_read;
1130 	    }
1131 	  break;
1132 	case DW_OP_GNU_entry_value:
1133 	  uvalue = read_leb128 (data, &bytes_read, 0);
1134 	  data += bytes_read;
1135 	  printf ("DW_OP_GNU_entry_value: (");
1136 	  if (decode_location_expression (data, pointer_size, offset_size,
1137 					  dwarf_version, uvalue,
1138 					  cu_offset, section))
1139 	    need_frame_base = 1;
1140 	  putchar (')');
1141 	  data += uvalue;
1142 	  break;
1143 	case DW_OP_GNU_const_type:
1144 	  uvalue = read_leb128 (data, &bytes_read, 0);
1145 	  data += bytes_read;
1146 	  printf ("DW_OP_GNU_const_type: <0x%s> ",
1147 		  dwarf_vmatoa ("x", cu_offset + uvalue));
1148 	  uvalue = byte_get (data++, 1);
1149 	  display_block (data, uvalue);
1150 	  data += uvalue;
1151 	  break;
1152 	case DW_OP_GNU_regval_type:
1153 	  uvalue = read_leb128 (data, &bytes_read, 0);
1154 	  data += bytes_read;
1155 	  printf ("DW_OP_GNU_regval_type: %s (%s)",
1156 		  dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1157 	  uvalue = read_leb128 (data, &bytes_read, 0);
1158 	  data += bytes_read;
1159 	  printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1160 	  break;
1161 	case DW_OP_GNU_deref_type:
1162 	  printf ("DW_OP_GNU_deref_type: %ld", (long) byte_get (data++, 1));
1163 	  uvalue = read_leb128 (data, &bytes_read, 0);
1164 	  data += bytes_read;
1165 	  printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1166 	  break;
1167 	case DW_OP_GNU_convert:
1168 	  uvalue = read_leb128 (data, &bytes_read, 0);
1169 	  data += bytes_read;
1170 	  printf ("DW_OP_GNU_convert <0x%s>",
1171 		  dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1172 	  break;
1173 	case DW_OP_GNU_reinterpret:
1174 	  uvalue = read_leb128 (data, &bytes_read, 0);
1175 	  data += bytes_read;
1176 	  printf ("DW_OP_GNU_reinterpret <0x%s>",
1177 		  dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1178 	  break;
1179 	case DW_OP_GNU_parameter_ref:
1180 	  printf ("DW_OP_GNU_parameter_ref: <0x%s>",
1181 		  dwarf_vmatoa ("x", cu_offset + byte_get (data, 4)));
1182 	  data += 4;
1183 	  break;
1184         case DW_OP_GNU_addr_index:
1185           uvalue = read_leb128 (data, &bytes_read, 0);
1186           data += bytes_read;
1187           printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1188           break;
1189 
1190 	  /* HP extensions.  */
1191 	case DW_OP_HP_is_value:
1192 	  printf ("DW_OP_HP_is_value");
1193 	  /* FIXME: Is there data associated with this OP ?  */
1194 	  break;
1195 	case DW_OP_HP_fltconst4:
1196 	  printf ("DW_OP_HP_fltconst4");
1197 	  /* FIXME: Is there data associated with this OP ?  */
1198 	  break;
1199 	case DW_OP_HP_fltconst8:
1200 	  printf ("DW_OP_HP_fltconst8");
1201 	  /* FIXME: Is there data associated with this OP ?  */
1202 	  break;
1203 	case DW_OP_HP_mod_range:
1204 	  printf ("DW_OP_HP_mod_range");
1205 	  /* FIXME: Is there data associated with this OP ?  */
1206 	  break;
1207 	case DW_OP_HP_unmod_range:
1208 	  printf ("DW_OP_HP_unmod_range");
1209 	  /* FIXME: Is there data associated with this OP ?  */
1210 	  break;
1211 	case DW_OP_HP_tls:
1212 	  printf ("DW_OP_HP_tls");
1213 	  /* FIXME: Is there data associated with this OP ?  */
1214 	  break;
1215 
1216 	  /* PGI (STMicroelectronics) extensions.  */
1217 	case DW_OP_PGI_omp_thread_num:
1218 	  /* Pushes the thread number for the current thread as it would be
1219 	     returned by the standard OpenMP library function:
1220 	     omp_get_thread_num().  The "current thread" is the thread for
1221 	     which the expression is being evaluated.  */
1222 	  printf ("DW_OP_PGI_omp_thread_num");
1223 	  break;
1224 
1225 	default:
1226 	  if (op >= DW_OP_lo_user
1227 	      && op <= DW_OP_hi_user)
1228 	    printf (_("(User defined location op)"));
1229 	  else
1230 	    printf (_("(Unknown location op)"));
1231 	  /* No way to tell where the next op is, so just bail.  */
1232 	  return need_frame_base;
1233 	}
1234 
1235       /* Separate the ops.  */
1236       if (data < end)
1237 	printf ("; ");
1238     }
1239 
1240   return need_frame_base;
1241 }
1242 
1243 static unsigned char *
1244 read_and_display_attr_value (unsigned long attribute,
1245 			     unsigned long form,
1246 			     unsigned char * data,
1247 			     dwarf_vma cu_offset,
1248 			     dwarf_vma pointer_size,
1249 			     dwarf_vma offset_size,
1250 			     int dwarf_version,
1251 			     debug_info * debug_info_p,
1252 			     int do_loc,
1253 			     struct dwarf_section * section)
1254 {
1255   dwarf_vma uvalue = 0;
1256   unsigned char *block_start = NULL;
1257   unsigned char * orig_data = data;
1258   unsigned int bytes_read;
1259 
1260   switch (form)
1261     {
1262     default:
1263       break;
1264 
1265     case DW_FORM_ref_addr:
1266       if (dwarf_version == 2)
1267 	{
1268 	  uvalue = byte_get (data, pointer_size);
1269 	  data += pointer_size;
1270 	}
1271       else if (dwarf_version == 3 || dwarf_version == 4)
1272 	{
1273 	  uvalue = byte_get (data, offset_size);
1274 	  data += offset_size;
1275 	}
1276       else
1277 	error (_("Internal error: DWARF version is not 2, 3 or 4.\n"));
1278 
1279       break;
1280 
1281     case DW_FORM_addr:
1282       uvalue = byte_get (data, pointer_size);
1283       data += pointer_size;
1284       break;
1285 
1286     case DW_FORM_strp:
1287     case DW_FORM_sec_offset:
1288     case DW_FORM_GNU_ref_alt:
1289     case DW_FORM_GNU_strp_alt:
1290       uvalue = byte_get (data, offset_size);
1291       data += offset_size;
1292       break;
1293 
1294     case DW_FORM_flag_present:
1295       uvalue = 1;
1296       break;
1297 
1298     case DW_FORM_ref1:
1299     case DW_FORM_flag:
1300     case DW_FORM_data1:
1301       uvalue = byte_get (data++, 1);
1302       break;
1303 
1304     case DW_FORM_ref2:
1305     case DW_FORM_data2:
1306       uvalue = byte_get (data, 2);
1307       data += 2;
1308       break;
1309 
1310     case DW_FORM_ref4:
1311     case DW_FORM_data4:
1312       uvalue = byte_get (data, 4);
1313       data += 4;
1314       break;
1315 
1316     case DW_FORM_sdata:
1317       uvalue = read_leb128 (data, & bytes_read, 1);
1318       data += bytes_read;
1319       break;
1320 
1321     case DW_FORM_GNU_str_index:
1322       uvalue = read_leb128 (data, & bytes_read, 0);
1323       data += bytes_read;
1324       break;
1325 
1326     case DW_FORM_ref_udata:
1327     case DW_FORM_udata:
1328       uvalue = read_leb128 (data, & bytes_read, 0);
1329       data += bytes_read;
1330       break;
1331 
1332     case DW_FORM_indirect:
1333       form = read_leb128 (data, & bytes_read, 0);
1334       data += bytes_read;
1335       if (!do_loc)
1336 	printf (" %s", get_FORM_name (form));
1337       return read_and_display_attr_value (attribute, form, data,
1338 					  cu_offset, pointer_size,
1339 					  offset_size, dwarf_version,
1340 					  debug_info_p, do_loc,
1341 					  section);
1342     case DW_FORM_GNU_addr_index:
1343       uvalue = read_leb128 (data, & bytes_read, 0);
1344       data += bytes_read;
1345       break;
1346     }
1347 
1348   switch (form)
1349     {
1350     case DW_FORM_ref_addr:
1351       if (!do_loc)
1352 	printf (" <0x%s>", dwarf_vmatoa ("x",uvalue));
1353       break;
1354 
1355     case DW_FORM_GNU_ref_alt:
1356       if (!do_loc)
1357 	printf (" <alt 0x%s>", dwarf_vmatoa ("x",uvalue));
1358       break;
1359 
1360     case DW_FORM_ref1:
1361     case DW_FORM_ref2:
1362     case DW_FORM_ref4:
1363     case DW_FORM_ref_udata:
1364       if (!do_loc)
1365 	printf (" <0x%s>", dwarf_vmatoa ("x", uvalue + cu_offset));
1366       break;
1367 
1368     case DW_FORM_data4:
1369     case DW_FORM_addr:
1370     case DW_FORM_sec_offset:
1371       if (!do_loc)
1372 	printf (" 0x%s", dwarf_vmatoa ("x", uvalue));
1373       break;
1374 
1375     case DW_FORM_flag_present:
1376     case DW_FORM_flag:
1377     case DW_FORM_data1:
1378     case DW_FORM_data2:
1379     case DW_FORM_sdata:
1380     case DW_FORM_udata:
1381       if (!do_loc)
1382 	printf (" %s", dwarf_vmatoa ("d", uvalue));
1383       break;
1384 
1385     case DW_FORM_ref8:
1386     case DW_FORM_data8:
1387       if (!do_loc)
1388 	{
1389 	  dwarf_vma high_bits;
1390 	  char buf[64];
1391 
1392 	  byte_get_64 (data, &high_bits, &uvalue);
1393 	  printf (" 0x%s",
1394 		  dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
1395 	}
1396       if ((do_loc || do_debug_loc || do_debug_ranges)
1397 	  && num_debug_info_entries == 0)
1398 	{
1399 	  if (sizeof (uvalue) == 8)
1400 	    uvalue = byte_get (data, 8);
1401 	  else
1402 	    error (_("DW_FORM_data8 is unsupported when sizeof (dwarf_vma) != 8\n"));
1403 	}
1404       data += 8;
1405       break;
1406 
1407     case DW_FORM_string:
1408       if (!do_loc)
1409 	printf (" %s", data);
1410       data += strlen ((char *) data) + 1;
1411       break;
1412 
1413     case DW_FORM_block:
1414     case DW_FORM_exprloc:
1415       uvalue = read_leb128 (data, & bytes_read, 0);
1416       block_start = data + bytes_read;
1417       if (do_loc)
1418 	data = block_start + uvalue;
1419       else
1420 	data = display_block (block_start, uvalue);
1421       break;
1422 
1423     case DW_FORM_block1:
1424       uvalue = byte_get (data, 1);
1425       block_start = data + 1;
1426       if (do_loc)
1427 	data = block_start + uvalue;
1428       else
1429 	data = display_block (block_start, uvalue);
1430       break;
1431 
1432     case DW_FORM_block2:
1433       uvalue = byte_get (data, 2);
1434       block_start = data + 2;
1435       if (do_loc)
1436 	data = block_start + uvalue;
1437       else
1438 	data = display_block (block_start, uvalue);
1439       break;
1440 
1441     case DW_FORM_block4:
1442       uvalue = byte_get (data, 4);
1443       block_start = data + 4;
1444       if (do_loc)
1445 	data = block_start + uvalue;
1446       else
1447 	data = display_block (block_start, uvalue);
1448       break;
1449 
1450     case DW_FORM_strp:
1451       if (!do_loc)
1452 	printf (_(" (indirect string, offset: 0x%s): %s"),
1453 		dwarf_vmatoa ("x", uvalue),
1454 		fetch_indirect_string (uvalue));
1455       break;
1456 
1457     case DW_FORM_GNU_str_index:
1458       if (!do_loc)
1459         {
1460           const char *suffix = strrchr (section->name, '.');
1461           int dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? 1 : 0;
1462 
1463           printf (_(" (indexed string: 0x%s): %s"),
1464                   dwarf_vmatoa ("x", uvalue),
1465                   fetch_indexed_string (uvalue, offset_size, dwo));
1466         }
1467       break;
1468 
1469     case DW_FORM_GNU_strp_alt:
1470       if (!do_loc)
1471 	printf (_(" (alt indirect string, offset: 0x%s)"),
1472 		dwarf_vmatoa ("x", uvalue));
1473       break;
1474 
1475     case DW_FORM_indirect:
1476       /* Handled above.  */
1477       break;
1478 
1479     case DW_FORM_ref_sig8:
1480       if (!do_loc)
1481 	{
1482 	  dwarf_vma high_bits;
1483 	  char buf[64];
1484 
1485 	  byte_get_64 (data, &high_bits, &uvalue);
1486 	  printf (" signature: 0x%s",
1487 		  dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
1488 	}
1489       data += 8;
1490       break;
1491 
1492     case DW_FORM_GNU_addr_index:
1493       if (!do_loc)
1494         printf (_(" (addr_index: 0x%s): %s"),
1495                 dwarf_vmatoa ("x", uvalue),
1496                 fetch_indexed_value (uvalue * pointer_size, pointer_size));
1497       break;
1498 
1499     default:
1500       warn (_("Unrecognized form: %lu\n"), form);
1501       break;
1502     }
1503 
1504   if ((do_loc || do_debug_loc || do_debug_ranges)
1505       && num_debug_info_entries == 0
1506       && debug_info_p != NULL)
1507     {
1508       switch (attribute)
1509 	{
1510 	case DW_AT_frame_base:
1511 	  have_frame_base = 1;
1512 	case DW_AT_location:
1513 	case DW_AT_string_length:
1514 	case DW_AT_return_addr:
1515 	case DW_AT_data_member_location:
1516 	case DW_AT_vtable_elem_location:
1517 	case DW_AT_segment:
1518 	case DW_AT_static_link:
1519 	case DW_AT_use_location:
1520 	case DW_AT_GNU_call_site_value:
1521 	case DW_AT_GNU_call_site_data_value:
1522 	case DW_AT_GNU_call_site_target:
1523 	case DW_AT_GNU_call_site_target_clobbered:
1524     	  if ((dwarf_version < 4
1525 	       && (form == DW_FORM_data4 || form == DW_FORM_data8))
1526 	      || form == DW_FORM_sec_offset)
1527 	    {
1528 	      /* Process location list.  */
1529 	      unsigned int lmax = debug_info_p->max_loc_offsets;
1530 	      unsigned int num = debug_info_p->num_loc_offsets;
1531 
1532 	      if (lmax == 0 || num >= lmax)
1533 		{
1534 		  lmax += 1024;
1535 		  debug_info_p->loc_offsets = (dwarf_vma *)
1536                       xcrealloc (debug_info_p->loc_offsets,
1537 				 lmax, sizeof (*debug_info_p->loc_offsets));
1538 		  debug_info_p->have_frame_base = (int *)
1539                       xcrealloc (debug_info_p->have_frame_base,
1540 				 lmax, sizeof (*debug_info_p->have_frame_base));
1541 		  debug_info_p->max_loc_offsets = lmax;
1542 		}
1543 	      debug_info_p->loc_offsets [num] = uvalue;
1544 	      debug_info_p->have_frame_base [num] = have_frame_base;
1545 	      debug_info_p->num_loc_offsets++;
1546 	    }
1547 	  break;
1548 
1549 	case DW_AT_low_pc:
1550 	  if (need_base_address)
1551 	    debug_info_p->base_address = uvalue;
1552 	  break;
1553 
1554 	case DW_AT_GNU_addr_base:
1555           debug_info_p->addr_base = uvalue;
1556 	  break;
1557 
1558 	case DW_AT_GNU_ranges_base:
1559           debug_info_p->ranges_base = uvalue;
1560 	  break;
1561 
1562 	case DW_AT_ranges:
1563     	  if ((dwarf_version < 4
1564 	       && (form == DW_FORM_data4 || form == DW_FORM_data8))
1565 	      || form == DW_FORM_sec_offset)
1566 	    {
1567 	      /* Process range list.  */
1568 	      unsigned int lmax = debug_info_p->max_range_lists;
1569 	      unsigned int num = debug_info_p->num_range_lists;
1570 
1571 	      if (lmax == 0 || num >= lmax)
1572 		{
1573 		  lmax += 1024;
1574 		  debug_info_p->range_lists = (dwarf_vma *)
1575                       xcrealloc (debug_info_p->range_lists,
1576 				 lmax, sizeof (*debug_info_p->range_lists));
1577 		  debug_info_p->max_range_lists = lmax;
1578 		}
1579 	      debug_info_p->range_lists [num] = uvalue;
1580 	      debug_info_p->num_range_lists++;
1581 	    }
1582 	  break;
1583 
1584 	default:
1585 	  break;
1586 	}
1587     }
1588 
1589   if (do_loc || attribute == 0)
1590     return data;
1591 
1592   /* For some attributes we can display further information.  */
1593   printf ("\t");
1594 
1595   switch (attribute)
1596     {
1597     case DW_AT_inline:
1598       switch (uvalue)
1599 	{
1600 	case DW_INL_not_inlined:
1601 	  printf (_("(not inlined)"));
1602 	  break;
1603 	case DW_INL_inlined:
1604 	  printf (_("(inlined)"));
1605 	  break;
1606 	case DW_INL_declared_not_inlined:
1607 	  printf (_("(declared as inline but ignored)"));
1608 	  break;
1609 	case DW_INL_declared_inlined:
1610 	  printf (_("(declared as inline and inlined)"));
1611 	  break;
1612 	default:
1613 	  printf (_("  (Unknown inline attribute value: %s)"),
1614 		  dwarf_vmatoa ("x", uvalue));
1615 	  break;
1616 	}
1617       break;
1618 
1619     case DW_AT_language:
1620       switch (uvalue)
1621 	{
1622 	  /* Ordered by the numeric value of these constants.  */
1623 	case DW_LANG_C89:		printf ("(ANSI C)"); break;
1624 	case DW_LANG_C:			printf ("(non-ANSI C)"); break;
1625 	case DW_LANG_Ada83:		printf ("(Ada)"); break;
1626 	case DW_LANG_C_plus_plus:	printf ("(C++)"); break;
1627 	case DW_LANG_Cobol74:		printf ("(Cobol 74)"); break;
1628 	case DW_LANG_Cobol85:		printf ("(Cobol 85)"); break;
1629 	case DW_LANG_Fortran77:		printf ("(FORTRAN 77)"); break;
1630 	case DW_LANG_Fortran90:		printf ("(Fortran 90)"); break;
1631 	case DW_LANG_Pascal83:		printf ("(ANSI Pascal)"); break;
1632 	case DW_LANG_Modula2:		printf ("(Modula 2)"); break;
1633 	  /* DWARF 2.1 values.	*/
1634 	case DW_LANG_Java:		printf ("(Java)"); break;
1635 	case DW_LANG_C99:		printf ("(ANSI C99)"); break;
1636 	case DW_LANG_Ada95:		printf ("(ADA 95)"); break;
1637 	case DW_LANG_Fortran95:		printf ("(Fortran 95)"); break;
1638 	  /* DWARF 3 values.  */
1639 	case DW_LANG_PLI:		printf ("(PLI)"); break;
1640 	case DW_LANG_ObjC:		printf ("(Objective C)"); break;
1641 	case DW_LANG_ObjC_plus_plus:	printf ("(Objective C++)"); break;
1642 	case DW_LANG_UPC:		printf ("(Unified Parallel C)"); break;
1643 	case DW_LANG_D:			printf ("(D)"); break;
1644 	  /* DWARF 4 values.  */
1645 	case DW_LANG_Python:		printf ("(Python)"); break;
1646 	  /* DWARF 5 values.  */
1647 	case DW_LANG_Go:		printf ("(Go)"); break;
1648 	  /* MIPS extension.  */
1649 	case DW_LANG_Mips_Assembler:	printf ("(MIPS assembler)"); break;
1650 	  /* UPC extension.  */
1651 	case DW_LANG_Upc:		printf ("(Unified Parallel C)"); break;
1652 	default:
1653 	  if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1654 	    printf (_("(implementation defined: %s)"),
1655 		    dwarf_vmatoa ("x", uvalue));
1656 	  else
1657 	    printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
1658 	  break;
1659 	}
1660       break;
1661 
1662     case DW_AT_encoding:
1663       switch (uvalue)
1664 	{
1665 	case DW_ATE_void:		printf ("(void)"); break;
1666 	case DW_ATE_address:		printf ("(machine address)"); break;
1667 	case DW_ATE_boolean:		printf ("(boolean)"); break;
1668 	case DW_ATE_complex_float:	printf ("(complex float)"); break;
1669 	case DW_ATE_float:		printf ("(float)"); break;
1670 	case DW_ATE_signed:		printf ("(signed)"); break;
1671 	case DW_ATE_signed_char:	printf ("(signed char)"); break;
1672 	case DW_ATE_unsigned:		printf ("(unsigned)"); break;
1673 	case DW_ATE_unsigned_char:	printf ("(unsigned char)"); break;
1674 	  /* DWARF 2.1 values:  */
1675 	case DW_ATE_imaginary_float:	printf ("(imaginary float)"); break;
1676 	case DW_ATE_decimal_float:	printf ("(decimal float)"); break;
1677 	  /* DWARF 3 values:  */
1678 	case DW_ATE_packed_decimal:	printf ("(packed_decimal)"); break;
1679 	case DW_ATE_numeric_string:	printf ("(numeric_string)"); break;
1680 	case DW_ATE_edited:		printf ("(edited)"); break;
1681 	case DW_ATE_signed_fixed:	printf ("(signed_fixed)"); break;
1682 	case DW_ATE_unsigned_fixed:	printf ("(unsigned_fixed)"); break;
1683 	  /* HP extensions:  */
1684 	case DW_ATE_HP_float80:		printf ("(HP_float80)"); break;
1685 	case DW_ATE_HP_complex_float80:	printf ("(HP_complex_float80)"); break;
1686 	case DW_ATE_HP_float128:	printf ("(HP_float128)"); break;
1687 	case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
1688 	case DW_ATE_HP_floathpintel:	printf ("(HP_floathpintel)"); break;
1689 	case DW_ATE_HP_imaginary_float80:	printf ("(HP_imaginary_float80)"); break;
1690 	case DW_ATE_HP_imaginary_float128:	printf ("(HP_imaginary_float128)"); break;
1691 
1692 	default:
1693 	  if (uvalue >= DW_ATE_lo_user
1694 	      && uvalue <= DW_ATE_hi_user)
1695 	    printf (_("(user defined type)"));
1696 	  else
1697 	    printf (_("(unknown type)"));
1698 	  break;
1699 	}
1700       break;
1701 
1702     case DW_AT_accessibility:
1703       switch (uvalue)
1704 	{
1705 	case DW_ACCESS_public:		printf ("(public)"); break;
1706 	case DW_ACCESS_protected:	printf ("(protected)"); break;
1707 	case DW_ACCESS_private:		printf ("(private)"); break;
1708 	default:
1709 	  printf (_("(unknown accessibility)"));
1710 	  break;
1711 	}
1712       break;
1713 
1714     case DW_AT_visibility:
1715       switch (uvalue)
1716 	{
1717 	case DW_VIS_local:		printf ("(local)"); break;
1718 	case DW_VIS_exported:		printf ("(exported)"); break;
1719 	case DW_VIS_qualified:		printf ("(qualified)"); break;
1720 	default:			printf (_("(unknown visibility)")); break;
1721 	}
1722       break;
1723 
1724     case DW_AT_virtuality:
1725       switch (uvalue)
1726 	{
1727 	case DW_VIRTUALITY_none:	printf ("(none)"); break;
1728 	case DW_VIRTUALITY_virtual:	printf ("(virtual)"); break;
1729 	case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1730 	default:			printf (_("(unknown virtuality)")); break;
1731 	}
1732       break;
1733 
1734     case DW_AT_identifier_case:
1735       switch (uvalue)
1736 	{
1737 	case DW_ID_case_sensitive:	printf ("(case_sensitive)"); break;
1738 	case DW_ID_up_case:		printf ("(up_case)"); break;
1739 	case DW_ID_down_case:		printf ("(down_case)"); break;
1740 	case DW_ID_case_insensitive:	printf ("(case_insensitive)"); break;
1741 	default:			printf (_("(unknown case)")); break;
1742 	}
1743       break;
1744 
1745     case DW_AT_calling_convention:
1746       switch (uvalue)
1747 	{
1748 	case DW_CC_normal:	printf ("(normal)"); break;
1749 	case DW_CC_program:	printf ("(program)"); break;
1750 	case DW_CC_nocall:	printf ("(nocall)"); break;
1751 	default:
1752 	  if (uvalue >= DW_CC_lo_user
1753 	      && uvalue <= DW_CC_hi_user)
1754 	    printf (_("(user defined)"));
1755 	  else
1756 	    printf (_("(unknown convention)"));
1757 	}
1758       break;
1759 
1760     case DW_AT_ordering:
1761       switch (uvalue)
1762 	{
1763 	case -1: printf (_("(undefined)")); break;
1764 	case 0:  printf ("(row major)"); break;
1765 	case 1:  printf ("(column major)"); break;
1766 	}
1767       break;
1768 
1769     case DW_AT_frame_base:
1770       have_frame_base = 1;
1771     case DW_AT_location:
1772     case DW_AT_string_length:
1773     case DW_AT_return_addr:
1774     case DW_AT_data_member_location:
1775     case DW_AT_vtable_elem_location:
1776     case DW_AT_segment:
1777     case DW_AT_static_link:
1778     case DW_AT_use_location:
1779     case DW_AT_GNU_call_site_value:
1780     case DW_AT_GNU_call_site_data_value:
1781     case DW_AT_GNU_call_site_target:
1782     case DW_AT_GNU_call_site_target_clobbered:
1783       if ((dwarf_version < 4
1784            && (form == DW_FORM_data4 || form == DW_FORM_data8))
1785 	  || form == DW_FORM_sec_offset)
1786 	printf (_("(location list)"));
1787       /* Fall through.  */
1788     case DW_AT_allocated:
1789     case DW_AT_associated:
1790     case DW_AT_data_location:
1791     case DW_AT_stride:
1792     case DW_AT_upper_bound:
1793     case DW_AT_lower_bound:
1794       if (block_start)
1795 	{
1796 	  int need_frame_base;
1797 
1798 	  printf ("(");
1799 	  need_frame_base = decode_location_expression (block_start,
1800 							pointer_size,
1801 							offset_size,
1802 							dwarf_version,
1803 							uvalue,
1804 							cu_offset, section);
1805 	  printf (")");
1806 	  if (need_frame_base && !have_frame_base)
1807 	    printf (_(" [without DW_AT_frame_base]"));
1808 	}
1809       break;
1810 
1811     case DW_AT_import:
1812       {
1813 	if (form == DW_FORM_ref_sig8
1814 	    || form == DW_FORM_GNU_ref_alt)
1815           break;
1816 
1817 	if (form == DW_FORM_ref1
1818 	    || form == DW_FORM_ref2
1819 	    || form == DW_FORM_ref4
1820 	    || form == DW_FORM_ref_udata)
1821 	  uvalue += cu_offset;
1822 
1823 	if (uvalue >= section->size)
1824 	  warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset %lx is too big.\n"),
1825 		dwarf_vmatoa ("x", uvalue),
1826 		(unsigned long) (orig_data - section->start));
1827 	else
1828 	  {
1829 	    unsigned long abbrev_number;
1830 	    abbrev_entry * entry;
1831 
1832 	    abbrev_number = read_leb128 (section->start + uvalue, NULL, 0);
1833 
1834 	    printf (_("[Abbrev Number: %ld"), abbrev_number);
1835 	    /* Don't look up abbrev for DW_FORM_ref_addr, as it very often will
1836 	       use different abbrev table, and we don't track .debug_info chunks
1837 	       yet.  */
1838 	    if (form != DW_FORM_ref_addr)
1839 	      {
1840 		for (entry = first_abbrev; entry != NULL; entry = entry->next)
1841 		  if (entry->entry == abbrev_number)
1842 		    break;
1843 		if (entry != NULL)
1844 		  printf (" (%s)", get_TAG_name (entry->tag));
1845 	      }
1846 	    printf ("]");
1847 	  }
1848       }
1849       break;
1850 
1851     default:
1852       break;
1853     }
1854 
1855   return data;
1856 }
1857 
1858 static const char *
1859 get_AT_name (unsigned long attribute)
1860 {
1861   const char *name;
1862 
1863   /* One value is shared by the MIPS and HP extensions:  */
1864   if (attribute == DW_AT_MIPS_fde)
1865     return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1866 
1867   name = get_DW_AT_name (attribute);
1868 
1869   if (name == NULL)
1870     {
1871       static char buffer[100];
1872 
1873       snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1874 		attribute);
1875       return buffer;
1876     }
1877 
1878   return name;
1879 }
1880 
1881 static unsigned char *
1882 read_and_display_attr (unsigned long attribute,
1883 		       unsigned long form,
1884 		       unsigned char * data,
1885 		       dwarf_vma cu_offset,
1886 		       dwarf_vma pointer_size,
1887 		       dwarf_vma offset_size,
1888 		       int dwarf_version,
1889 		       debug_info * debug_info_p,
1890 		       int do_loc,
1891 		       struct dwarf_section * section)
1892 {
1893   if (!do_loc)
1894     printf ("   %-18s:", get_AT_name (attribute));
1895   data = read_and_display_attr_value (attribute, form, data, cu_offset,
1896 				      pointer_size, offset_size,
1897 				      dwarf_version, debug_info_p,
1898 				      do_loc, section);
1899   if (!do_loc)
1900     printf ("\n");
1901   return data;
1902 }
1903 
1904 
1905 /* Process the contents of a .debug_info section.  If do_loc is non-zero
1906    then we are scanning for location lists and we do not want to display
1907    anything to the user.  If do_types is non-zero, we are processing
1908    a .debug_types section instead of a .debug_info section.  */
1909 
1910 static int
1911 process_debug_info (struct dwarf_section *section,
1912 		    void *file,
1913                     enum dwarf_section_display_enum abbrev_sec,
1914 		    int do_loc,
1915 		    int do_types)
1916 {
1917   unsigned char *start = section->start;
1918   unsigned char *end = start + section->size;
1919   unsigned char *section_begin;
1920   unsigned int unit;
1921   unsigned int num_units = 0;
1922 
1923   if ((do_loc || do_debug_loc || do_debug_ranges)
1924       && num_debug_info_entries == 0
1925       && ! do_types)
1926     {
1927       dwarf_vma length;
1928 
1929       /* First scan the section to get the number of comp units.  */
1930       for (section_begin = start, num_units = 0; section_begin < end;
1931 	   num_units ++)
1932 	{
1933 	  /* Read the first 4 bytes.  For a 32-bit DWARF section, this
1934 	     will be the length.  For a 64-bit DWARF section, it'll be
1935 	     the escape code 0xffffffff followed by an 8 byte length.  */
1936 	  length = byte_get (section_begin, 4);
1937 
1938 	  if (length == 0xffffffff)
1939 	    {
1940 	      length = byte_get (section_begin + 4, 8);
1941 	      section_begin += length + 12;
1942 	    }
1943 	  else if (length >= 0xfffffff0 && length < 0xffffffff)
1944 	    {
1945 	      warn (_("Reserved length value (0x%s) found in section %s\n"),
1946 		    dwarf_vmatoa ("x", length), section->name);
1947 	      return 0;
1948 	    }
1949 	  else
1950 	    section_begin += length + 4;
1951 
1952 	  /* Negative values are illegal, they may even cause infinite
1953 	     looping.  This can happen if we can't accurately apply
1954 	     relocations to an object file.  */
1955 	  if ((signed long) length <= 0)
1956 	    {
1957 	      warn (_("Corrupt unit length (0x%s) found in section %s\n"),
1958 		    dwarf_vmatoa ("x", length), section->name);
1959 	      return 0;
1960 	    }
1961 	}
1962 
1963       if (num_units == 0)
1964 	{
1965 	  error (_("No comp units in %s section ?"), section->name);
1966 	  return 0;
1967 	}
1968 
1969       /* Then allocate an array to hold the information.  */
1970       debug_information = (debug_info *) cmalloc (num_units,
1971                                                   sizeof (* debug_information));
1972       if (debug_information == NULL)
1973 	{
1974 	  error (_("Not enough memory for a debug info array of %u entries"),
1975 		 num_units);
1976 	  return 0;
1977 	}
1978     }
1979 
1980   if (!do_loc)
1981     {
1982       if (dwarf_start_die == 0)
1983 	printf (_("Contents of the %s section:\n\n"), section->name);
1984 
1985       load_debug_section (str, file);
1986       load_debug_section (str_dwo, file);
1987       load_debug_section (str_index, file);
1988       load_debug_section (str_index_dwo, file);
1989       load_debug_section (debug_addr, file);
1990     }
1991 
1992   load_debug_section (abbrev_sec, file);
1993   if (debug_displays [abbrev_sec].section.start == NULL)
1994     {
1995       warn (_("Unable to locate %s section!\n"),
1996 	    debug_displays [abbrev_sec].section.name);
1997       return 0;
1998     }
1999 
2000   for (section_begin = start, unit = 0; start < end; unit++)
2001     {
2002       DWARF2_Internal_CompUnit compunit;
2003       unsigned char *hdrptr;
2004       unsigned char *tags;
2005       int level, last_level, saved_level;
2006       dwarf_vma cu_offset;
2007       int offset_size;
2008       int initial_length_size;
2009       dwarf_vma signature_high = 0;
2010       dwarf_vma signature_low = 0;
2011       dwarf_vma type_offset = 0;
2012 
2013       hdrptr = start;
2014 
2015       compunit.cu_length = byte_get (hdrptr, 4);
2016       hdrptr += 4;
2017 
2018       if (compunit.cu_length == 0xffffffff)
2019 	{
2020 	  compunit.cu_length = byte_get (hdrptr, 8);
2021 	  hdrptr += 8;
2022 	  offset_size = 8;
2023 	  initial_length_size = 12;
2024 	}
2025       else
2026 	{
2027 	  offset_size = 4;
2028 	  initial_length_size = 4;
2029 	}
2030 
2031       compunit.cu_version = byte_get (hdrptr, 2);
2032       hdrptr += 2;
2033 
2034       cu_offset = start - section_begin;
2035 
2036       compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
2037       hdrptr += offset_size;
2038 
2039       compunit.cu_pointer_size = byte_get (hdrptr, 1);
2040       hdrptr += 1;
2041 
2042       if (do_types)
2043         {
2044           byte_get_64 (hdrptr, &signature_high, &signature_low);
2045           hdrptr += 8;
2046           type_offset = byte_get (hdrptr, offset_size);
2047           hdrptr += offset_size;
2048         }
2049 
2050       if ((do_loc || do_debug_loc || do_debug_ranges)
2051 	  && num_debug_info_entries == 0
2052 	  && ! do_types)
2053 	{
2054 	  debug_information [unit].cu_offset = cu_offset;
2055 	  debug_information [unit].pointer_size
2056 	    = compunit.cu_pointer_size;
2057 	  debug_information [unit].offset_size = offset_size;
2058 	  debug_information [unit].dwarf_version = compunit.cu_version;
2059 	  debug_information [unit].base_address = 0;
2060 	  debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
2061 	  debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
2062 	  debug_information [unit].loc_offsets = NULL;
2063 	  debug_information [unit].have_frame_base = NULL;
2064 	  debug_information [unit].max_loc_offsets = 0;
2065 	  debug_information [unit].num_loc_offsets = 0;
2066 	  debug_information [unit].range_lists = NULL;
2067 	  debug_information [unit].max_range_lists= 0;
2068 	  debug_information [unit].num_range_lists = 0;
2069 	}
2070 
2071       if (!do_loc && dwarf_start_die == 0)
2072 	{
2073 	  printf (_("  Compilation Unit @ offset 0x%s:\n"),
2074 		  dwarf_vmatoa ("x", cu_offset));
2075 	  printf (_("   Length:        0x%s (%s)\n"),
2076 		  dwarf_vmatoa ("x", compunit.cu_length),
2077 		  offset_size == 8 ? "64-bit" : "32-bit");
2078 	  printf (_("   Version:       %d\n"), compunit.cu_version);
2079 	  printf (_("   Abbrev Offset: 0x%s\n"),
2080 		  dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
2081 	  printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
2082 	  if (do_types)
2083 	    {
2084 	      char buf[64];
2085 
2086 	      printf (_("   Signature:     0x%s\n"),
2087 		      dwarf_vmatoa64 (signature_high, signature_low,
2088 				      buf, sizeof (buf)));
2089 	      printf (_("   Type Offset:   0x%s\n"),
2090 		      dwarf_vmatoa ("x", type_offset));
2091 	    }
2092 	}
2093 
2094       if (cu_offset + compunit.cu_length + initial_length_size
2095 	  > section->size)
2096 	{
2097 	  warn (_("Debug info is corrupted, length of CU at %s"
2098 	  	  " extends beyond end of section (length = %s)\n"),
2099 		dwarf_vmatoa ("x", cu_offset),
2100 		dwarf_vmatoa ("x", compunit.cu_length));
2101 	  break;
2102 	}
2103       tags = hdrptr;
2104       start += compunit.cu_length + initial_length_size;
2105 
2106       if (compunit.cu_version != 2
2107 	  && compunit.cu_version != 3
2108 	  && compunit.cu_version != 4)
2109 	{
2110 	  warn (_("CU at offset %s contains corrupt or "
2111 		  "unsupported version number: %d.\n"),
2112 		dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
2113 	  continue;
2114 	}
2115 
2116       free_abbrevs ();
2117 
2118       /* Process the abbrevs used by this compilation unit. DWARF
2119 	 sections under Mach-O have non-zero addresses.  */
2120       if (compunit.cu_abbrev_offset >= debug_displays [abbrev_sec].section.size)
2121 	warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
2122 	      (unsigned long) compunit.cu_abbrev_offset,
2123 	      (unsigned long) debug_displays [abbrev_sec].section.size);
2124       else
2125 	process_abbrev_section
2126 	  ((unsigned char *) debug_displays [abbrev_sec].section.start
2127 	   + compunit.cu_abbrev_offset,
2128 	   (unsigned char *) debug_displays [abbrev_sec].section.start
2129 	   + debug_displays [abbrev_sec].section.size);
2130 
2131       level = 0;
2132       last_level = level;
2133       saved_level = -1;
2134       while (tags < start)
2135 	{
2136 	  unsigned int bytes_read;
2137 	  unsigned long abbrev_number;
2138 	  unsigned long die_offset;
2139 	  abbrev_entry *entry;
2140 	  abbrev_attr *attr;
2141 	  int do_printing = 1;
2142 
2143 	  die_offset = tags - section_begin;
2144 
2145 	  abbrev_number = read_leb128 (tags, & bytes_read, 0);
2146 	  tags += bytes_read;
2147 
2148 	  /* A null DIE marks the end of a list of siblings or it may also be
2149 	     a section padding.  */
2150 	  if (abbrev_number == 0)
2151 	    {
2152 	      /* Check if it can be a section padding for the last CU.  */
2153 	      if (level == 0 && start == end)
2154 		{
2155 		  unsigned char *chk;
2156 
2157 		  for (chk = tags; chk < start; chk++)
2158 		    if (*chk != 0)
2159 		      break;
2160 		  if (chk == start)
2161 		    break;
2162 		}
2163 
2164 	      --level;
2165 	      if (level < 0)
2166 		{
2167 		  static unsigned num_bogus_warns = 0;
2168 
2169 		  if (num_bogus_warns < 3)
2170 		    {
2171 		      warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
2172 			    die_offset, section->name);
2173 		      num_bogus_warns ++;
2174 		      if (num_bogus_warns == 3)
2175 			warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
2176 		    }
2177 		}
2178 	      if (dwarf_start_die != 0 && level < saved_level)
2179 		return 1;
2180 	      continue;
2181 	    }
2182 
2183 	  if (!do_loc)
2184 	    {
2185 	      if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
2186 		do_printing = 0;
2187 	      else
2188 		{
2189 		  if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
2190 		    saved_level = level;
2191 		  do_printing = (dwarf_cutoff_level == -1
2192 				 || level < dwarf_cutoff_level);
2193 		  if (do_printing)
2194 		    printf (_(" <%d><%lx>: Abbrev Number: %lu"),
2195 			    level, die_offset, abbrev_number);
2196 		  else if (dwarf_cutoff_level == -1
2197 			   || last_level < dwarf_cutoff_level)
2198 		    printf (_(" <%d><%lx>: ...\n"), level, die_offset);
2199 		  last_level = level;
2200 		}
2201 	    }
2202 
2203 	  /* Scan through the abbreviation list until we reach the
2204 	     correct entry.  */
2205 	  for (entry = first_abbrev;
2206 	       entry && entry->entry != abbrev_number;
2207 	       entry = entry->next)
2208 	    continue;
2209 
2210 	  if (entry == NULL)
2211 	    {
2212 	      if (!do_loc && do_printing)
2213 		{
2214 		  printf ("\n");
2215 		  fflush (stdout);
2216 		}
2217 	      warn (_("DIE at offset %lx refers to abbreviation number %lu which does not exist\n"),
2218 		    die_offset, abbrev_number);
2219 	      return 0;
2220 	    }
2221 
2222 	  if (!do_loc && do_printing)
2223 	    printf (" (%s)\n", get_TAG_name (entry->tag));
2224 
2225 	  switch (entry->tag)
2226 	    {
2227 	    default:
2228 	      need_base_address = 0;
2229 	      break;
2230 	    case DW_TAG_compile_unit:
2231 	      need_base_address = 1;
2232 	      break;
2233 	    case DW_TAG_entry_point:
2234 	    case DW_TAG_subprogram:
2235 	      need_base_address = 0;
2236 	      /* Assuming that there is no DW_AT_frame_base.  */
2237 	      have_frame_base = 0;
2238 	      break;
2239 	    }
2240 
2241 	  for (attr = entry->first_attr; attr; attr = attr->next)
2242 	    {
2243 	      debug_info *arg;
2244 
2245 	      if (! do_loc && do_printing)
2246 		/* Show the offset from where the tag was extracted.  */
2247 		printf ("    <%lx>", (unsigned long)(tags - section_begin));
2248 
2249 	      arg = debug_information;
2250 	      if (debug_information)
2251 		arg += unit;
2252 
2253 	      tags = read_and_display_attr (attr->attribute,
2254 					    attr->form,
2255 					    tags, cu_offset,
2256 					    compunit.cu_pointer_size,
2257 					    offset_size,
2258 					    compunit.cu_version,
2259 					    arg,
2260 					    do_loc || ! do_printing, section);
2261 	    }
2262 
2263  	  if (entry->children)
2264  	    ++level;
2265  	}
2266     }
2267 
2268   /* Set num_debug_info_entries here so that it can be used to check if
2269      we need to process .debug_loc and .debug_ranges sections.  */
2270   if ((do_loc || do_debug_loc || do_debug_ranges)
2271       && num_debug_info_entries == 0
2272       && ! do_types)
2273     num_debug_info_entries = num_units;
2274 
2275   if (!do_loc)
2276     printf ("\n");
2277 
2278   return 1;
2279 }
2280 
2281 /* Locate and scan the .debug_info section in the file and record the pointer
2282    sizes and offsets for the compilation units in it.  Usually an executable
2283    will have just one pointer size, but this is not guaranteed, and so we try
2284    not to make any assumptions.  Returns zero upon failure, or the number of
2285    compilation units upon success.  */
2286 
2287 static unsigned int
2288 load_debug_info (void * file)
2289 {
2290   /* Reset the last pointer size so that we can issue correct error
2291      messages if we are displaying the contents of more than one section.  */
2292   last_pointer_size = 0;
2293   warned_about_missing_comp_units = FALSE;
2294 
2295   /* If we have already tried and failed to load the .debug_info
2296      section then do not bother to repear the task.  */
2297   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2298     return 0;
2299 
2300   /* If we already have the information there is nothing else to do.  */
2301   if (num_debug_info_entries > 0)
2302     return num_debug_info_entries;
2303 
2304   if (load_debug_section (info, file)
2305       && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
2306     return num_debug_info_entries;
2307   else if (load_debug_section (info_dwo, file)
2308            && process_debug_info (&debug_displays [info_dwo].section, file,
2309                                   abbrev_dwo, 1, 0))
2310     return num_debug_info_entries;
2311 
2312   num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2313   return 0;
2314 }
2315 
2316 static int
2317 display_debug_lines_raw (struct dwarf_section *section,
2318 			 unsigned char *data,
2319                          unsigned char *end)
2320 {
2321   unsigned char *start = section->start;
2322 
2323   printf (_("Raw dump of debug contents of section %s:\n\n"),
2324           section->name);
2325 
2326   while (data < end)
2327     {
2328       DWARF2_Internal_LineInfo linfo;
2329       unsigned char *standard_opcodes;
2330       unsigned char *end_of_sequence;
2331       unsigned char *hdrptr;
2332       unsigned long hdroff;
2333       int initial_length_size;
2334       int offset_size;
2335       int i;
2336 
2337       hdrptr = data;
2338       hdroff = hdrptr - start;
2339 
2340       /* Check the length of the block.  */
2341       linfo.li_length = byte_get (hdrptr, 4);
2342       hdrptr += 4;
2343 
2344       if (linfo.li_length == 0xffffffff)
2345 	{
2346 	  /* This section is 64-bit DWARF 3.  */
2347 	  linfo.li_length = byte_get (hdrptr, 8);
2348 	  hdrptr += 8;
2349 	  offset_size = 8;
2350 	  initial_length_size = 12;
2351 	}
2352       else
2353 	{
2354 	  offset_size = 4;
2355 	  initial_length_size = 4;
2356 	}
2357 
2358       if (linfo.li_length + initial_length_size > section->size)
2359 	{
2360 	  warn
2361 	    (_("The information in section %s appears to be corrupt - the section is too small\n"),
2362 	     section->name);
2363 	  return 0;
2364 	}
2365 
2366       /* Check its version number.  */
2367       linfo.li_version = byte_get (hdrptr, 2);
2368       hdrptr += 2;
2369       if (linfo.li_version != 2
2370 	  && linfo.li_version != 3
2371 	  && linfo.li_version != 4)
2372 	{
2373 	  warn (_("Only DWARF version 2, 3 and 4 line info is currently supported.\n"));
2374 	  return 0;
2375 	}
2376 
2377       linfo.li_prologue_length = byte_get (hdrptr, offset_size);
2378       hdrptr += offset_size;
2379       linfo.li_min_insn_length = byte_get (hdrptr, 1);
2380       hdrptr++;
2381       if (linfo.li_version >= 4)
2382 	{
2383 	  linfo.li_max_ops_per_insn = byte_get (hdrptr, 1);
2384 	  hdrptr++;
2385 	  if (linfo.li_max_ops_per_insn == 0)
2386 	    {
2387 	      warn (_("Invalid maximum operations per insn.\n"));
2388 	      return 0;
2389 	    }
2390 	}
2391       else
2392 	linfo.li_max_ops_per_insn = 1;
2393       linfo.li_default_is_stmt = byte_get (hdrptr, 1);
2394       hdrptr++;
2395       linfo.li_line_base = byte_get (hdrptr, 1);
2396       hdrptr++;
2397       linfo.li_line_range = byte_get (hdrptr, 1);
2398       hdrptr++;
2399       linfo.li_opcode_base = byte_get (hdrptr, 1);
2400       hdrptr++;
2401 
2402       /* Sign extend the line base field.  */
2403       linfo.li_line_base <<= 24;
2404       linfo.li_line_base >>= 24;
2405 
2406       printf (_("  Offset:                      0x%lx\n"), hdroff);
2407       printf (_("  Length:                      %ld\n"), (long) linfo.li_length);
2408       printf (_("  DWARF Version:               %d\n"), linfo.li_version);
2409       printf (_("  Prologue Length:             %d\n"), linfo.li_prologue_length);
2410       printf (_("  Minimum Instruction Length:  %d\n"), linfo.li_min_insn_length);
2411       if (linfo.li_version >= 4)
2412 	printf (_("  Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
2413       printf (_("  Initial value of 'is_stmt':  %d\n"), linfo.li_default_is_stmt);
2414       printf (_("  Line Base:                   %d\n"), linfo.li_line_base);
2415       printf (_("  Line Range:                  %d\n"), linfo.li_line_range);
2416       printf (_("  Opcode Base:                 %d\n"), linfo.li_opcode_base);
2417 
2418       end_of_sequence = data + linfo.li_length + initial_length_size;
2419 
2420       reset_state_machine (linfo.li_default_is_stmt);
2421 
2422       /* Display the contents of the Opcodes table.  */
2423       standard_opcodes = hdrptr;
2424 
2425       printf (_("\n Opcodes:\n"));
2426 
2427       for (i = 1; i < linfo.li_opcode_base; i++)
2428 	printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
2429 
2430       /* Display the contents of the Directory table.  */
2431       data = standard_opcodes + linfo.li_opcode_base - 1;
2432 
2433       if (*data == 0)
2434 	printf (_("\n The Directory Table is empty.\n"));
2435       else
2436 	{
2437 	  printf (_("\n The Directory Table:\n"));
2438 
2439 	  while (*data != 0)
2440 	    {
2441 	      printf ("  %s\n", data);
2442 
2443 	      data += strlen ((char *) data) + 1;
2444 	    }
2445 	}
2446 
2447       /* Skip the NUL at the end of the table.  */
2448       data++;
2449 
2450       /* Display the contents of the File Name table.  */
2451       if (*data == 0)
2452 	printf (_("\n The File Name Table is empty.\n"));
2453       else
2454 	{
2455 	  printf (_("\n The File Name Table:\n"));
2456 	  printf (_("  Entry\tDir\tTime\tSize\tName\n"));
2457 
2458 	  while (*data != 0)
2459 	    {
2460 	      unsigned char *name;
2461 	      unsigned int bytes_read;
2462 
2463 	      printf ("  %d\t", ++state_machine_regs.last_file_entry);
2464 	      name = data;
2465 
2466 	      data += strlen ((char *) data) + 1;
2467 
2468 	      printf ("%s\t",
2469 		      dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2470 	      data += bytes_read;
2471 	      printf ("%s\t",
2472 		      dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2473 	      data += bytes_read;
2474 	      printf ("%s\t",
2475 		      dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2476 	      data += bytes_read;
2477 	      printf ("%s\n", name);
2478 	    }
2479 	}
2480 
2481       /* Skip the NUL at the end of the table.  */
2482       data++;
2483 
2484       /* Now display the statements.  */
2485       printf (_("\n Line Number Statements:\n"));
2486 
2487       while (data < end_of_sequence)
2488 	{
2489 	  unsigned char op_code;
2490 	  dwarf_signed_vma adv;
2491 	  dwarf_vma uladv;
2492 	  unsigned int bytes_read;
2493 
2494 	  op_code = *data++;
2495 
2496 	  if (op_code >= linfo.li_opcode_base)
2497 	    {
2498 	      op_code -= linfo.li_opcode_base;
2499 	      uladv = (op_code / linfo.li_line_range);
2500 	      if (linfo.li_max_ops_per_insn == 1)
2501 		{
2502 		  uladv *= linfo.li_min_insn_length;
2503 		  state_machine_regs.address += uladv;
2504 		  printf (_("  Special opcode %d: "
2505 			    "advance Address by %s to 0x%s"),
2506 			  op_code, dwarf_vmatoa ("u", uladv),
2507 			  dwarf_vmatoa ("x", state_machine_regs.address));
2508 		}
2509 	      else
2510 		{
2511 		  state_machine_regs.address
2512 		    += ((state_machine_regs.op_index + uladv)
2513 			/ linfo.li_max_ops_per_insn)
2514 		       * linfo.li_min_insn_length;
2515 		  state_machine_regs.op_index
2516 		    = (state_machine_regs.op_index + uladv)
2517 		      % linfo.li_max_ops_per_insn;
2518 		  printf (_("  Special opcode %d: "
2519 			    "advance Address by %s to 0x%s[%d]"),
2520 			  op_code, dwarf_vmatoa ("u", uladv),
2521 			  dwarf_vmatoa ("x", state_machine_regs.address),
2522 			  state_machine_regs.op_index);
2523 		}
2524 	      adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
2525 	      state_machine_regs.line += adv;
2526 	      printf (_(" and Line by %s to %d\n"),
2527 		      dwarf_vmatoa ("d", adv), state_machine_regs.line);
2528 	    }
2529 	  else switch (op_code)
2530 	    {
2531 	    case DW_LNS_extended_op:
2532 	      data += process_extended_line_op (data, linfo.li_default_is_stmt);
2533 	      break;
2534 
2535 	    case DW_LNS_copy:
2536 	      printf (_("  Copy\n"));
2537 	      break;
2538 
2539 	    case DW_LNS_advance_pc:
2540 	      uladv = read_leb128 (data, & bytes_read, 0);
2541 	      data += bytes_read;
2542 	      if (linfo.li_max_ops_per_insn == 1)
2543 		{
2544 		  uladv *= linfo.li_min_insn_length;
2545 		  state_machine_regs.address += uladv;
2546 		  printf (_("  Advance PC by %s to 0x%s\n"),
2547 			  dwarf_vmatoa ("u", uladv),
2548 			  dwarf_vmatoa ("x", state_machine_regs.address));
2549 		}
2550 	      else
2551 		{
2552 		  state_machine_regs.address
2553 		    += ((state_machine_regs.op_index + uladv)
2554 			/ linfo.li_max_ops_per_insn)
2555 		       * linfo.li_min_insn_length;
2556 		  state_machine_regs.op_index
2557 		    = (state_machine_regs.op_index + uladv)
2558 		      % linfo.li_max_ops_per_insn;
2559 		  printf (_("  Advance PC by %s to 0x%s[%d]\n"),
2560 			  dwarf_vmatoa ("u", uladv),
2561 			  dwarf_vmatoa ("x", state_machine_regs.address),
2562 			  state_machine_regs.op_index);
2563 		}
2564 	      break;
2565 
2566 	    case DW_LNS_advance_line:
2567 	      adv = read_sleb128 (data, & bytes_read);
2568 	      data += bytes_read;
2569 	      state_machine_regs.line += adv;
2570 	      printf (_("  Advance Line by %s to %d\n"),
2571 		        dwarf_vmatoa ("d", adv),
2572 			state_machine_regs.line);
2573 	      break;
2574 
2575 	    case DW_LNS_set_file:
2576 	      adv = read_leb128 (data, & bytes_read, 0);
2577 	      data += bytes_read;
2578 	      printf (_("  Set File Name to entry %s in the File Name Table\n"),
2579 		      dwarf_vmatoa ("d", adv));
2580 	      state_machine_regs.file = adv;
2581 	      break;
2582 
2583 	    case DW_LNS_set_column:
2584 	      uladv = read_leb128 (data, & bytes_read, 0);
2585 	      data += bytes_read;
2586 	      printf (_("  Set column to %s\n"),
2587 		      dwarf_vmatoa ("u", uladv));
2588 	      state_machine_regs.column = uladv;
2589 	      break;
2590 
2591 	    case DW_LNS_negate_stmt:
2592 	      adv = state_machine_regs.is_stmt;
2593 	      adv = ! adv;
2594 	      printf (_("  Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
2595 	      state_machine_regs.is_stmt = adv;
2596 	      break;
2597 
2598 	    case DW_LNS_set_basic_block:
2599 	      printf (_("  Set basic block\n"));
2600 	      state_machine_regs.basic_block = 1;
2601 	      break;
2602 
2603 	    case DW_LNS_const_add_pc:
2604 	      uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
2605 	      if (linfo.li_max_ops_per_insn)
2606 		{
2607 		  uladv *= linfo.li_min_insn_length;
2608 		  state_machine_regs.address += uladv;
2609 		  printf (_("  Advance PC by constant %s to 0x%s\n"),
2610 			  dwarf_vmatoa ("u", uladv),
2611 			  dwarf_vmatoa ("x", state_machine_regs.address));
2612 		}
2613 	      else
2614 		{
2615 		  state_machine_regs.address
2616 		    += ((state_machine_regs.op_index + uladv)
2617 			/ linfo.li_max_ops_per_insn)
2618 		       * linfo.li_min_insn_length;
2619 		  state_machine_regs.op_index
2620 		    = (state_machine_regs.op_index + uladv)
2621 		      % linfo.li_max_ops_per_insn;
2622 		  printf (_("  Advance PC by constant %s to 0x%s[%d]\n"),
2623 			  dwarf_vmatoa ("u", uladv),
2624 			  dwarf_vmatoa ("x", state_machine_regs.address),
2625 			  state_machine_regs.op_index);
2626 		}
2627 	      break;
2628 
2629 	    case DW_LNS_fixed_advance_pc:
2630 	      uladv = byte_get (data, 2);
2631 	      data += 2;
2632 	      state_machine_regs.address += uladv;
2633 	      state_machine_regs.op_index = 0;
2634 	      printf (_("  Advance PC by fixed size amount %s to 0x%s\n"),
2635 		      dwarf_vmatoa ("u", uladv),
2636 		      dwarf_vmatoa ("x", state_machine_regs.address));
2637 	      break;
2638 
2639 	    case DW_LNS_set_prologue_end:
2640 	      printf (_("  Set prologue_end to true\n"));
2641 	      break;
2642 
2643 	    case DW_LNS_set_epilogue_begin:
2644 	      printf (_("  Set epilogue_begin to true\n"));
2645 	      break;
2646 
2647 	    case DW_LNS_set_isa:
2648 	      uladv = read_leb128 (data, & bytes_read, 0);
2649 	      data += bytes_read;
2650 	      printf (_("  Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
2651 	      break;
2652 
2653 	    default:
2654 	      printf (_("  Unknown opcode %d with operands: "), op_code);
2655 
2656 	      for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2657 		{
2658 		  printf ("0x%s%s", dwarf_vmatoa ("x", read_leb128 (data,
2659 							 &bytes_read, 0)),
2660 			  i == 1 ? "" : ", ");
2661 		  data += bytes_read;
2662 		}
2663 	      putchar ('\n');
2664 	      break;
2665 	    }
2666 	}
2667       putchar ('\n');
2668     }
2669 
2670   return 1;
2671 }
2672 
2673 typedef struct
2674 {
2675   unsigned char *name;
2676   unsigned int directory_index;
2677   unsigned int modification_date;
2678   unsigned int length;
2679 } File_Entry;
2680 
2681 /* Output a decoded representation of the .debug_line section.  */
2682 
2683 static int
2684 display_debug_lines_decoded (struct dwarf_section *section,
2685 			     unsigned char *data,
2686 			     unsigned char *end)
2687 {
2688   printf (_("Decoded dump of debug contents of section %s:\n\n"),
2689           section->name);
2690 
2691   while (data < end)
2692     {
2693       /* This loop amounts to one iteration per compilation unit.  */
2694       DWARF2_Internal_LineInfo linfo;
2695       unsigned char *standard_opcodes;
2696       unsigned char *end_of_sequence;
2697       unsigned char *hdrptr;
2698       int initial_length_size;
2699       int offset_size;
2700       int i;
2701       File_Entry *file_table = NULL;
2702       unsigned int n_files = 0;
2703       unsigned char **directory_table = NULL;
2704       unsigned int n_directories = 0;
2705 
2706       hdrptr = data;
2707 
2708       /* Extract information from the Line Number Program Header.
2709         (section 6.2.4 in the Dwarf3 doc).  */
2710 
2711       /* Get the length of this CU's line number information block.  */
2712       linfo.li_length = byte_get (hdrptr, 4);
2713       hdrptr += 4;
2714 
2715       if (linfo.li_length == 0xffffffff)
2716         {
2717           /* This section is 64-bit DWARF 3.  */
2718           linfo.li_length = byte_get (hdrptr, 8);
2719           hdrptr += 8;
2720           offset_size = 8;
2721           initial_length_size = 12;
2722         }
2723       else
2724         {
2725           offset_size = 4;
2726           initial_length_size = 4;
2727         }
2728 
2729       if (linfo.li_length + initial_length_size > section->size)
2730         {
2731           warn (_("The line info appears to be corrupt - "
2732                   "the section is too small\n"));
2733           return 0;
2734         }
2735 
2736       /* Get this CU's Line Number Block version number.  */
2737       linfo.li_version = byte_get (hdrptr, 2);
2738       hdrptr += 2;
2739       if (linfo.li_version != 2
2740 	  && linfo.li_version != 3
2741 	  && linfo.li_version != 4)
2742         {
2743           warn (_("Only DWARF version 2, 3 and 4 line info is currently "
2744                 "supported.\n"));
2745           return 0;
2746         }
2747 
2748       linfo.li_prologue_length = byte_get (hdrptr, offset_size);
2749       hdrptr += offset_size;
2750       linfo.li_min_insn_length = byte_get (hdrptr, 1);
2751       hdrptr++;
2752       if (linfo.li_version >= 4)
2753 	{
2754 	  linfo.li_max_ops_per_insn = byte_get (hdrptr, 1);
2755 	  hdrptr++;
2756 	  if (linfo.li_max_ops_per_insn == 0)
2757 	    {
2758 	      warn (_("Invalid maximum operations per insn.\n"));
2759 	      return 0;
2760 	    }
2761 	}
2762       else
2763 	linfo.li_max_ops_per_insn = 1;
2764       linfo.li_default_is_stmt = byte_get (hdrptr, 1);
2765       hdrptr++;
2766       linfo.li_line_base = byte_get (hdrptr, 1);
2767       hdrptr++;
2768       linfo.li_line_range = byte_get (hdrptr, 1);
2769       hdrptr++;
2770       linfo.li_opcode_base = byte_get (hdrptr, 1);
2771       hdrptr++;
2772 
2773       /* Sign extend the line base field.  */
2774       linfo.li_line_base <<= 24;
2775       linfo.li_line_base >>= 24;
2776 
2777       /* Find the end of this CU's Line Number Information Block.  */
2778       end_of_sequence = data + linfo.li_length + initial_length_size;
2779 
2780       reset_state_machine (linfo.li_default_is_stmt);
2781 
2782       /* Save a pointer to the contents of the Opcodes table.  */
2783       standard_opcodes = hdrptr;
2784 
2785       /* Traverse the Directory table just to count entries.  */
2786       data = standard_opcodes + linfo.li_opcode_base - 1;
2787       if (*data != 0)
2788         {
2789           unsigned char *ptr_directory_table = data;
2790 
2791 	  while (*data != 0)
2792 	    {
2793 	      data += strlen ((char *) data) + 1;
2794 	      n_directories++;
2795 	    }
2796 
2797           /* Go through the directory table again to save the directories.  */
2798           directory_table = (unsigned char **)
2799               xmalloc (n_directories * sizeof (unsigned char *));
2800 
2801           i = 0;
2802           while (*ptr_directory_table != 0)
2803             {
2804               directory_table[i] = ptr_directory_table;
2805               ptr_directory_table += strlen ((char *) ptr_directory_table) + 1;
2806               i++;
2807             }
2808         }
2809       /* Skip the NUL at the end of the table.  */
2810       data++;
2811 
2812       /* Traverse the File Name table just to count the entries.  */
2813       if (*data != 0)
2814         {
2815           unsigned char *ptr_file_name_table = data;
2816 
2817           while (*data != 0)
2818             {
2819 	      unsigned int bytes_read;
2820 
2821               /* Skip Name, directory index, last modification time and length
2822                  of file.  */
2823               data += strlen ((char *) data) + 1;
2824               read_leb128 (data, & bytes_read, 0);
2825               data += bytes_read;
2826               read_leb128 (data, & bytes_read, 0);
2827               data += bytes_read;
2828               read_leb128 (data, & bytes_read, 0);
2829               data += bytes_read;
2830 
2831               n_files++;
2832             }
2833 
2834           /* Go through the file table again to save the strings.  */
2835           file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
2836 
2837           i = 0;
2838           while (*ptr_file_name_table != 0)
2839             {
2840               unsigned int bytes_read;
2841 
2842               file_table[i].name = ptr_file_name_table;
2843               ptr_file_name_table += strlen ((char *) ptr_file_name_table) + 1;
2844 
2845               /* We are not interested in directory, time or size.  */
2846               file_table[i].directory_index = read_leb128 (ptr_file_name_table,
2847                                                            & bytes_read, 0);
2848               ptr_file_name_table += bytes_read;
2849               file_table[i].modification_date = read_leb128 (ptr_file_name_table,
2850 							     & bytes_read, 0);
2851               ptr_file_name_table += bytes_read;
2852               file_table[i].length = read_leb128 (ptr_file_name_table, & bytes_read, 0);
2853               ptr_file_name_table += bytes_read;
2854               i++;
2855             }
2856           i = 0;
2857 
2858           /* Print the Compilation Unit's name and a header.  */
2859           if (directory_table == NULL)
2860             {
2861               printf (_("CU: %s:\n"), file_table[0].name);
2862               printf (_("File name                            Line number    Starting address\n"));
2863             }
2864           else
2865             {
2866               unsigned int ix = file_table[0].directory_index;
2867               const char *directory = ix ? (char *)directory_table[ix - 1] : ".";
2868               if (do_wide || strlen (directory) < 76)
2869 		printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
2870               else
2871 		printf ("%s:\n", file_table[0].name);
2872 
2873               printf (_("File name                            Line number    Starting address\n"));
2874             }
2875         }
2876 
2877       /* Skip the NUL at the end of the table.  */
2878       data++;
2879 
2880       /* This loop iterates through the Dwarf Line Number Program.  */
2881       while (data < end_of_sequence)
2882         {
2883 	  unsigned char op_code;
2884           int adv;
2885           unsigned long int uladv;
2886           unsigned int bytes_read;
2887           int is_special_opcode = 0;
2888 
2889           op_code = *data++;
2890 
2891           if (op_code >= linfo.li_opcode_base)
2892 	    {
2893 	      op_code -= linfo.li_opcode_base;
2894 	      uladv = (op_code / linfo.li_line_range);
2895 	      if (linfo.li_max_ops_per_insn == 1)
2896 		{
2897 		  uladv *= linfo.li_min_insn_length;
2898 		  state_machine_regs.address += uladv;
2899 		}
2900 	      else
2901 		{
2902 		  state_machine_regs.address
2903 		    += ((state_machine_regs.op_index + uladv)
2904 			/ linfo.li_max_ops_per_insn)
2905 		       * linfo.li_min_insn_length;
2906 		  state_machine_regs.op_index
2907 		    = (state_machine_regs.op_index + uladv)
2908 		      % linfo.li_max_ops_per_insn;
2909 		}
2910 
2911               adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
2912               state_machine_regs.line += adv;
2913               is_special_opcode = 1;
2914             }
2915           else switch (op_code)
2916             {
2917             case DW_LNS_extended_op:
2918               {
2919                 unsigned int ext_op_code_len;
2920                 unsigned char ext_op_code;
2921                 unsigned char *op_code_data = data;
2922 
2923                 ext_op_code_len = read_leb128 (op_code_data, &bytes_read, 0);
2924                 op_code_data += bytes_read;
2925 
2926                 if (ext_op_code_len == 0)
2927                   {
2928                     warn (_("badly formed extended line op encountered!\n"));
2929                     break;
2930                   }
2931                 ext_op_code_len += bytes_read;
2932                 ext_op_code = *op_code_data++;
2933 
2934                 switch (ext_op_code)
2935                   {
2936                   case DW_LNE_end_sequence:
2937                     reset_state_machine (linfo.li_default_is_stmt);
2938                     break;
2939                   case DW_LNE_set_address:
2940                     state_machine_regs.address =
2941                     byte_get (op_code_data, ext_op_code_len - bytes_read - 1);
2942 		    state_machine_regs.op_index = 0;
2943                     break;
2944                   case DW_LNE_define_file:
2945                     {
2946                       file_table = (File_Entry *) xrealloc
2947                         (file_table, (n_files + 1) * sizeof (File_Entry));
2948 
2949                       ++state_machine_regs.last_file_entry;
2950                       /* Source file name.  */
2951                       file_table[n_files].name = op_code_data;
2952                       op_code_data += strlen ((char *) op_code_data) + 1;
2953                       /* Directory index.  */
2954                       file_table[n_files].directory_index =
2955                         read_leb128 (op_code_data, & bytes_read, 0);
2956                       op_code_data += bytes_read;
2957                       /* Last modification time.  */
2958                       file_table[n_files].modification_date =
2959                         read_leb128 (op_code_data, & bytes_read, 0);
2960                       op_code_data += bytes_read;
2961                       /* File length.  */
2962                       file_table[n_files].length =
2963                         read_leb128 (op_code_data, & bytes_read, 0);
2964 
2965                       n_files++;
2966                       break;
2967                     }
2968                   case DW_LNE_set_discriminator:
2969                   case DW_LNE_HP_set_sequence:
2970                     /* Simply ignored.  */
2971                     break;
2972 
2973                   default:
2974                     printf (_("UNKNOWN (%u): length %d\n"),
2975                             ext_op_code, ext_op_code_len - bytes_read);
2976                     break;
2977                   }
2978                 data += ext_op_code_len;
2979                 break;
2980               }
2981             case DW_LNS_copy:
2982               break;
2983 
2984             case DW_LNS_advance_pc:
2985               uladv = read_leb128 (data, & bytes_read, 0);
2986               data += bytes_read;
2987 	      if (linfo.li_max_ops_per_insn == 1)
2988 		{
2989 		  uladv *= linfo.li_min_insn_length;
2990 		  state_machine_regs.address += uladv;
2991 		}
2992 	      else
2993 		{
2994 		  state_machine_regs.address
2995 		    += ((state_machine_regs.op_index + uladv)
2996 			/ linfo.li_max_ops_per_insn)
2997 		       * linfo.li_min_insn_length;
2998 		  state_machine_regs.op_index
2999 		    = (state_machine_regs.op_index + uladv)
3000 		      % linfo.li_max_ops_per_insn;
3001 		}
3002               break;
3003 
3004             case DW_LNS_advance_line:
3005               adv = read_sleb128 (data, & bytes_read);
3006               data += bytes_read;
3007               state_machine_regs.line += adv;
3008               break;
3009 
3010             case DW_LNS_set_file:
3011               adv = read_leb128 (data, & bytes_read, 0);
3012               data += bytes_read;
3013               state_machine_regs.file = adv;
3014               if (file_table[state_machine_regs.file - 1].directory_index == 0)
3015                 {
3016                   /* If directory index is 0, that means current directory.  */
3017                   printf ("\n./%s:[++]\n",
3018                           file_table[state_machine_regs.file - 1].name);
3019                 }
3020               else
3021                 {
3022                   /* The directory index starts counting at 1.  */
3023                   printf ("\n%s/%s:\n",
3024                           directory_table[file_table[state_machine_regs.file - 1].directory_index - 1],
3025                           file_table[state_machine_regs.file - 1].name);
3026                 }
3027               break;
3028 
3029             case DW_LNS_set_column:
3030               uladv = read_leb128 (data, & bytes_read, 0);
3031               data += bytes_read;
3032               state_machine_regs.column = uladv;
3033               break;
3034 
3035             case DW_LNS_negate_stmt:
3036               adv = state_machine_regs.is_stmt;
3037               adv = ! adv;
3038               state_machine_regs.is_stmt = adv;
3039               break;
3040 
3041             case DW_LNS_set_basic_block:
3042               state_machine_regs.basic_block = 1;
3043               break;
3044 
3045             case DW_LNS_const_add_pc:
3046 	      uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
3047 	      if (linfo.li_max_ops_per_insn == 1)
3048 		{
3049 		  uladv *= linfo.li_min_insn_length;
3050 		  state_machine_regs.address += uladv;
3051 		}
3052 	      else
3053 		{
3054 		  state_machine_regs.address
3055 		    += ((state_machine_regs.op_index + uladv)
3056 			/ linfo.li_max_ops_per_insn)
3057 		       * linfo.li_min_insn_length;
3058 		  state_machine_regs.op_index
3059 		    = (state_machine_regs.op_index + uladv)
3060 		      % linfo.li_max_ops_per_insn;
3061 		}
3062               break;
3063 
3064             case DW_LNS_fixed_advance_pc:
3065               uladv = byte_get (data, 2);
3066               data += 2;
3067               state_machine_regs.address += uladv;
3068 	      state_machine_regs.op_index = 0;
3069               break;
3070 
3071             case DW_LNS_set_prologue_end:
3072               break;
3073 
3074             case DW_LNS_set_epilogue_begin:
3075               break;
3076 
3077             case DW_LNS_set_isa:
3078               uladv = read_leb128 (data, & bytes_read, 0);
3079               data += bytes_read;
3080               printf (_("  Set ISA to %lu\n"), uladv);
3081               break;
3082 
3083             default:
3084               printf (_("  Unknown opcode %d with operands: "), op_code);
3085 
3086               for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
3087                 {
3088                   printf ("0x%s%s", dwarf_vmatoa ("x", read_leb128 (data,
3089 							 &bytes_read, 0)),
3090                           i == 1 ? "" : ", ");
3091                   data += bytes_read;
3092                 }
3093               putchar ('\n');
3094               break;
3095             }
3096 
3097           /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
3098              to the DWARF address/line matrix.  */
3099           if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
3100 	      || (op_code == DW_LNS_copy))
3101             {
3102               const unsigned int MAX_FILENAME_LENGTH = 35;
3103               char *fileName = (char *)file_table[state_machine_regs.file - 1].name;
3104               char *newFileName = NULL;
3105               size_t fileNameLength = strlen (fileName);
3106 
3107               if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
3108                 {
3109                   newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
3110                   /* Truncate file name */
3111                   strncpy (newFileName,
3112                            fileName + fileNameLength - MAX_FILENAME_LENGTH,
3113                            MAX_FILENAME_LENGTH + 1);
3114                 }
3115               else
3116                 {
3117                   newFileName = (char *) xmalloc (fileNameLength + 1);
3118                   strncpy (newFileName, fileName, fileNameLength + 1);
3119                 }
3120 
3121               if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
3122                 {
3123 		  if (linfo.li_max_ops_per_insn == 1)
3124 		    printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x\n",
3125 			    newFileName, state_machine_regs.line,
3126 			    state_machine_regs.address);
3127 		  else
3128 		    printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
3129 			    newFileName, state_machine_regs.line,
3130 			    state_machine_regs.address,
3131 			    state_machine_regs.op_index);
3132                 }
3133               else
3134                 {
3135 		  if (linfo.li_max_ops_per_insn == 1)
3136 		    printf ("%s  %11d  %#18" DWARF_VMA_FMT "x\n",
3137 			    newFileName, state_machine_regs.line,
3138 			    state_machine_regs.address);
3139 		  else
3140 		    printf ("%s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
3141 			    newFileName, state_machine_regs.line,
3142 			    state_machine_regs.address,
3143 			    state_machine_regs.op_index);
3144                 }
3145 
3146               if (op_code == DW_LNE_end_sequence)
3147 		printf ("\n");
3148 
3149               free (newFileName);
3150             }
3151         }
3152       free (file_table);
3153       file_table = NULL;
3154       free (directory_table);
3155       directory_table = NULL;
3156       putchar ('\n');
3157     }
3158 
3159   return 1;
3160 }
3161 
3162 static int
3163 display_debug_lines (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
3164 {
3165   unsigned char *data = section->start;
3166   unsigned char *end = data + section->size;
3167   int retValRaw = 1;
3168   int retValDecoded = 1;
3169 
3170   if (do_debug_lines == 0)
3171     do_debug_lines |= FLAG_DEBUG_LINES_RAW;
3172 
3173   if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
3174     retValRaw = display_debug_lines_raw (section, data, end);
3175 
3176   if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
3177     retValDecoded = display_debug_lines_decoded (section, data, end);
3178 
3179   if (!retValRaw || !retValDecoded)
3180     return 0;
3181 
3182   return 1;
3183 }
3184 
3185 static debug_info *
3186 find_debug_info_for_offset (unsigned long offset)
3187 {
3188   unsigned int i;
3189 
3190   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
3191     return NULL;
3192 
3193   for (i = 0; i < num_debug_info_entries; i++)
3194     if (debug_information[i].cu_offset == offset)
3195       return debug_information + i;
3196 
3197   return NULL;
3198 }
3199 
3200 static int
3201 display_debug_pubnames (struct dwarf_section *section,
3202 			void *file ATTRIBUTE_UNUSED)
3203 {
3204   DWARF2_Internal_PubNames names;
3205   unsigned char *start = section->start;
3206   unsigned char *end = start + section->size;
3207 
3208   /* It does not matter if this load fails,
3209      we test for that later on.  */
3210   load_debug_info (file);
3211 
3212   printf (_("Contents of the %s section:\n\n"), section->name);
3213 
3214   while (start < end)
3215     {
3216       unsigned char *data;
3217       unsigned long offset;
3218       int offset_size, initial_length_size;
3219 
3220       data = start;
3221 
3222       names.pn_length = byte_get (data, 4);
3223       data += 4;
3224       if (names.pn_length == 0xffffffff)
3225 	{
3226 	  names.pn_length = byte_get (data, 8);
3227 	  data += 8;
3228 	  offset_size = 8;
3229 	  initial_length_size = 12;
3230 	}
3231       else
3232 	{
3233 	  offset_size = 4;
3234 	  initial_length_size = 4;
3235 	}
3236 
3237       names.pn_version = byte_get (data, 2);
3238       data += 2;
3239 
3240       names.pn_offset = byte_get (data, offset_size);
3241       data += offset_size;
3242 
3243       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
3244 	  && num_debug_info_entries > 0
3245 	  && find_debug_info_for_offset (names.pn_offset) == NULL)
3246 	warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
3247 	      (unsigned long) names.pn_offset, section->name);
3248 
3249       names.pn_size = byte_get (data, offset_size);
3250       data += offset_size;
3251 
3252       start += names.pn_length + initial_length_size;
3253 
3254       if (names.pn_version != 2 && names.pn_version != 3)
3255 	{
3256 	  static int warned = 0;
3257 
3258 	  if (! warned)
3259 	    {
3260 	      warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
3261 	      warned = 1;
3262 	    }
3263 
3264 	  continue;
3265 	}
3266 
3267       printf (_("  Length:                              %ld\n"),
3268 	      (long) names.pn_length);
3269       printf (_("  Version:                             %d\n"),
3270 	      names.pn_version);
3271       printf (_("  Offset into .debug_info section:     0x%lx\n"),
3272 	      (unsigned long) names.pn_offset);
3273       printf (_("  Size of area in .debug_info section: %ld\n"),
3274 	      (long) names.pn_size);
3275 
3276       printf (_("\n    Offset\tName\n"));
3277 
3278       do
3279 	{
3280 	  offset = byte_get (data, offset_size);
3281 
3282 	  if (offset != 0)
3283 	    {
3284 	      data += offset_size;
3285 	      printf ("    %-6lx\t%s\n", offset, data);
3286 	      data += strlen ((char *) data) + 1;
3287 	    }
3288 	}
3289       while (offset != 0);
3290     }
3291 
3292   printf ("\n");
3293   return 1;
3294 }
3295 
3296 static int
3297 display_debug_macinfo (struct dwarf_section *section,
3298 		       void *file ATTRIBUTE_UNUSED)
3299 {
3300   unsigned char *start = section->start;
3301   unsigned char *end = start + section->size;
3302   unsigned char *curr = start;
3303   unsigned int bytes_read;
3304   enum dwarf_macinfo_record_type op;
3305 
3306   printf (_("Contents of the %s section:\n\n"), section->name);
3307 
3308   while (curr < end)
3309     {
3310       unsigned int lineno;
3311       const char *string;
3312 
3313       op = (enum dwarf_macinfo_record_type) *curr;
3314       curr++;
3315 
3316       switch (op)
3317 	{
3318 	case DW_MACINFO_start_file:
3319 	  {
3320 	    unsigned int filenum;
3321 
3322 	    lineno = read_leb128 (curr, & bytes_read, 0);
3323 	    curr += bytes_read;
3324 	    filenum = read_leb128 (curr, & bytes_read, 0);
3325 	    curr += bytes_read;
3326 
3327 	    printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
3328 		    lineno, filenum);
3329 	  }
3330 	  break;
3331 
3332 	case DW_MACINFO_end_file:
3333 	  printf (_(" DW_MACINFO_end_file\n"));
3334 	  break;
3335 
3336 	case DW_MACINFO_define:
3337 	  lineno = read_leb128 (curr, & bytes_read, 0);
3338 	  curr += bytes_read;
3339 	  string = (char *) curr;
3340 	  curr += strlen (string) + 1;
3341 	  printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
3342 		  lineno, string);
3343 	  break;
3344 
3345 	case DW_MACINFO_undef:
3346 	  lineno = read_leb128 (curr, & bytes_read, 0);
3347 	  curr += bytes_read;
3348 	  string = (char *) curr;
3349 	  curr += strlen (string) + 1;
3350 	  printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
3351 		  lineno, string);
3352 	  break;
3353 
3354 	case DW_MACINFO_vendor_ext:
3355 	  {
3356 	    unsigned int constant;
3357 
3358 	    constant = read_leb128 (curr, & bytes_read, 0);
3359 	    curr += bytes_read;
3360 	    string = (char *) curr;
3361 	    curr += strlen (string) + 1;
3362 	    printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
3363 		    constant, string);
3364 	  }
3365 	  break;
3366 	}
3367     }
3368 
3369   return 1;
3370 }
3371 
3372 /* Given LINE_OFFSET into the .debug_line section, attempt to return
3373    filename and dirname corresponding to file name table entry with index
3374    FILEIDX.  Return NULL on failure.  */
3375 
3376 static unsigned char *
3377 get_line_filename_and_dirname (dwarf_vma line_offset, dwarf_vma fileidx,
3378 			       unsigned char **dir_name)
3379 {
3380   struct dwarf_section *section = &debug_displays [line].section;
3381   unsigned char *hdrptr, *dirtable, *file_name;
3382   unsigned int offset_size, initial_length_size;
3383   unsigned int version, opcode_base, bytes_read;
3384   dwarf_vma length, diridx;
3385 
3386   *dir_name = NULL;
3387   if (section->start == NULL
3388       || line_offset >= section->size
3389       || fileidx == 0)
3390     return NULL;
3391 
3392   hdrptr = section->start + line_offset;
3393   length = byte_get (hdrptr, 4);
3394   hdrptr += 4;
3395   if (length == 0xffffffff)
3396     {
3397       /* This section is 64-bit DWARF 3.  */
3398       length = byte_get (hdrptr, 8);
3399       hdrptr += 8;
3400       offset_size = 8;
3401       initial_length_size = 12;
3402     }
3403   else
3404     {
3405       offset_size = 4;
3406       initial_length_size = 4;
3407     }
3408   if (length + initial_length_size > section->size)
3409     return NULL;
3410   version = byte_get (hdrptr, 2);
3411   hdrptr += 2;
3412   if (version != 2 && version != 3 && version != 4)
3413     return NULL;
3414   hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length.  */
3415   if (version >= 4)
3416     hdrptr++;		    /* Skip max_ops_per_insn.  */
3417   hdrptr += 3;		    /* Skip default_is_stmt, line_base, line_range.  */
3418   opcode_base = byte_get (hdrptr, 1);
3419   if (opcode_base == 0)
3420     return NULL;
3421   hdrptr++;
3422   hdrptr += opcode_base - 1;
3423   dirtable = hdrptr;
3424   /* Skip over dirname table.  */
3425   while (*hdrptr != '\0')
3426     hdrptr += strlen ((char *) hdrptr) + 1;
3427   hdrptr++;		    /* Skip the NUL at the end of the table.  */
3428   /* Now skip over preceding filename table entries.  */
3429   for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
3430     {
3431       hdrptr += strlen ((char *) hdrptr) + 1;
3432       read_leb128 (hdrptr, &bytes_read, 0);
3433       hdrptr += bytes_read;
3434       read_leb128 (hdrptr, &bytes_read, 0);
3435       hdrptr += bytes_read;
3436       read_leb128 (hdrptr, &bytes_read, 0);
3437       hdrptr += bytes_read;
3438     }
3439   if (*hdrptr == '\0')
3440     return NULL;
3441   file_name = hdrptr;
3442   hdrptr += strlen ((char *) hdrptr) + 1;
3443   diridx = read_leb128 (hdrptr, &bytes_read, 0);
3444   if (diridx == 0)
3445     return file_name;
3446   for (; *dirtable != '\0' && diridx > 1; diridx--)
3447     dirtable += strlen ((char *) dirtable) + 1;
3448   if (*dirtable == '\0')
3449     return NULL;
3450   *dir_name = dirtable;
3451   return file_name;
3452 }
3453 
3454 static int
3455 display_debug_macro (struct dwarf_section *section,
3456 		     void *file)
3457 {
3458   unsigned char *start = section->start;
3459   unsigned char *end = start + section->size;
3460   unsigned char *curr = start;
3461   unsigned char *extended_op_buf[256];
3462   unsigned int bytes_read;
3463 
3464   load_debug_section (str, file);
3465   load_debug_section (line, file);
3466 
3467   printf (_("Contents of the %s section:\n\n"), section->name);
3468 
3469   while (curr < end)
3470     {
3471       unsigned int lineno, version, flags;
3472       unsigned int offset_size = 4;
3473       const char *string;
3474       dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
3475       unsigned char **extended_ops = NULL;
3476 
3477       version = byte_get (curr, 2);
3478       curr += 2;
3479 
3480       if (version != 4)
3481 	{
3482 	  error (_("Only GNU extension to DWARF 4 of %s is currently supported.\n"),
3483 		 section->name);
3484 	  return 0;
3485 	}
3486 
3487       flags = byte_get (curr++, 1);
3488       if (flags & 1)
3489 	offset_size = 8;
3490       printf (_("  Offset:                      0x%lx\n"),
3491 	      (unsigned long) sec_offset);
3492       printf (_("  Version:                     %d\n"), version);
3493       printf (_("  Offset size:                 %d\n"), offset_size);
3494       if (flags & 2)
3495 	{
3496 	  line_offset = byte_get (curr, offset_size);
3497 	  curr += offset_size;
3498 	  printf (_("  Offset into .debug_line:     0x%lx\n"),
3499 		  (unsigned long) line_offset);
3500 	}
3501       if (flags & 4)
3502 	{
3503 	  unsigned int i, count = byte_get (curr++, 1), op;
3504 	  dwarf_vma nargs, n;
3505 	  memset (extended_op_buf, 0, sizeof (extended_op_buf));
3506 	  extended_ops = extended_op_buf;
3507 	  if (count)
3508 	    {
3509 	      printf (_("  Extension opcode arguments:\n"));
3510 	      for (i = 0; i < count; i++)
3511 		{
3512 		  op = byte_get (curr++, 1);
3513 		  extended_ops[op] = curr;
3514 		  nargs = read_leb128 (curr, &bytes_read, 0);
3515 		  curr += bytes_read;
3516 		  if (nargs == 0)
3517 		    printf (_("    DW_MACRO_GNU_%02x has no arguments\n"), op);
3518 		  else
3519 		    {
3520 		      printf (_("    DW_MACRO_GNU_%02x arguments: "), op);
3521 		      for (n = 0; n < nargs; n++)
3522 			{
3523 			  unsigned int form = byte_get (curr++, 1);
3524 			  printf ("%s%s", get_FORM_name (form),
3525 				  n == nargs - 1 ? "\n" : ", ");
3526 			  switch (form)
3527 			    {
3528 			    case DW_FORM_data1:
3529 			    case DW_FORM_data2:
3530 			    case DW_FORM_data4:
3531 			    case DW_FORM_data8:
3532 			    case DW_FORM_sdata:
3533 			    case DW_FORM_udata:
3534 			    case DW_FORM_block:
3535 			    case DW_FORM_block1:
3536 			    case DW_FORM_block2:
3537 			    case DW_FORM_block4:
3538 			    case DW_FORM_flag:
3539 			    case DW_FORM_string:
3540 			    case DW_FORM_strp:
3541 			    case DW_FORM_sec_offset:
3542 			      break;
3543 			    default:
3544 			      error (_("Invalid extension opcode form %s\n"),
3545 				     get_FORM_name (form));
3546 			      return 0;
3547 			    }
3548 			}
3549 		    }
3550 		}
3551 	    }
3552 	}
3553       printf ("\n");
3554 
3555       while (1)
3556 	{
3557 	  unsigned int op;
3558 
3559 	  if (curr >= end)
3560 	    {
3561 	      error (_(".debug_macro section not zero terminated\n"));
3562 	      return 0;
3563 	    }
3564 
3565 	  op = byte_get (curr++, 1);
3566 	  if (op == 0)
3567 	    break;
3568 
3569 	  switch (op)
3570 	    {
3571 	    case DW_MACRO_GNU_start_file:
3572 	      {
3573 		unsigned int filenum;
3574 		unsigned char *file_name = NULL, *dir_name = NULL;
3575 
3576 		lineno = read_leb128 (curr, &bytes_read, 0);
3577 		curr += bytes_read;
3578 		filenum = read_leb128 (curr, &bytes_read, 0);
3579 		curr += bytes_read;
3580 
3581 		if ((flags & 2) == 0)
3582 		  error (_("DW_MACRO_GNU_start_file used, but no .debug_line offset provided.\n"));
3583 		else
3584 		  file_name
3585 		    = get_line_filename_and_dirname (line_offset, filenum,
3586 						     &dir_name);
3587 		if (file_name == NULL)
3588 		  printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d\n"),
3589 			  lineno, filenum);
3590 		else
3591 		  printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
3592 			  lineno, filenum,
3593 			  dir_name != NULL ? (const char *) dir_name : "",
3594 			  dir_name != NULL ? "/" : "", file_name);
3595 	      }
3596 	      break;
3597 
3598 	    case DW_MACRO_GNU_end_file:
3599 	      printf (_(" DW_MACRO_GNU_end_file\n"));
3600 	      break;
3601 
3602 	    case DW_MACRO_GNU_define:
3603 	      lineno = read_leb128 (curr, &bytes_read, 0);
3604 	      curr += bytes_read;
3605 	      string = (char *) curr;
3606 	      curr += strlen (string) + 1;
3607 	      printf (_(" DW_MACRO_GNU_define - lineno : %d macro : %s\n"),
3608 		      lineno, string);
3609 	      break;
3610 
3611 	    case DW_MACRO_GNU_undef:
3612 	      lineno = read_leb128 (curr, &bytes_read, 0);
3613 	      curr += bytes_read;
3614 	      string = (char *) curr;
3615 	      curr += strlen (string) + 1;
3616 	      printf (_(" DW_MACRO_GNU_undef - lineno : %d macro : %s\n"),
3617 		      lineno, string);
3618 	      break;
3619 
3620 	    case DW_MACRO_GNU_define_indirect:
3621 	      lineno = read_leb128 (curr, &bytes_read, 0);
3622 	      curr += bytes_read;
3623 	      offset = byte_get (curr, offset_size);
3624 	      curr += offset_size;
3625 	      string = fetch_indirect_string (offset);
3626 	      printf (_(" DW_MACRO_GNU_define_indirect - lineno : %d macro : %s\n"),
3627 		      lineno, string);
3628 	      break;
3629 
3630 	    case DW_MACRO_GNU_undef_indirect:
3631 	      lineno = read_leb128 (curr, &bytes_read, 0);
3632 	      curr += bytes_read;
3633 	      offset = byte_get (curr, offset_size);
3634 	      curr += offset_size;
3635 	      string = fetch_indirect_string (offset);
3636 	      printf (_(" DW_MACRO_GNU_undef_indirect - lineno : %d macro : %s\n"),
3637 		      lineno, string);
3638 	      break;
3639 
3640 	    case DW_MACRO_GNU_transparent_include:
3641 	      offset = byte_get (curr, offset_size);
3642 	      curr += offset_size;
3643 	      printf (_(" DW_MACRO_GNU_transparent_include - offset : 0x%lx\n"),
3644 		      (unsigned long) offset);
3645 	      break;
3646 
3647 	    case DW_MACRO_GNU_define_indirect_alt:
3648 	      lineno = read_leb128 (curr, &bytes_read, 0);
3649 	      curr += bytes_read;
3650 	      offset = byte_get (curr, offset_size);
3651 	      curr += offset_size;
3652 	      printf (_(" DW_MACRO_GNU_define_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
3653 		      lineno, (unsigned long) offset);
3654 	      break;
3655 
3656 	    case DW_MACRO_GNU_undef_indirect_alt:
3657 	      lineno = read_leb128 (curr, &bytes_read, 0);
3658 	      curr += bytes_read;
3659 	      offset = byte_get (curr, offset_size);
3660 	      curr += offset_size;
3661 	      printf (_(" DW_MACRO_GNU_undef_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
3662 		      lineno, (unsigned long) offset);
3663 	      break;
3664 
3665 	    case DW_MACRO_GNU_transparent_include_alt:
3666 	      offset = byte_get (curr, offset_size);
3667 	      curr += offset_size;
3668 	      printf (_(" DW_MACRO_GNU_transparent_include_alt - offset : 0x%lx\n"),
3669 		      (unsigned long) offset);
3670 	      break;
3671 
3672 	    default:
3673 	      if (extended_ops == NULL || extended_ops[op] == NULL)
3674 		{
3675 		  error (_(" Unknown macro opcode %02x seen\n"), op);
3676 		  return 0;
3677 		}
3678 	      else
3679 		{
3680 		  /* Skip over unhandled opcodes.  */
3681 		  dwarf_vma nargs, n;
3682 		  unsigned char *desc = extended_ops[op];
3683 		  nargs = read_leb128 (desc, &bytes_read, 0);
3684 		  desc += bytes_read;
3685 		  if (nargs == 0)
3686 		    {
3687 		      printf (_(" DW_MACRO_GNU_%02x\n"), op);
3688 		      break;
3689 		    }
3690 		  printf (_(" DW_MACRO_GNU_%02x -"), op);
3691 		  for (n = 0; n < nargs; n++)
3692 		    {
3693 		      curr
3694 			= read_and_display_attr_value (0, byte_get (desc++, 1),
3695 						       curr, 0, 0, offset_size,
3696 						       version, NULL, 0, NULL);
3697 		      if (n != nargs - 1)
3698 			printf (",");
3699 		    }
3700 		  printf ("\n");
3701 		}
3702 	      break;
3703 	    }
3704 	}
3705 
3706       printf ("\n");
3707     }
3708 
3709   return 1;
3710 }
3711 
3712 static int
3713 display_debug_abbrev (struct dwarf_section *section,
3714 		      void *file ATTRIBUTE_UNUSED)
3715 {
3716   abbrev_entry *entry;
3717   unsigned char *start = section->start;
3718   unsigned char *end = start + section->size;
3719 
3720   printf (_("Contents of the %s section:\n\n"), section->name);
3721 
3722   do
3723     {
3724       unsigned char *last;
3725 
3726       free_abbrevs ();
3727 
3728       last = start;
3729       start = process_abbrev_section (start, end);
3730 
3731       if (first_abbrev == NULL)
3732 	continue;
3733 
3734       printf (_("  Number TAG (0x%lx)\n"), (long) (last - section->start));
3735 
3736       for (entry = first_abbrev; entry; entry = entry->next)
3737 	{
3738 	  abbrev_attr *attr;
3739 
3740 	  printf ("   %ld      %s    [%s]\n",
3741 		  entry->entry,
3742 		  get_TAG_name (entry->tag),
3743 		  entry->children ? _("has children") : _("no children"));
3744 
3745 	  for (attr = entry->first_attr; attr; attr = attr->next)
3746 	    printf ("    %-18s %s\n",
3747 		    get_AT_name (attr->attribute),
3748 		    get_FORM_name (attr->form));
3749 	}
3750     }
3751   while (start);
3752 
3753   printf ("\n");
3754 
3755   return 1;
3756 }
3757 
3758 /* Display a location list from a normal (ie, non-dwo) .debug_loc section.  */
3759 
3760 static void
3761 display_loc_list (struct dwarf_section *section,
3762                   unsigned char **start_ptr,
3763                   int debug_info_entry,
3764                   unsigned long offset,
3765                   unsigned long base_address,
3766                   int has_frame_base)
3767 {
3768   unsigned char *start = *start_ptr;
3769   unsigned char *section_end = section->start + section->size;
3770   unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
3771   unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
3772   unsigned int offset_size = debug_information [debug_info_entry].offset_size;
3773   int dwarf_version = debug_information [debug_info_entry].dwarf_version;
3774 
3775   dwarf_vma begin;
3776   dwarf_vma end;
3777   unsigned short length;
3778   int need_frame_base;
3779 
3780   while (1)
3781     {
3782       if (start + 2 * pointer_size > section_end)
3783         {
3784           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3785                 offset);
3786           break;
3787         }
3788 
3789       /* Note: we use sign extension here in order to be sure that we can detect
3790          the -1 escape value.  Sign extension into the top 32 bits of a 32-bit
3791          address will not affect the values that we display since we always show
3792          hex values, and always the bottom 32-bits.  */
3793       begin = byte_get_signed (start, pointer_size);
3794       start += pointer_size;
3795       end = byte_get_signed (start, pointer_size);
3796       start += pointer_size;
3797 
3798       printf ("    %8.8lx ", offset);
3799 
3800       if (begin == 0 && end == 0)
3801         {
3802           printf (_("<End of list>\n"));
3803           break;
3804         }
3805 
3806       /* Check base address specifiers.  */
3807       if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
3808         {
3809           base_address = end;
3810           print_dwarf_vma (begin, pointer_size);
3811           print_dwarf_vma (end, pointer_size);
3812           printf (_("(base address)\n"));
3813           continue;
3814         }
3815 
3816       if (start + 2 > section_end)
3817         {
3818           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3819                 offset);
3820           break;
3821         }
3822 
3823       length = byte_get (start, 2);
3824       start += 2;
3825 
3826       if (start + length > section_end)
3827         {
3828           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3829                 offset);
3830           break;
3831         }
3832 
3833       print_dwarf_vma (begin + base_address, pointer_size);
3834       print_dwarf_vma (end + base_address, pointer_size);
3835 
3836       putchar ('(');
3837       need_frame_base = decode_location_expression (start,
3838                                                     pointer_size,
3839                                                     offset_size,
3840                                                     dwarf_version,
3841                                                     length,
3842                                                     cu_offset, section);
3843       putchar (')');
3844 
3845       if (need_frame_base && !has_frame_base)
3846         printf (_(" [without DW_AT_frame_base]"));
3847 
3848       if (begin == end)
3849         fputs (_(" (start == end)"), stdout);
3850       else if (begin > end)
3851         fputs (_(" (start > end)"), stdout);
3852 
3853       putchar ('\n');
3854 
3855       start += length;
3856     }
3857 
3858   *start_ptr = start;
3859 }
3860 
3861 /* Display a location list from a .dwo section. It uses address indexes rather
3862    than embedded addresses.  This code closely follows display_loc_list, but the
3863    two are sufficiently different that combining things is very ugly.  */
3864 
3865 static void
3866 display_loc_list_dwo (struct dwarf_section *section,
3867                       unsigned char **start_ptr,
3868                       int debug_info_entry,
3869                       unsigned long offset,
3870                       int has_frame_base)
3871 {
3872   unsigned char *start = *start_ptr;
3873   unsigned char *section_end = section->start + section->size;
3874   unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
3875   unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
3876   unsigned int offset_size = debug_information [debug_info_entry].offset_size;
3877   int dwarf_version = debug_information [debug_info_entry].dwarf_version;
3878   int entry_type;
3879   unsigned short length;
3880   int need_frame_base;
3881   dwarf_vma idx;
3882   unsigned int bytes_read;
3883 
3884   while (1)
3885     {
3886       printf ("    %8.8lx ", offset);
3887 
3888       if (start + 2 > section_end)
3889         {
3890           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3891                 offset);
3892           break;
3893         }
3894 
3895       entry_type = byte_get (start, 1);
3896       start++;
3897       switch (entry_type)
3898         {
3899           case 0: /* A terminating entry.  */
3900             idx = byte_get (start, 1);
3901             start++;
3902             *start_ptr = start;
3903             if (idx == 0)
3904               printf (_("<End of list>\n"));
3905             else
3906               warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3907                     offset);
3908             return;
3909           case 1: /* A base-address entry.  */
3910             idx = read_leb128 (start, &bytes_read, 0);
3911             start += bytes_read;
3912             print_dwarf_vma (idx, pointer_size);
3913             printf (_("(base address index)\n"));
3914             continue;
3915           case 2: /* A normal entry.  */
3916             idx = read_leb128 (start, &bytes_read, 0);
3917             start += bytes_read;
3918             print_dwarf_vma (idx, pointer_size);
3919             idx = read_leb128 (start, &bytes_read, 0);
3920             start += bytes_read;
3921             print_dwarf_vma (idx, pointer_size);
3922             break;
3923           default:
3924             warn (_("Unknown location-list type 0x%x.\n"), entry_type);
3925             *start_ptr = start;
3926             return;
3927         }
3928 
3929       if (start + 2 > section_end)
3930         {
3931           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3932                 offset);
3933           break;
3934         }
3935 
3936       length = byte_get (start, 2);
3937       start += 2;
3938 
3939       if (start + length > section_end)
3940         {
3941           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3942                 offset);
3943           break;
3944         }
3945 
3946       putchar ('(');
3947       need_frame_base = decode_location_expression (start,
3948                                                     pointer_size,
3949                                                     offset_size,
3950                                                     dwarf_version,
3951                                                     length,
3952                                                     cu_offset, section);
3953       putchar (')');
3954 
3955       if (need_frame_base && !has_frame_base)
3956         printf (_(" [without DW_AT_frame_base]"));
3957 
3958       putchar ('\n');
3959 
3960       start += length;
3961     }
3962 
3963   *start_ptr = start;
3964 }
3965 
3966 /* Sort array of indexes in ascending order of loc_offsets[idx].  */
3967 
3968 static dwarf_vma *loc_offsets;
3969 
3970 static int
3971 loc_offsets_compar (const void *ap, const void *bp)
3972 {
3973   dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
3974   dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
3975 
3976   return (a > b) - (b > a);
3977 }
3978 
3979 static int
3980 display_debug_loc (struct dwarf_section *section, void *file)
3981 {
3982   unsigned char *start = section->start;
3983   unsigned long bytes;
3984   unsigned char *section_begin = start;
3985   unsigned int num_loc_list = 0;
3986   unsigned long last_offset = 0;
3987   unsigned int first = 0;
3988   unsigned int i;
3989   unsigned int j;
3990   unsigned int k;
3991   int seen_first_offset = 0;
3992   int locs_sorted = 1;
3993   unsigned char *next;
3994   unsigned int *array = NULL;
3995   const char *suffix = strrchr (section->name, '.');
3996   int is_dwo = 0;
3997 
3998   if (suffix && strcmp (suffix, ".dwo") == 0)
3999     is_dwo = 1;
4000 
4001   bytes = section->size;
4002 
4003   if (bytes == 0)
4004     {
4005       printf (_("\nThe %s section is empty.\n"), section->name);
4006       return 0;
4007     }
4008 
4009   if (load_debug_info (file) == 0)
4010     {
4011       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4012 	    section->name);
4013       return 0;
4014     }
4015 
4016   /* Check the order of location list in .debug_info section. If
4017      offsets of location lists are in the ascending order, we can
4018      use `debug_information' directly.  */
4019   for (i = 0; i < num_debug_info_entries; i++)
4020     {
4021       unsigned int num;
4022 
4023       num = debug_information [i].num_loc_offsets;
4024       if (num > num_loc_list)
4025 	num_loc_list = num;
4026 
4027       /* Check if we can use `debug_information' directly.  */
4028       if (locs_sorted && num != 0)
4029 	{
4030 	  if (!seen_first_offset)
4031 	    {
4032 	      /* This is the first location list.  */
4033 	      last_offset = debug_information [i].loc_offsets [0];
4034 	      first = i;
4035 	      seen_first_offset = 1;
4036 	      j = 1;
4037 	    }
4038 	  else
4039 	    j = 0;
4040 
4041 	  for (; j < num; j++)
4042 	    {
4043 	      if (last_offset >
4044 		  debug_information [i].loc_offsets [j])
4045 		{
4046 		  locs_sorted = 0;
4047 		  break;
4048 		}
4049 	      last_offset = debug_information [i].loc_offsets [j];
4050 	    }
4051 	}
4052     }
4053 
4054   if (!seen_first_offset)
4055     error (_("No location lists in .debug_info section!\n"));
4056 
4057   /* DWARF sections under Mach-O have non-zero addresses.  */
4058   if (debug_information [first].num_loc_offsets > 0
4059       && debug_information [first].loc_offsets [0] != section->address)
4060     warn (_("Location lists in %s section start at 0x%s\n"),
4061 	  section->name,
4062 	  dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
4063 
4064   if (!locs_sorted)
4065     array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
4066   printf (_("Contents of the %s section:\n\n"), section->name);
4067   if (!is_dwo)
4068     printf (_("    Offset   Begin    End      Expression\n"));
4069   else
4070     printf (_("    Offset   Begin idx End idx  Expression\n"));
4071 
4072   seen_first_offset = 0;
4073   for (i = first; i < num_debug_info_entries; i++)
4074     {
4075       unsigned long offset;
4076       unsigned long base_address;
4077       int has_frame_base;
4078 
4079       if (!locs_sorted)
4080 	{
4081 	  for (k = 0; k < debug_information [i].num_loc_offsets; k++)
4082 	    array[k] = k;
4083 	  loc_offsets = debug_information [i].loc_offsets;
4084 	  qsort (array, debug_information [i].num_loc_offsets,
4085 		 sizeof (*array), loc_offsets_compar);
4086 	}
4087 
4088       for (k = 0; k < debug_information [i].num_loc_offsets; k++)
4089 	{
4090 	  j = locs_sorted ? k : array[k];
4091 	  if (k
4092 	      && debug_information [i].loc_offsets [locs_sorted
4093 						    ? k - 1 : array [k - 1]]
4094 		 == debug_information [i].loc_offsets [j])
4095 	    continue;
4096 	  has_frame_base = debug_information [i].have_frame_base [j];
4097 	  /* DWARF sections under Mach-O have non-zero addresses.  */
4098 	  offset = debug_information [i].loc_offsets [j] - section->address;
4099 	  next = section_begin + offset;
4100 	  base_address = debug_information [i].base_address;
4101 
4102 	  if (!seen_first_offset)
4103 	    seen_first_offset = 1;
4104 	  else
4105 	    {
4106 	      if (start < next)
4107 		warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
4108 		      (unsigned long) (start - section_begin),
4109 		      (unsigned long) (next - section_begin));
4110 	      else if (start > next)
4111 		warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
4112 		      (unsigned long) (start - section_begin),
4113 		      (unsigned long) (next - section_begin));
4114 	    }
4115 	  start = next;
4116 
4117 	  if (offset >= bytes)
4118 	    {
4119 	      warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
4120 		    offset);
4121 	      continue;
4122 	    }
4123 
4124           if (is_dwo)
4125             display_loc_list_dwo (section, &start, i, offset, has_frame_base);
4126           else
4127             display_loc_list (section, &start, i, offset, base_address,
4128                               has_frame_base);
4129 	}
4130     }
4131 
4132   if (start < section->start + section->size)
4133     warn (_("There are %ld unused bytes at the end of section %s\n"),
4134 	  (long) (section->start + section->size - start), section->name);
4135   putchar ('\n');
4136   free (array);
4137   return 1;
4138 }
4139 
4140 static int
4141 display_debug_str (struct dwarf_section *section,
4142 		   void *file ATTRIBUTE_UNUSED)
4143 {
4144   unsigned char *start = section->start;
4145   unsigned long bytes = section->size;
4146   dwarf_vma addr = section->address;
4147 
4148   if (bytes == 0)
4149     {
4150       printf (_("\nThe %s section is empty.\n"), section->name);
4151       return 0;
4152     }
4153 
4154   printf (_("Contents of the %s section:\n\n"), section->name);
4155 
4156   while (bytes)
4157     {
4158       int j;
4159       int k;
4160       int lbytes;
4161 
4162       lbytes = (bytes > 16 ? 16 : bytes);
4163 
4164       printf ("  0x%8.8lx ", (unsigned long) addr);
4165 
4166       for (j = 0; j < 16; j++)
4167 	{
4168 	  if (j < lbytes)
4169 	    printf ("%2.2x", start[j]);
4170 	  else
4171 	    printf ("  ");
4172 
4173 	  if ((j & 3) == 3)
4174 	    printf (" ");
4175 	}
4176 
4177       for (j = 0; j < lbytes; j++)
4178 	{
4179 	  k = start[j];
4180 	  if (k >= ' ' && k < 0x80)
4181 	    printf ("%c", k);
4182 	  else
4183 	    printf (".");
4184 	}
4185 
4186       putchar ('\n');
4187 
4188       start += lbytes;
4189       addr  += lbytes;
4190       bytes -= lbytes;
4191     }
4192 
4193   putchar ('\n');
4194 
4195   return 1;
4196 }
4197 
4198 static int
4199 display_debug_info (struct dwarf_section *section, void *file)
4200 {
4201   return process_debug_info (section, file, section->abbrev_sec, 0, 0);
4202 }
4203 
4204 static int
4205 display_debug_types (struct dwarf_section *section, void *file)
4206 {
4207   return process_debug_info (section, file, section->abbrev_sec, 0, 1);
4208 }
4209 
4210 static int
4211 display_trace_info (struct dwarf_section *section, void *file)
4212 {
4213   return process_debug_info (section, file, section->abbrev_sec, 0, 0);
4214 }
4215 
4216 static int
4217 display_debug_aranges (struct dwarf_section *section,
4218 		       void *file ATTRIBUTE_UNUSED)
4219 {
4220   unsigned char *start = section->start;
4221   unsigned char *end = start + section->size;
4222 
4223   printf (_("Contents of the %s section:\n\n"), section->name);
4224 
4225   /* It does not matter if this load fails,
4226      we test for that later on.  */
4227   load_debug_info (file);
4228 
4229   while (start < end)
4230     {
4231       unsigned char *hdrptr;
4232       DWARF2_Internal_ARange arange;
4233       unsigned char *addr_ranges;
4234       dwarf_vma length;
4235       dwarf_vma address;
4236       unsigned char address_size;
4237       int excess;
4238       int offset_size;
4239       int initial_length_size;
4240 
4241       hdrptr = start;
4242 
4243       arange.ar_length = byte_get (hdrptr, 4);
4244       hdrptr += 4;
4245 
4246       if (arange.ar_length == 0xffffffff)
4247 	{
4248 	  arange.ar_length = byte_get (hdrptr, 8);
4249 	  hdrptr += 8;
4250 	  offset_size = 8;
4251 	  initial_length_size = 12;
4252 	}
4253       else
4254 	{
4255 	  offset_size = 4;
4256 	  initial_length_size = 4;
4257 	}
4258 
4259       arange.ar_version = byte_get (hdrptr, 2);
4260       hdrptr += 2;
4261 
4262       arange.ar_info_offset = byte_get (hdrptr, offset_size);
4263       hdrptr += offset_size;
4264 
4265       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
4266 	  && num_debug_info_entries > 0
4267 	  && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
4268 	warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
4269 	      (unsigned long) arange.ar_info_offset, section->name);
4270 
4271       arange.ar_pointer_size = byte_get (hdrptr, 1);
4272       hdrptr += 1;
4273 
4274       arange.ar_segment_size = byte_get (hdrptr, 1);
4275       hdrptr += 1;
4276 
4277       if (arange.ar_version != 2 && arange.ar_version != 3)
4278 	{
4279 	  warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
4280 	  break;
4281 	}
4282 
4283       printf (_("  Length:                   %ld\n"),
4284 	      (long) arange.ar_length);
4285       printf (_("  Version:                  %d\n"), arange.ar_version);
4286       printf (_("  Offset into .debug_info:  0x%lx\n"),
4287 	      (unsigned long) arange.ar_info_offset);
4288       printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
4289       printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
4290 
4291       address_size = arange.ar_pointer_size + arange.ar_segment_size;
4292 
4293       if (address_size == 0)
4294 	{
4295 	  error (_("Invalid address size in %s section!\n"),
4296 		 section->name);
4297 	  break;
4298 	}
4299 
4300       /* The DWARF spec does not require that the address size be a power
4301 	 of two, but we do.  This will have to change if we ever encounter
4302 	 an uneven architecture.  */
4303       if ((address_size & (address_size - 1)) != 0)
4304 	{
4305 	  warn (_("Pointer size + Segment size is not a power of two.\n"));
4306 	  break;
4307 	}
4308 
4309       if (address_size > 4)
4310 	printf (_("\n    Address            Length\n"));
4311       else
4312 	printf (_("\n    Address    Length\n"));
4313 
4314       addr_ranges = hdrptr;
4315 
4316       /* Must pad to an alignment boundary that is twice the address size.  */
4317       excess = (hdrptr - start) % (2 * address_size);
4318       if (excess)
4319 	addr_ranges += (2 * address_size) - excess;
4320 
4321       start += arange.ar_length + initial_length_size;
4322 
4323       while (addr_ranges + 2 * address_size <= start)
4324 	{
4325 	  address = byte_get (addr_ranges, address_size);
4326 
4327 	  addr_ranges += address_size;
4328 
4329 	  length  = byte_get (addr_ranges, address_size);
4330 
4331 	  addr_ranges += address_size;
4332 
4333 	  printf ("    ");
4334 	  print_dwarf_vma (address, address_size);
4335 	  print_dwarf_vma (length, address_size);
4336 	  putchar ('\n');
4337 	}
4338     }
4339 
4340   printf ("\n");
4341 
4342   return 1;
4343 }
4344 
4345 /* Comparison function for qsort.  */
4346 static int
4347 comp_addr_base (const void * v0, const void * v1)
4348 {
4349   debug_info * info0 = (debug_info *) v0;
4350   debug_info * info1 = (debug_info *) v1;
4351   return info0->addr_base - info1->addr_base;
4352 }
4353 
4354 /* Display the debug_addr section.  */
4355 static int
4356 display_debug_addr (struct dwarf_section *section,
4357                     void *file)
4358 {
4359   debug_info **debug_addr_info;
4360   unsigned char *entry;
4361   unsigned char *end;
4362   unsigned int i;
4363   unsigned int count;
4364 
4365   if (section->size == 0)
4366     {
4367       printf (_("\nThe %s section is empty.\n"), section->name);
4368       return 0;
4369     }
4370 
4371   if (load_debug_info (file) == 0)
4372     {
4373       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4374 	    section->name);
4375       return 0;
4376     }
4377 
4378   printf (_("Contents of the %s section:\n\n"), section->name);
4379 
4380   debug_addr_info = (debug_info **) xmalloc (num_debug_info_entries + 1
4381                                              * sizeof (debug_info *));
4382 
4383   count = 0;
4384   for (i = 0; i < num_debug_info_entries; i++)
4385     {
4386       if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
4387         debug_addr_info [count++] = &debug_information [i];
4388     }
4389 
4390   /* Add a sentinel to make iteration convenient.  */
4391   debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
4392   debug_addr_info [count]->addr_base = section->size;
4393 
4394   qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
4395   for (i = 0; i < count; i++)
4396     {
4397       unsigned int idx;
4398 
4399       printf (_("  For compilation unit at offset 0x%s:\n"),
4400               dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
4401 
4402       printf (_("\tIndex\tOffset\n"));
4403       entry = section->start + debug_addr_info [i]->addr_base;
4404       end = section->start + debug_addr_info [i + 1]->addr_base;
4405       idx = 0;
4406       while (entry < end)
4407         {
4408           dwarf_vma base = byte_get (entry, debug_addr_info [i]->pointer_size);
4409           printf (_("\t%d:\t%s\n"), idx, dwarf_vmatoa ("x", base));
4410           entry += debug_addr_info [i]->pointer_size;
4411           idx++;
4412         }
4413     }
4414   printf ("\n");
4415 
4416   free (debug_addr_info);
4417   return 1;
4418 }
4419 
4420 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections.  */
4421 static int
4422 display_debug_str_offsets (struct dwarf_section *section,
4423                            void *file ATTRIBUTE_UNUSED)
4424 {
4425   if (section->size == 0)
4426     {
4427       printf (_("\nThe %s section is empty.\n"), section->name);
4428       return 0;
4429     }
4430   /* TODO: Dump the contents.  This is made somewhat difficult by not knowing
4431      what the offset size is for this section.  */
4432   return 1;
4433 }
4434 
4435 /* Each debug_information[x].range_lists[y] gets this representation for
4436    sorting purposes.  */
4437 
4438 struct range_entry
4439 {
4440   /* The debug_information[x].range_lists[y] value.  */
4441   unsigned long ranges_offset;
4442 
4443   /* Original debug_information to find parameters of the data.  */
4444   debug_info *debug_info_p;
4445 };
4446 
4447 /* Sort struct range_entry in ascending order of its RANGES_OFFSET.  */
4448 
4449 static int
4450 range_entry_compar (const void *ap, const void *bp)
4451 {
4452   const struct range_entry *a_re = (const struct range_entry *) ap;
4453   const struct range_entry *b_re = (const struct range_entry *) bp;
4454   const unsigned long a = a_re->ranges_offset;
4455   const unsigned long b = b_re->ranges_offset;
4456 
4457   return (a > b) - (b > a);
4458 }
4459 
4460 static int
4461 display_debug_ranges (struct dwarf_section *section,
4462 		      void *file ATTRIBUTE_UNUSED)
4463 {
4464   unsigned char *start = section->start;
4465   unsigned char *last_start = start;
4466   unsigned long bytes;
4467   unsigned char *section_begin = start;
4468   unsigned int num_range_list, i;
4469   struct range_entry *range_entries, *range_entry_fill;
4470 
4471   bytes = section->size;
4472 
4473   if (bytes == 0)
4474     {
4475       printf (_("\nThe %s section is empty.\n"), section->name);
4476       return 0;
4477     }
4478 
4479   if (load_debug_info (file) == 0)
4480     {
4481       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4482 	    section->name);
4483       return 0;
4484     }
4485 
4486   num_range_list = 0;
4487   for (i = 0; i < num_debug_info_entries; i++)
4488     num_range_list += debug_information [i].num_range_lists;
4489 
4490   if (num_range_list == 0)
4491     {
4492       /* This can happen when the file was compiled with -gsplit-debug
4493          which removes references to range lists from the primary .o file.  */
4494       printf (_("No range lists in .debug_info section.\n"));
4495       return 1;
4496     }
4497 
4498   range_entries = (struct range_entry *)
4499       xmalloc (sizeof (*range_entries) * num_range_list);
4500   range_entry_fill = range_entries;
4501 
4502   for (i = 0; i < num_debug_info_entries; i++)
4503     {
4504       debug_info *debug_info_p = &debug_information[i];
4505       unsigned int j;
4506 
4507       for (j = 0; j < debug_info_p->num_range_lists; j++)
4508 	{
4509 	  range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
4510 	  range_entry_fill->debug_info_p = debug_info_p;
4511 	  range_entry_fill++;
4512 	}
4513     }
4514 
4515   qsort (range_entries, num_range_list, sizeof (*range_entries),
4516 	 range_entry_compar);
4517 
4518   /* DWARF sections under Mach-O have non-zero addresses.  */
4519   if (dwarf_check != 0 && range_entries[0].ranges_offset != section->address)
4520     warn (_("Range lists in %s section start at 0x%lx\n"),
4521 	  section->name, range_entries[0].ranges_offset);
4522 
4523   printf (_("Contents of the %s section:\n\n"), section->name);
4524   printf (_("    Offset   Begin    End\n"));
4525 
4526   for (i = 0; i < num_range_list; i++)
4527     {
4528       struct range_entry *range_entry = &range_entries[i];
4529       debug_info *debug_info_p = range_entry->debug_info_p;
4530       unsigned int pointer_size;
4531       unsigned long offset;
4532       unsigned char *next;
4533       unsigned long base_address;
4534 
4535       pointer_size = debug_info_p->pointer_size;
4536 
4537       /* DWARF sections under Mach-O have non-zero addresses.  */
4538       offset = range_entry->ranges_offset - section->address;
4539       next = section_begin + offset;
4540       base_address = debug_info_p->base_address;
4541 
4542       if (dwarf_check != 0 && i > 0)
4543 	{
4544 	  if (start < next)
4545 	    warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
4546 		  (unsigned long) (start - section_begin),
4547 		  (unsigned long) (next - section_begin), section->name);
4548 	  else if (start > next)
4549 	    {
4550 	      if (next == last_start)
4551 		continue;
4552 	      warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
4553 		    (unsigned long) (start - section_begin),
4554 		    (unsigned long) (next - section_begin), section->name);
4555 	    }
4556 	}
4557       start = next;
4558       last_start = next;
4559 
4560       while (1)
4561 	{
4562 	  dwarf_vma begin;
4563 	  dwarf_vma end;
4564 
4565 	  /* Note: we use sign extension here in order to be sure that
4566 	     we can detect the -1 escape value.  Sign extension into the
4567 	     top 32 bits of a 32-bit address will not affect the values
4568 	     that we display since we always show hex values, and always
4569 	     the bottom 32-bits.  */
4570 	  begin = byte_get_signed (start, pointer_size);
4571 	  start += pointer_size;
4572 	  end = byte_get_signed (start, pointer_size);
4573 	  start += pointer_size;
4574 
4575 	  printf ("    %8.8lx ", offset);
4576 
4577 	  if (begin == 0 && end == 0)
4578 	    {
4579 	      printf (_("<End of list>\n"));
4580 	      break;
4581 	    }
4582 
4583 	  /* Check base address specifiers.  */
4584 	  if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
4585 	    {
4586 	      base_address = end;
4587 	      print_dwarf_vma (begin, pointer_size);
4588 	      print_dwarf_vma (end, pointer_size);
4589 	      printf ("(base address)\n");
4590 	      continue;
4591 	    }
4592 
4593 	  print_dwarf_vma (begin + base_address, pointer_size);
4594 	  print_dwarf_vma (end + base_address, pointer_size);
4595 
4596 	  if (begin == end)
4597 	    fputs (_("(start == end)"), stdout);
4598 	  else if (begin > end)
4599 	    fputs (_("(start > end)"), stdout);
4600 
4601 	  putchar ('\n');
4602 	}
4603     }
4604   putchar ('\n');
4605 
4606   free (range_entries);
4607 
4608   return 1;
4609 }
4610 
4611 typedef struct Frame_Chunk
4612 {
4613   struct Frame_Chunk *next;
4614   unsigned char *chunk_start;
4615   int ncols;
4616   /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
4617   short int *col_type;
4618   int *col_offset;
4619   char *augmentation;
4620   unsigned int code_factor;
4621   int data_factor;
4622   unsigned long pc_begin;
4623   unsigned long pc_range;
4624   int cfa_reg;
4625   int cfa_offset;
4626   int ra;
4627   unsigned char fde_encoding;
4628   unsigned char cfa_exp;
4629   unsigned char ptr_size;
4630   unsigned char segment_size;
4631 }
4632 Frame_Chunk;
4633 
4634 static const char *const *dwarf_regnames;
4635 static unsigned int dwarf_regnames_count;
4636 
4637 /* A marker for a col_type that means this column was never referenced
4638    in the frame info.  */
4639 #define DW_CFA_unreferenced (-1)
4640 
4641 /* Return 0 if not more space is needed, 1 if more space is needed,
4642    -1 for invalid reg.  */
4643 
4644 static int
4645 frame_need_space (Frame_Chunk *fc, unsigned int reg)
4646 {
4647   int prev = fc->ncols;
4648 
4649   if (reg < (unsigned int) fc->ncols)
4650     return 0;
4651 
4652   if (dwarf_regnames_count
4653       && reg > dwarf_regnames_count)
4654     return -1;
4655 
4656   fc->ncols = reg + 1;
4657   fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
4658                                           sizeof (short int));
4659   fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
4660 
4661   while (prev < fc->ncols)
4662     {
4663       fc->col_type[prev] = DW_CFA_unreferenced;
4664       fc->col_offset[prev] = 0;
4665       prev++;
4666     }
4667   return 1;
4668 }
4669 
4670 static const char *const dwarf_regnames_i386[] =
4671 {
4672   "eax", "ecx", "edx", "ebx",
4673   "esp", "ebp", "esi", "edi",
4674   "eip", "eflags", NULL,
4675   "st0", "st1", "st2", "st3",
4676   "st4", "st5", "st6", "st7",
4677   NULL, NULL,
4678   "xmm0", "xmm1", "xmm2", "xmm3",
4679   "xmm4", "xmm5", "xmm6", "xmm7",
4680   "mm0", "mm1", "mm2", "mm3",
4681   "mm4", "mm5", "mm6", "mm7",
4682   "fcw", "fsw", "mxcsr",
4683   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
4684   "tr", "ldtr"
4685 };
4686 
4687 void
4688 init_dwarf_regnames_i386 (void)
4689 {
4690   dwarf_regnames = dwarf_regnames_i386;
4691   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
4692 }
4693 
4694 static const char *const dwarf_regnames_x86_64[] =
4695 {
4696   "rax", "rdx", "rcx", "rbx",
4697   "rsi", "rdi", "rbp", "rsp",
4698   "r8",  "r9",  "r10", "r11",
4699   "r12", "r13", "r14", "r15",
4700   "rip",
4701   "xmm0",  "xmm1",  "xmm2",  "xmm3",
4702   "xmm4",  "xmm5",  "xmm6",  "xmm7",
4703   "xmm8",  "xmm9",  "xmm10", "xmm11",
4704   "xmm12", "xmm13", "xmm14", "xmm15",
4705   "st0", "st1", "st2", "st3",
4706   "st4", "st5", "st6", "st7",
4707   "mm0", "mm1", "mm2", "mm3",
4708   "mm4", "mm5", "mm6", "mm7",
4709   "rflags",
4710   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
4711   "fs.base", "gs.base", NULL, NULL,
4712   "tr", "ldtr",
4713   "mxcsr", "fcw", "fsw"
4714 };
4715 
4716 void
4717 init_dwarf_regnames_x86_64 (void)
4718 {
4719   dwarf_regnames = dwarf_regnames_x86_64;
4720   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
4721 }
4722 
4723 void
4724 init_dwarf_regnames (unsigned int e_machine)
4725 {
4726   switch (e_machine)
4727     {
4728     case EM_386:
4729     case EM_486:
4730       init_dwarf_regnames_i386 ();
4731       break;
4732 
4733     case EM_X86_64:
4734     case EM_L1OM:
4735     case EM_K1OM:
4736       init_dwarf_regnames_x86_64 ();
4737       break;
4738 
4739     default:
4740       break;
4741     }
4742 }
4743 
4744 static const char *
4745 regname (unsigned int regno, int row)
4746 {
4747   static char reg[64];
4748   if (dwarf_regnames
4749       && regno < dwarf_regnames_count
4750       && dwarf_regnames [regno] != NULL)
4751     {
4752       if (row)
4753 	return dwarf_regnames [regno];
4754       snprintf (reg, sizeof (reg), "r%d (%s)", regno,
4755 		dwarf_regnames [regno]);
4756     }
4757   else
4758     snprintf (reg, sizeof (reg), "r%d", regno);
4759   return reg;
4760 }
4761 
4762 static void
4763 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
4764 {
4765   int r;
4766   char tmp[100];
4767 
4768   if (*max_regs < fc->ncols)
4769     *max_regs = fc->ncols;
4770 
4771   if (*need_col_headers)
4772     {
4773       static const char *sloc = "   LOC";
4774 
4775       *need_col_headers = 0;
4776 
4777       printf ("%-*s CFA      ", eh_addr_size * 2, sloc);
4778 
4779       for (r = 0; r < *max_regs; r++)
4780 	if (fc->col_type[r] != DW_CFA_unreferenced)
4781 	  {
4782 	    if (r == fc->ra)
4783 	      printf ("ra      ");
4784 	    else
4785 	      printf ("%-5s ", regname (r, 1));
4786 	  }
4787 
4788       printf ("\n");
4789     }
4790 
4791   printf ("%0*lx ", eh_addr_size * 2, fc->pc_begin);
4792   if (fc->cfa_exp)
4793     strcpy (tmp, "exp");
4794   else
4795     sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), fc->cfa_offset);
4796   printf ("%-8s ", tmp);
4797 
4798   for (r = 0; r < fc->ncols; r++)
4799     {
4800       if (fc->col_type[r] != DW_CFA_unreferenced)
4801 	{
4802 	  switch (fc->col_type[r])
4803 	    {
4804 	    case DW_CFA_undefined:
4805 	      strcpy (tmp, "u");
4806 	      break;
4807 	    case DW_CFA_same_value:
4808 	      strcpy (tmp, "s");
4809 	      break;
4810 	    case DW_CFA_offset:
4811 	      sprintf (tmp, "c%+d", fc->col_offset[r]);
4812 	      break;
4813 	    case DW_CFA_val_offset:
4814 	      sprintf (tmp, "v%+d", fc->col_offset[r]);
4815 	      break;
4816 	    case DW_CFA_register:
4817 	      sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
4818 	      break;
4819 	    case DW_CFA_expression:
4820 	      strcpy (tmp, "exp");
4821 	      break;
4822 	    case DW_CFA_val_expression:
4823 	      strcpy (tmp, "vexp");
4824 	      break;
4825 	    default:
4826 	      strcpy (tmp, "n/a");
4827 	      break;
4828 	    }
4829 	  printf ("%-5s ", tmp);
4830 	}
4831     }
4832   printf ("\n");
4833 }
4834 
4835 #define GET(N)	byte_get (start, N); start += N
4836 #define LEB()	read_leb128 (start, & length_return, 0); start += length_return
4837 #define SLEB()	read_sleb128 (start, & length_return); start += length_return
4838 
4839 static int
4840 display_debug_frames (struct dwarf_section *section,
4841 		      void *file ATTRIBUTE_UNUSED)
4842 {
4843   unsigned char *start = section->start;
4844   unsigned char *end = start + section->size;
4845   unsigned char *section_start = start;
4846   Frame_Chunk *chunks = 0;
4847   Frame_Chunk *remembered_state = 0;
4848   Frame_Chunk *rs;
4849   int is_eh = strcmp (section->name, ".eh_frame") == 0;
4850   unsigned int length_return;
4851   int max_regs = 0;
4852   const char *bad_reg = _("bad register: ");
4853   int saved_eh_addr_size = eh_addr_size;
4854 
4855   printf (_("Contents of the %s section:\n"), section->name);
4856 
4857   while (start < end)
4858     {
4859       unsigned char *saved_start;
4860       unsigned char *block_end;
4861       unsigned long length;
4862       unsigned long cie_id;
4863       Frame_Chunk *fc;
4864       Frame_Chunk *cie;
4865       int need_col_headers = 1;
4866       unsigned char *augmentation_data = NULL;
4867       unsigned long augmentation_data_len = 0;
4868       int encoded_ptr_size = saved_eh_addr_size;
4869       int offset_size;
4870       int initial_length_size;
4871 
4872       saved_start = start;
4873       length = byte_get (start, 4); start += 4;
4874 
4875       if (length == 0)
4876 	{
4877 	  printf ("\n%08lx ZERO terminator\n\n",
4878 		    (unsigned long)(saved_start - section_start));
4879 	  continue;
4880 	}
4881 
4882       if (length == 0xffffffff)
4883 	{
4884 	  length = byte_get (start, 8);
4885 	  start += 8;
4886 	  offset_size = 8;
4887 	  initial_length_size = 12;
4888 	}
4889       else
4890 	{
4891 	  offset_size = 4;
4892 	  initial_length_size = 4;
4893 	}
4894 
4895       block_end = saved_start + length + initial_length_size;
4896       if (block_end > end)
4897 	{
4898 	  warn ("Invalid length %#08lx in FDE at %#08lx\n",
4899 		length, (unsigned long)(saved_start - section_start));
4900 	  block_end = end;
4901 	}
4902       cie_id = byte_get (start, offset_size); start += offset_size;
4903 
4904       if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
4905 	{
4906 	  int version;
4907 
4908 	  fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
4909 	  memset (fc, 0, sizeof (Frame_Chunk));
4910 
4911 	  fc->next = chunks;
4912 	  chunks = fc;
4913 	  fc->chunk_start = saved_start;
4914 	  fc->ncols = 0;
4915 	  fc->col_type = (short int *) xmalloc (sizeof (short int));
4916 	  fc->col_offset = (int *) xmalloc (sizeof (int));
4917 	  frame_need_space (fc, max_regs - 1);
4918 
4919 	  version = *start++;
4920 
4921 	  fc->augmentation = (char *) start;
4922 	  start = (unsigned char *) strchr ((char *) start, '\0') + 1;
4923 
4924 	  if (strcmp (fc->augmentation, "eh") == 0)
4925 	    start += eh_addr_size;
4926 
4927 	  if (version >= 4)
4928 	    {
4929 	      fc->ptr_size = GET (1);
4930 	      fc->segment_size = GET (1);
4931 	      eh_addr_size = fc->ptr_size;
4932 	    }
4933 	  else
4934 	    {
4935 	      fc->ptr_size = eh_addr_size;
4936 	      fc->segment_size = 0;
4937 	    }
4938 	  fc->code_factor = LEB ();
4939 	  fc->data_factor = SLEB ();
4940 	  if (version == 1)
4941 	    {
4942 	      fc->ra = GET (1);
4943 	    }
4944 	  else
4945 	    {
4946 	      fc->ra = LEB ();
4947 	    }
4948 
4949 	  if (fc->augmentation[0] == 'z')
4950 	    {
4951 	      augmentation_data_len = LEB ();
4952 	      augmentation_data = start;
4953 	      start += augmentation_data_len;
4954 	    }
4955 	  cie = fc;
4956 
4957 	  if (do_debug_frames_interp)
4958 	    printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
4959 		    (unsigned long)(saved_start - section_start), length, cie_id,
4960 		    fc->augmentation, fc->code_factor, fc->data_factor,
4961 		    fc->ra);
4962 	  else
4963 	    {
4964 	      printf ("\n%08lx %08lx %08lx CIE\n",
4965 		      (unsigned long)(saved_start - section_start), length, cie_id);
4966 	      printf ("  Version:               %d\n", version);
4967 	      printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
4968 	      if (version >= 4)
4969 		{
4970 		  printf ("  Pointer Size:          %u\n", fc->ptr_size);
4971 		  printf ("  Segment Size:          %u\n", fc->segment_size);
4972 		}
4973 	      printf ("  Code alignment factor: %u\n", fc->code_factor);
4974 	      printf ("  Data alignment factor: %d\n", fc->data_factor);
4975 	      printf ("  Return address column: %d\n", fc->ra);
4976 
4977 	      if (augmentation_data_len)
4978 		{
4979 		  unsigned long i;
4980 		  printf ("  Augmentation data:    ");
4981 		  for (i = 0; i < augmentation_data_len; ++i)
4982 		    printf (" %02x", augmentation_data[i]);
4983 		  putchar ('\n');
4984 		}
4985 	      putchar ('\n');
4986 	    }
4987 
4988 	  if (augmentation_data_len)
4989 	    {
4990 	      unsigned char *p, *q;
4991 	      p = (unsigned char *) fc->augmentation + 1;
4992 	      q = augmentation_data;
4993 
4994 	      while (1)
4995 		{
4996 		  if (*p == 'L')
4997 		    q++;
4998 		  else if (*p == 'P')
4999 		    q += 1 + size_of_encoded_value (*q);
5000 		  else if (*p == 'R')
5001 		    fc->fde_encoding = *q++;
5002 		  else if (*p == 'S')
5003 		    ;
5004 		  else
5005 		    break;
5006 		  p++;
5007 		}
5008 
5009 	      if (fc->fde_encoding)
5010 		encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
5011 	    }
5012 
5013 	  frame_need_space (fc, fc->ra);
5014 	}
5015       else
5016 	{
5017 	  unsigned char *look_for;
5018 	  static Frame_Chunk fde_fc;
5019 	  unsigned long segment_selector;
5020 
5021 	  fc = & fde_fc;
5022 	  memset (fc, 0, sizeof (Frame_Chunk));
5023 
5024 	  look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
5025 
5026 	  for (cie = chunks; cie ; cie = cie->next)
5027 	    if (cie->chunk_start == look_for)
5028 	      break;
5029 
5030 	  if (!cie)
5031 	    {
5032 	      warn ("Invalid CIE pointer %#08lx in FDE at %#08lx\n",
5033 		    cie_id, (unsigned long)(saved_start - section_start));
5034 	      fc->ncols = 0;
5035 	      fc->col_type = (short int *) xmalloc (sizeof (short int));
5036 	      fc->col_offset = (int *) xmalloc (sizeof (int));
5037 	      frame_need_space (fc, max_regs - 1);
5038 	      cie = fc;
5039 	      fc->augmentation = "";
5040 	      fc->fde_encoding = 0;
5041 	      fc->ptr_size = eh_addr_size;
5042 	      fc->segment_size = 0;
5043 	    }
5044 	  else
5045 	    {
5046 	      fc->ncols = cie->ncols;
5047 	      fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
5048 	      fc->col_offset =  (int *) xcmalloc (fc->ncols, sizeof (int));
5049 	      memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
5050 	      memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
5051 	      fc->augmentation = cie->augmentation;
5052 	      fc->ptr_size = cie->ptr_size;
5053 	      eh_addr_size = cie->ptr_size;
5054 	      fc->segment_size = cie->segment_size;
5055 	      fc->code_factor = cie->code_factor;
5056 	      fc->data_factor = cie->data_factor;
5057 	      fc->cfa_reg = cie->cfa_reg;
5058 	      fc->cfa_offset = cie->cfa_offset;
5059 	      fc->ra = cie->ra;
5060 	      frame_need_space (fc, max_regs - 1);
5061 	      fc->fde_encoding = cie->fde_encoding;
5062 	    }
5063 
5064 	  if (fc->fde_encoding)
5065 	    encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
5066 
5067 	  segment_selector = 0;
5068 	  if (fc->segment_size)
5069 	    {
5070 	      segment_selector = byte_get (start, fc->segment_size);
5071 	      start += fc->segment_size;
5072 	    }
5073 	  fc->pc_begin = get_encoded_value (start, fc->fde_encoding, section);
5074 	  start += encoded_ptr_size;
5075 	  fc->pc_range = byte_get (start, encoded_ptr_size);
5076 	  start += encoded_ptr_size;
5077 
5078 	  if (cie->augmentation[0] == 'z')
5079 	    {
5080 	      augmentation_data_len = LEB ();
5081 	      augmentation_data = start;
5082 	      start += augmentation_data_len;
5083 	    }
5084 
5085 	  printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=",
5086 		  (unsigned long)(saved_start - section_start), length, cie_id,
5087 		  (unsigned long)(cie->chunk_start - section_start));
5088 	  if (fc->segment_size)
5089 	    printf ("%04lx:", segment_selector);
5090 	  printf ("%08lx..%08lx\n", fc->pc_begin, fc->pc_begin + fc->pc_range);
5091 	  if (! do_debug_frames_interp && augmentation_data_len)
5092 	    {
5093 	      unsigned long i;
5094 
5095 	      printf ("  Augmentation data:    ");
5096 	      for (i = 0; i < augmentation_data_len; ++i)
5097 		printf (" %02x", augmentation_data[i]);
5098 	      putchar ('\n');
5099 	      putchar ('\n');
5100 	    }
5101 	}
5102 
5103       /* At this point, fc is the current chunk, cie (if any) is set, and
5104 	 we're about to interpret instructions for the chunk.  */
5105       /* ??? At present we need to do this always, since this sizes the
5106 	 fc->col_type and fc->col_offset arrays, which we write into always.
5107 	 We should probably split the interpreted and non-interpreted bits
5108 	 into two different routines, since there's so much that doesn't
5109 	 really overlap between them.  */
5110       if (1 || do_debug_frames_interp)
5111 	{
5112 	  /* Start by making a pass over the chunk, allocating storage
5113 	     and taking note of what registers are used.  */
5114 	  unsigned char *tmp = start;
5115 
5116 	  while (start < block_end)
5117 	    {
5118 	      unsigned op, opa;
5119 	      unsigned long reg, temp;
5120 
5121 	      op = *start++;
5122 	      opa = op & 0x3f;
5123 	      if (op & 0xc0)
5124 		op &= 0xc0;
5125 
5126 	      /* Warning: if you add any more cases to this switch, be
5127 		 sure to add them to the corresponding switch below.  */
5128 	      switch (op)
5129 		{
5130 		case DW_CFA_advance_loc:
5131 		  break;
5132 		case DW_CFA_offset:
5133 		  LEB ();
5134 		  if (frame_need_space (fc, opa) >= 0)
5135 		    fc->col_type[opa] = DW_CFA_undefined;
5136 		  break;
5137 		case DW_CFA_restore:
5138 		  if (frame_need_space (fc, opa) >= 0)
5139 		    fc->col_type[opa] = DW_CFA_undefined;
5140 		  break;
5141 		case DW_CFA_set_loc:
5142 		  start += encoded_ptr_size;
5143 		  break;
5144 		case DW_CFA_advance_loc1:
5145 		  start += 1;
5146 		  break;
5147 		case DW_CFA_advance_loc2:
5148 		  start += 2;
5149 		  break;
5150 		case DW_CFA_advance_loc4:
5151 		  start += 4;
5152 		  break;
5153 		case DW_CFA_offset_extended:
5154 		case DW_CFA_val_offset:
5155 		  reg = LEB (); LEB ();
5156 		  if (frame_need_space (fc, reg) >= 0)
5157 		    fc->col_type[reg] = DW_CFA_undefined;
5158 		  break;
5159 		case DW_CFA_restore_extended:
5160 		  reg = LEB ();
5161 		  frame_need_space (fc, reg);
5162 		  if (frame_need_space (fc, reg) >= 0)
5163 		    fc->col_type[reg] = DW_CFA_undefined;
5164 		  break;
5165 		case DW_CFA_undefined:
5166 		  reg = LEB ();
5167 		  if (frame_need_space (fc, reg) >= 0)
5168 		    fc->col_type[reg] = DW_CFA_undefined;
5169 		  break;
5170 		case DW_CFA_same_value:
5171 		  reg = LEB ();
5172 		  if (frame_need_space (fc, reg) >= 0)
5173 		    fc->col_type[reg] = DW_CFA_undefined;
5174 		  break;
5175 		case DW_CFA_register:
5176 		  reg = LEB (); LEB ();
5177 		  if (frame_need_space (fc, reg) >= 0)
5178 		    fc->col_type[reg] = DW_CFA_undefined;
5179 		  break;
5180 		case DW_CFA_def_cfa:
5181 		  LEB (); LEB ();
5182 		  break;
5183 		case DW_CFA_def_cfa_register:
5184 		  LEB ();
5185 		  break;
5186 		case DW_CFA_def_cfa_offset:
5187 		  LEB ();
5188 		  break;
5189 		case DW_CFA_def_cfa_expression:
5190 		  temp = LEB ();
5191 		  start += temp;
5192 		  break;
5193 		case DW_CFA_expression:
5194 		case DW_CFA_val_expression:
5195 		  reg = LEB ();
5196 		  temp = LEB ();
5197 		  start += temp;
5198 		  if (frame_need_space (fc, reg) >= 0)
5199 		    fc->col_type[reg] = DW_CFA_undefined;
5200 		  break;
5201 		case DW_CFA_offset_extended_sf:
5202 		case DW_CFA_val_offset_sf:
5203 		  reg = LEB (); SLEB ();
5204 		  if (frame_need_space (fc, reg) >= 0)
5205 		    fc->col_type[reg] = DW_CFA_undefined;
5206 		  break;
5207 		case DW_CFA_def_cfa_sf:
5208 		  LEB (); SLEB ();
5209 		  break;
5210 		case DW_CFA_def_cfa_offset_sf:
5211 		  SLEB ();
5212 		  break;
5213 		case DW_CFA_MIPS_advance_loc8:
5214 		  start += 8;
5215 		  break;
5216 		case DW_CFA_GNU_args_size:
5217 		  LEB ();
5218 		  break;
5219 		case DW_CFA_GNU_negative_offset_extended:
5220 		  reg = LEB (); LEB ();
5221 		  if (frame_need_space (fc, reg) >= 0)
5222 		    fc->col_type[reg] = DW_CFA_undefined;
5223 		  break;
5224 		default:
5225 		  break;
5226 		}
5227 	    }
5228 	  start = tmp;
5229 	}
5230 
5231       /* Now we know what registers are used, make a second pass over
5232 	 the chunk, this time actually printing out the info.  */
5233 
5234       while (start < block_end)
5235 	{
5236 	  unsigned op, opa;
5237 	  unsigned long ul, reg, roffs;
5238 	  long l, ofs;
5239 	  dwarf_vma vma;
5240 	  const char *reg_prefix = "";
5241 
5242 	  op = *start++;
5243 	  opa = op & 0x3f;
5244 	  if (op & 0xc0)
5245 	    op &= 0xc0;
5246 
5247 	  /* Warning: if you add any more cases to this switch, be
5248 	     sure to add them to the corresponding switch above.  */
5249 	  switch (op)
5250 	    {
5251 	    case DW_CFA_advance_loc:
5252 	      if (do_debug_frames_interp)
5253 		frame_display_row (fc, &need_col_headers, &max_regs);
5254 	      else
5255 		printf ("  DW_CFA_advance_loc: %d to %08lx\n",
5256 			opa * fc->code_factor,
5257 			fc->pc_begin + opa * fc->code_factor);
5258 	      fc->pc_begin += opa * fc->code_factor;
5259 	      break;
5260 
5261 	    case DW_CFA_offset:
5262 	      roffs = LEB ();
5263 	      if (opa >= (unsigned int) fc->ncols)
5264 		reg_prefix = bad_reg;
5265 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5266 		printf ("  DW_CFA_offset: %s%s at cfa%+ld\n",
5267 			reg_prefix, regname (opa, 0),
5268 			roffs * fc->data_factor);
5269 	      if (*reg_prefix == '\0')
5270 		{
5271 		  fc->col_type[opa] = DW_CFA_offset;
5272 		  fc->col_offset[opa] = roffs * fc->data_factor;
5273 		}
5274 	      break;
5275 
5276 	    case DW_CFA_restore:
5277 	      if (opa >= (unsigned int) cie->ncols
5278 		  || opa >= (unsigned int) fc->ncols)
5279 		reg_prefix = bad_reg;
5280 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5281 		printf ("  DW_CFA_restore: %s%s\n",
5282 			reg_prefix, regname (opa, 0));
5283 	      if (*reg_prefix == '\0')
5284 		{
5285 		  fc->col_type[opa] = cie->col_type[opa];
5286 		  fc->col_offset[opa] = cie->col_offset[opa];
5287 		  if (do_debug_frames_interp
5288 		      && fc->col_type[opa] == DW_CFA_unreferenced)
5289 		    fc->col_type[opa] = DW_CFA_undefined;
5290 		}
5291 	      break;
5292 
5293 	    case DW_CFA_set_loc:
5294 	      vma = get_encoded_value (start, fc->fde_encoding, section);
5295 	      start += encoded_ptr_size;
5296 	      if (do_debug_frames_interp)
5297 		frame_display_row (fc, &need_col_headers, &max_regs);
5298 	      else
5299 		printf ("  DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
5300 	      fc->pc_begin = vma;
5301 	      break;
5302 
5303 	    case DW_CFA_advance_loc1:
5304 	      ofs = byte_get (start, 1); start += 1;
5305 	      if (do_debug_frames_interp)
5306 		frame_display_row (fc, &need_col_headers, &max_regs);
5307 	      else
5308 		printf ("  DW_CFA_advance_loc1: %ld to %08lx\n",
5309 			ofs * fc->code_factor,
5310 			fc->pc_begin + ofs * fc->code_factor);
5311 	      fc->pc_begin += ofs * fc->code_factor;
5312 	      break;
5313 
5314 	    case DW_CFA_advance_loc2:
5315 	      ofs = byte_get (start, 2); start += 2;
5316 	      if (do_debug_frames_interp)
5317 		frame_display_row (fc, &need_col_headers, &max_regs);
5318 	      else
5319 		printf ("  DW_CFA_advance_loc2: %ld to %08lx\n",
5320 			ofs * fc->code_factor,
5321 			fc->pc_begin + ofs * fc->code_factor);
5322 	      fc->pc_begin += ofs * fc->code_factor;
5323 	      break;
5324 
5325 	    case DW_CFA_advance_loc4:
5326 	      ofs = byte_get (start, 4); start += 4;
5327 	      if (do_debug_frames_interp)
5328 		frame_display_row (fc, &need_col_headers, &max_regs);
5329 	      else
5330 		printf ("  DW_CFA_advance_loc4: %ld to %08lx\n",
5331 			ofs * fc->code_factor,
5332 			fc->pc_begin + ofs * fc->code_factor);
5333 	      fc->pc_begin += ofs * fc->code_factor;
5334 	      break;
5335 
5336 	    case DW_CFA_offset_extended:
5337 	      reg = LEB ();
5338 	      roffs = LEB ();
5339 	      if (reg >= (unsigned int) fc->ncols)
5340 		reg_prefix = bad_reg;
5341 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5342 		printf ("  DW_CFA_offset_extended: %s%s at cfa%+ld\n",
5343 			reg_prefix, regname (reg, 0),
5344 			roffs * fc->data_factor);
5345 	      if (*reg_prefix == '\0')
5346 		{
5347 		  fc->col_type[reg] = DW_CFA_offset;
5348 		  fc->col_offset[reg] = roffs * fc->data_factor;
5349 		}
5350 	      break;
5351 
5352 	    case DW_CFA_val_offset:
5353 	      reg = LEB ();
5354 	      roffs = LEB ();
5355 	      if (reg >= (unsigned int) fc->ncols)
5356 		reg_prefix = bad_reg;
5357 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5358 		printf ("  DW_CFA_val_offset: %s%s at cfa%+ld\n",
5359 			reg_prefix, regname (reg, 0),
5360 			roffs * fc->data_factor);
5361 	      if (*reg_prefix == '\0')
5362 		{
5363 		  fc->col_type[reg] = DW_CFA_val_offset;
5364 		  fc->col_offset[reg] = roffs * fc->data_factor;
5365 		}
5366 	      break;
5367 
5368 	    case DW_CFA_restore_extended:
5369 	      reg = LEB ();
5370 	      if (reg >= (unsigned int) cie->ncols
5371 		  || reg >= (unsigned int) fc->ncols)
5372 		reg_prefix = bad_reg;
5373 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5374 		printf ("  DW_CFA_restore_extended: %s%s\n",
5375 			reg_prefix, regname (reg, 0));
5376 	      if (*reg_prefix == '\0')
5377 		{
5378 		  fc->col_type[reg] = cie->col_type[reg];
5379 		  fc->col_offset[reg] = cie->col_offset[reg];
5380 		}
5381 	      break;
5382 
5383 	    case DW_CFA_undefined:
5384 	      reg = LEB ();
5385 	      if (reg >= (unsigned int) fc->ncols)
5386 		reg_prefix = bad_reg;
5387 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5388 		printf ("  DW_CFA_undefined: %s%s\n",
5389 			reg_prefix, regname (reg, 0));
5390 	      if (*reg_prefix == '\0')
5391 		{
5392 		  fc->col_type[reg] = DW_CFA_undefined;
5393 		  fc->col_offset[reg] = 0;
5394 		}
5395 	      break;
5396 
5397 	    case DW_CFA_same_value:
5398 	      reg = LEB ();
5399 	      if (reg >= (unsigned int) fc->ncols)
5400 		reg_prefix = bad_reg;
5401 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5402 		printf ("  DW_CFA_same_value: %s%s\n",
5403 			reg_prefix, regname (reg, 0));
5404 	      if (*reg_prefix == '\0')
5405 		{
5406 		  fc->col_type[reg] = DW_CFA_same_value;
5407 		  fc->col_offset[reg] = 0;
5408 		}
5409 	      break;
5410 
5411 	    case DW_CFA_register:
5412 	      reg = LEB ();
5413 	      roffs = LEB ();
5414 	      if (reg >= (unsigned int) fc->ncols)
5415 		reg_prefix = bad_reg;
5416 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5417 		{
5418 		  printf ("  DW_CFA_register: %s%s in ",
5419 			  reg_prefix, regname (reg, 0));
5420 		  puts (regname (roffs, 0));
5421 		}
5422 	      if (*reg_prefix == '\0')
5423 		{
5424 		  fc->col_type[reg] = DW_CFA_register;
5425 		  fc->col_offset[reg] = roffs;
5426 		}
5427 	      break;
5428 
5429 	    case DW_CFA_remember_state:
5430 	      if (! do_debug_frames_interp)
5431 		printf ("  DW_CFA_remember_state\n");
5432 	      rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
5433 	      rs->ncols = fc->ncols;
5434 	      rs->col_type = (short int *) xcmalloc (rs->ncols,
5435                                                      sizeof (short int));
5436 	      rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (int));
5437 	      memcpy (rs->col_type, fc->col_type, rs->ncols);
5438 	      memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
5439 	      rs->next = remembered_state;
5440 	      remembered_state = rs;
5441 	      break;
5442 
5443 	    case DW_CFA_restore_state:
5444 	      if (! do_debug_frames_interp)
5445 		printf ("  DW_CFA_restore_state\n");
5446 	      rs = remembered_state;
5447 	      if (rs)
5448 		{
5449 		  remembered_state = rs->next;
5450 		  frame_need_space (fc, rs->ncols - 1);
5451 		  memcpy (fc->col_type, rs->col_type, rs->ncols);
5452 		  memcpy (fc->col_offset, rs->col_offset,
5453 			  rs->ncols * sizeof (int));
5454 		  free (rs->col_type);
5455 		  free (rs->col_offset);
5456 		  free (rs);
5457 		}
5458 	      else if (do_debug_frames_interp)
5459 		printf ("Mismatched DW_CFA_restore_state\n");
5460 	      break;
5461 
5462 	    case DW_CFA_def_cfa:
5463 	      fc->cfa_reg = LEB ();
5464 	      fc->cfa_offset = LEB ();
5465 	      fc->cfa_exp = 0;
5466 	      if (! do_debug_frames_interp)
5467 		printf ("  DW_CFA_def_cfa: %s ofs %d\n",
5468 			regname (fc->cfa_reg, 0), fc->cfa_offset);
5469 	      break;
5470 
5471 	    case DW_CFA_def_cfa_register:
5472 	      fc->cfa_reg = LEB ();
5473 	      fc->cfa_exp = 0;
5474 	      if (! do_debug_frames_interp)
5475 		printf ("  DW_CFA_def_cfa_register: %s\n",
5476 			regname (fc->cfa_reg, 0));
5477 	      break;
5478 
5479 	    case DW_CFA_def_cfa_offset:
5480 	      fc->cfa_offset = LEB ();
5481 	      if (! do_debug_frames_interp)
5482 		printf ("  DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
5483 	      break;
5484 
5485 	    case DW_CFA_nop:
5486 	      if (! do_debug_frames_interp)
5487 		printf ("  DW_CFA_nop\n");
5488 	      break;
5489 
5490 	    case DW_CFA_def_cfa_expression:
5491 	      ul = LEB ();
5492 	      if (! do_debug_frames_interp)
5493 		{
5494 		  printf ("  DW_CFA_def_cfa_expression (");
5495 		  decode_location_expression (start, eh_addr_size, 0, -1,
5496 					      ul, 0, section);
5497 		  printf (")\n");
5498 		}
5499 	      fc->cfa_exp = 1;
5500 	      start += ul;
5501 	      break;
5502 
5503 	    case DW_CFA_expression:
5504 	      reg = LEB ();
5505 	      ul = LEB ();
5506 	      if (reg >= (unsigned int) fc->ncols)
5507 		reg_prefix = bad_reg;
5508 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5509 		{
5510 		  printf ("  DW_CFA_expression: %s%s (",
5511 			  reg_prefix, regname (reg, 0));
5512 		  decode_location_expression (start, eh_addr_size, 0, -1,
5513 					      ul, 0, section);
5514 		  printf (")\n");
5515 		}
5516 	      if (*reg_prefix == '\0')
5517 		fc->col_type[reg] = DW_CFA_expression;
5518 	      start += ul;
5519 	      break;
5520 
5521 	    case DW_CFA_val_expression:
5522 	      reg = LEB ();
5523 	      ul = LEB ();
5524 	      if (reg >= (unsigned int) fc->ncols)
5525 		reg_prefix = bad_reg;
5526 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5527 		{
5528 		  printf ("  DW_CFA_val_expression: %s%s (",
5529 			  reg_prefix, regname (reg, 0));
5530 		  decode_location_expression (start, eh_addr_size, 0, -1,
5531 					      ul, 0, section);
5532 		  printf (")\n");
5533 		}
5534 	      if (*reg_prefix == '\0')
5535 		fc->col_type[reg] = DW_CFA_val_expression;
5536 	      start += ul;
5537 	      break;
5538 
5539 	    case DW_CFA_offset_extended_sf:
5540 	      reg = LEB ();
5541 	      l = SLEB ();
5542 	      if (frame_need_space (fc, reg) < 0)
5543 		reg_prefix = bad_reg;
5544 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5545 		printf ("  DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
5546 			reg_prefix, regname (reg, 0),
5547 			l * fc->data_factor);
5548 	      if (*reg_prefix == '\0')
5549 		{
5550 		  fc->col_type[reg] = DW_CFA_offset;
5551 		  fc->col_offset[reg] = l * fc->data_factor;
5552 		}
5553 	      break;
5554 
5555 	    case DW_CFA_val_offset_sf:
5556 	      reg = LEB ();
5557 	      l = SLEB ();
5558 	      if (frame_need_space (fc, reg) < 0)
5559 		reg_prefix = bad_reg;
5560 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5561 		printf ("  DW_CFA_val_offset_sf: %s%s at cfa%+ld\n",
5562 			reg_prefix, regname (reg, 0),
5563 			l * fc->data_factor);
5564 	      if (*reg_prefix == '\0')
5565 		{
5566 		  fc->col_type[reg] = DW_CFA_val_offset;
5567 		  fc->col_offset[reg] = l * fc->data_factor;
5568 		}
5569 	      break;
5570 
5571 	    case DW_CFA_def_cfa_sf:
5572 	      fc->cfa_reg = LEB ();
5573 	      fc->cfa_offset = SLEB ();
5574 	      fc->cfa_offset = fc->cfa_offset * fc->data_factor;
5575 	      fc->cfa_exp = 0;
5576 	      if (! do_debug_frames_interp)
5577 		printf ("  DW_CFA_def_cfa_sf: %s ofs %d\n",
5578 			regname (fc->cfa_reg, 0), fc->cfa_offset);
5579 	      break;
5580 
5581 	    case DW_CFA_def_cfa_offset_sf:
5582 	      fc->cfa_offset = SLEB ();
5583 	      fc->cfa_offset = fc->cfa_offset * fc->data_factor;
5584 	      if (! do_debug_frames_interp)
5585 		printf ("  DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
5586 	      break;
5587 
5588 	    case DW_CFA_MIPS_advance_loc8:
5589 	      ofs = byte_get (start, 8); start += 8;
5590 	      if (do_debug_frames_interp)
5591 		frame_display_row (fc, &need_col_headers, &max_regs);
5592 	      else
5593 		printf ("  DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
5594 			ofs * fc->code_factor,
5595 			fc->pc_begin + ofs * fc->code_factor);
5596 	      fc->pc_begin += ofs * fc->code_factor;
5597 	      break;
5598 
5599 	    case DW_CFA_GNU_window_save:
5600 	      if (! do_debug_frames_interp)
5601 		printf ("  DW_CFA_GNU_window_save\n");
5602 	      break;
5603 
5604 	    case DW_CFA_GNU_args_size:
5605 	      ul = LEB ();
5606 	      if (! do_debug_frames_interp)
5607 		printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
5608 	      break;
5609 
5610 	    case DW_CFA_GNU_negative_offset_extended:
5611 	      reg = LEB ();
5612 	      l = - LEB ();
5613 	      if (frame_need_space (fc, reg) < 0)
5614 		reg_prefix = bad_reg;
5615 	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5616 		printf ("  DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
5617 			reg_prefix, regname (reg, 0),
5618 			l * fc->data_factor);
5619 	      if (*reg_prefix == '\0')
5620 		{
5621 		  fc->col_type[reg] = DW_CFA_offset;
5622 		  fc->col_offset[reg] = l * fc->data_factor;
5623 		}
5624 	      break;
5625 
5626 	    default:
5627 	      if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
5628 		printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
5629 	      else
5630 		warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
5631 	      start = block_end;
5632 	    }
5633 	}
5634 
5635       if (do_debug_frames_interp)
5636 	frame_display_row (fc, &need_col_headers, &max_regs);
5637 
5638       start = block_end;
5639       eh_addr_size = saved_eh_addr_size;
5640     }
5641 
5642   printf ("\n");
5643 
5644   return 1;
5645 }
5646 
5647 #undef GET
5648 #undef LEB
5649 #undef SLEB
5650 
5651 static int
5652 display_gdb_index (struct dwarf_section *section,
5653 		   void *file ATTRIBUTE_UNUSED)
5654 {
5655   unsigned char *start = section->start;
5656   uint32_t version;
5657   uint32_t cu_list_offset, tu_list_offset;
5658   uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
5659   unsigned int cu_list_elements, tu_list_elements;
5660   unsigned int address_table_size, symbol_table_slots;
5661   unsigned char *cu_list, *tu_list;
5662   unsigned char *address_table, *symbol_table, *constant_pool;
5663   unsigned int i;
5664 
5665   /* The documentation for the format of this file is in gdb/dwarf2read.c.  */
5666 
5667   printf (_("Contents of the %s section:\n"), section->name);
5668 
5669   if (section->size < 6 * sizeof (uint32_t))
5670     {
5671       warn (_("Truncated header in the %s section.\n"), section->name);
5672       return 0;
5673     }
5674 
5675   version = byte_get_little_endian (start, 4);
5676   printf (_("Version %ld\n"), (long) version);
5677 
5678   /* Prior versions are obsolete, and future versions may not be
5679      backwards compatible.  */
5680   if (version < 3 || version > 7)
5681     {
5682       warn (_("Unsupported version %lu.\n"), (unsigned long) version);
5683       return 0;
5684     }
5685   if (version < 4)
5686     warn (_("The address table data in version 3 may be wrong.\n"));
5687   if (version < 5)
5688     warn (_("Version 4 does not support case insensitive lookups.\n"));
5689   if (version < 6)
5690     warn (_("Version 5 does not include inlined functions.\n"));
5691   if (version < 7)
5692       warn (_("Version 6 does not include symbol attributes.\n"));
5693 
5694   cu_list_offset = byte_get_little_endian (start + 4, 4);
5695   tu_list_offset = byte_get_little_endian (start + 8, 4);
5696   address_table_offset = byte_get_little_endian (start + 12, 4);
5697   symbol_table_offset = byte_get_little_endian (start + 16, 4);
5698   constant_pool_offset = byte_get_little_endian (start + 20, 4);
5699 
5700   if (cu_list_offset > section->size
5701       || tu_list_offset > section->size
5702       || address_table_offset > section->size
5703       || symbol_table_offset > section->size
5704       || constant_pool_offset > section->size)
5705     {
5706       warn (_("Corrupt header in the %s section.\n"), section->name);
5707       return 0;
5708     }
5709 
5710   cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
5711   tu_list_elements = (address_table_offset - tu_list_offset) / 8;
5712   address_table_size = symbol_table_offset - address_table_offset;
5713   symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
5714 
5715   cu_list = start + cu_list_offset;
5716   tu_list = start + tu_list_offset;
5717   address_table = start + address_table_offset;
5718   symbol_table = start + symbol_table_offset;
5719   constant_pool = start + constant_pool_offset;
5720 
5721   printf (_("\nCU table:\n"));
5722   for (i = 0; i < cu_list_elements; i += 2)
5723     {
5724       uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
5725       uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
5726 
5727       printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
5728 	      (unsigned long) cu_offset,
5729 	      (unsigned long) (cu_offset + cu_length - 1));
5730     }
5731 
5732   printf (_("\nTU table:\n"));
5733   for (i = 0; i < tu_list_elements; i += 3)
5734     {
5735       uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
5736       uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
5737       uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
5738 
5739       printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
5740 	      (unsigned long) tu_offset,
5741 	      (unsigned long) type_offset);
5742       print_dwarf_vma (signature, 8);
5743       printf ("\n");
5744     }
5745 
5746   printf (_("\nAddress table:\n"));
5747   for (i = 0; i < address_table_size; i += 2 * 8 + 4)
5748     {
5749       uint64_t low = byte_get_little_endian (address_table + i, 8);
5750       uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
5751       uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
5752 
5753       print_dwarf_vma (low, 8);
5754       print_dwarf_vma (high, 8);
5755       printf (_("%lu\n"), (unsigned long) cu_index);
5756     }
5757 
5758   printf (_("\nSymbol table:\n"));
5759   for (i = 0; i < symbol_table_slots; ++i)
5760     {
5761       uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
5762       uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
5763       uint32_t num_cus, cu;
5764 
5765       if (name_offset != 0
5766 	  || cu_vector_offset != 0)
5767 	{
5768 	  unsigned int j;
5769 
5770 	  printf ("[%3u] %s:", i, constant_pool + name_offset);
5771 	  num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
5772 	  if (num_cus > 1)
5773 	    printf ("\n");
5774 	  for (j = 0; j < num_cus; ++j)
5775 	    {
5776 	      gdb_index_symbol_kind kind;
5777 
5778 	      cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
5779 	      kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
5780 	      cu = GDB_INDEX_CU_VALUE (cu);
5781 	      /* Convert to TU number if it's for a type unit.  */
5782 	      if (cu >= cu_list_elements / 2)
5783 		printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
5784 			(unsigned long) (cu - cu_list_elements / 2));
5785 	      else
5786 		printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
5787 
5788 	      switch (kind)
5789 		{
5790 		case GDB_INDEX_SYMBOL_KIND_NONE:
5791 		  printf (_(" [no symbol information]"));
5792 		  break;
5793 		case GDB_INDEX_SYMBOL_KIND_TYPE:
5794 		  printf (_(" [type]"));
5795 		  break;
5796 		case GDB_INDEX_SYMBOL_KIND_VARIABLE:
5797 		  printf (_(" [variable]"));
5798 		  break;
5799 		case GDB_INDEX_SYMBOL_KIND_FUNCTION:
5800 		  printf (_(" [function]"));
5801 		  break;
5802 		case GDB_INDEX_SYMBOL_KIND_OTHER:
5803 		  printf (_(" [other]"));
5804 		  break;
5805 		default:
5806 		  printf (_(" [unknown: %d]"), kind);
5807 		  break;
5808 		}
5809 	      if (num_cus > 1)
5810 		printf ("\n");
5811 	    }
5812 	  if (num_cus <= 1)
5813 	    printf ("\n");
5814 	}
5815     }
5816 
5817   return 1;
5818 }
5819 
5820 static int
5821 display_debug_not_supported (struct dwarf_section *section,
5822 			     void *file ATTRIBUTE_UNUSED)
5823 {
5824   printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
5825 	    section->name);
5826 
5827   return 1;
5828 }
5829 
5830 void *
5831 cmalloc (size_t nmemb, size_t size)
5832 {
5833   /* Check for overflow.  */
5834   if (nmemb >= ~(size_t) 0 / size)
5835     return NULL;
5836   else
5837     return malloc (nmemb * size);
5838 }
5839 
5840 void *
5841 xcmalloc (size_t nmemb, size_t size)
5842 {
5843   /* Check for overflow.  */
5844   if (nmemb >= ~(size_t) 0 / size)
5845     return NULL;
5846   else
5847     return xmalloc (nmemb * size);
5848 }
5849 
5850 void *
5851 xcrealloc (void *ptr, size_t nmemb, size_t size)
5852 {
5853   /* Check for overflow.  */
5854   if (nmemb >= ~(size_t) 0 / size)
5855     return NULL;
5856   else
5857     return xrealloc (ptr, nmemb * size);
5858 }
5859 
5860 void
5861 free_debug_memory (void)
5862 {
5863   unsigned int i;
5864 
5865   free_abbrevs ();
5866 
5867   for (i = 0; i < max; i++)
5868     free_debug_section ((enum dwarf_section_display_enum) i);
5869 
5870   if (debug_information != NULL)
5871     {
5872       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
5873 	{
5874 	  for (i = 0; i < num_debug_info_entries; i++)
5875 	    {
5876 	      if (!debug_information [i].max_loc_offsets)
5877 		{
5878 		  free (debug_information [i].loc_offsets);
5879 		  free (debug_information [i].have_frame_base);
5880 		}
5881 	      if (!debug_information [i].max_range_lists)
5882 		free (debug_information [i].range_lists);
5883 	    }
5884 	}
5885 
5886       free (debug_information);
5887       debug_information = NULL;
5888       num_debug_info_entries = 0;
5889     }
5890 }
5891 
5892 void
5893 dwarf_select_sections_by_names (const char *names)
5894 {
5895   typedef struct
5896   {
5897     const char * option;
5898     int *        variable;
5899     int          val;
5900   }
5901   debug_dump_long_opts;
5902 
5903   static const debug_dump_long_opts opts_table [] =
5904     {
5905       /* Please keep this table alpha- sorted.  */
5906       { "Ranges", & do_debug_ranges, 1 },
5907       { "abbrev", & do_debug_abbrevs, 1 },
5908       { "aranges", & do_debug_aranges, 1 },
5909       { "frames", & do_debug_frames, 1 },
5910       { "frames-interp", & do_debug_frames_interp, 1 },
5911       { "info", & do_debug_info, 1 },
5912       { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility.  */
5913       { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
5914       { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
5915       { "loc",  & do_debug_loc, 1 },
5916       { "macro", & do_debug_macinfo, 1 },
5917       { "pubnames", & do_debug_pubnames, 1 },
5918       { "pubtypes", & do_debug_pubtypes, 1 },
5919       /* This entry is for compatability
5920 	 with earlier versions of readelf.  */
5921       { "ranges", & do_debug_aranges, 1 },
5922       { "str", & do_debug_str, 1 },
5923       /* The special .gdb_index section.  */
5924       { "gdb_index", & do_gdb_index, 1 },
5925       /* These trace_* sections are used by Itanium VMS.  */
5926       { "trace_abbrev", & do_trace_abbrevs, 1 },
5927       { "trace_aranges", & do_trace_aranges, 1 },
5928       { "trace_info", & do_trace_info, 1 },
5929       { NULL, NULL, 0 }
5930     };
5931 
5932   const char *p;
5933 
5934   p = names;
5935   while (*p)
5936     {
5937       const debug_dump_long_opts * entry;
5938 
5939       for (entry = opts_table; entry->option; entry++)
5940 	{
5941 	  size_t len = strlen (entry->option);
5942 
5943 	  if (strncmp (p, entry->option, len) == 0
5944 	      && (p[len] == ',' || p[len] == '\0'))
5945 	    {
5946 	      * entry->variable |= entry->val;
5947 
5948 	      /* The --debug-dump=frames-interp option also
5949 		 enables the --debug-dump=frames option.  */
5950 	      if (do_debug_frames_interp)
5951 		do_debug_frames = 1;
5952 
5953 	      p += len;
5954 	      break;
5955 	    }
5956 	}
5957 
5958       if (entry->option == NULL)
5959 	{
5960 	  warn (_("Unrecognized debug option '%s'\n"), p);
5961 	  p = strchr (p, ',');
5962 	  if (p == NULL)
5963 	    break;
5964 	}
5965 
5966       if (*p == ',')
5967 	p++;
5968     }
5969 }
5970 
5971 void
5972 dwarf_select_sections_by_letters (const char *letters)
5973 {
5974   unsigned int lindex = 0;
5975 
5976   while (letters[lindex])
5977     switch (letters[lindex++])
5978       {
5979       case 'i':
5980 	do_debug_info = 1;
5981 	break;
5982 
5983       case 'a':
5984 	do_debug_abbrevs = 1;
5985 	break;
5986 
5987       case 'l':
5988 	do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5989 	break;
5990 
5991       case 'L':
5992 	do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
5993 	break;
5994 
5995       case 'p':
5996 	do_debug_pubnames = 1;
5997 	break;
5998 
5999       case 't':
6000 	do_debug_pubtypes = 1;
6001 	break;
6002 
6003       case 'r':
6004 	do_debug_aranges = 1;
6005 	break;
6006 
6007       case 'R':
6008 	do_debug_ranges = 1;
6009 	break;
6010 
6011       case 'F':
6012 	do_debug_frames_interp = 1;
6013       case 'f':
6014 	do_debug_frames = 1;
6015 	break;
6016 
6017       case 'm':
6018 	do_debug_macinfo = 1;
6019 	break;
6020 
6021       case 's':
6022 	do_debug_str = 1;
6023 	break;
6024 
6025       case 'o':
6026 	do_debug_loc = 1;
6027 	break;
6028 
6029       default:
6030 	warn (_("Unrecognized debug option '%s'\n"), optarg);
6031 	break;
6032       }
6033 }
6034 
6035 void
6036 dwarf_select_sections_all (void)
6037 {
6038   do_debug_info = 1;
6039   do_debug_abbrevs = 1;
6040   do_debug_lines = FLAG_DEBUG_LINES_RAW;
6041   do_debug_pubnames = 1;
6042   do_debug_pubtypes = 1;
6043   do_debug_aranges = 1;
6044   do_debug_ranges = 1;
6045   do_debug_frames = 1;
6046   do_debug_macinfo = 1;
6047   do_debug_str = 1;
6048   do_debug_loc = 1;
6049   do_gdb_index = 1;
6050   do_trace_info = 1;
6051   do_trace_abbrevs = 1;
6052   do_trace_aranges = 1;
6053 }
6054 
6055 struct dwarf_section_display debug_displays[] =
6056 {
6057   { { ".debug_abbrev",	    ".zdebug_abbrev",	NULL, NULL, 0, 0, abbrev },
6058     display_debug_abbrev,   &do_debug_abbrevs,	0 },
6059   { { ".debug_aranges",	    ".zdebug_aranges",	NULL, NULL, 0, 0, abbrev },
6060     display_debug_aranges,  &do_debug_aranges,	1 },
6061   { { ".debug_frame",       ".zdebug_frame",	NULL, NULL, 0, 0, abbrev },
6062     display_debug_frames,   &do_debug_frames,	1 },
6063   { { ".debug_info",	    ".zdebug_info",	NULL, NULL, 0, 0, abbrev },
6064     display_debug_info,	    &do_debug_info,	1 },
6065   { { ".debug_line",	    ".zdebug_line",	NULL, NULL, 0, 0, abbrev },
6066     display_debug_lines,    &do_debug_lines,	1 },
6067   { { ".debug_pubnames",    ".zdebug_pubnames",	NULL, NULL, 0, 0, abbrev },
6068     display_debug_pubnames, &do_debug_pubnames,	0 },
6069   { { ".eh_frame",	    "",			NULL, NULL, 0, 0, abbrev },
6070     display_debug_frames,   &do_debug_frames,	1 },
6071   { { ".debug_macinfo",	    ".zdebug_macinfo",	NULL, NULL, 0, 0, abbrev },
6072     display_debug_macinfo,  &do_debug_macinfo,	0 },
6073   { { ".debug_macro",	    ".zdebug_macro",	NULL, NULL, 0, 0, abbrev },
6074     display_debug_macro,    &do_debug_macinfo,	1 },
6075   { { ".debug_str",	    ".zdebug_str",	NULL, NULL, 0, 0, abbrev },
6076     display_debug_str,	    &do_debug_str,	0 },
6077   { { ".debug_loc",	    ".zdebug_loc",	NULL, NULL, 0, 0, abbrev },
6078     display_debug_loc,	    &do_debug_loc,	1 },
6079   { { ".debug_pubtypes",    ".zdebug_pubtypes",	NULL, NULL, 0, 0, abbrev },
6080     display_debug_pubnames, &do_debug_pubtypes,	0 },
6081   { { ".debug_ranges",	    ".zdebug_ranges",	NULL, NULL, 0, 0, abbrev },
6082     display_debug_ranges,   &do_debug_ranges,	1 },
6083   { { ".debug_static_func", ".zdebug_static_func", NULL, NULL, 0, 0, abbrev },
6084     display_debug_not_supported, NULL,		0 },
6085   { { ".debug_static_vars", ".zdebug_static_vars", NULL, NULL, 0, 0, abbrev },
6086     display_debug_not_supported, NULL,		0 },
6087   { { ".debug_types",	    ".zdebug_types",	NULL, NULL, 0, 0, abbrev },
6088     display_debug_types,    &do_debug_info,	1 },
6089   { { ".debug_weaknames",   ".zdebug_weaknames", NULL, NULL, 0, 0, abbrev },
6090     display_debug_not_supported, NULL,		0 },
6091   { { ".gdb_index",	    "",	                NULL, NULL, 0, 0, abbrev },
6092     display_gdb_index,			&do_gdb_index,		0 },
6093   { { ".trace_info",	    "",			NULL, NULL, 0, 0, trace_abbrev },
6094     display_trace_info,			&do_trace_info,		1 },
6095   { { ".trace_abbrev",	    "",			NULL, NULL, 0, 0, abbrev },
6096     display_debug_abbrev,		&do_trace_abbrevs,	0 },
6097   { { ".trace_aranges",	    "",			NULL, NULL, 0, 0, abbrev },
6098     display_debug_aranges,		&do_trace_aranges,	0 },
6099   { { ".debug_info.dwo",    ".zdebug_info.dwo",	NULL, NULL, 0, 0, abbrev_dwo },
6100     display_debug_info,			&do_debug_info,		1 },
6101   { { ".debug_abbrev.dwo",  ".zdebug_abbrev.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6102     display_debug_abbrev,		&do_debug_abbrevs,	0 },
6103   { { ".debug_types.dwo",   ".zdebug_types.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6104     display_debug_types,		&do_debug_info,		1 },
6105   { { ".debug_line.dwo",   ".zdebug_line.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6106     display_debug_lines,   &do_debug_lines,	1 },
6107   { { ".debug_loc.dwo",	    ".zdebug_loc.dwo",	NULL, NULL, 0, 0, abbrev_dwo },
6108     display_debug_loc,	    &do_debug_loc,	1 },
6109   { { ".debug_macro.dwo",   ".zdebug_macro.dwo",NULL, NULL, 0, 0, abbrev },
6110     display_debug_macro,    &do_debug_macinfo,	1 },
6111   { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo",NULL, NULL, 0, 0, abbrev },
6112     display_debug_macinfo,  &do_debug_macinfo,	0 },
6113   { { ".debug_str.dwo",   ".zdebug_str.dwo", NULL, NULL, 0, 0, str_dwo },
6114     display_debug_str,     &do_debug_str,	        1 },
6115   { { ".debug_str_offsets",".zdebug_str_offsets", NULL, NULL, 0, 0, abbrev },
6116     display_debug_str_offsets, NULL,		0 },
6117   { { ".debug_str_offsets.dwo",".zdebug_str_offsets.dwo", NULL, NULL, 0, 0,
6118       abbrev },
6119     display_debug_str_offsets, NULL,		0 },
6120   { { ".debug_addr",".zdebug_addr",             NULL, NULL, 0, 0, debug_addr },
6121     display_debug_addr, NULL,		1 },
6122 };
6123