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