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