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