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