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