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