Merge branch 'vendor/TNFTP'
[dragonfly.git] / contrib / binutils-2.22 / 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 ((dwarf_version < 4
1489                && (form == DW_FORM_data4 || 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 ((dwarf_version < 4
1520                && (form == DW_FORM_data4 || 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 || attribute == 0)
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 ((dwarf_version < 4
1738            && (form == DW_FORM_data4 || 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     case DW_AT_GNU_macros:                      return "DW_AT_GNU_macros";
1964
1965       /* UPC extension.  */
1966     case DW_AT_upc_threads_scaled:      return "DW_AT_upc_threads_scaled";
1967
1968     /* PGI (STMicroelectronics) extensions.  */
1969     case DW_AT_PGI_lbase:               return "DW_AT_PGI_lbase";
1970     case DW_AT_PGI_soffset:             return "DW_AT_PGI_soffset";
1971     case DW_AT_PGI_lstride:             return "DW_AT_PGI_lstride";
1972
1973     default:
1974       {
1975         static char buffer[100];
1976
1977         snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1978                   attribute);
1979         return buffer;
1980       }
1981     }
1982 }
1983
1984 static unsigned char *
1985 read_and_display_attr (unsigned long attribute,
1986                        unsigned long form,
1987                        unsigned char * data,
1988                        dwarf_vma cu_offset,
1989                        dwarf_vma pointer_size,
1990                        dwarf_vma offset_size,
1991                        int dwarf_version,
1992                        debug_info * debug_info_p,
1993                        int do_loc,
1994                        struct dwarf_section * section)
1995 {
1996   if (!do_loc)
1997     printf ("   %-18s:", get_AT_name (attribute));
1998   data = read_and_display_attr_value (attribute, form, data, cu_offset,
1999                                       pointer_size, offset_size,
2000                                       dwarf_version, debug_info_p,
2001                                       do_loc, section);
2002   if (!do_loc)
2003     printf ("\n");
2004   return data;
2005 }
2006
2007
2008 /* Process the contents of a .debug_info section.  If do_loc is non-zero
2009    then we are scanning for location lists and we do not want to display
2010    anything to the user.  If do_types is non-zero, we are processing
2011    a .debug_types section instead of a .debug_info section.  */
2012
2013 static int
2014 process_debug_info (struct dwarf_section *section,
2015                     void *file,
2016                     enum dwarf_section_display_enum abbrev_sec,
2017                     int do_loc,
2018                     int do_types)
2019 {
2020   unsigned char *start = section->start;
2021   unsigned char *end = start + section->size;
2022   unsigned char *section_begin;
2023   unsigned int unit;
2024   unsigned int num_units = 0;
2025
2026   if ((do_loc || do_debug_loc || do_debug_ranges)
2027       && num_debug_info_entries == 0
2028       && ! do_types)
2029     {
2030       dwarf_vma length;
2031
2032       /* First scan the section to get the number of comp units.  */
2033       for (section_begin = start, num_units = 0; section_begin < end;
2034            num_units ++)
2035         {
2036           /* Read the first 4 bytes.  For a 32-bit DWARF section, this
2037              will be the length.  For a 64-bit DWARF section, it'll be
2038              the escape code 0xffffffff followed by an 8 byte length.  */
2039           length = byte_get (section_begin, 4);
2040
2041           if (length == 0xffffffff)
2042             {
2043               length = byte_get (section_begin + 4, 8);
2044               section_begin += length + 12;
2045             }
2046           else if (length >= 0xfffffff0 && length < 0xffffffff)
2047             {
2048               warn (_("Reserved length value (0x%s) found in section %s\n"),
2049                     dwarf_vmatoa ("x", length), section->name);
2050               return 0;
2051             }
2052           else
2053             section_begin += length + 4;
2054
2055           /* Negative values are illegal, they may even cause infinite
2056              looping.  This can happen if we can't accurately apply
2057              relocations to an object file.  */
2058           if ((signed long) length <= 0)
2059             {
2060               warn (_("Corrupt unit length (0x%s) found in section %s\n"),
2061                     dwarf_vmatoa ("x", length), section->name);
2062               return 0;
2063             }
2064         }
2065
2066       if (num_units == 0)
2067         {
2068           error (_("No comp units in %s section ?"), section->name);
2069           return 0;
2070         }
2071
2072       /* Then allocate an array to hold the information.  */
2073       debug_information = (debug_info *) cmalloc (num_units,
2074                                                   sizeof (* debug_information));
2075       if (debug_information == NULL)
2076         {
2077           error (_("Not enough memory for a debug info array of %u entries"),
2078                  num_units);
2079           return 0;
2080         }
2081     }
2082
2083   if (!do_loc)
2084     {
2085       if (dwarf_start_die == 0)
2086         printf (_("Contents of the %s section:\n\n"), section->name);
2087
2088       load_debug_section (str, file);
2089     }
2090
2091   load_debug_section (abbrev_sec, file);
2092   if (debug_displays [abbrev_sec].section.start == NULL)
2093     {
2094       warn (_("Unable to locate %s section!\n"),
2095             debug_displays [abbrev_sec].section.name);
2096       return 0;
2097     }
2098
2099   for (section_begin = start, unit = 0; start < end; unit++)
2100     {
2101       DWARF2_Internal_CompUnit compunit;
2102       unsigned char *hdrptr;
2103       unsigned char *tags;
2104       int level, last_level, saved_level;
2105       dwarf_vma cu_offset;
2106       int offset_size;
2107       int initial_length_size;
2108       unsigned char signature[8] = { 0 };
2109       dwarf_vma type_offset = 0;
2110
2111       hdrptr = start;
2112
2113       compunit.cu_length = byte_get (hdrptr, 4);
2114       hdrptr += 4;
2115
2116       if (compunit.cu_length == 0xffffffff)
2117         {
2118           compunit.cu_length = byte_get (hdrptr, 8);
2119           hdrptr += 8;
2120           offset_size = 8;
2121           initial_length_size = 12;
2122         }
2123       else
2124         {
2125           offset_size = 4;
2126           initial_length_size = 4;
2127         }
2128
2129       compunit.cu_version = byte_get (hdrptr, 2);
2130       hdrptr += 2;
2131
2132       cu_offset = start - section_begin;
2133
2134       compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
2135       hdrptr += offset_size;
2136
2137       compunit.cu_pointer_size = byte_get (hdrptr, 1);
2138       hdrptr += 1;
2139
2140       if (do_types)
2141         {
2142           int i;
2143
2144           for (i = 0; i < 8; i++)
2145             {
2146               signature[i] = byte_get (hdrptr, 1);
2147               hdrptr += 1;
2148             }
2149
2150           type_offset = byte_get (hdrptr, offset_size);
2151           hdrptr += offset_size;
2152         }
2153
2154       if ((do_loc || do_debug_loc || do_debug_ranges)
2155           && num_debug_info_entries == 0
2156           && ! do_types)
2157         {
2158           debug_information [unit].cu_offset = cu_offset;
2159           debug_information [unit].pointer_size
2160             = compunit.cu_pointer_size;
2161           debug_information [unit].offset_size = offset_size;
2162           debug_information [unit].dwarf_version = compunit.cu_version;
2163           debug_information [unit].base_address = 0;
2164           debug_information [unit].loc_offsets = NULL;
2165           debug_information [unit].have_frame_base = NULL;
2166           debug_information [unit].max_loc_offsets = 0;
2167           debug_information [unit].num_loc_offsets = 0;
2168           debug_information [unit].range_lists = NULL;
2169           debug_information [unit].max_range_lists= 0;
2170           debug_information [unit].num_range_lists = 0;
2171         }
2172
2173       if (!do_loc && dwarf_start_die == 0)
2174         {
2175           printf (_("  Compilation Unit @ offset 0x%s:\n"),
2176                   dwarf_vmatoa ("x", cu_offset));
2177           printf (_("   Length:        0x%s (%s)\n"),
2178                   dwarf_vmatoa ("x", compunit.cu_length),
2179                   offset_size == 8 ? "64-bit" : "32-bit");
2180           printf (_("   Version:       %d\n"), compunit.cu_version);
2181           printf (_("   Abbrev Offset: %s\n"),
2182                   dwarf_vmatoa ("d", compunit.cu_abbrev_offset));
2183           printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
2184           if (do_types)
2185             {
2186               int i;
2187               printf (_("   Signature:     "));
2188               for (i = 0; i < 8; i++)
2189                 printf ("%02x", signature[i]);
2190               printf ("\n");
2191              printf (_("   Type Offset:   0x%s\n"),
2192                      dwarf_vmatoa ("x", type_offset));
2193             }
2194         }
2195
2196       if (cu_offset + compunit.cu_length + initial_length_size
2197           > section->size)
2198         {
2199           warn (_("Debug info is corrupted, length of CU at %s"
2200                   " extends beyond end of section (length = %s)\n"),
2201                 dwarf_vmatoa ("x", cu_offset),
2202                 dwarf_vmatoa ("x", compunit.cu_length));
2203           break;
2204         }
2205       tags = hdrptr;
2206       start += compunit.cu_length + initial_length_size;
2207
2208       if (compunit.cu_version != 2
2209           && compunit.cu_version != 3
2210           && compunit.cu_version != 4)
2211         {
2212           warn (_("CU at offset %s contains corrupt or "
2213                   "unsupported version number: %d.\n"),
2214                 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
2215           continue;
2216         }
2217
2218       free_abbrevs ();
2219
2220       /* Process the abbrevs used by this compilation unit. DWARF
2221          sections under Mach-O have non-zero addresses.  */
2222       if (compunit.cu_abbrev_offset >= debug_displays [abbrev_sec].section.size)
2223         warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
2224               (unsigned long) compunit.cu_abbrev_offset,
2225               (unsigned long) debug_displays [abbrev_sec].section.size);
2226       else
2227         process_abbrev_section
2228           ((unsigned char *) debug_displays [abbrev_sec].section.start
2229            + compunit.cu_abbrev_offset,
2230            (unsigned char *) debug_displays [abbrev_sec].section.start
2231            + debug_displays [abbrev_sec].section.size);
2232
2233       level = 0;
2234       last_level = level;
2235       saved_level = -1;
2236       while (tags < start)
2237         {
2238           unsigned int bytes_read;
2239           unsigned long abbrev_number;
2240           unsigned long die_offset;
2241           abbrev_entry *entry;
2242           abbrev_attr *attr;
2243           int do_printing = 1;
2244
2245           die_offset = tags - section_begin;
2246
2247           abbrev_number = read_leb128 (tags, & bytes_read, 0);
2248           tags += bytes_read;
2249
2250           /* A null DIE marks the end of a list of siblings or it may also be
2251              a section padding.  */
2252           if (abbrev_number == 0)
2253             {
2254               /* Check if it can be a section padding for the last CU.  */
2255               if (level == 0 && start == end)
2256                 {
2257                   unsigned char *chk;
2258
2259                   for (chk = tags; chk < start; chk++)
2260                     if (*chk != 0)
2261                       break;
2262                   if (chk == start)
2263                     break;
2264                 }
2265
2266               --level;
2267               if (level < 0)
2268                 {
2269                   static unsigned num_bogus_warns = 0;
2270
2271                   if (num_bogus_warns < 3)
2272                     {
2273                       warn (_("Bogus end-of-siblings marker detected at offset %lx in .debug_info section\n"),
2274                             die_offset);
2275                       num_bogus_warns ++;
2276                       if (num_bogus_warns == 3)
2277                         warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
2278                     }
2279                 }
2280               if (dwarf_start_die != 0 && level < saved_level)
2281                 return 1;
2282               continue;
2283             }
2284
2285           if (!do_loc)
2286             {
2287               if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
2288                 do_printing = 0;
2289               else
2290                 {
2291                   if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
2292                     saved_level = level;
2293                   do_printing = (dwarf_cutoff_level == -1
2294                                  || level < dwarf_cutoff_level);
2295                   if (do_printing)
2296                     printf (_(" <%d><%lx>: Abbrev Number: %lu"),
2297                             level, die_offset, abbrev_number);
2298                   else if (dwarf_cutoff_level == -1
2299                            || last_level < dwarf_cutoff_level)
2300                     printf (_(" <%d><%lx>: ...\n"), level, die_offset);
2301                   last_level = level;
2302                 }
2303             }
2304
2305           /* Scan through the abbreviation list until we reach the
2306              correct entry.  */
2307           for (entry = first_abbrev;
2308                entry && entry->entry != abbrev_number;
2309                entry = entry->next)
2310             continue;
2311
2312           if (entry == NULL)
2313             {
2314               if (!do_loc && do_printing)
2315                 {
2316                   printf ("\n");
2317                   fflush (stdout);
2318                 }
2319               warn (_("DIE at offset %lx refers to abbreviation number %lu which does not exist\n"),
2320                     die_offset, abbrev_number);
2321               return 0;
2322             }
2323
2324           if (!do_loc && do_printing)
2325             printf (" (%s)\n", get_TAG_name (entry->tag));
2326
2327           switch (entry->tag)
2328             {
2329             default:
2330               need_base_address = 0;
2331               break;
2332             case DW_TAG_compile_unit:
2333               need_base_address = 1;
2334               break;
2335             case DW_TAG_entry_point:
2336             case DW_TAG_subprogram:
2337               need_base_address = 0;
2338               /* Assuming that there is no DW_AT_frame_base.  */
2339               have_frame_base = 0;
2340               break;
2341             }
2342
2343           for (attr = entry->first_attr; attr; attr = attr->next)
2344             {
2345               debug_info *arg;
2346
2347               if (! do_loc && do_printing)
2348                 /* Show the offset from where the tag was extracted.  */
2349                 printf ("    <%lx>", (unsigned long)(tags - section_begin));
2350
2351               arg = debug_information;
2352               if (debug_information)
2353                 arg += unit;
2354
2355               tags = read_and_display_attr (attr->attribute,
2356                                             attr->form,
2357                                             tags, cu_offset,
2358                                             compunit.cu_pointer_size,
2359                                             offset_size,
2360                                             compunit.cu_version,
2361                                             arg,
2362                                             do_loc || ! do_printing, section);
2363             }
2364
2365           if (entry->children)
2366             ++level;
2367         }
2368     }
2369
2370   /* Set num_debug_info_entries here so that it can be used to check if
2371      we need to process .debug_loc and .debug_ranges sections.  */
2372   if ((do_loc || do_debug_loc || do_debug_ranges)
2373       && num_debug_info_entries == 0
2374       && ! do_types)
2375     num_debug_info_entries = num_units;
2376
2377   if (!do_loc)
2378     printf ("\n");
2379
2380   return 1;
2381 }
2382
2383 /* Locate and scan the .debug_info section in the file and record the pointer
2384    sizes and offsets for the compilation units in it.  Usually an executable
2385    will have just one pointer size, but this is not guaranteed, and so we try
2386    not to make any assumptions.  Returns zero upon failure, or the number of
2387    compilation units upon success.  */
2388
2389 static unsigned int
2390 load_debug_info (void * file)
2391 {
2392   /* Reset the last pointer size so that we can issue correct error
2393      messages if we are displaying the contents of more than one section.  */
2394   last_pointer_size = 0;
2395   warned_about_missing_comp_units = FALSE;
2396
2397   /* If we have already tried and failed to load the .debug_info
2398      section then do not bother to repear the task.  */
2399   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2400     return 0;
2401
2402   /* If we already have the information there is nothing else to do.  */
2403   if (num_debug_info_entries > 0)
2404     return num_debug_info_entries;
2405
2406   if (load_debug_section (info, file)
2407       && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
2408     return num_debug_info_entries;
2409
2410   num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2411   return 0;
2412 }
2413
2414 static int
2415 display_debug_lines_raw (struct dwarf_section *section,
2416                          unsigned char *data,
2417                          unsigned char *end)
2418 {
2419   unsigned char *start = section->start;
2420
2421   printf (_("Raw dump of debug contents of section %s:\n\n"),
2422           section->name);
2423
2424   while (data < end)
2425     {
2426       DWARF2_Internal_LineInfo linfo;
2427       unsigned char *standard_opcodes;
2428       unsigned char *end_of_sequence;
2429       unsigned char *hdrptr;
2430       unsigned long hdroff;
2431       int initial_length_size;
2432       int offset_size;
2433       int i;
2434
2435       hdrptr = data;
2436       hdroff = hdrptr - start;
2437
2438       /* Check the length of the block.  */
2439       linfo.li_length = byte_get (hdrptr, 4);
2440       hdrptr += 4;
2441
2442       if (linfo.li_length == 0xffffffff)
2443         {
2444           /* This section is 64-bit DWARF 3.  */
2445           linfo.li_length = byte_get (hdrptr, 8);
2446           hdrptr += 8;
2447           offset_size = 8;
2448           initial_length_size = 12;
2449         }
2450       else
2451         {
2452           offset_size = 4;
2453           initial_length_size = 4;
2454         }
2455
2456       if (linfo.li_length + initial_length_size > section->size)
2457         {
2458           warn
2459             (_("The information in section %s appears to be corrupt - the section is too small\n"),
2460              section->name);
2461           return 0;
2462         }
2463
2464       /* Check its version number.  */
2465       linfo.li_version = byte_get (hdrptr, 2);
2466       hdrptr += 2;
2467       if (linfo.li_version != 2
2468           && linfo.li_version != 3
2469           && linfo.li_version != 4)
2470         {
2471           warn (_("Only DWARF version 2, 3 and 4 line info is currently supported.\n"));
2472           return 0;
2473         }
2474
2475       linfo.li_prologue_length = byte_get (hdrptr, offset_size);
2476       hdrptr += offset_size;
2477       linfo.li_min_insn_length = byte_get (hdrptr, 1);
2478       hdrptr++;
2479       if (linfo.li_version >= 4)
2480         {
2481           linfo.li_max_ops_per_insn = byte_get (hdrptr, 1);
2482           hdrptr++;
2483           if (linfo.li_max_ops_per_insn == 0)
2484             {
2485               warn (_("Invalid maximum operations per insn.\n"));
2486               return 0;
2487             }
2488         }
2489       else
2490         linfo.li_max_ops_per_insn = 1;
2491       linfo.li_default_is_stmt = byte_get (hdrptr, 1);
2492       hdrptr++;
2493       linfo.li_line_base = byte_get (hdrptr, 1);
2494       hdrptr++;
2495       linfo.li_line_range = byte_get (hdrptr, 1);
2496       hdrptr++;
2497       linfo.li_opcode_base = byte_get (hdrptr, 1);
2498       hdrptr++;
2499
2500       /* Sign extend the line base field.  */
2501       linfo.li_line_base <<= 24;
2502       linfo.li_line_base >>= 24;
2503
2504       printf (_("  Offset:                      0x%lx\n"), hdroff);
2505       printf (_("  Length:                      %ld\n"), (long) linfo.li_length);
2506       printf (_("  DWARF Version:               %d\n"), linfo.li_version);
2507       printf (_("  Prologue Length:             %d\n"), linfo.li_prologue_length);
2508       printf (_("  Minimum Instruction Length:  %d\n"), linfo.li_min_insn_length);
2509       if (linfo.li_version >= 4)
2510         printf (_("  Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
2511       printf (_("  Initial value of 'is_stmt':  %d\n"), linfo.li_default_is_stmt);
2512       printf (_("  Line Base:                   %d\n"), linfo.li_line_base);
2513       printf (_("  Line Range:                  %d\n"), linfo.li_line_range);
2514       printf (_("  Opcode Base:                 %d\n"), linfo.li_opcode_base);
2515
2516       end_of_sequence = data + linfo.li_length + initial_length_size;
2517
2518       reset_state_machine (linfo.li_default_is_stmt);
2519
2520       /* Display the contents of the Opcodes table.  */
2521       standard_opcodes = hdrptr;
2522
2523       printf (_("\n Opcodes:\n"));
2524
2525       for (i = 1; i < linfo.li_opcode_base; i++)
2526         printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
2527
2528       /* Display the contents of the Directory table.  */
2529       data = standard_opcodes + linfo.li_opcode_base - 1;
2530
2531       if (*data == 0)
2532         printf (_("\n The Directory Table is empty.\n"));
2533       else
2534         {
2535           printf (_("\n The Directory Table:\n"));
2536
2537           while (*data != 0)
2538             {
2539               printf ("  %s\n", data);
2540
2541               data += strlen ((char *) data) + 1;
2542             }
2543         }
2544
2545       /* Skip the NUL at the end of the table.  */
2546       data++;
2547
2548       /* Display the contents of the File Name table.  */
2549       if (*data == 0)
2550         printf (_("\n The File Name Table is empty.\n"));
2551       else
2552         {
2553           printf (_("\n The File Name Table:\n"));
2554           printf (_("  Entry\tDir\tTime\tSize\tName\n"));
2555
2556           while (*data != 0)
2557             {
2558               unsigned char *name;
2559               unsigned int bytes_read;
2560
2561               printf ("  %d\t", ++state_machine_regs.last_file_entry);
2562               name = data;
2563
2564               data += strlen ((char *) data) + 1;
2565
2566               printf ("%s\t",
2567                       dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2568               data += bytes_read;
2569               printf ("%s\t",
2570                       dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2571               data += bytes_read;
2572               printf ("%s\t",
2573                       dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2574               data += bytes_read;
2575               printf ("%s\n", name);
2576             }
2577         }
2578
2579       /* Skip the NUL at the end of the table.  */
2580       data++;
2581
2582       /* Now display the statements.  */
2583       printf (_("\n Line Number Statements:\n"));
2584
2585       while (data < end_of_sequence)
2586         {
2587           unsigned char op_code;
2588           dwarf_signed_vma adv;
2589           dwarf_vma uladv;
2590           unsigned int bytes_read;
2591
2592           op_code = *data++;
2593
2594           if (op_code >= linfo.li_opcode_base)
2595             {
2596               op_code -= linfo.li_opcode_base;
2597               uladv = (op_code / linfo.li_line_range);
2598               if (linfo.li_max_ops_per_insn == 1)
2599                 {
2600                   uladv *= linfo.li_min_insn_length;
2601                   state_machine_regs.address += uladv;
2602                   printf (_("  Special opcode %d: "
2603                             "advance Address by %s to 0x%s"),
2604                           op_code, dwarf_vmatoa ("u", uladv),
2605                           dwarf_vmatoa ("x", state_machine_regs.address));
2606                 }
2607               else
2608                 {
2609                   state_machine_regs.address
2610                     += ((state_machine_regs.op_index + uladv)
2611                         / linfo.li_max_ops_per_insn)
2612                        * linfo.li_min_insn_length;
2613                   state_machine_regs.op_index
2614                     = (state_machine_regs.op_index + uladv)
2615                       % linfo.li_max_ops_per_insn;
2616                   printf (_("  Special opcode %d: "
2617                             "advance Address by %s to 0x%s[%d]"),
2618                           op_code, dwarf_vmatoa ("u", uladv),
2619                           dwarf_vmatoa ("x", state_machine_regs.address),
2620                           state_machine_regs.op_index);
2621                 }
2622               adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
2623               state_machine_regs.line += adv;
2624               printf (_(" and Line by %s to %d\n"),
2625                       dwarf_vmatoa ("d", adv), state_machine_regs.line);
2626             }
2627           else switch (op_code)
2628             {
2629             case DW_LNS_extended_op:
2630               data += process_extended_line_op (data, linfo.li_default_is_stmt);
2631               break;
2632
2633             case DW_LNS_copy:
2634               printf (_("  Copy\n"));
2635               break;
2636
2637             case DW_LNS_advance_pc:
2638               uladv = read_leb128 (data, & bytes_read, 0);
2639               data += bytes_read;
2640               if (linfo.li_max_ops_per_insn == 1)
2641                 {
2642                   uladv *= linfo.li_min_insn_length;
2643                   state_machine_regs.address += uladv;
2644                   printf (_("  Advance PC by %s to 0x%s\n"),
2645                           dwarf_vmatoa ("u", uladv),
2646                           dwarf_vmatoa ("x", state_machine_regs.address));
2647                 }
2648               else
2649                 {
2650                   state_machine_regs.address
2651                     += ((state_machine_regs.op_index + uladv)
2652                         / linfo.li_max_ops_per_insn)
2653                        * linfo.li_min_insn_length;
2654                   state_machine_regs.op_index
2655                     = (state_machine_regs.op_index + uladv)
2656                       % linfo.li_max_ops_per_insn;
2657                   printf (_("  Advance PC by %s to 0x%s[%d]\n"),
2658                           dwarf_vmatoa ("u", uladv),
2659                           dwarf_vmatoa ("x", state_machine_regs.address),
2660                           state_machine_regs.op_index);
2661                 }
2662               break;
2663
2664             case DW_LNS_advance_line:
2665               adv = read_sleb128 (data, & bytes_read);
2666               data += bytes_read;
2667               state_machine_regs.line += adv;
2668               printf (_("  Advance Line by %s to %d\n"),
2669                         dwarf_vmatoa ("d", adv),
2670                         state_machine_regs.line);
2671               break;
2672
2673             case DW_LNS_set_file:
2674               adv = read_leb128 (data, & bytes_read, 0);
2675               data += bytes_read;
2676               printf (_("  Set File Name to entry %s in the File Name Table\n"),
2677                       dwarf_vmatoa ("d", adv));
2678               state_machine_regs.file = adv;
2679               break;
2680
2681             case DW_LNS_set_column:
2682               uladv = read_leb128 (data, & bytes_read, 0);
2683               data += bytes_read;
2684               printf (_("  Set column to %s\n"),
2685                       dwarf_vmatoa ("u", uladv));
2686               state_machine_regs.column = uladv;
2687               break;
2688
2689             case DW_LNS_negate_stmt:
2690               adv = state_machine_regs.is_stmt;
2691               adv = ! adv;
2692               printf (_("  Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
2693               state_machine_regs.is_stmt = adv;
2694               break;
2695
2696             case DW_LNS_set_basic_block:
2697               printf (_("  Set basic block\n"));
2698               state_machine_regs.basic_block = 1;
2699               break;
2700
2701             case DW_LNS_const_add_pc:
2702               uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
2703               if (linfo.li_max_ops_per_insn)
2704                 {
2705                   uladv *= linfo.li_min_insn_length;
2706                   state_machine_regs.address += uladv;
2707                   printf (_("  Advance PC by constant %s to 0x%s\n"),
2708                           dwarf_vmatoa ("u", uladv),
2709                           dwarf_vmatoa ("x", state_machine_regs.address));
2710                 }
2711               else
2712                 {
2713                   state_machine_regs.address
2714                     += ((state_machine_regs.op_index + uladv)
2715                         / linfo.li_max_ops_per_insn)
2716                        * linfo.li_min_insn_length;
2717                   state_machine_regs.op_index
2718                     = (state_machine_regs.op_index + uladv)
2719                       % linfo.li_max_ops_per_insn;
2720                   printf (_("  Advance PC by constant %s to 0x%s[%d]\n"),
2721                           dwarf_vmatoa ("u", uladv),
2722                           dwarf_vmatoa ("x", state_machine_regs.address),
2723                           state_machine_regs.op_index);
2724                 }
2725               break;
2726
2727             case DW_LNS_fixed_advance_pc:
2728               uladv = byte_get (data, 2);
2729               data += 2;
2730               state_machine_regs.address += uladv;
2731               state_machine_regs.op_index = 0;
2732               printf (_("  Advance PC by fixed size amount %s to 0x%s\n"),
2733                       dwarf_vmatoa ("u", uladv),
2734                       dwarf_vmatoa ("x", state_machine_regs.address));
2735               break;
2736
2737             case DW_LNS_set_prologue_end:
2738               printf (_("  Set prologue_end to true\n"));
2739               break;
2740
2741             case DW_LNS_set_epilogue_begin:
2742               printf (_("  Set epilogue_begin to true\n"));
2743               break;
2744
2745             case DW_LNS_set_isa:
2746               uladv = read_leb128 (data, & bytes_read, 0);
2747               data += bytes_read;
2748               printf (_("  Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
2749               break;
2750
2751             default:
2752               printf (_("  Unknown opcode %d with operands: "), op_code);
2753
2754               for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2755                 {
2756                   printf ("0x%s%s", dwarf_vmatoa ("x", read_leb128 (data,
2757                                                          &bytes_read, 0)),
2758                           i == 1 ? "" : ", ");
2759                   data += bytes_read;
2760                 }
2761               putchar ('\n');
2762               break;
2763             }
2764         }
2765       putchar ('\n');
2766     }
2767
2768   return 1;
2769 }
2770
2771 typedef struct
2772 {
2773   unsigned char *name;
2774   unsigned int directory_index;
2775   unsigned int modification_date;
2776   unsigned int length;
2777 } File_Entry;
2778
2779 /* Output a decoded representation of the .debug_line section.  */
2780
2781 static int
2782 display_debug_lines_decoded (struct dwarf_section *section,
2783                              unsigned char *data,
2784                              unsigned char *end)
2785 {
2786   printf (_("Decoded dump of debug contents of section %s:\n\n"),
2787           section->name);
2788
2789   while (data < end)
2790     {
2791       /* This loop amounts to one iteration per compilation unit.  */
2792       DWARF2_Internal_LineInfo linfo;
2793       unsigned char *standard_opcodes;
2794       unsigned char *end_of_sequence;
2795       unsigned char *hdrptr;
2796       int initial_length_size;
2797       int offset_size;
2798       int i;
2799       File_Entry *file_table = NULL;
2800       unsigned char **directory_table = NULL;
2801
2802       hdrptr = data;
2803
2804       /* Extract information from the Line Number Program Header.
2805         (section 6.2.4 in the Dwarf3 doc).  */
2806
2807       /* Get the length of this CU's line number information block.  */
2808       linfo.li_length = byte_get (hdrptr, 4);
2809       hdrptr += 4;
2810
2811       if (linfo.li_length == 0xffffffff)
2812         {
2813           /* This section is 64-bit DWARF 3.  */
2814           linfo.li_length = byte_get (hdrptr, 8);
2815           hdrptr += 8;
2816           offset_size = 8;
2817           initial_length_size = 12;
2818         }
2819       else
2820         {
2821           offset_size = 4;
2822           initial_length_size = 4;
2823         }
2824
2825       if (linfo.li_length + initial_length_size > section->size)
2826         {
2827           warn (_("The line info appears to be corrupt - "
2828                   "the section is too small\n"));
2829           return 0;
2830         }
2831
2832       /* Get this CU's Line Number Block version number.  */
2833       linfo.li_version = byte_get (hdrptr, 2);
2834       hdrptr += 2;
2835       if (linfo.li_version != 2
2836           && linfo.li_version != 3
2837           && linfo.li_version != 4)
2838         {
2839           warn (_("Only DWARF version 2, 3 and 4 line info is currently "
2840                 "supported.\n"));
2841           return 0;
2842         }
2843
2844       linfo.li_prologue_length = byte_get (hdrptr, offset_size);
2845       hdrptr += offset_size;
2846       linfo.li_min_insn_length = byte_get (hdrptr, 1);
2847       hdrptr++;
2848       if (linfo.li_version >= 4)
2849         {
2850           linfo.li_max_ops_per_insn = byte_get (hdrptr, 1);
2851           hdrptr++;
2852           if (linfo.li_max_ops_per_insn == 0)
2853             {
2854               warn (_("Invalid maximum operations per insn.\n"));
2855               return 0;
2856             }
2857         }
2858       else
2859         linfo.li_max_ops_per_insn = 1;
2860       linfo.li_default_is_stmt = byte_get (hdrptr, 1);
2861       hdrptr++;
2862       linfo.li_line_base = byte_get (hdrptr, 1);
2863       hdrptr++;
2864       linfo.li_line_range = byte_get (hdrptr, 1);
2865       hdrptr++;
2866       linfo.li_opcode_base = byte_get (hdrptr, 1);
2867       hdrptr++;
2868
2869       /* Sign extend the line base field.  */
2870       linfo.li_line_base <<= 24;
2871       linfo.li_line_base >>= 24;
2872
2873       /* Find the end of this CU's Line Number Information Block.  */
2874       end_of_sequence = data + linfo.li_length + initial_length_size;
2875
2876       reset_state_machine (linfo.li_default_is_stmt);
2877
2878       /* Save a pointer to the contents of the Opcodes table.  */
2879       standard_opcodes = hdrptr;
2880
2881       /* Traverse the Directory table just to count entries.  */
2882       data = standard_opcodes + linfo.li_opcode_base - 1;
2883       if (*data != 0)
2884         {
2885           unsigned int n_directories = 0;
2886           unsigned char *ptr_directory_table = data;
2887
2888           while (*data != 0)
2889             {
2890               data += strlen ((char *) data) + 1;
2891               n_directories++;
2892             }
2893
2894           /* Go through the directory table again to save the directories.  */
2895           directory_table = (unsigned char **)
2896               xmalloc (n_directories * sizeof (unsigned char *));
2897
2898           i = 0;
2899           while (*ptr_directory_table != 0)
2900             {
2901               directory_table[i] = ptr_directory_table;
2902               ptr_directory_table += strlen ((char *) ptr_directory_table) + 1;
2903               i++;
2904             }
2905         }
2906       /* Skip the NUL at the end of the table.  */
2907       data++;
2908
2909       /* Traverse the File Name table just to count the entries.  */
2910       if (*data != 0)
2911         {
2912           unsigned int n_files = 0;
2913           unsigned char *ptr_file_name_table = data;
2914
2915           while (*data != 0)
2916             {
2917               unsigned int bytes_read;
2918
2919               /* Skip Name, directory index, last modification time and length
2920                  of file.  */
2921               data += strlen ((char *) data) + 1;
2922               read_leb128 (data, & bytes_read, 0);
2923               data += bytes_read;
2924               read_leb128 (data, & bytes_read, 0);
2925               data += bytes_read;
2926               read_leb128 (data, & bytes_read, 0);
2927               data += bytes_read;
2928
2929               n_files++;
2930             }
2931
2932           /* Go through the file table again to save the strings.  */
2933           file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
2934
2935           i = 0;
2936           while (*ptr_file_name_table != 0)
2937             {
2938               unsigned int bytes_read;
2939
2940               file_table[i].name = ptr_file_name_table;
2941               ptr_file_name_table += strlen ((char *) ptr_file_name_table) + 1;
2942
2943               /* We are not interested in directory, time or size.  */
2944               file_table[i].directory_index = read_leb128 (ptr_file_name_table,
2945                                                            & bytes_read, 0);
2946               ptr_file_name_table += bytes_read;
2947               file_table[i].modification_date = read_leb128 (ptr_file_name_table,
2948                                                              & bytes_read, 0);
2949               ptr_file_name_table += bytes_read;
2950               file_table[i].length = read_leb128 (ptr_file_name_table, & bytes_read, 0);
2951               ptr_file_name_table += bytes_read;
2952               i++;
2953             }
2954           i = 0;
2955
2956           /* Print the Compilation Unit's name and a header.  */
2957           if (directory_table == NULL)
2958             {
2959               printf (_("CU: %s:\n"), file_table[0].name);
2960               printf (_("File name                            Line number    Starting address\n"));
2961             }
2962           else
2963             {
2964               if (do_wide || strlen ((char *) directory_table[0]) < 76)
2965                 printf (_("CU: %s/%s:\n"), directory_table[0],
2966                         file_table[0].name);
2967               else
2968                 printf ("%s:\n", file_table[0].name);
2969
2970               printf (_("File name                            Line number    Starting address\n"));
2971             }
2972         }
2973
2974       /* Skip the NUL at the end of the table.  */
2975       data++;
2976
2977       /* This loop iterates through the Dwarf Line Number Program.  */
2978       while (data < end_of_sequence)
2979         {
2980           unsigned char op_code;
2981           int adv;
2982           unsigned long int uladv;
2983           unsigned int bytes_read;
2984           int is_special_opcode = 0;
2985
2986           op_code = *data++;
2987
2988           if (op_code >= linfo.li_opcode_base)
2989             {
2990               op_code -= linfo.li_opcode_base;
2991               uladv = (op_code / linfo.li_line_range);
2992               if (linfo.li_max_ops_per_insn == 1)
2993                 {
2994                   uladv *= linfo.li_min_insn_length;
2995                   state_machine_regs.address += uladv;
2996                 }
2997               else
2998                 {
2999                   state_machine_regs.address
3000                     += ((state_machine_regs.op_index + uladv)
3001                         / linfo.li_max_ops_per_insn)
3002                        * linfo.li_min_insn_length;
3003                   state_machine_regs.op_index
3004                     = (state_machine_regs.op_index + uladv)
3005                       % linfo.li_max_ops_per_insn;
3006                 }
3007
3008               adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
3009               state_machine_regs.line += adv;
3010               is_special_opcode = 1;
3011             }
3012           else switch (op_code)
3013             {
3014             case DW_LNS_extended_op:
3015               {
3016                 unsigned int ext_op_code_len;
3017                 unsigned char ext_op_code;
3018                 unsigned char *op_code_data = data;
3019
3020                 ext_op_code_len = read_leb128 (op_code_data, &bytes_read, 0);
3021                 op_code_data += bytes_read;
3022
3023                 if (ext_op_code_len == 0)
3024                   {
3025                     warn (_("badly formed extended line op encountered!\n"));
3026                     break;
3027                   }
3028                 ext_op_code_len += bytes_read;
3029                 ext_op_code = *op_code_data++;
3030
3031                 switch (ext_op_code)
3032                   {
3033                   case DW_LNE_end_sequence:
3034                     reset_state_machine (linfo.li_default_is_stmt);
3035                     break;
3036                   case DW_LNE_set_address:
3037                     state_machine_regs.address =
3038                     byte_get (op_code_data, ext_op_code_len - bytes_read - 1);
3039                     state_machine_regs.op_index = 0;
3040                     break;
3041                   case DW_LNE_define_file:
3042                     {
3043                       unsigned int dir_index = 0;
3044
3045                       ++state_machine_regs.last_file_entry;
3046                       op_code_data += strlen ((char *) op_code_data) + 1;
3047                       dir_index = read_leb128 (op_code_data, & bytes_read, 0);
3048                       op_code_data += bytes_read;
3049                       read_leb128 (op_code_data, & bytes_read, 0);
3050                       op_code_data += bytes_read;
3051                       read_leb128 (op_code_data, & bytes_read, 0);
3052
3053                       printf ("%s:\n", directory_table[dir_index]);
3054                       break;
3055                     }
3056                   default:
3057                     printf (_("UNKNOWN: length %d\n"), ext_op_code_len - bytes_read);
3058                     break;
3059                   }
3060                 data += ext_op_code_len;
3061                 break;
3062               }
3063             case DW_LNS_copy:
3064               break;
3065
3066             case DW_LNS_advance_pc:
3067               uladv = read_leb128 (data, & bytes_read, 0);
3068               data += bytes_read;
3069               if (linfo.li_max_ops_per_insn == 1)
3070                 {
3071                   uladv *= linfo.li_min_insn_length;
3072                   state_machine_regs.address += uladv;
3073                 }
3074               else
3075                 {
3076                   state_machine_regs.address
3077                     += ((state_machine_regs.op_index + uladv)
3078                         / linfo.li_max_ops_per_insn)
3079                        * linfo.li_min_insn_length;
3080                   state_machine_regs.op_index
3081                     = (state_machine_regs.op_index + uladv)
3082                       % linfo.li_max_ops_per_insn;
3083                 }
3084               break;
3085
3086             case DW_LNS_advance_line:
3087               adv = read_sleb128 (data, & bytes_read);
3088               data += bytes_read;
3089               state_machine_regs.line += adv;
3090               break;
3091
3092             case DW_LNS_set_file:
3093               adv = read_leb128 (data, & bytes_read, 0);
3094               data += bytes_read;
3095               state_machine_regs.file = adv;
3096               if (file_table[state_machine_regs.file - 1].directory_index == 0)
3097                 {
3098                   /* If directory index is 0, that means current directory.  */
3099                   printf ("\n./%s:[++]\n",
3100                           file_table[state_machine_regs.file - 1].name);
3101                 }
3102               else
3103                 {
3104                   /* The directory index starts counting at 1.  */
3105                   printf ("\n%s/%s:\n",
3106                           directory_table[file_table[state_machine_regs.file - 1].directory_index - 1],
3107                           file_table[state_machine_regs.file - 1].name);
3108                 }
3109               break;
3110
3111             case DW_LNS_set_column:
3112               uladv = read_leb128 (data, & bytes_read, 0);
3113               data += bytes_read;
3114               state_machine_regs.column = uladv;
3115               break;
3116
3117             case DW_LNS_negate_stmt:
3118               adv = state_machine_regs.is_stmt;
3119               adv = ! adv;
3120               state_machine_regs.is_stmt = adv;
3121               break;
3122
3123             case DW_LNS_set_basic_block:
3124               state_machine_regs.basic_block = 1;
3125               break;
3126
3127             case DW_LNS_const_add_pc:
3128               uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
3129               if (linfo.li_max_ops_per_insn == 1)
3130                 {
3131                   uladv *= linfo.li_min_insn_length;
3132                   state_machine_regs.address += uladv;
3133                 }
3134               else
3135                 {
3136                   state_machine_regs.address
3137                     += ((state_machine_regs.op_index + uladv)
3138                         / linfo.li_max_ops_per_insn)
3139                        * linfo.li_min_insn_length;
3140                   state_machine_regs.op_index
3141                     = (state_machine_regs.op_index + uladv)
3142                       % linfo.li_max_ops_per_insn;
3143                 }
3144               break;
3145
3146             case DW_LNS_fixed_advance_pc:
3147               uladv = byte_get (data, 2);
3148               data += 2;
3149               state_machine_regs.address += uladv;
3150               state_machine_regs.op_index = 0;
3151               break;
3152
3153             case DW_LNS_set_prologue_end:
3154               break;
3155
3156             case DW_LNS_set_epilogue_begin:
3157               break;
3158
3159             case DW_LNS_set_isa:
3160               uladv = read_leb128 (data, & bytes_read, 0);
3161               data += bytes_read;
3162               printf (_("  Set ISA to %lu\n"), uladv);
3163               break;
3164
3165             default:
3166               printf (_("  Unknown opcode %d with operands: "), op_code);
3167
3168               for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
3169                 {
3170                   printf ("0x%s%s", dwarf_vmatoa ("x", read_leb128 (data,
3171                                                          &bytes_read, 0)),
3172                           i == 1 ? "" : ", ");
3173                   data += bytes_read;
3174                 }
3175               putchar ('\n');
3176               break;
3177             }
3178
3179           /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
3180              to the DWARF address/line matrix.  */
3181           if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
3182               || (op_code == DW_LNS_copy))
3183             {
3184               const unsigned int MAX_FILENAME_LENGTH = 35;
3185               char *fileName = (char *)file_table[state_machine_regs.file - 1].name;
3186               char *newFileName = NULL;
3187               size_t fileNameLength = strlen (fileName);
3188
3189               if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
3190                 {
3191                   newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
3192                   /* Truncate file name */
3193                   strncpy (newFileName,
3194                            fileName + fileNameLength - MAX_FILENAME_LENGTH,
3195                            MAX_FILENAME_LENGTH + 1);
3196                 }
3197               else
3198                 {
3199                   newFileName = (char *) xmalloc (fileNameLength + 1);
3200                   strncpy (newFileName, fileName, fileNameLength + 1);
3201                 }
3202
3203               if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
3204                 {
3205                   if (linfo.li_max_ops_per_insn == 1)
3206                     printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x\n",
3207                             newFileName, state_machine_regs.line,
3208                             state_machine_regs.address);
3209                   else
3210                     printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
3211                             newFileName, state_machine_regs.line,
3212                             state_machine_regs.address,
3213                             state_machine_regs.op_index);
3214                 }
3215               else
3216                 {
3217                   if (linfo.li_max_ops_per_insn == 1)
3218                     printf ("%s  %11d  %#18" DWARF_VMA_FMT "x\n",
3219                             newFileName, state_machine_regs.line,
3220                             state_machine_regs.address);
3221                   else
3222                     printf ("%s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
3223                             newFileName, state_machine_regs.line,
3224                             state_machine_regs.address,
3225                             state_machine_regs.op_index);
3226                 }
3227
3228               if (op_code == DW_LNE_end_sequence)
3229                 printf ("\n");
3230
3231               free (newFileName);
3232             }
3233         }
3234       free (file_table);
3235       file_table = NULL;
3236       free (directory_table);
3237       directory_table = NULL;
3238       putchar ('\n');
3239     }
3240
3241   return 1;
3242 }
3243
3244 static int
3245 display_debug_lines (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
3246 {
3247   unsigned char *data = section->start;
3248   unsigned char *end = data + section->size;
3249   int retValRaw = 1;
3250   int retValDecoded = 1;
3251
3252   if (do_debug_lines == 0)
3253     do_debug_lines |= FLAG_DEBUG_LINES_RAW;
3254
3255   if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
3256     retValRaw = display_debug_lines_raw (section, data, end);
3257
3258   if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
3259     retValDecoded = display_debug_lines_decoded (section, data, end);
3260
3261   if (!retValRaw || !retValDecoded)
3262     return 0;
3263
3264   return 1;
3265 }
3266
3267 static debug_info *
3268 find_debug_info_for_offset (unsigned long offset)
3269 {
3270   unsigned int i;
3271
3272   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
3273     return NULL;
3274
3275   for (i = 0; i < num_debug_info_entries; i++)
3276     if (debug_information[i].cu_offset == offset)
3277       return debug_information + i;
3278
3279   return NULL;
3280 }
3281
3282 static int
3283 display_debug_pubnames (struct dwarf_section *section,
3284                         void *file ATTRIBUTE_UNUSED)
3285 {
3286   DWARF2_Internal_PubNames names;
3287   unsigned char *start = section->start;
3288   unsigned char *end = start + section->size;
3289
3290   /* It does not matter if this load fails,
3291      we test for that later on.  */
3292   load_debug_info (file);
3293
3294   printf (_("Contents of the %s section:\n\n"), section->name);
3295
3296   while (start < end)
3297     {
3298       unsigned char *data;
3299       unsigned long offset;
3300       int offset_size, initial_length_size;
3301
3302       data = start;
3303
3304       names.pn_length = byte_get (data, 4);
3305       data += 4;
3306       if (names.pn_length == 0xffffffff)
3307         {
3308           names.pn_length = byte_get (data, 8);
3309           data += 8;
3310           offset_size = 8;
3311           initial_length_size = 12;
3312         }
3313       else
3314         {
3315           offset_size = 4;
3316           initial_length_size = 4;
3317         }
3318
3319       names.pn_version = byte_get (data, 2);
3320       data += 2;
3321
3322       names.pn_offset = byte_get (data, offset_size);
3323       data += offset_size;
3324
3325       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
3326           && num_debug_info_entries > 0
3327           && find_debug_info_for_offset (names.pn_offset) == NULL)
3328         warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
3329               (unsigned long) names.pn_offset, section->name);
3330
3331       names.pn_size = byte_get (data, offset_size);
3332       data += offset_size;
3333
3334       start += names.pn_length + initial_length_size;
3335
3336       if (names.pn_version != 2 && names.pn_version != 3)
3337         {
3338           static int warned = 0;
3339
3340           if (! warned)
3341             {
3342               warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
3343               warned = 1;
3344             }
3345
3346           continue;
3347         }
3348
3349       printf (_("  Length:                              %ld\n"),
3350               (long) names.pn_length);
3351       printf (_("  Version:                             %d\n"),
3352               names.pn_version);
3353       printf (_("  Offset into .debug_info section:     0x%lx\n"),
3354               (unsigned long) names.pn_offset);
3355       printf (_("  Size of area in .debug_info section: %ld\n"),
3356               (long) names.pn_size);
3357
3358       printf (_("\n    Offset\tName\n"));
3359
3360       do
3361         {
3362           offset = byte_get (data, offset_size);
3363
3364           if (offset != 0)
3365             {
3366               data += offset_size;
3367               printf ("    %-6lx\t%s\n", offset, data);
3368               data += strlen ((char *) data) + 1;
3369             }
3370         }
3371       while (offset != 0);
3372     }
3373
3374   printf ("\n");
3375   return 1;
3376 }
3377
3378 static int
3379 display_debug_macinfo (struct dwarf_section *section,
3380                        void *file ATTRIBUTE_UNUSED)
3381 {
3382   unsigned char *start = section->start;
3383   unsigned char *end = start + section->size;
3384   unsigned char *curr = start;
3385   unsigned int bytes_read;
3386   enum dwarf_macinfo_record_type op;
3387
3388   printf (_("Contents of the %s section:\n\n"), section->name);
3389
3390   while (curr < end)
3391     {
3392       unsigned int lineno;
3393       const char *string;
3394
3395       op = (enum dwarf_macinfo_record_type) *curr;
3396       curr++;
3397
3398       switch (op)
3399         {
3400         case DW_MACINFO_start_file:
3401           {
3402             unsigned int filenum;
3403
3404             lineno = read_leb128 (curr, & bytes_read, 0);
3405             curr += bytes_read;
3406             filenum = read_leb128 (curr, & bytes_read, 0);
3407             curr += bytes_read;
3408
3409             printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
3410                     lineno, filenum);
3411           }
3412           break;
3413
3414         case DW_MACINFO_end_file:
3415           printf (_(" DW_MACINFO_end_file\n"));
3416           break;
3417
3418         case DW_MACINFO_define:
3419           lineno = read_leb128 (curr, & bytes_read, 0);
3420           curr += bytes_read;
3421           string = (char *) curr;
3422           curr += strlen (string) + 1;
3423           printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
3424                   lineno, string);
3425           break;
3426
3427         case DW_MACINFO_undef:
3428           lineno = read_leb128 (curr, & bytes_read, 0);
3429           curr += bytes_read;
3430           string = (char *) curr;
3431           curr += strlen (string) + 1;
3432           printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
3433                   lineno, string);
3434           break;
3435
3436         case DW_MACINFO_vendor_ext:
3437           {
3438             unsigned int constant;
3439
3440             constant = read_leb128 (curr, & bytes_read, 0);
3441             curr += bytes_read;
3442             string = (char *) curr;
3443             curr += strlen (string) + 1;
3444             printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
3445                     constant, string);
3446           }
3447           break;
3448         }
3449     }
3450
3451   return 1;
3452 }
3453
3454 /* Given LINE_OFFSET into the .debug_line section, attempt to return
3455    filename and dirname corresponding to file name table entry with index
3456    FILEIDX.  Return NULL on failure.  */
3457
3458 static unsigned char *
3459 get_line_filename_and_dirname (dwarf_vma line_offset, dwarf_vma fileidx,
3460                                unsigned char **dir_name)
3461 {
3462   struct dwarf_section *section = &debug_displays [line].section;
3463   unsigned char *hdrptr, *dirtable, *file_name;
3464   unsigned int offset_size, initial_length_size;
3465   unsigned int version, opcode_base, bytes_read;
3466   dwarf_vma length, diridx;
3467
3468   *dir_name = NULL;
3469   if (section->start == NULL
3470       || line_offset >= section->size
3471       || fileidx == 0)
3472     return NULL;
3473
3474   hdrptr = section->start + line_offset;
3475   length = byte_get (hdrptr, 4);
3476   hdrptr += 4;
3477   if (length == 0xffffffff)
3478     {
3479       /* This section is 64-bit DWARF 3.  */
3480       length = byte_get (hdrptr, 8);
3481       hdrptr += 8;
3482       offset_size = 8;
3483       initial_length_size = 12;
3484     }
3485   else
3486     {
3487       offset_size = 4;
3488       initial_length_size = 4;
3489     }
3490   if (length + initial_length_size > section->size)
3491     return NULL;
3492   version = byte_get (hdrptr, 2);
3493   hdrptr += 2;
3494   if (version != 2 && version != 3 && version != 4)
3495     return NULL;
3496   hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length.  */
3497   if (version >= 4)
3498     hdrptr++;               /* Skip max_ops_per_insn.  */
3499   hdrptr += 3;              /* Skip default_is_stmt, line_base, line_range.  */
3500   opcode_base = byte_get (hdrptr, 1);
3501   if (opcode_base == 0)
3502     return NULL;
3503   hdrptr++;
3504   hdrptr += opcode_base - 1;
3505   dirtable = hdrptr;
3506   /* Skip over dirname table.  */
3507   while (*hdrptr != '\0')
3508     hdrptr += strlen ((char *) hdrptr) + 1;
3509   hdrptr++;                 /* Skip the NUL at the end of the table.  */
3510   /* Now skip over preceding filename table entries.  */
3511   for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
3512     {
3513       hdrptr += strlen ((char *) hdrptr) + 1;
3514       read_leb128 (hdrptr, &bytes_read, 0);
3515       hdrptr += bytes_read;
3516       read_leb128 (hdrptr, &bytes_read, 0);
3517       hdrptr += bytes_read;
3518       read_leb128 (hdrptr, &bytes_read, 0);
3519       hdrptr += bytes_read;
3520     }
3521   if (*hdrptr == '\0')
3522     return NULL;
3523   file_name = hdrptr;
3524   hdrptr += strlen ((char *) hdrptr) + 1;
3525   diridx = read_leb128 (hdrptr, &bytes_read, 0);
3526   if (diridx == 0)
3527     return file_name;
3528   for (; *dirtable != '\0' && diridx > 1; diridx--)
3529     dirtable += strlen ((char *) dirtable) + 1;
3530   if (*dirtable == '\0')
3531     return NULL;
3532   *dir_name = dirtable;
3533   return file_name;
3534 }
3535
3536 static int
3537 display_debug_macro (struct dwarf_section *section,
3538                      void *file)
3539 {
3540   unsigned char *start = section->start;
3541   unsigned char *end = start + section->size;
3542   unsigned char *curr = start;
3543   unsigned char *extended_op_buf[256];
3544   unsigned int bytes_read;
3545
3546   load_debug_section (str, file);
3547   load_debug_section (line, file);
3548
3549   printf (_("Contents of the %s section:\n\n"), section->name);
3550
3551   while (curr < end)
3552     {
3553       unsigned int lineno, version, flags;
3554       unsigned int offset_size = 4;
3555       const char *string;
3556       dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
3557       unsigned char **extended_ops = NULL;
3558
3559       version = byte_get (curr, 2);
3560       curr += 2;
3561
3562       if (version != 4)
3563         {
3564           error (_("Only GNU extension to DWARF 4 of %s is currently supported.\n"),
3565                  section->name);
3566           return 0;
3567         }
3568
3569       flags = byte_get (curr++, 1);
3570       if (flags & 1)
3571         offset_size = 8;
3572       printf (_("  Offset:                      0x%lx\n"),
3573               (unsigned long) sec_offset);
3574       printf (_("  Version:                     %d\n"), version);
3575       printf (_("  Offset size:                 %d\n"), offset_size);
3576       if (flags & 2)
3577         {
3578           line_offset = byte_get (curr, offset_size);
3579           curr += offset_size;
3580           printf (_("  Offset into .debug_line:     0x%lx\n"),
3581                   (unsigned long) line_offset);
3582         }
3583       if (flags & 4)
3584         {
3585           unsigned int i, count = byte_get (curr++, 1), op;
3586           dwarf_vma nargs, n;
3587           memset (extended_op_buf, 0, sizeof (extended_op_buf));
3588           extended_ops = extended_op_buf;
3589           if (count)
3590             {
3591               printf (_("  Extension opcode arguments:\n"));
3592               for (i = 0; i < count; i++)
3593                 {
3594                   op = byte_get (curr++, 1);
3595                   extended_ops[op] = curr;
3596                   nargs = read_leb128 (curr, &bytes_read, 0);
3597                   curr += bytes_read;
3598                   if (nargs == 0)
3599                     printf (_("    DW_MACRO_GNU_%02x has no arguments\n"), op);
3600                   else
3601                     {
3602                       printf (_("    DW_MACRO_GNU_%02x arguments: "), op);
3603                       for (n = 0; n < nargs; n++)
3604                         {
3605                           unsigned int form = byte_get (curr++, 1);
3606                           printf ("%s%s", get_FORM_name (form),
3607                                   n == nargs - 1 ? "\n" : ", ");
3608                           switch (form)
3609                             {
3610                             case DW_FORM_data1:
3611                             case DW_FORM_data2:
3612                             case DW_FORM_data4:
3613                             case DW_FORM_data8:
3614                             case DW_FORM_sdata:
3615                             case DW_FORM_udata:
3616                             case DW_FORM_block:
3617                             case DW_FORM_block1:
3618                             case DW_FORM_block2:
3619                             case DW_FORM_block4:
3620                             case DW_FORM_flag:
3621                             case DW_FORM_string:
3622                             case DW_FORM_strp:
3623                             case DW_FORM_sec_offset:
3624                               break;
3625                             default:
3626                               error (_("Invalid extension opcode form %s\n"),
3627                                      get_FORM_name (form));
3628                               return 0;
3629                             }
3630                         }
3631                     }
3632                 }
3633             }
3634         }
3635       printf ("\n");
3636
3637       while (1)
3638         {
3639           unsigned int op;
3640
3641           if (curr >= end)
3642             {
3643               error (_(".debug_macro section not zero terminated\n"));
3644               return 0;
3645             }
3646
3647           op = byte_get (curr++, 1);
3648           if (op == 0)
3649             break;
3650
3651           switch (op)
3652             {
3653             case DW_MACRO_GNU_start_file:
3654               {
3655                 unsigned int filenum;
3656                 unsigned char *file_name = NULL, *dir_name = NULL;
3657
3658                 lineno = read_leb128 (curr, &bytes_read, 0);
3659                 curr += bytes_read;
3660                 filenum = read_leb128 (curr, &bytes_read, 0);
3661                 curr += bytes_read;
3662
3663                 if ((flags & 2) == 0)
3664                   error (_("DW_MACRO_GNU_start_file used, but no .debug_line offset provided.\n"));
3665                 else
3666                   file_name
3667                     = get_line_filename_and_dirname (line_offset, filenum,
3668                                                      &dir_name);
3669                 if (file_name == NULL)
3670                   printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d\n"),
3671                           lineno, filenum);
3672                 else
3673                   printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
3674                           lineno, filenum,
3675                           dir_name != NULL ? (const char *) dir_name : "",
3676                           dir_name != NULL ? "/" : "", file_name);
3677               }
3678               break;
3679
3680             case DW_MACRO_GNU_end_file:
3681               printf (_(" DW_MACRO_GNU_end_file\n"));
3682               break;
3683
3684             case DW_MACRO_GNU_define:
3685               lineno = read_leb128 (curr, &bytes_read, 0);
3686               curr += bytes_read;
3687               string = (char *) curr;
3688               curr += strlen (string) + 1;
3689               printf (_(" DW_MACRO_GNU_define - lineno : %d macro : %s\n"),
3690                       lineno, string);
3691               break;
3692
3693             case DW_MACRO_GNU_undef:
3694               lineno = read_leb128 (curr, &bytes_read, 0);
3695               curr += bytes_read;
3696               string = (char *) curr;
3697               curr += strlen (string) + 1;
3698               printf (_(" DW_MACRO_GNU_undef - lineno : %d macro : %s\n"),
3699                       lineno, string);
3700               break;
3701
3702             case DW_MACRO_GNU_define_indirect:
3703               lineno = read_leb128 (curr, &bytes_read, 0);
3704               curr += bytes_read;
3705               offset = byte_get (curr, offset_size);
3706               curr += offset_size;
3707               string = fetch_indirect_string (offset);
3708               printf (_(" DW_MACRO_GNU_define_indirect - lineno : %d macro : %s\n"),
3709                       lineno, string);
3710               break;
3711
3712             case DW_MACRO_GNU_undef_indirect:
3713               lineno = read_leb128 (curr, &bytes_read, 0);
3714               curr += bytes_read;
3715               offset = byte_get (curr, offset_size);
3716               curr += offset_size;
3717               string = fetch_indirect_string (offset);
3718               printf (_(" DW_MACRO_GNU_undef_indirect - lineno : %d macro : %s\n"),
3719                       lineno, string);
3720               break;
3721
3722             case DW_MACRO_GNU_transparent_include:
3723               offset = byte_get (curr, offset_size);
3724               curr += offset_size;
3725               printf (_(" DW_MACRO_GNU_transparent_include - offset : 0x%lx\n"),
3726                       (unsigned long) offset);
3727               break;
3728
3729             default:
3730               if (extended_ops == NULL || extended_ops[op] == NULL)
3731                 {
3732                   error (_(" Unknown macro opcode %02x seen\n"), op);
3733                   return 0;
3734                 }
3735               else
3736                 {
3737                   /* Skip over unhandled opcodes.  */
3738                   dwarf_vma nargs, n;
3739                   unsigned char *desc = extended_ops[op];
3740                   nargs = read_leb128 (desc, &bytes_read, 0);
3741                   desc += bytes_read;
3742                   if (nargs == 0)
3743                     {
3744                       printf (_(" DW_MACRO_GNU_%02x\n"), op);
3745                       break;
3746                     }
3747                   printf (_(" DW_MACRO_GNU_%02x -"), op);
3748                   for (n = 0; n < nargs; n++)
3749                     {
3750                       curr
3751                         = read_and_display_attr_value (0, byte_get (desc++, 1),
3752                                                        curr, 0, 0, offset_size,
3753                                                        version, NULL, 0, NULL);
3754                       if (n != nargs - 1)
3755                         printf (",");
3756                     }
3757                   printf ("\n");
3758                 }
3759               break;
3760             }
3761         }
3762
3763       printf ("\n");
3764     }   
3765
3766   return 1;
3767 }
3768
3769 static int
3770 display_debug_abbrev (struct dwarf_section *section,
3771                       void *file ATTRIBUTE_UNUSED)
3772 {
3773   abbrev_entry *entry;
3774   unsigned char *start = section->start;
3775   unsigned char *end = start + section->size;
3776
3777   printf (_("Contents of the %s section:\n\n"), section->name);
3778
3779   do
3780     {
3781       free_abbrevs ();
3782
3783       start = process_abbrev_section (start, end);
3784
3785       if (first_abbrev == NULL)
3786         continue;
3787
3788       printf (_("  Number TAG\n"));
3789
3790       for (entry = first_abbrev; entry; entry = entry->next)
3791         {
3792           abbrev_attr *attr;
3793
3794           printf ("   %ld      %s    [%s]\n",
3795                   entry->entry,
3796                   get_TAG_name (entry->tag),
3797                   entry->children ? _("has children") : _("no children"));
3798
3799           for (attr = entry->first_attr; attr; attr = attr->next)
3800             printf ("    %-18s %s\n",
3801                     get_AT_name (attr->attribute),
3802                     get_FORM_name (attr->form));
3803         }
3804     }
3805   while (start);
3806
3807   printf ("\n");
3808
3809   return 1;
3810 }
3811
3812 /* Sort array of indexes in ascending order of loc_offsets[idx].  */
3813
3814 static dwarf_vma *loc_offsets;
3815
3816 static int
3817 loc_offsets_compar (const void *ap, const void *bp)
3818 {
3819   dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
3820   dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
3821
3822   return (a > b) - (b > a);
3823 }
3824
3825 static int
3826 display_debug_loc (struct dwarf_section *section, void *file)
3827 {
3828   unsigned char *start = section->start;
3829   unsigned char *section_end;
3830   unsigned long bytes;
3831   unsigned char *section_begin = start;
3832   unsigned int num_loc_list = 0;
3833   unsigned long last_offset = 0;
3834   unsigned int first = 0;
3835   unsigned int i;
3836   unsigned int j;
3837   unsigned int k;
3838   int seen_first_offset = 0;
3839   int locs_sorted = 1;
3840   unsigned char *next;
3841   unsigned int *array = NULL;
3842
3843   bytes = section->size;
3844   section_end = start + bytes;
3845
3846   if (bytes == 0)
3847     {
3848       printf (_("\nThe %s section is empty.\n"), section->name);
3849       return 0;
3850     }
3851
3852   if (load_debug_info (file) == 0)
3853     {
3854       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
3855             section->name);
3856       return 0;
3857     }
3858
3859   /* Check the order of location list in .debug_info section. If
3860      offsets of location lists are in the ascending order, we can
3861      use `debug_information' directly.  */
3862   for (i = 0; i < num_debug_info_entries; i++)
3863     {
3864       unsigned int num;
3865
3866       num = debug_information [i].num_loc_offsets;
3867       if (num > num_loc_list)
3868         num_loc_list = num;
3869
3870       /* Check if we can use `debug_information' directly.  */
3871       if (locs_sorted && num != 0)
3872         {
3873           if (!seen_first_offset)
3874             {
3875               /* This is the first location list.  */
3876               last_offset = debug_information [i].loc_offsets [0];
3877               first = i;
3878               seen_first_offset = 1;
3879               j = 1;
3880             }
3881           else
3882             j = 0;
3883
3884           for (; j < num; j++)
3885             {
3886               if (last_offset >
3887                   debug_information [i].loc_offsets [j])
3888                 {
3889                   locs_sorted = 0;
3890                   break;
3891                 }
3892               last_offset = debug_information [i].loc_offsets [j];
3893             }
3894         }
3895     }
3896
3897   if (!seen_first_offset)
3898     error (_("No location lists in .debug_info section!\n"));
3899
3900   /* DWARF sections under Mach-O have non-zero addresses.  */
3901   if (debug_information [first].num_loc_offsets > 0
3902       && debug_information [first].loc_offsets [0] != section->address)
3903     warn (_("Location lists in %s section start at 0x%s\n"),
3904           section->name,
3905           dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
3906
3907   if (!locs_sorted)
3908     array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
3909   printf (_("Contents of the %s section:\n\n"), section->name);
3910   printf (_("    Offset   Begin    End      Expression\n"));
3911
3912   seen_first_offset = 0;
3913   for (i = first; i < num_debug_info_entries; i++)
3914     {
3915       dwarf_vma begin;
3916       dwarf_vma end;
3917       unsigned short length;
3918       unsigned long offset;
3919       unsigned int pointer_size;
3920       unsigned int offset_size;
3921       int dwarf_version;
3922       unsigned long cu_offset;
3923       unsigned long base_address;
3924       int need_frame_base;
3925       int has_frame_base;
3926
3927       pointer_size = debug_information [i].pointer_size;
3928       cu_offset = debug_information [i].cu_offset;
3929       offset_size = debug_information [i].offset_size;
3930       dwarf_version = debug_information [i].dwarf_version;
3931       if (!locs_sorted)
3932         {
3933           for (k = 0; k < debug_information [i].num_loc_offsets; k++)
3934             array[k] = k;
3935           loc_offsets = debug_information [i].loc_offsets;
3936           qsort (array, debug_information [i].num_loc_offsets,
3937                  sizeof (*array), loc_offsets_compar);
3938         }
3939
3940       for (k = 0; k < debug_information [i].num_loc_offsets; k++)
3941         {
3942           j = locs_sorted ? k : array[k];
3943           if (k
3944               && debug_information [i].loc_offsets [locs_sorted
3945                                                     ? k - 1 : array [k - 1]]
3946                  == debug_information [i].loc_offsets [j])
3947             continue;
3948           has_frame_base = debug_information [i].have_frame_base [j];
3949           /* DWARF sections under Mach-O have non-zero addresses.  */
3950           offset = debug_information [i].loc_offsets [j] - section->address;
3951           next = section_begin + offset;
3952           base_address = debug_information [i].base_address;
3953
3954           if (!seen_first_offset)
3955             seen_first_offset = 1;
3956           else
3957             {
3958               if (start < next)
3959                 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
3960                       (unsigned long) (start - section_begin),
3961                       (unsigned long) (next - section_begin));
3962               else if (start > next)
3963                 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
3964                       (unsigned long) (start - section_begin),
3965                       (unsigned long) (next - section_begin));
3966             }
3967           start = next;
3968
3969           if (offset >= bytes)
3970             {
3971               warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
3972                     offset);
3973               continue;
3974             }
3975
3976           while (1)
3977             {
3978               if (start + 2 * pointer_size > section_end)
3979                 {
3980                   warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3981                         offset);
3982                   break;
3983                 }
3984
3985               /* Note: we use sign extension here in order to be sure that
3986                  we can detect the -1 escape value.  Sign extension into the
3987                  top 32 bits of a 32-bit address will not affect the values
3988                  that we display since we always show hex values, and always
3989                  the bottom 32-bits.  */
3990               begin = byte_get_signed (start, pointer_size);
3991               start += pointer_size;
3992               end = byte_get_signed (start, pointer_size);
3993               start += pointer_size;
3994
3995               printf ("    %8.8lx ", offset);
3996
3997               if (begin == 0 && end == 0)
3998                 {
3999                   printf (_("<End of list>\n"));
4000                   break;
4001                 }
4002
4003               /* Check base address specifiers.  */
4004               if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
4005                 {
4006                   base_address = end;
4007                   print_dwarf_vma (begin, pointer_size);
4008                   print_dwarf_vma (end, pointer_size);
4009                   printf (_("(base address)\n"));
4010                   continue;
4011                 }
4012
4013               if (start + 2 > section_end)
4014                 {
4015                   warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4016                         offset);
4017                   break;
4018                 }
4019
4020               length = byte_get (start, 2);
4021               start += 2;
4022
4023               if (start + length > section_end)
4024                 {
4025                   warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4026                         offset);
4027                   break;
4028                 }
4029
4030               print_dwarf_vma (begin + base_address, pointer_size);
4031               print_dwarf_vma (end + base_address, pointer_size);
4032
4033               putchar ('(');
4034               need_frame_base = decode_location_expression (start,
4035                                                             pointer_size,
4036                                                             offset_size,
4037                                                             dwarf_version,
4038                                                             length,
4039                                                             cu_offset, section);
4040               putchar (')');
4041
4042               if (need_frame_base && !has_frame_base)
4043                 printf (_(" [without DW_AT_frame_base]"));
4044
4045               if (begin == end)
4046                 fputs (_(" (start == end)"), stdout);
4047               else if (begin > end)
4048                 fputs (_(" (start > end)"), stdout);
4049
4050               putchar ('\n');
4051
4052               start += length;
4053             }
4054         }
4055     }
4056
4057   if (start < section_end)
4058     warn (_("There are %ld unused bytes at the end of section %s\n"),
4059           (long) (section_end - start), section->name);
4060   putchar ('\n');
4061   free (array);
4062   return 1;
4063 }
4064
4065 static int
4066 display_debug_str (struct dwarf_section *section,
4067                    void *file ATTRIBUTE_UNUSED)
4068 {
4069   unsigned char *start = section->start;
4070   unsigned long bytes = section->size;
4071   dwarf_vma addr = section->address;
4072
4073   if (bytes == 0)
4074     {
4075       printf (_("\nThe %s section is empty.\n"), section->name);
4076       return 0;
4077     }
4078
4079   printf (_("Contents of the %s section:\n\n"), section->name);
4080
4081   while (bytes)
4082     {
4083       int j;
4084       int k;
4085       int lbytes;
4086
4087       lbytes = (bytes > 16 ? 16 : bytes);
4088
4089       printf ("  0x%8.8lx ", (unsigned long) addr);
4090
4091       for (j = 0; j < 16; j++)
4092         {
4093           if (j < lbytes)
4094             printf ("%2.2x", start[j]);
4095           else
4096             printf ("  ");
4097
4098           if ((j & 3) == 3)
4099             printf (" ");
4100         }
4101
4102       for (j = 0; j < lbytes; j++)
4103         {
4104           k = start[j];
4105           if (k >= ' ' && k < 0x80)
4106             printf ("%c", k);
4107           else
4108             printf (".");
4109         }
4110
4111       putchar ('\n');
4112
4113       start += lbytes;
4114       addr  += lbytes;
4115       bytes -= lbytes;
4116     }
4117
4118   putchar ('\n');
4119
4120   return 1;
4121 }
4122
4123 static int
4124 display_debug_info (struct dwarf_section *section, void *file)
4125 {
4126   return process_debug_info (section, file, abbrev, 0, 0);
4127 }
4128
4129 static int
4130 display_debug_types (struct dwarf_section *section, void *file)
4131 {
4132   return process_debug_info (section, file, abbrev, 0, 1);
4133 }
4134
4135 static int
4136 display_trace_info (struct dwarf_section *section, void *file)
4137 {
4138   return process_debug_info (section, file, trace_abbrev, 0, 0);
4139 }
4140
4141 static int
4142 display_debug_aranges (struct dwarf_section *section,
4143                        void *file ATTRIBUTE_UNUSED)
4144 {
4145   unsigned char *start = section->start;
4146   unsigned char *end = start + section->size;
4147
4148   printf (_("Contents of the %s section:\n\n"), section->name);
4149
4150   /* It does not matter if this load fails,
4151      we test for that later on.  */
4152   load_debug_info (file);
4153
4154   while (start < end)
4155     {
4156       unsigned char *hdrptr;
4157       DWARF2_Internal_ARange arange;
4158       unsigned char *addr_ranges;
4159       dwarf_vma length;
4160       dwarf_vma address;
4161       unsigned char address_size;
4162       int excess;
4163       int offset_size;
4164       int initial_length_size;
4165
4166       hdrptr = start;
4167
4168       arange.ar_length = byte_get (hdrptr, 4);
4169       hdrptr += 4;
4170
4171       if (arange.ar_length == 0xffffffff)
4172         {
4173           arange.ar_length = byte_get (hdrptr, 8);
4174           hdrptr += 8;
4175           offset_size = 8;
4176           initial_length_size = 12;
4177         }
4178       else
4179         {
4180           offset_size = 4;
4181           initial_length_size = 4;
4182         }
4183
4184       arange.ar_version = byte_get (hdrptr, 2);
4185       hdrptr += 2;
4186
4187       arange.ar_info_offset = byte_get (hdrptr, offset_size);
4188       hdrptr += offset_size;
4189
4190       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
4191           && num_debug_info_entries > 0
4192           && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
4193         warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
4194               (unsigned long) arange.ar_info_offset, section->name);
4195
4196       arange.ar_pointer_size = byte_get (hdrptr, 1);
4197       hdrptr += 1;
4198
4199       arange.ar_segment_size = byte_get (hdrptr, 1);
4200       hdrptr += 1;
4201
4202       if (arange.ar_version != 2 && arange.ar_version != 3)
4203         {
4204           warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
4205           break;
4206         }
4207
4208       printf (_("  Length:                   %ld\n"),
4209               (long) arange.ar_length);
4210       printf (_("  Version:                  %d\n"), arange.ar_version);
4211       printf (_("  Offset into .debug_info:  0x%lx\n"),
4212               (unsigned long) arange.ar_info_offset);
4213       printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
4214       printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
4215
4216       address_size = arange.ar_pointer_size + arange.ar_segment_size;
4217
4218       if (address_size == 0)
4219         {
4220           error (_("Invalid address size in %s section!\n"),
4221                  section->name);
4222           break;
4223         }
4224
4225       /* The DWARF spec does not require that the address size be a power
4226          of two, but we do.  This will have to change if we ever encounter
4227          an uneven architecture.  */
4228       if ((address_size & (address_size - 1)) != 0)
4229         {
4230           warn (_("Pointer size + Segment size is not a power of two.\n"));
4231           break;
4232         }
4233
4234       if (address_size > 4)
4235         printf (_("\n    Address            Length\n"));
4236       else
4237         printf (_("\n    Address    Length\n"));
4238
4239       addr_ranges = hdrptr;
4240
4241       /* Must pad to an alignment boundary that is twice the address size.  */
4242       excess = (hdrptr - start) % (2 * address_size);
4243       if (excess)
4244         addr_ranges += (2 * address_size) - excess;
4245
4246       start += arange.ar_length + initial_length_size;
4247
4248       while (addr_ranges + 2 * address_size <= start)
4249         {
4250           address = byte_get (addr_ranges, address_size);
4251
4252           addr_ranges += address_size;
4253
4254           length  = byte_get (addr_ranges, address_size);
4255
4256           addr_ranges += address_size;
4257
4258           printf ("    ");
4259           print_dwarf_vma (address, address_size);
4260           print_dwarf_vma (length, address_size);
4261           putchar ('\n');
4262         }
4263     }
4264
4265   printf ("\n");
4266
4267   return 1;
4268 }
4269
4270 /* Each debug_information[x].range_lists[y] gets this representation for
4271    sorting purposes.  */
4272
4273 struct range_entry
4274 {
4275   /* The debug_information[x].range_lists[y] value.  */
4276   unsigned long ranges_offset;
4277
4278   /* Original debug_information to find parameters of the data.  */
4279   debug_info *debug_info_p;
4280 };
4281
4282 /* Sort struct range_entry in ascending order of its RANGES_OFFSET.  */
4283
4284 static int
4285 range_entry_compar (const void *ap, const void *bp)
4286 {
4287   const struct range_entry *a_re = (const struct range_entry *) ap;
4288   const struct range_entry *b_re = (const struct range_entry *) bp;
4289   const unsigned long a = a_re->ranges_offset;
4290   const unsigned long b = b_re->ranges_offset;
4291
4292   return (a > b) - (b > a);
4293 }
4294
4295 static int
4296 display_debug_ranges (struct dwarf_section *section,
4297                       void *file ATTRIBUTE_UNUSED)
4298 {
4299   unsigned char *start = section->start;
4300   unsigned long bytes;
4301   unsigned char *section_begin = start;
4302   unsigned int num_range_list, i;
4303   struct range_entry *range_entries, *range_entry_fill;
4304
4305   bytes = section->size;
4306
4307   if (bytes == 0)
4308     {
4309       printf (_("\nThe %s section is empty.\n"), section->name);
4310       return 0;
4311     }
4312
4313   if (load_debug_info (file) == 0)
4314     {
4315       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4316             section->name);
4317       return 0;
4318     }
4319
4320   num_range_list = 0;
4321   for (i = 0; i < num_debug_info_entries; i++)
4322     num_range_list += debug_information [i].num_range_lists;
4323
4324   if (num_range_list == 0)
4325     error (_("No range lists in .debug_info section!\n"));
4326
4327   range_entries = (struct range_entry *)
4328       xmalloc (sizeof (*range_entries) * num_range_list);
4329   range_entry_fill = range_entries;
4330
4331   for (i = 0; i < num_debug_info_entries; i++)
4332     {
4333       debug_info *debug_info_p = &debug_information[i];
4334       unsigned int j;
4335
4336       for (j = 0; j < debug_info_p->num_range_lists; j++)
4337         {
4338           range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
4339           range_entry_fill->debug_info_p = debug_info_p;
4340           range_entry_fill++;
4341         }
4342     }
4343
4344   qsort (range_entries, num_range_list, sizeof (*range_entries),
4345          range_entry_compar);
4346
4347   /* DWARF sections under Mach-O have non-zero addresses.  */
4348   if (range_entries[0].ranges_offset != section->address)
4349     warn (_("Range lists in %s section start at 0x%lx\n"),
4350           section->name, range_entries[0].ranges_offset);
4351
4352   printf (_("Contents of the %s section:\n\n"), section->name);
4353   printf (_("    Offset   Begin    End\n"));
4354
4355   for (i = 0; i < num_range_list; i++)
4356     {
4357       struct range_entry *range_entry = &range_entries[i];
4358       debug_info *debug_info_p = range_entry->debug_info_p;
4359       unsigned int pointer_size;
4360       unsigned long offset;
4361       unsigned char *next;
4362       unsigned long base_address;
4363
4364       pointer_size = debug_info_p->pointer_size;
4365
4366       /* DWARF sections under Mach-O have non-zero addresses.  */
4367       offset = range_entry->ranges_offset - section->address;
4368       next = section_begin + offset;
4369       base_address = debug_info_p->base_address;
4370
4371       if (i > 0)
4372         {
4373           if (start < next)
4374             warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
4375                   (unsigned long) (start - section_begin),
4376                   (unsigned long) (next - section_begin), section->name);
4377           else if (start > next)
4378             warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
4379                   (unsigned long) (start - section_begin),
4380                   (unsigned long) (next - section_begin), section->name);
4381         }
4382       start = next;
4383
4384       while (1)
4385         {
4386           dwarf_vma begin;
4387           dwarf_vma end;
4388
4389           /* Note: we use sign extension here in order to be sure that
4390              we can detect the -1 escape value.  Sign extension into the
4391              top 32 bits of a 32-bit address will not affect the values
4392              that we display since we always show hex values, and always
4393              the bottom 32-bits.  */
4394           begin = byte_get_signed (start, pointer_size);
4395           start += pointer_size;
4396           end = byte_get_signed (start, pointer_size);
4397           start += pointer_size;
4398
4399           printf ("    %8.8lx ", offset);
4400
4401           if (begin == 0 && end == 0)
4402             {
4403               printf (_("<End of list>\n"));
4404               break;
4405             }
4406
4407           /* Check base address specifiers.  */
4408           if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
4409             {
4410               base_address = end;
4411               print_dwarf_vma (begin, pointer_size);
4412               print_dwarf_vma (end, pointer_size);
4413               printf ("(base address)\n");
4414               continue;
4415             }
4416
4417           print_dwarf_vma (begin + base_address, pointer_size);
4418           print_dwarf_vma (end + base_address, pointer_size);
4419
4420           if (begin == end)
4421             fputs (_("(start == end)"), stdout);
4422           else if (begin > end)
4423             fputs (_("(start > end)"), stdout);
4424
4425           putchar ('\n');
4426         }
4427     }
4428   putchar ('\n');
4429
4430   free (range_entries);
4431
4432   return 1;
4433 }
4434
4435 typedef struct Frame_Chunk
4436 {
4437   struct Frame_Chunk *next;
4438   unsigned char *chunk_start;
4439   int ncols;
4440   /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
4441   short int *col_type;
4442   int *col_offset;
4443   char *augmentation;
4444   unsigned int code_factor;
4445   int data_factor;
4446   unsigned long pc_begin;
4447   unsigned long pc_range;
4448   int cfa_reg;
4449   int cfa_offset;
4450   int ra;
4451   unsigned char fde_encoding;
4452   unsigned char cfa_exp;
4453   unsigned char ptr_size;
4454   unsigned char segment_size;
4455 }
4456 Frame_Chunk;
4457
4458 static const char *const *dwarf_regnames;
4459 static unsigned int dwarf_regnames_count;
4460
4461 /* A marker for a col_type that means this column was never referenced
4462    in the frame info.  */
4463 #define DW_CFA_unreferenced (-1)
4464
4465 /* Return 0 if not more space is needed, 1 if more space is needed,
4466    -1 for invalid reg.  */
4467
4468 static int
4469 frame_need_space (Frame_Chunk *fc, unsigned int reg)
4470 {
4471   int prev = fc->ncols;
4472
4473   if (reg < (unsigned int) fc->ncols)
4474     return 0;
4475
4476   if (dwarf_regnames_count
4477       && reg > dwarf_regnames_count)
4478     return -1;
4479
4480   fc->ncols = reg + 1;
4481   fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
4482                                           sizeof (short int));
4483   fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
4484
4485   while (prev < fc->ncols)
4486     {
4487       fc->col_type[prev] = DW_CFA_unreferenced;
4488       fc->col_offset[prev] = 0;
4489       prev++;
4490     }
4491   return 1;
4492 }
4493
4494 static const char *const dwarf_regnames_i386[] =
4495 {
4496   "eax", "ecx", "edx", "ebx",
4497   "esp", "ebp", "esi", "edi",
4498   "eip", "eflags", NULL,
4499   "st0", "st1", "st2", "st3",
4500   "st4", "st5", "st6", "st7",
4501   NULL, NULL,
4502   "xmm0", "xmm1", "xmm2", "xmm3",
4503   "xmm4", "xmm5", "xmm6", "xmm7",
4504   "mm0", "mm1", "mm2", "mm3",
4505   "mm4", "mm5", "mm6", "mm7",
4506   "fcw", "fsw", "mxcsr",
4507   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
4508   "tr", "ldtr"
4509 };
4510
4511 void
4512 init_dwarf_regnames_i386 (void)
4513 {
4514   dwarf_regnames = dwarf_regnames_i386;
4515   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
4516 }
4517
4518 static const char *const dwarf_regnames_x86_64[] =
4519 {
4520   "rax", "rdx", "rcx", "rbx",
4521   "rsi", "rdi", "rbp", "rsp",
4522   "r8",  "r9",  "r10", "r11",
4523   "r12", "r13", "r14", "r15",
4524   "rip",
4525   "xmm0",  "xmm1",  "xmm2",  "xmm3",
4526   "xmm4",  "xmm5",  "xmm6",  "xmm7",
4527   "xmm8",  "xmm9",  "xmm10", "xmm11",
4528   "xmm12", "xmm13", "xmm14", "xmm15",
4529   "st0", "st1", "st2", "st3",
4530   "st4", "st5", "st6", "st7",
4531   "mm0", "mm1", "mm2", "mm3",
4532   "mm4", "mm5", "mm6", "mm7",
4533   "rflags",
4534   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
4535   "fs.base", "gs.base", NULL, NULL,
4536   "tr", "ldtr",
4537   "mxcsr", "fcw", "fsw"
4538 };
4539
4540 void
4541 init_dwarf_regnames_x86_64 (void)
4542 {
4543   dwarf_regnames = dwarf_regnames_x86_64;
4544   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
4545 }
4546
4547 void
4548 init_dwarf_regnames (unsigned int e_machine)
4549 {
4550   switch (e_machine)
4551     {
4552     case EM_386:
4553     case EM_486:
4554       init_dwarf_regnames_i386 ();
4555       break;
4556
4557     case EM_X86_64:
4558     case EM_L1OM:
4559     case EM_K1OM:
4560       init_dwarf_regnames_x86_64 ();
4561       break;
4562
4563     default:
4564       break;
4565     }
4566 }
4567
4568 static const char *
4569 regname (unsigned int regno, int row)
4570 {
4571   static char reg[64];
4572   if (dwarf_regnames
4573       && regno < dwarf_regnames_count
4574       && dwarf_regnames [regno] != NULL)
4575     {
4576       if (row)
4577         return dwarf_regnames [regno];
4578       snprintf (reg, sizeof (reg), "r%d (%s)", regno,
4579                 dwarf_regnames [regno]);
4580     }
4581   else
4582     snprintf (reg, sizeof (reg), "r%d", regno);
4583   return reg;
4584 }
4585
4586 static void
4587 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
4588 {
4589   int r;
4590   char tmp[100];
4591
4592   if (*max_regs < fc->ncols)
4593     *max_regs = fc->ncols;
4594
4595   if (*need_col_headers)
4596     {
4597       static const char *sloc = "   LOC";
4598
4599       *need_col_headers = 0;
4600
4601       printf ("%-*s CFA      ", eh_addr_size * 2, sloc);
4602
4603       for (r = 0; r < *max_regs; r++)
4604         if (fc->col_type[r] != DW_CFA_unreferenced)
4605           {
4606             if (r == fc->ra)
4607               printf ("ra      ");
4608             else
4609               printf ("%-5s ", regname (r, 1));
4610           }
4611
4612       printf ("\n");
4613     }
4614
4615   printf ("%0*lx ", eh_addr_size * 2, fc->pc_begin);
4616   if (fc->cfa_exp)
4617     strcpy (tmp, "exp");
4618   else
4619     sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), fc->cfa_offset);
4620   printf ("%-8s ", tmp);
4621
4622   for (r = 0; r < fc->ncols; r++)
4623     {
4624       if (fc->col_type[r] != DW_CFA_unreferenced)
4625         {
4626           switch (fc->col_type[r])
4627             {
4628             case DW_CFA_undefined:
4629               strcpy (tmp, "u");
4630               break;
4631             case DW_CFA_same_value:
4632               strcpy (tmp, "s");
4633               break;
4634             case DW_CFA_offset:
4635               sprintf (tmp, "c%+d", fc->col_offset[r]);
4636               break;
4637             case DW_CFA_val_offset:
4638               sprintf (tmp, "v%+d", fc->col_offset[r]);
4639               break;
4640             case DW_CFA_register:
4641               sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
4642               break;
4643             case DW_CFA_expression:
4644               strcpy (tmp, "exp");
4645               break;
4646             case DW_CFA_val_expression:
4647               strcpy (tmp, "vexp");
4648               break;
4649             default:
4650               strcpy (tmp, "n/a");
4651               break;
4652             }
4653           printf ("%-5s ", tmp);
4654         }
4655     }
4656   printf ("\n");
4657 }
4658
4659 #define GET(N)  byte_get (start, N); start += N
4660 #define LEB()   read_leb128 (start, & length_return, 0); start += length_return
4661 #define SLEB()  read_sleb128 (start, & length_return); start += length_return
4662
4663 static int
4664 display_debug_frames (struct dwarf_section *section,
4665                       void *file ATTRIBUTE_UNUSED)
4666 {
4667   unsigned char *start = section->start;
4668   unsigned char *end = start + section->size;
4669   unsigned char *section_start = start;
4670   Frame_Chunk *chunks = 0;
4671   Frame_Chunk *remembered_state = 0;
4672   Frame_Chunk *rs;
4673   int is_eh = strcmp (section->name, ".eh_frame") == 0;
4674   unsigned int length_return;
4675   int max_regs = 0;
4676   const char *bad_reg = _("bad register: ");
4677   int saved_eh_addr_size = eh_addr_size;
4678
4679   printf (_("Contents of the %s section:\n"), section->name);
4680
4681   while (start < end)
4682     {
4683       unsigned char *saved_start;
4684       unsigned char *block_end;
4685       unsigned long length;
4686       unsigned long cie_id;
4687       Frame_Chunk *fc;
4688       Frame_Chunk *cie;
4689       int need_col_headers = 1;
4690       unsigned char *augmentation_data = NULL;
4691       unsigned long augmentation_data_len = 0;
4692       int encoded_ptr_size = saved_eh_addr_size;
4693       int offset_size;
4694       int initial_length_size;
4695
4696       saved_start = start;
4697       length = byte_get (start, 4); start += 4;
4698
4699       if (length == 0)
4700         {
4701           printf ("\n%08lx ZERO terminator\n\n",
4702                     (unsigned long)(saved_start - section_start));
4703           continue;
4704         }
4705
4706       if (length == 0xffffffff)
4707         {
4708           length = byte_get (start, 8);
4709           start += 8;
4710           offset_size = 8;
4711           initial_length_size = 12;
4712         }
4713       else
4714         {
4715           offset_size = 4;
4716           initial_length_size = 4;
4717         }
4718
4719       block_end = saved_start + length + initial_length_size;
4720       if (block_end > end)
4721         {
4722           warn ("Invalid length %#08lx in FDE at %#08lx\n",
4723                 length, (unsigned long)(saved_start - section_start));
4724           block_end = end;
4725         }
4726       cie_id = byte_get (start, offset_size); start += offset_size;
4727
4728       if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
4729         {
4730           int version;
4731
4732           fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
4733           memset (fc, 0, sizeof (Frame_Chunk));
4734
4735           fc->next = chunks;
4736           chunks = fc;
4737           fc->chunk_start = saved_start;
4738           fc->ncols = 0;
4739           fc->col_type = (short int *) xmalloc (sizeof (short int));
4740           fc->col_offset = (int *) xmalloc (sizeof (int));
4741           frame_need_space (fc, max_regs - 1);
4742
4743           version = *start++;
4744
4745           fc->augmentation = (char *) start;
4746           start = (unsigned char *) strchr ((char *) start, '\0') + 1;
4747
4748           if (strcmp (fc->augmentation, "eh") == 0)
4749             start += eh_addr_size;
4750
4751           if (version >= 4)
4752             {
4753               fc->ptr_size = GET (1);
4754               fc->segment_size = GET (1);
4755               eh_addr_size = fc->ptr_size;
4756             }
4757           else
4758             {
4759               fc->ptr_size = eh_addr_size;
4760               fc->segment_size = 0;
4761             }
4762           fc->code_factor = LEB ();
4763           fc->data_factor = SLEB ();
4764           if (version == 1)
4765             {
4766               fc->ra = GET (1);
4767             }
4768           else
4769             {
4770               fc->ra = LEB ();
4771             }
4772
4773           if (fc->augmentation[0] == 'z')
4774             {
4775               augmentation_data_len = LEB ();
4776               augmentation_data = start;
4777               start += augmentation_data_len;
4778             }
4779           cie = fc;
4780
4781           if (do_debug_frames_interp)
4782             printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
4783                     (unsigned long)(saved_start - section_start), length, cie_id,
4784                     fc->augmentation, fc->code_factor, fc->data_factor,
4785                     fc->ra);
4786           else
4787             {
4788               printf ("\n%08lx %08lx %08lx CIE\n",
4789                       (unsigned long)(saved_start - section_start), length, cie_id);
4790               printf ("  Version:               %d\n", version);
4791               printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
4792               if (version >= 4)
4793                 {
4794                   printf ("  Pointer Size:          %u\n", fc->ptr_size);
4795                   printf ("  Segment Size:          %u\n", fc->segment_size);
4796                 }
4797               printf ("  Code alignment factor: %u\n", fc->code_factor);
4798               printf ("  Data alignment factor: %d\n", fc->data_factor);
4799               printf ("  Return address column: %d\n", fc->ra);
4800
4801               if (augmentation_data_len)
4802                 {
4803                   unsigned long i;
4804                   printf ("  Augmentation data:    ");
4805                   for (i = 0; i < augmentation_data_len; ++i)
4806                     printf (" %02x", augmentation_data[i]);
4807                   putchar ('\n');
4808                 }
4809               putchar ('\n');
4810             }
4811
4812           if (augmentation_data_len)
4813             {
4814               unsigned char *p, *q;
4815               p = (unsigned char *) fc->augmentation + 1;
4816               q = augmentation_data;
4817
4818               while (1)
4819                 {
4820                   if (*p == 'L')
4821                     q++;
4822                   else if (*p == 'P')
4823                     q += 1 + size_of_encoded_value (*q);
4824                   else if (*p == 'R')
4825                     fc->fde_encoding = *q++;
4826                   else if (*p == 'S')
4827                     ;
4828                   else
4829                     break;
4830                   p++;
4831                 }
4832
4833               if (fc->fde_encoding)
4834                 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
4835             }
4836
4837           frame_need_space (fc, fc->ra);
4838         }
4839       else
4840         {
4841           unsigned char *look_for;
4842           static Frame_Chunk fde_fc;
4843           unsigned long segment_selector;
4844
4845           fc = & fde_fc;
4846           memset (fc, 0, sizeof (Frame_Chunk));
4847
4848           look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
4849
4850           for (cie = chunks; cie ; cie = cie->next)
4851             if (cie->chunk_start == look_for)
4852               break;
4853
4854           if (!cie)
4855             {
4856               warn ("Invalid CIE pointer %#08lx in FDE at %#08lx\n",
4857                     cie_id, (unsigned long)(saved_start - section_start));
4858               fc->ncols = 0;
4859               fc->col_type = (short int *) xmalloc (sizeof (short int));
4860               fc->col_offset = (int *) xmalloc (sizeof (int));
4861               frame_need_space (fc, max_regs - 1);
4862               cie = fc;
4863               fc->augmentation = "";
4864               fc->fde_encoding = 0;
4865               fc->ptr_size = eh_addr_size;
4866               fc->segment_size = 0;
4867             }
4868           else
4869             {
4870               fc->ncols = cie->ncols;
4871               fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
4872               fc->col_offset =  (int *) xcmalloc (fc->ncols, sizeof (int));
4873               memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
4874               memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
4875               fc->augmentation = cie->augmentation;
4876               fc->ptr_size = cie->ptr_size;
4877               eh_addr_size = cie->ptr_size;
4878               fc->segment_size = cie->segment_size;
4879               fc->code_factor = cie->code_factor;
4880               fc->data_factor = cie->data_factor;
4881               fc->cfa_reg = cie->cfa_reg;
4882               fc->cfa_offset = cie->cfa_offset;
4883               fc->ra = cie->ra;
4884               frame_need_space (fc, max_regs - 1);
4885               fc->fde_encoding = cie->fde_encoding;
4886             }
4887
4888           if (fc->fde_encoding)
4889             encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
4890
4891           segment_selector = 0;
4892           if (fc->segment_size)
4893             {
4894               segment_selector = byte_get (start, fc->segment_size);
4895               start += fc->segment_size;
4896             }
4897           fc->pc_begin = get_encoded_value (start, fc->fde_encoding, section);
4898           start += encoded_ptr_size;
4899           fc->pc_range = byte_get (start, encoded_ptr_size);
4900           start += encoded_ptr_size;
4901
4902           if (cie->augmentation[0] == 'z')
4903             {
4904               augmentation_data_len = LEB ();
4905               augmentation_data = start;
4906               start += augmentation_data_len;
4907             }
4908
4909           printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=",
4910                   (unsigned long)(saved_start - section_start), length, cie_id,
4911                   (unsigned long)(cie->chunk_start - section_start));
4912           if (fc->segment_size)
4913             printf ("%04lx:", segment_selector);
4914           printf ("%08lx..%08lx\n", fc->pc_begin, fc->pc_begin + fc->pc_range);
4915           if (! do_debug_frames_interp && augmentation_data_len)
4916             {
4917               unsigned long i;
4918
4919               printf ("  Augmentation data:    ");
4920               for (i = 0; i < augmentation_data_len; ++i)
4921                 printf (" %02x", augmentation_data[i]);
4922               putchar ('\n');
4923               putchar ('\n');
4924             }
4925         }
4926
4927       /* At this point, fc is the current chunk, cie (if any) is set, and
4928          we're about to interpret instructions for the chunk.  */
4929       /* ??? At present we need to do this always, since this sizes the
4930          fc->col_type and fc->col_offset arrays, which we write into always.
4931          We should probably split the interpreted and non-interpreted bits
4932          into two different routines, since there's so much that doesn't
4933          really overlap between them.  */
4934       if (1 || do_debug_frames_interp)
4935         {
4936           /* Start by making a pass over the chunk, allocating storage
4937              and taking note of what registers are used.  */
4938           unsigned char *tmp = start;
4939
4940           while (start < block_end)
4941             {
4942               unsigned op, opa;
4943               unsigned long reg, temp;
4944
4945               op = *start++;
4946               opa = op & 0x3f;
4947               if (op & 0xc0)
4948                 op &= 0xc0;
4949
4950               /* Warning: if you add any more cases to this switch, be
4951                  sure to add them to the corresponding switch below.  */
4952               switch (op)
4953                 {
4954                 case DW_CFA_advance_loc:
4955                   break;
4956                 case DW_CFA_offset:
4957                   LEB ();
4958                   if (frame_need_space (fc, opa) >= 0)
4959                     fc->col_type[opa] = DW_CFA_undefined;
4960                   break;
4961                 case DW_CFA_restore:
4962                   if (frame_need_space (fc, opa) >= 0)
4963                     fc->col_type[opa] = DW_CFA_undefined;
4964                   break;
4965                 case DW_CFA_set_loc:
4966                   start += encoded_ptr_size;
4967                   break;
4968                 case DW_CFA_advance_loc1:
4969                   start += 1;
4970                   break;
4971                 case DW_CFA_advance_loc2:
4972                   start += 2;
4973                   break;
4974                 case DW_CFA_advance_loc4:
4975                   start += 4;
4976                   break;
4977                 case DW_CFA_offset_extended:
4978                 case DW_CFA_val_offset:
4979                   reg = LEB (); LEB ();
4980                   if (frame_need_space (fc, reg) >= 0)
4981                     fc->col_type[reg] = DW_CFA_undefined;
4982                   break;
4983                 case DW_CFA_restore_extended:
4984                   reg = LEB ();
4985                   frame_need_space (fc, reg);
4986                   if (frame_need_space (fc, reg) >= 0)
4987                     fc->col_type[reg] = DW_CFA_undefined;
4988                   break;
4989                 case DW_CFA_undefined:
4990                   reg = LEB ();
4991                   if (frame_need_space (fc, reg) >= 0)
4992                     fc->col_type[reg] = DW_CFA_undefined;
4993                   break;
4994                 case DW_CFA_same_value:
4995                   reg = LEB ();
4996                   if (frame_need_space (fc, reg) >= 0)
4997                     fc->col_type[reg] = DW_CFA_undefined;
4998                   break;
4999                 case DW_CFA_register:
5000                   reg = LEB (); LEB ();
5001                   if (frame_need_space (fc, reg) >= 0)
5002                     fc->col_type[reg] = DW_CFA_undefined;
5003                   break;
5004                 case DW_CFA_def_cfa:
5005                   LEB (); LEB ();
5006                   break;
5007                 case DW_CFA_def_cfa_register:
5008                   LEB ();
5009                   break;
5010                 case DW_CFA_def_cfa_offset:
5011                   LEB ();
5012                   break;
5013                 case DW_CFA_def_cfa_expression:
5014                   temp = LEB ();
5015                   start += temp;
5016                   break;
5017                 case DW_CFA_expression:
5018                 case DW_CFA_val_expression:
5019                   reg = LEB ();
5020                   temp = LEB ();
5021                   start += temp;
5022                   if (frame_need_space (fc, reg) >= 0)
5023                     fc->col_type[reg] = DW_CFA_undefined;
5024                   break;
5025                 case DW_CFA_offset_extended_sf:
5026                 case DW_CFA_val_offset_sf:
5027                   reg = LEB (); SLEB ();
5028                   if (frame_need_space (fc, reg) >= 0)
5029                     fc->col_type[reg] = DW_CFA_undefined;
5030                   break;
5031                 case DW_CFA_def_cfa_sf:
5032                   LEB (); SLEB ();
5033                   break;
5034                 case DW_CFA_def_cfa_offset_sf:
5035                   SLEB ();
5036                   break;
5037                 case DW_CFA_MIPS_advance_loc8:
5038                   start += 8;
5039                   break;
5040                 case DW_CFA_GNU_args_size:
5041                   LEB ();
5042                   break;
5043                 case DW_CFA_GNU_negative_offset_extended:
5044                   reg = LEB (); LEB ();
5045                   if (frame_need_space (fc, reg) >= 0)
5046                     fc->col_type[reg] = DW_CFA_undefined;
5047                   break;
5048                 default:
5049                   break;
5050                 }
5051             }
5052           start = tmp;
5053         }
5054
5055       /* Now we know what registers are used, make a second pass over
5056          the chunk, this time actually printing out the info.  */
5057
5058       while (start < block_end)
5059         {
5060           unsigned op, opa;
5061           unsigned long ul, reg, roffs;
5062           long l, ofs;
5063           dwarf_vma vma;
5064           const char *reg_prefix = "";
5065
5066           op = *start++;
5067           opa = op & 0x3f;
5068           if (op & 0xc0)
5069             op &= 0xc0;
5070
5071           /* Warning: if you add any more cases to this switch, be
5072              sure to add them to the corresponding switch above.  */
5073           switch (op)
5074             {
5075             case DW_CFA_advance_loc:
5076               if (do_debug_frames_interp)
5077                 frame_display_row (fc, &need_col_headers, &max_regs);
5078               else
5079                 printf ("  DW_CFA_advance_loc: %d to %08lx\n",
5080                         opa * fc->code_factor,
5081                         fc->pc_begin + opa * fc->code_factor);
5082               fc->pc_begin += opa * fc->code_factor;
5083               break;
5084
5085             case DW_CFA_offset:
5086               roffs = LEB ();
5087               if (opa >= (unsigned int) fc->ncols)
5088                 reg_prefix = bad_reg;
5089               if (! do_debug_frames_interp || *reg_prefix != '\0')
5090                 printf ("  DW_CFA_offset: %s%s at cfa%+ld\n",
5091                         reg_prefix, regname (opa, 0),
5092                         roffs * fc->data_factor);
5093               if (*reg_prefix == '\0')
5094                 {
5095                   fc->col_type[opa] = DW_CFA_offset;
5096                   fc->col_offset[opa] = roffs * fc->data_factor;
5097                 }
5098               break;
5099
5100             case DW_CFA_restore:
5101               if (opa >= (unsigned int) cie->ncols
5102                   || opa >= (unsigned int) fc->ncols)
5103                 reg_prefix = bad_reg;
5104               if (! do_debug_frames_interp || *reg_prefix != '\0')
5105                 printf ("  DW_CFA_restore: %s%s\n",
5106                         reg_prefix, regname (opa, 0));
5107               if (*reg_prefix == '\0')
5108                 {
5109                   fc->col_type[opa] = cie->col_type[opa];
5110                   fc->col_offset[opa] = cie->col_offset[opa];
5111                 }
5112               break;
5113
5114             case DW_CFA_set_loc:
5115               vma = get_encoded_value (start, fc->fde_encoding, section);
5116               start += encoded_ptr_size;
5117               if (do_debug_frames_interp)
5118                 frame_display_row (fc, &need_col_headers, &max_regs);
5119               else
5120                 printf ("  DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
5121               fc->pc_begin = vma;
5122               break;
5123
5124             case DW_CFA_advance_loc1:
5125               ofs = byte_get (start, 1); start += 1;
5126               if (do_debug_frames_interp)
5127                 frame_display_row (fc, &need_col_headers, &max_regs);
5128               else
5129                 printf ("  DW_CFA_advance_loc1: %ld to %08lx\n",
5130                         ofs * fc->code_factor,
5131                         fc->pc_begin + ofs * fc->code_factor);
5132               fc->pc_begin += ofs * fc->code_factor;
5133               break;
5134
5135             case DW_CFA_advance_loc2:
5136               ofs = byte_get (start, 2); start += 2;
5137               if (do_debug_frames_interp)
5138                 frame_display_row (fc, &need_col_headers, &max_regs);
5139               else
5140                 printf ("  DW_CFA_advance_loc2: %ld to %08lx\n",
5141                         ofs * fc->code_factor,
5142                         fc->pc_begin + ofs * fc->code_factor);
5143               fc->pc_begin += ofs * fc->code_factor;
5144               break;
5145
5146             case DW_CFA_advance_loc4:
5147               ofs = byte_get (start, 4); start += 4;
5148               if (do_debug_frames_interp)
5149                 frame_display_row (fc, &need_col_headers, &max_regs);
5150               else
5151                 printf ("  DW_CFA_advance_loc4: %ld to %08lx\n",
5152                         ofs * fc->code_factor,
5153                         fc->pc_begin + ofs * fc->code_factor);
5154               fc->pc_begin += ofs * fc->code_factor;
5155               break;
5156
5157             case DW_CFA_offset_extended:
5158               reg = LEB ();
5159               roffs = LEB ();
5160               if (reg >= (unsigned int) fc->ncols)
5161                 reg_prefix = bad_reg;
5162               if (! do_debug_frames_interp || *reg_prefix != '\0')
5163                 printf ("  DW_CFA_offset_extended: %s%s at cfa%+ld\n",
5164                         reg_prefix, regname (reg, 0),
5165                         roffs * fc->data_factor);
5166               if (*reg_prefix == '\0')
5167                 {
5168                   fc->col_type[reg] = DW_CFA_offset;
5169                   fc->col_offset[reg] = roffs * fc->data_factor;
5170                 }
5171               break;
5172
5173             case DW_CFA_val_offset:
5174               reg = LEB ();
5175               roffs = LEB ();
5176               if (reg >= (unsigned int) fc->ncols)
5177                 reg_prefix = bad_reg;
5178               if (! do_debug_frames_interp || *reg_prefix != '\0')
5179                 printf ("  DW_CFA_val_offset: %s%s at cfa%+ld\n",
5180                         reg_prefix, regname (reg, 0),
5181                         roffs * fc->data_factor);
5182               if (*reg_prefix == '\0')
5183                 {
5184                   fc->col_type[reg] = DW_CFA_val_offset;
5185                   fc->col_offset[reg] = roffs * fc->data_factor;
5186                 }
5187               break;
5188
5189             case DW_CFA_restore_extended:
5190               reg = LEB ();
5191               if (reg >= (unsigned int) cie->ncols
5192                   || reg >= (unsigned int) fc->ncols)
5193                 reg_prefix = bad_reg;
5194               if (! do_debug_frames_interp || *reg_prefix != '\0')
5195                 printf ("  DW_CFA_restore_extended: %s%s\n",
5196                         reg_prefix, regname (reg, 0));
5197               if (*reg_prefix == '\0')
5198                 {
5199                   fc->col_type[reg] = cie->col_type[reg];
5200                   fc->col_offset[reg] = cie->col_offset[reg];
5201                 }
5202               break;
5203
5204             case DW_CFA_undefined:
5205               reg = LEB ();
5206               if (reg >= (unsigned int) fc->ncols)
5207                 reg_prefix = bad_reg;
5208               if (! do_debug_frames_interp || *reg_prefix != '\0')
5209                 printf ("  DW_CFA_undefined: %s%s\n",
5210                         reg_prefix, regname (reg, 0));
5211               if (*reg_prefix == '\0')
5212                 {
5213                   fc->col_type[reg] = DW_CFA_undefined;
5214                   fc->col_offset[reg] = 0;
5215                 }
5216               break;
5217
5218             case DW_CFA_same_value:
5219               reg = LEB ();
5220               if (reg >= (unsigned int) fc->ncols)
5221                 reg_prefix = bad_reg;
5222               if (! do_debug_frames_interp || *reg_prefix != '\0')
5223                 printf ("  DW_CFA_same_value: %s%s\n",
5224                         reg_prefix, regname (reg, 0));
5225               if (*reg_prefix == '\0')
5226                 {
5227                   fc->col_type[reg] = DW_CFA_same_value;
5228                   fc->col_offset[reg] = 0;
5229                 }
5230               break;
5231
5232             case DW_CFA_register:
5233               reg = LEB ();
5234               roffs = LEB ();
5235               if (reg >= (unsigned int) fc->ncols)
5236                 reg_prefix = bad_reg;
5237               if (! do_debug_frames_interp || *reg_prefix != '\0')
5238                 {
5239                   printf ("  DW_CFA_register: %s%s in ",
5240                           reg_prefix, regname (reg, 0));
5241                   puts (regname (roffs, 0));
5242                 }
5243               if (*reg_prefix == '\0')
5244                 {
5245                   fc->col_type[reg] = DW_CFA_register;
5246                   fc->col_offset[reg] = roffs;
5247                 }
5248               break;
5249
5250             case DW_CFA_remember_state:
5251               if (! do_debug_frames_interp)
5252                 printf ("  DW_CFA_remember_state\n");
5253               rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
5254               rs->ncols = fc->ncols;
5255               rs->col_type = (short int *) xcmalloc (rs->ncols,
5256                                                      sizeof (short int));
5257               rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (int));
5258               memcpy (rs->col_type, fc->col_type, rs->ncols);
5259               memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
5260               rs->next = remembered_state;
5261               remembered_state = rs;
5262               break;
5263
5264             case DW_CFA_restore_state:
5265               if (! do_debug_frames_interp)
5266                 printf ("  DW_CFA_restore_state\n");
5267               rs = remembered_state;
5268               if (rs)
5269                 {
5270                   remembered_state = rs->next;
5271                   frame_need_space (fc, rs->ncols - 1);
5272                   memcpy (fc->col_type, rs->col_type, rs->ncols);
5273                   memcpy (fc->col_offset, rs->col_offset,
5274                           rs->ncols * sizeof (int));
5275                   free (rs->col_type);
5276                   free (rs->col_offset);
5277                   free (rs);
5278                 }
5279               else if (do_debug_frames_interp)
5280                 printf ("Mismatched DW_CFA_restore_state\n");
5281               break;
5282
5283             case DW_CFA_def_cfa:
5284               fc->cfa_reg = LEB ();
5285               fc->cfa_offset = LEB ();
5286               fc->cfa_exp = 0;
5287               if (! do_debug_frames_interp)
5288                 printf ("  DW_CFA_def_cfa: %s ofs %d\n",
5289                         regname (fc->cfa_reg, 0), fc->cfa_offset);
5290               break;
5291
5292             case DW_CFA_def_cfa_register:
5293               fc->cfa_reg = LEB ();
5294               fc->cfa_exp = 0;
5295               if (! do_debug_frames_interp)
5296                 printf ("  DW_CFA_def_cfa_register: %s\n",
5297                         regname (fc->cfa_reg, 0));
5298               break;
5299
5300             case DW_CFA_def_cfa_offset:
5301               fc->cfa_offset = LEB ();
5302               if (! do_debug_frames_interp)
5303                 printf ("  DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
5304               break;
5305
5306             case DW_CFA_nop:
5307               if (! do_debug_frames_interp)
5308                 printf ("  DW_CFA_nop\n");
5309               break;
5310
5311             case DW_CFA_def_cfa_expression:
5312               ul = LEB ();
5313               if (! do_debug_frames_interp)
5314                 {
5315                   printf ("  DW_CFA_def_cfa_expression (");
5316                   decode_location_expression (start, eh_addr_size, 0, -1,
5317                                               ul, 0, section);
5318                   printf (")\n");
5319                 }
5320               fc->cfa_exp = 1;
5321               start += ul;
5322               break;
5323
5324             case DW_CFA_expression:
5325               reg = LEB ();
5326               ul = LEB ();
5327               if (reg >= (unsigned int) fc->ncols)
5328                 reg_prefix = bad_reg;
5329               if (! do_debug_frames_interp || *reg_prefix != '\0')
5330                 {
5331                   printf ("  DW_CFA_expression: %s%s (",
5332                           reg_prefix, regname (reg, 0));
5333                   decode_location_expression (start, eh_addr_size, 0, -1,
5334                                               ul, 0, section);
5335                   printf (")\n");
5336                 }
5337               if (*reg_prefix == '\0')
5338                 fc->col_type[reg] = DW_CFA_expression;
5339               start += ul;
5340               break;
5341
5342             case DW_CFA_val_expression:
5343               reg = LEB ();
5344               ul = LEB ();
5345               if (reg >= (unsigned int) fc->ncols)
5346                 reg_prefix = bad_reg;
5347               if (! do_debug_frames_interp || *reg_prefix != '\0')
5348                 {
5349                   printf ("  DW_CFA_val_expression: %s%s (",
5350                           reg_prefix, regname (reg, 0));
5351                   decode_location_expression (start, eh_addr_size, 0, -1,
5352                                               ul, 0, section);
5353                   printf (")\n");
5354                 }
5355               if (*reg_prefix == '\0')
5356                 fc->col_type[reg] = DW_CFA_val_expression;
5357               start += ul;
5358               break;
5359
5360             case DW_CFA_offset_extended_sf:
5361               reg = LEB ();
5362               l = SLEB ();
5363               if (frame_need_space (fc, reg) < 0)
5364                 reg_prefix = bad_reg;
5365               if (! do_debug_frames_interp || *reg_prefix != '\0')
5366                 printf ("  DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
5367                         reg_prefix, regname (reg, 0),
5368                         l * fc->data_factor);
5369               if (*reg_prefix == '\0')
5370                 {
5371                   fc->col_type[reg] = DW_CFA_offset;
5372                   fc->col_offset[reg] = l * fc->data_factor;
5373                 }
5374               break;
5375
5376             case DW_CFA_val_offset_sf:
5377               reg = LEB ();
5378               l = SLEB ();
5379               if (frame_need_space (fc, reg) < 0)
5380                 reg_prefix = bad_reg;
5381               if (! do_debug_frames_interp || *reg_prefix != '\0')
5382                 printf ("  DW_CFA_val_offset_sf: %s%s at cfa%+ld\n",
5383                         reg_prefix, regname (reg, 0),
5384                         l * fc->data_factor);
5385               if (*reg_prefix == '\0')
5386                 {
5387                   fc->col_type[reg] = DW_CFA_val_offset;
5388                   fc->col_offset[reg] = l * fc->data_factor;
5389                 }
5390               break;
5391
5392             case DW_CFA_def_cfa_sf:
5393               fc->cfa_reg = LEB ();
5394               fc->cfa_offset = SLEB ();
5395               fc->cfa_offset = fc->cfa_offset * fc->data_factor;
5396               fc->cfa_exp = 0;
5397               if (! do_debug_frames_interp)
5398                 printf ("  DW_CFA_def_cfa_sf: %s ofs %d\n",
5399                         regname (fc->cfa_reg, 0), fc->cfa_offset);
5400               break;
5401
5402             case DW_CFA_def_cfa_offset_sf:
5403               fc->cfa_offset = SLEB ();
5404               fc->cfa_offset = fc->cfa_offset * fc->data_factor;
5405               if (! do_debug_frames_interp)
5406                 printf ("  DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
5407               break;
5408
5409             case DW_CFA_MIPS_advance_loc8:
5410               ofs = byte_get (start, 8); start += 8;
5411               if (do_debug_frames_interp)
5412                 frame_display_row (fc, &need_col_headers, &max_regs);
5413               else
5414                 printf ("  DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
5415                         ofs * fc->code_factor,
5416                         fc->pc_begin + ofs * fc->code_factor);
5417               fc->pc_begin += ofs * fc->code_factor;
5418               break;
5419
5420             case DW_CFA_GNU_window_save:
5421               if (! do_debug_frames_interp)
5422                 printf ("  DW_CFA_GNU_window_save\n");
5423               break;
5424
5425             case DW_CFA_GNU_args_size:
5426               ul = LEB ();
5427               if (! do_debug_frames_interp)
5428                 printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
5429               break;
5430
5431             case DW_CFA_GNU_negative_offset_extended:
5432               reg = LEB ();
5433               l = - LEB ();
5434               if (frame_need_space (fc, reg) < 0)
5435                 reg_prefix = bad_reg;
5436               if (! do_debug_frames_interp || *reg_prefix != '\0')
5437                 printf ("  DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
5438                         reg_prefix, regname (reg, 0),
5439                         l * fc->data_factor);
5440               if (*reg_prefix == '\0')
5441                 {
5442                   fc->col_type[reg] = DW_CFA_offset;
5443                   fc->col_offset[reg] = l * fc->data_factor;
5444                 }
5445               break;
5446
5447             default:
5448               if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
5449                 printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
5450               else
5451                 warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
5452               start = block_end;
5453             }
5454         }
5455
5456       if (do_debug_frames_interp)
5457         frame_display_row (fc, &need_col_headers, &max_regs);
5458
5459       start = block_end;
5460       eh_addr_size = saved_eh_addr_size;
5461     }
5462
5463   printf ("\n");
5464
5465   return 1;
5466 }
5467
5468 #undef GET
5469 #undef LEB
5470 #undef SLEB
5471
5472 static int
5473 display_gdb_index (struct dwarf_section *section,
5474                    void *file ATTRIBUTE_UNUSED)
5475 {
5476   unsigned char *start = section->start;
5477   uint32_t version;
5478   uint32_t cu_list_offset, tu_list_offset;
5479   uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
5480   unsigned int cu_list_elements, tu_list_elements;
5481   unsigned int address_table_size, symbol_table_slots;
5482   unsigned char *cu_list, *tu_list;
5483   unsigned char *address_table, *symbol_table, *constant_pool;
5484   unsigned int i;
5485
5486   /* The documentation for the format of this file is in gdb/dwarf2read.c.  */
5487
5488   printf (_("Contents of the %s section:\n"), section->name);
5489
5490   if (section->size < 6 * sizeof (uint32_t))
5491     {
5492       warn (_("Truncated header in the %s section.\n"), section->name);
5493       return 0;
5494     }
5495
5496   version = byte_get_little_endian (start, 4);
5497   printf (_("Version %ld\n"), (long) version);
5498
5499   /* Prior versions are obsolete, and future versions may not be
5500      backwards compatible.  */
5501   switch (version)
5502     {
5503     case 3:
5504       warn (_("The address table data in version 3 may be wrong.\n"));
5505       break;
5506     case 4:
5507       warn (_("Version 4 does not support case insensitive lookups.\n"));
5508       break;
5509     case 5:
5510       break;
5511     default:
5512       warn (_("Unsupported version %lu.\n"), (unsigned long) version);
5513       return 0;
5514     }
5515
5516   cu_list_offset = byte_get_little_endian (start + 4, 4);
5517   tu_list_offset = byte_get_little_endian (start + 8, 4);
5518   address_table_offset = byte_get_little_endian (start + 12, 4);
5519   symbol_table_offset = byte_get_little_endian (start + 16, 4);
5520   constant_pool_offset = byte_get_little_endian (start + 20, 4);
5521
5522   if (cu_list_offset > section->size
5523       || tu_list_offset > section->size
5524       || address_table_offset > section->size
5525       || symbol_table_offset > section->size
5526       || constant_pool_offset > section->size)
5527     {
5528       warn (_("Corrupt header in the %s section.\n"), section->name);
5529       return 0;
5530     }
5531
5532   cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
5533   tu_list_elements = (address_table_offset - tu_list_offset) / 8;
5534   address_table_size = symbol_table_offset - address_table_offset;
5535   symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
5536
5537   cu_list = start + cu_list_offset;
5538   tu_list = start + tu_list_offset;
5539   address_table = start + address_table_offset;
5540   symbol_table = start + symbol_table_offset;
5541   constant_pool = start + constant_pool_offset;
5542
5543   printf (_("\nCU table:\n"));
5544   for (i = 0; i < cu_list_elements; i += 2)
5545     {
5546       uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
5547       uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
5548
5549       printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
5550               (unsigned long) cu_offset,
5551               (unsigned long) (cu_offset + cu_length - 1));
5552     }
5553
5554   printf (_("\nTU table:\n"));
5555   for (i = 0; i < tu_list_elements; i += 3)
5556     {
5557       uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
5558       uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
5559       uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
5560
5561       printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
5562               (unsigned long) tu_offset,
5563               (unsigned long) type_offset);
5564       print_dwarf_vma (signature, 8);
5565       printf ("\n");
5566     }
5567
5568   printf (_("\nAddress table:\n"));
5569   for (i = 0; i < address_table_size; i += 2 * 8 + 4)
5570     {
5571       uint64_t low = byte_get_little_endian (address_table + i, 8);
5572       uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
5573       uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
5574
5575       print_dwarf_vma (low, 8);
5576       print_dwarf_vma (high, 8);
5577       printf (_("%lu\n"), (unsigned long) cu_index);
5578     }
5579
5580   printf (_("\nSymbol table:\n"));
5581   for (i = 0; i < symbol_table_slots; ++i)
5582     {
5583       uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
5584       uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
5585       uint32_t num_cus, cu;
5586
5587       if (name_offset != 0
5588           || cu_vector_offset != 0)
5589         {
5590           unsigned int j;
5591
5592           printf ("[%3u] %s:", i, constant_pool + name_offset);
5593           num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
5594           for (j = 0; j < num_cus; ++j)
5595             {
5596               cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
5597               /* Convert to TU number if it's for a type unit.  */
5598               if (cu >= cu_list_elements / 2)
5599                 printf (" T%lu", (unsigned long) (cu - cu_list_elements / 2));
5600               else
5601                 printf (" %lu", (unsigned long) cu);
5602             }
5603           printf ("\n");
5604         }
5605     }
5606
5607   return 1;
5608 }
5609
5610 static int
5611 display_debug_not_supported (struct dwarf_section *section,
5612                              void *file ATTRIBUTE_UNUSED)
5613 {
5614   printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
5615             section->name);
5616
5617   return 1;
5618 }
5619
5620 void *
5621 cmalloc (size_t nmemb, size_t size)
5622 {
5623   /* Check for overflow.  */
5624   if (nmemb >= ~(size_t) 0 / size)
5625     return NULL;
5626   else
5627     return malloc (nmemb * size);
5628 }
5629
5630 void *
5631 xcmalloc (size_t nmemb, size_t size)
5632 {
5633   /* Check for overflow.  */
5634   if (nmemb >= ~(size_t) 0 / size)
5635     return NULL;
5636   else
5637     return xmalloc (nmemb * size);
5638 }
5639
5640 void *
5641 xcrealloc (void *ptr, size_t nmemb, size_t size)
5642 {
5643   /* Check for overflow.  */
5644   if (nmemb >= ~(size_t) 0 / size)
5645     return NULL;
5646   else
5647     return xrealloc (ptr, nmemb * size);
5648 }
5649
5650 void
5651 free_debug_memory (void)
5652 {
5653   unsigned int i;
5654
5655   free_abbrevs ();
5656
5657   for (i = 0; i < max; i++)
5658     free_debug_section ((enum dwarf_section_display_enum) i);
5659
5660   if (debug_information != NULL)
5661     {
5662       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
5663         {
5664           for (i = 0; i < num_debug_info_entries; i++)
5665             {
5666               if (!debug_information [i].max_loc_offsets)
5667                 {
5668                   free (debug_information [i].loc_offsets);
5669                   free (debug_information [i].have_frame_base);
5670                 }
5671               if (!debug_information [i].max_range_lists)
5672                 free (debug_information [i].range_lists);
5673             }
5674         }
5675
5676       free (debug_information);
5677       debug_information = NULL;
5678       num_debug_info_entries = 0;
5679     }
5680 }
5681
5682 void
5683 dwarf_select_sections_by_names (const char *names)
5684 {
5685   typedef struct
5686   {
5687     const char * option;
5688     int *        variable;
5689     int          val;
5690   }
5691   debug_dump_long_opts;
5692
5693   static const debug_dump_long_opts opts_table [] =
5694     {
5695       /* Please keep this table alpha- sorted.  */
5696       { "Ranges", & do_debug_ranges, 1 },
5697       { "abbrev", & do_debug_abbrevs, 1 },
5698       { "aranges", & do_debug_aranges, 1 },
5699       { "frames", & do_debug_frames, 1 },
5700       { "frames-interp", & do_debug_frames_interp, 1 },
5701       { "info", & do_debug_info, 1 },
5702       { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility.  */
5703       { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
5704       { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
5705       { "loc",  & do_debug_loc, 1 },
5706       { "macro", & do_debug_macinfo, 1 },
5707       { "pubnames", & do_debug_pubnames, 1 },
5708       { "pubtypes", & do_debug_pubtypes, 1 },
5709       /* This entry is for compatability
5710          with earlier versions of readelf.  */
5711       { "ranges", & do_debug_aranges, 1 },
5712       { "str", & do_debug_str, 1 },
5713       /* The special .gdb_index section.  */
5714       { "gdb_index", & do_gdb_index, 1 },
5715       /* These trace_* sections are used by Itanium VMS.  */
5716       { "trace_abbrev", & do_trace_abbrevs, 1 },
5717       { "trace_aranges", & do_trace_aranges, 1 },
5718       { "trace_info", & do_trace_info, 1 },
5719       { NULL, NULL, 0 }
5720     };
5721
5722   const char *p;
5723
5724   p = names;
5725   while (*p)
5726     {
5727       const debug_dump_long_opts * entry;
5728
5729       for (entry = opts_table; entry->option; entry++)
5730         {
5731           size_t len = strlen (entry->option);
5732
5733           if (strncmp (p, entry->option, len) == 0
5734               && (p[len] == ',' || p[len] == '\0'))
5735             {
5736               * entry->variable |= entry->val;
5737
5738               /* The --debug-dump=frames-interp option also
5739                  enables the --debug-dump=frames option.  */
5740               if (do_debug_frames_interp)
5741                 do_debug_frames = 1;
5742
5743               p += len;
5744               break;
5745             }
5746         }
5747
5748       if (entry->option == NULL)
5749         {
5750           warn (_("Unrecognized debug option '%s'\n"), p);
5751           p = strchr (p, ',');
5752           if (p == NULL)
5753             break;
5754         }
5755
5756       if (*p == ',')
5757         p++;
5758     }
5759 }
5760
5761 void
5762 dwarf_select_sections_by_letters (const char *letters)
5763 {
5764   unsigned int lindex = 0;
5765
5766   while (letters[lindex])
5767     switch (letters[lindex++])
5768       {
5769       case 'i':
5770         do_debug_info = 1;
5771         break;
5772
5773       case 'a':
5774         do_debug_abbrevs = 1;
5775         break;
5776
5777       case 'l':
5778         do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5779         break;
5780
5781       case 'L':
5782         do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
5783         break;
5784
5785       case 'p':
5786         do_debug_pubnames = 1;
5787         break;
5788
5789       case 't':
5790         do_debug_pubtypes = 1;
5791         break;
5792
5793       case 'r':
5794         do_debug_aranges = 1;
5795         break;
5796
5797       case 'R':
5798         do_debug_ranges = 1;
5799         break;
5800
5801       case 'F':
5802         do_debug_frames_interp = 1;
5803       case 'f':
5804         do_debug_frames = 1;
5805         break;
5806
5807       case 'm':
5808         do_debug_macinfo = 1;
5809         break;
5810
5811       case 's':
5812         do_debug_str = 1;
5813         break;
5814
5815       case 'o':
5816         do_debug_loc = 1;
5817         break;
5818
5819       default:
5820         warn (_("Unrecognized debug option '%s'\n"), optarg);
5821         break;
5822       }
5823 }
5824
5825 void
5826 dwarf_select_sections_all (void)
5827 {
5828   do_debug_info = 1;
5829   do_debug_abbrevs = 1;
5830   do_debug_lines = FLAG_DEBUG_LINES_RAW;
5831   do_debug_pubnames = 1;
5832   do_debug_pubtypes = 1;
5833   do_debug_aranges = 1;
5834   do_debug_ranges = 1;
5835   do_debug_frames = 1;
5836   do_debug_macinfo = 1;
5837   do_debug_str = 1;
5838   do_debug_loc = 1;
5839   do_gdb_index = 1;
5840   do_trace_info = 1;
5841   do_trace_abbrevs = 1;
5842   do_trace_aranges = 1;
5843 }
5844
5845 struct dwarf_section_display debug_displays[] =
5846 {
5847   { { ".debug_abbrev",          ".zdebug_abbrev",       NULL, NULL, 0, 0 },
5848     display_debug_abbrev,               &do_debug_abbrevs,      0 },
5849   { { ".debug_aranges",         ".zdebug_aranges",      NULL, NULL, 0, 0 },
5850     display_debug_aranges,              &do_debug_aranges,      1 },
5851   { { ".debug_frame",           ".zdebug_frame",        NULL, NULL, 0, 0 },
5852     display_debug_frames,               &do_debug_frames,       1 },
5853   { { ".debug_info",            ".zdebug_info",         NULL, NULL, 0, 0 },
5854     display_debug_info,                 &do_debug_info,         1 },
5855   { { ".debug_line",            ".zdebug_line",         NULL, NULL, 0, 0 },
5856     display_debug_lines,                &do_debug_lines,        1 },
5857   { { ".debug_pubnames",        ".zdebug_pubnames",     NULL, NULL, 0, 0 },
5858     display_debug_pubnames,             &do_debug_pubnames,     0 },
5859   { { ".eh_frame",              "",                     NULL, NULL, 0, 0 },
5860     display_debug_frames,               &do_debug_frames,       1 },
5861   { { ".debug_macinfo",         ".zdebug_macinfo",      NULL, NULL, 0, 0 },
5862     display_debug_macinfo,              &do_debug_macinfo,      0 },
5863   { { ".debug_macro",           ".zdebug_macro",        NULL, NULL, 0, 0 },
5864     display_debug_macro,                &do_debug_macinfo,      1 },
5865   { { ".debug_str",             ".zdebug_str",          NULL, NULL, 0, 0 },
5866     display_debug_str,                  &do_debug_str,          0 },
5867   { { ".debug_loc",             ".zdebug_loc",          NULL, NULL, 0, 0 },
5868     display_debug_loc,                  &do_debug_loc,          1 },
5869   { { ".debug_pubtypes",        ".zdebug_pubtypes",     NULL, NULL, 0, 0 },
5870     display_debug_pubnames,             &do_debug_pubtypes,     0 },
5871   { { ".debug_ranges",          ".zdebug_ranges",       NULL, NULL, 0, 0 },
5872     display_debug_ranges,               &do_debug_ranges,       1 },
5873   { { ".debug_static_func",     ".zdebug_static_func",  NULL, NULL, 0, 0 },
5874     display_debug_not_supported,        NULL,                   0 },
5875   { { ".debug_static_vars",     ".zdebug_static_vars",  NULL, NULL, 0, 0 },
5876     display_debug_not_supported,        NULL,                   0 },
5877   { { ".debug_types",           ".zdebug_types",        NULL, NULL, 0, 0 },
5878     display_debug_types,                &do_debug_info,         1 },
5879   { { ".debug_weaknames",       ".zdebug_weaknames",    NULL, NULL, 0, 0 },
5880     display_debug_not_supported,        NULL,                   0 },
5881   { { ".gdb_index",             "",                     NULL, NULL, 0, 0 },
5882     display_gdb_index,                  &do_gdb_index,          0 },
5883   { { ".trace_info",            "",                     NULL, NULL, 0, 0 },
5884     display_trace_info,                 &do_trace_info,         1 },
5885   { { ".trace_abbrev",          "",                     NULL, NULL, 0, 0 },
5886     display_debug_abbrev,               &do_trace_abbrevs,      0 },
5887   { { ".trace_aranges",         "",                     NULL, NULL, 0, 0 },
5888     display_debug_aranges,              &do_trace_aranges,      0 }
5889 };