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