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