* dwarf.c (process_debug_info): Check for corrupt lengths.
[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 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "libiberty.h"
24 #include "bfd.h"
25 #include "bucomm.h"
26 #include "elf/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         case DW_OP_form_tls_address:
899           printf ("DW_OP_form_tls_address");
900           break;
901
902           /* GNU extensions.  */
903         case DW_OP_GNU_push_tls_address:
904           printf ("DW_OP_GNU_push_tls_address");
905           break;
906
907         default:
908           if (op >= DW_OP_lo_user
909               && op <= DW_OP_hi_user)
910             printf (_("(User defined location op)"));
911           else
912             printf (_("(Unknown location op)"));
913           /* No way to tell where the next op is, so just bail.  */
914           return need_frame_base;
915         }
916
917       /* Separate the ops.  */
918       if (data < end)
919         printf ("; ");
920     }
921
922   return need_frame_base;
923 }
924
925 static unsigned char *
926 read_and_display_attr_value (unsigned long attribute,
927                              unsigned long form,
928                              unsigned char *data,
929                              unsigned long cu_offset,
930                              unsigned long pointer_size,
931                              unsigned long offset_size,
932                              int dwarf_version,
933                              debug_info *debug_info_p,
934                              int do_loc)
935 {
936   unsigned long uvalue = 0;
937   unsigned char *block_start = NULL;
938   unsigned int bytes_read;
939
940   switch (form)
941     {
942     default:
943       break;
944
945     case DW_FORM_ref_addr:
946       if (dwarf_version == 2)
947         {
948           uvalue = byte_get (data, pointer_size);
949           data += pointer_size;
950         }
951       else if (dwarf_version == 3)
952         {
953           uvalue = byte_get (data, offset_size);
954           data += offset_size;
955         }
956       else
957         {
958           error (_("Internal error: DWARF version is not 2 or 3.\n"));
959         }
960       break;
961
962     case DW_FORM_addr:
963       uvalue = byte_get (data, pointer_size);
964       data += pointer_size;
965       break;
966
967     case DW_FORM_strp:
968       uvalue = byte_get (data, offset_size);
969       data += offset_size;
970       break;
971
972     case DW_FORM_ref1:
973     case DW_FORM_flag:
974     case DW_FORM_data1:
975       uvalue = byte_get (data++, 1);
976       break;
977
978     case DW_FORM_ref2:
979     case DW_FORM_data2:
980       uvalue = byte_get (data, 2);
981       data += 2;
982       break;
983
984     case DW_FORM_ref4:
985     case DW_FORM_data4:
986       uvalue = byte_get (data, 4);
987       data += 4;
988       break;
989
990     case DW_FORM_sdata:
991       uvalue = read_leb128 (data, & bytes_read, 1);
992       data += bytes_read;
993       break;
994
995     case DW_FORM_ref_udata:
996     case DW_FORM_udata:
997       uvalue = read_leb128 (data, & bytes_read, 0);
998       data += bytes_read;
999       break;
1000
1001     case DW_FORM_indirect:
1002       form = read_leb128 (data, & bytes_read, 0);
1003       data += bytes_read;
1004       if (!do_loc)
1005         printf (" %s", get_FORM_name (form));
1006       return read_and_display_attr_value (attribute, form, data,
1007                                           cu_offset, pointer_size,
1008                                           offset_size, dwarf_version,
1009                                           debug_info_p, do_loc);
1010     }
1011
1012   switch (form)
1013     {
1014     case DW_FORM_ref_addr:
1015       if (!do_loc)
1016         printf (" <#%lx>", uvalue);
1017       break;
1018
1019     case DW_FORM_ref1:
1020     case DW_FORM_ref2:
1021     case DW_FORM_ref4:
1022     case DW_FORM_ref_udata:
1023       if (!do_loc)
1024         printf (" <%lx>", uvalue + cu_offset);
1025       break;
1026
1027     case DW_FORM_data4:
1028     case DW_FORM_addr:
1029       if (!do_loc)
1030         printf (" %#lx", uvalue);
1031       break;
1032
1033     case DW_FORM_flag:
1034     case DW_FORM_data1:
1035     case DW_FORM_data2:
1036     case DW_FORM_sdata:
1037     case DW_FORM_udata:
1038       if (!do_loc)
1039         printf (" %ld", uvalue);
1040       break;
1041
1042     case DW_FORM_ref8:
1043     case DW_FORM_data8:
1044       if (!do_loc)
1045         {
1046           uvalue = byte_get (data, 4);
1047           printf (" %lx", uvalue);
1048           printf (" %lx", (unsigned long) byte_get (data + 4, 4));
1049         }
1050       if ((do_loc || do_debug_loc || do_debug_ranges)
1051           && num_debug_info_entries == 0)
1052         {
1053           if (sizeof (uvalue) == 8)
1054             uvalue = byte_get (data, 8);
1055           else
1056             error (_("DW_FORM_data8 is unsupported when sizeof (unsigned long) != 8\n"));
1057         }
1058       data += 8;
1059       break;
1060
1061     case DW_FORM_string:
1062       if (!do_loc)
1063         printf (" %s", data);
1064       data += strlen ((char *) data) + 1;
1065       break;
1066
1067     case DW_FORM_block:
1068       uvalue = read_leb128 (data, & bytes_read, 0);
1069       block_start = data + bytes_read;
1070       if (do_loc)
1071         data = block_start + uvalue;
1072       else
1073         data = display_block (block_start, uvalue);
1074       break;
1075
1076     case DW_FORM_block1:
1077       uvalue = byte_get (data, 1);
1078       block_start = data + 1;
1079       if (do_loc)
1080         data = block_start + uvalue;
1081       else
1082         data = display_block (block_start, uvalue);
1083       break;
1084
1085     case DW_FORM_block2:
1086       uvalue = byte_get (data, 2);
1087       block_start = data + 2;
1088       if (do_loc)
1089         data = block_start + uvalue;
1090       else
1091         data = display_block (block_start, uvalue);
1092       break;
1093
1094     case DW_FORM_block4:
1095       uvalue = byte_get (data, 4);
1096       block_start = data + 4;
1097       if (do_loc)
1098         data = block_start + uvalue;
1099       else
1100         data = display_block (block_start, uvalue);
1101       break;
1102
1103     case DW_FORM_strp:
1104       if (!do_loc)
1105         printf (_(" (indirect string, offset: 0x%lx): %s"),
1106                 uvalue, fetch_indirect_string (uvalue));
1107       break;
1108
1109     case DW_FORM_indirect:
1110       /* Handled above.  */
1111       break;
1112
1113     default:
1114       warn (_("Unrecognized form: %lu\n"), form);
1115       break;
1116     }
1117
1118   /* For some attributes we can display further information.  */
1119   if ((do_loc || do_debug_loc || do_debug_ranges)
1120       && num_debug_info_entries == 0)
1121     {
1122       switch (attribute)
1123         {
1124         case DW_AT_frame_base:
1125           have_frame_base = 1;
1126         case DW_AT_location:
1127         case DW_AT_data_member_location:
1128         case DW_AT_vtable_elem_location:
1129         case DW_AT_allocated:
1130         case DW_AT_associated:
1131         case DW_AT_data_location:
1132         case DW_AT_stride:
1133         case DW_AT_upper_bound:
1134         case DW_AT_lower_bound:
1135           if (form == DW_FORM_data4 || form == DW_FORM_data8)
1136             {
1137               /* Process location list.  */
1138               unsigned int max = debug_info_p->max_loc_offsets;
1139               unsigned int num = debug_info_p->num_loc_offsets;
1140
1141               if (max == 0 || num >= max)
1142                 {
1143                   max += 1024;
1144                   debug_info_p->loc_offsets
1145                     = xcrealloc (debug_info_p->loc_offsets,
1146                                  max, sizeof (*debug_info_p->loc_offsets));
1147                   debug_info_p->have_frame_base
1148                     = xcrealloc (debug_info_p->have_frame_base,
1149                                  max, sizeof (*debug_info_p->have_frame_base));
1150                   debug_info_p->max_loc_offsets = max;
1151                 }
1152               debug_info_p->loc_offsets [num] = uvalue;
1153               debug_info_p->have_frame_base [num] = have_frame_base;
1154               debug_info_p->num_loc_offsets++;
1155             }
1156           break;
1157         
1158         case DW_AT_low_pc:
1159           if (need_base_address)
1160             debug_info_p->base_address = uvalue;
1161           break;
1162
1163         case DW_AT_ranges:
1164           if (form == DW_FORM_data4 || form == DW_FORM_data8)
1165             {
1166               /* Process range list.  */
1167               unsigned int max = debug_info_p->max_range_lists;
1168               unsigned int num = debug_info_p->num_range_lists;
1169
1170               if (max == 0 || num >= max)
1171                 {
1172                   max += 1024;
1173                   debug_info_p->range_lists
1174                     = xcrealloc (debug_info_p->range_lists,
1175                                  max, sizeof (*debug_info_p->range_lists));
1176                   debug_info_p->max_range_lists = max;
1177                 }
1178               debug_info_p->range_lists [num] = uvalue;
1179               debug_info_p->num_range_lists++;
1180             }
1181           break;
1182
1183         default:
1184           break;
1185         }
1186     }
1187
1188   if (do_loc)
1189     return data;
1190
1191   printf ("\t");
1192
1193   switch (attribute)
1194     {
1195     case DW_AT_inline:
1196       switch (uvalue)
1197         {
1198         case DW_INL_not_inlined:
1199           printf (_("(not inlined)"));
1200           break;
1201         case DW_INL_inlined:
1202           printf (_("(inlined)"));
1203           break;
1204         case DW_INL_declared_not_inlined:
1205           printf (_("(declared as inline but ignored)"));
1206           break;
1207         case DW_INL_declared_inlined:
1208           printf (_("(declared as inline and inlined)"));
1209           break;
1210         default:
1211           printf (_("  (Unknown inline attribute value: %lx)"), uvalue);
1212           break;
1213         }
1214       break;
1215
1216     case DW_AT_language:
1217       switch (uvalue)
1218         {
1219           /* Ordered by the numeric value of these constants.  */
1220         case DW_LANG_C89:               printf ("(ANSI C)"); break;
1221         case DW_LANG_C:                 printf ("(non-ANSI C)"); break;
1222         case DW_LANG_Ada83:             printf ("(Ada)"); break;
1223         case DW_LANG_C_plus_plus:       printf ("(C++)"); break;
1224         case DW_LANG_Cobol74:           printf ("(Cobol 74)"); break;
1225         case DW_LANG_Cobol85:           printf ("(Cobol 85)"); break;
1226         case DW_LANG_Fortran77:         printf ("(FORTRAN 77)"); break;
1227         case DW_LANG_Fortran90:         printf ("(Fortran 90)"); break;
1228         case DW_LANG_Pascal83:          printf ("(ANSI Pascal)"); break;
1229         case DW_LANG_Modula2:           printf ("(Modula 2)"); break;
1230           /* DWARF 2.1 values.  */
1231         case DW_LANG_Java:              printf ("(Java)"); break;
1232         case DW_LANG_C99:               printf ("(ANSI C99)"); break;
1233         case DW_LANG_Ada95:             printf ("(ADA 95)"); break;
1234         case DW_LANG_Fortran95:         printf ("(Fortran 95)"); break;
1235           /* DWARF 3 values.  */
1236         case DW_LANG_PLI:               printf ("(PLI)"); break;
1237         case DW_LANG_ObjC:              printf ("(Objective C)"); break;
1238         case DW_LANG_ObjC_plus_plus:    printf ("(Objective C++)"); break;
1239         case DW_LANG_UPC:               printf ("(Unified Parallel C)"); break;
1240         case DW_LANG_D:                 printf ("(D)"); break;
1241           /* MIPS extension.  */
1242         case DW_LANG_Mips_Assembler:    printf ("(MIPS assembler)"); break;
1243           /* UPC extension.  */
1244         case DW_LANG_Upc:               printf ("(Unified Parallel C)"); break;
1245         default:
1246           if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1247             printf ("(implementation defined: %lx)", uvalue);
1248           else
1249             printf ("(Unknown: %lx)", uvalue);
1250           break;
1251         }
1252       break;
1253
1254     case DW_AT_encoding:
1255       switch (uvalue)
1256         {
1257         case DW_ATE_void:               printf ("(void)"); break;
1258         case DW_ATE_address:            printf ("(machine address)"); break;
1259         case DW_ATE_boolean:            printf ("(boolean)"); break;
1260         case DW_ATE_complex_float:      printf ("(complex float)"); break;
1261         case DW_ATE_float:              printf ("(float)"); break;
1262         case DW_ATE_signed:             printf ("(signed)"); break;
1263         case DW_ATE_signed_char:        printf ("(signed char)"); break;
1264         case DW_ATE_unsigned:           printf ("(unsigned)"); break;
1265         case DW_ATE_unsigned_char:      printf ("(unsigned char)"); break;
1266           /* DWARF 2.1 value.  */
1267         case DW_ATE_imaginary_float:    printf ("(imaginary float)"); break;
1268         case DW_ATE_decimal_float:      printf ("(decimal float)"); break;
1269         default:
1270           if (uvalue >= DW_ATE_lo_user
1271               && uvalue <= DW_ATE_hi_user)
1272             printf ("(user defined type)");
1273           else
1274             printf ("(unknown type)");
1275           break;
1276         }
1277       break;
1278
1279     case DW_AT_accessibility:
1280       switch (uvalue)
1281         {
1282         case DW_ACCESS_public:          printf ("(public)"); break;
1283         case DW_ACCESS_protected:       printf ("(protected)"); break;
1284         case DW_ACCESS_private:         printf ("(private)"); break;
1285         default:
1286           printf ("(unknown accessibility)");
1287           break;
1288         }
1289       break;
1290
1291     case DW_AT_visibility:
1292       switch (uvalue)
1293         {
1294         case DW_VIS_local:              printf ("(local)"); break;
1295         case DW_VIS_exported:           printf ("(exported)"); break;
1296         case DW_VIS_qualified:          printf ("(qualified)"); break;
1297         default:                        printf ("(unknown visibility)"); break;
1298         }
1299       break;
1300
1301     case DW_AT_virtuality:
1302       switch (uvalue)
1303         {
1304         case DW_VIRTUALITY_none:        printf ("(none)"); break;
1305         case DW_VIRTUALITY_virtual:     printf ("(virtual)"); break;
1306         case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1307         default:                        printf ("(unknown virtuality)"); break;
1308         }
1309       break;
1310
1311     case DW_AT_identifier_case:
1312       switch (uvalue)
1313         {
1314         case DW_ID_case_sensitive:      printf ("(case_sensitive)"); break;
1315         case DW_ID_up_case:             printf ("(up_case)"); break;
1316         case DW_ID_down_case:           printf ("(down_case)"); break;
1317         case DW_ID_case_insensitive:    printf ("(case_insensitive)"); break;
1318         default:                        printf ("(unknown case)"); break;
1319         }
1320       break;
1321
1322     case DW_AT_calling_convention:
1323       switch (uvalue)
1324         {
1325         case DW_CC_normal:      printf ("(normal)"); break;
1326         case DW_CC_program:     printf ("(program)"); break;
1327         case DW_CC_nocall:      printf ("(nocall)"); break;
1328         default:
1329           if (uvalue >= DW_CC_lo_user
1330               && uvalue <= DW_CC_hi_user)
1331             printf ("(user defined)");
1332           else
1333             printf ("(unknown convention)");
1334         }
1335       break;
1336
1337     case DW_AT_ordering:
1338       switch (uvalue)
1339         {
1340         case -1: printf ("(undefined)"); break;
1341         case 0:  printf ("(row major)"); break;
1342         case 1:  printf ("(column major)"); break;
1343         }
1344       break;
1345
1346     case DW_AT_frame_base:
1347       have_frame_base = 1;
1348     case DW_AT_location:
1349     case DW_AT_data_member_location:
1350     case DW_AT_vtable_elem_location:
1351     case DW_AT_allocated:
1352     case DW_AT_associated:
1353     case DW_AT_data_location:
1354     case DW_AT_stride:
1355     case DW_AT_upper_bound:
1356     case DW_AT_lower_bound:
1357       if (block_start)
1358         {
1359           int need_frame_base;
1360
1361           printf ("(");
1362           need_frame_base = decode_location_expression (block_start,
1363                                                         pointer_size,
1364                                                         uvalue,
1365                                                         cu_offset);
1366           printf (")");
1367           if (need_frame_base && !have_frame_base)
1368             printf (_(" [without DW_AT_frame_base]"));
1369         }
1370       else if (form == DW_FORM_data4 || form == DW_FORM_data8)
1371         printf (_("(location list)"));
1372
1373       break;
1374
1375     default:
1376       break;
1377     }
1378
1379   return data;
1380 }
1381
1382 static char *
1383 get_AT_name (unsigned long attribute)
1384 {
1385   switch (attribute)
1386     {
1387     case DW_AT_sibling:                 return "DW_AT_sibling";
1388     case DW_AT_location:                return "DW_AT_location";
1389     case DW_AT_name:                    return "DW_AT_name";
1390     case DW_AT_ordering:                return "DW_AT_ordering";
1391     case DW_AT_subscr_data:             return "DW_AT_subscr_data";
1392     case DW_AT_byte_size:               return "DW_AT_byte_size";
1393     case DW_AT_bit_offset:              return "DW_AT_bit_offset";
1394     case DW_AT_bit_size:                return "DW_AT_bit_size";
1395     case DW_AT_element_list:            return "DW_AT_element_list";
1396     case DW_AT_stmt_list:               return "DW_AT_stmt_list";
1397     case DW_AT_low_pc:                  return "DW_AT_low_pc";
1398     case DW_AT_high_pc:                 return "DW_AT_high_pc";
1399     case DW_AT_language:                return "DW_AT_language";
1400     case DW_AT_member:                  return "DW_AT_member";
1401     case DW_AT_discr:                   return "DW_AT_discr";
1402     case DW_AT_discr_value:             return "DW_AT_discr_value";
1403     case DW_AT_visibility:              return "DW_AT_visibility";
1404     case DW_AT_import:                  return "DW_AT_import";
1405     case DW_AT_string_length:           return "DW_AT_string_length";
1406     case DW_AT_common_reference:        return "DW_AT_common_reference";
1407     case DW_AT_comp_dir:                return "DW_AT_comp_dir";
1408     case DW_AT_const_value:             return "DW_AT_const_value";
1409     case DW_AT_containing_type:         return "DW_AT_containing_type";
1410     case DW_AT_default_value:           return "DW_AT_default_value";
1411     case DW_AT_inline:                  return "DW_AT_inline";
1412     case DW_AT_is_optional:             return "DW_AT_is_optional";
1413     case DW_AT_lower_bound:             return "DW_AT_lower_bound";
1414     case DW_AT_producer:                return "DW_AT_producer";
1415     case DW_AT_prototyped:              return "DW_AT_prototyped";
1416     case DW_AT_return_addr:             return "DW_AT_return_addr";
1417     case DW_AT_start_scope:             return "DW_AT_start_scope";
1418     case DW_AT_stride_size:             return "DW_AT_stride_size";
1419     case DW_AT_upper_bound:             return "DW_AT_upper_bound";
1420     case DW_AT_abstract_origin:         return "DW_AT_abstract_origin";
1421     case DW_AT_accessibility:           return "DW_AT_accessibility";
1422     case DW_AT_address_class:           return "DW_AT_address_class";
1423     case DW_AT_artificial:              return "DW_AT_artificial";
1424     case DW_AT_base_types:              return "DW_AT_base_types";
1425     case DW_AT_calling_convention:      return "DW_AT_calling_convention";
1426     case DW_AT_count:                   return "DW_AT_count";
1427     case DW_AT_data_member_location:    return "DW_AT_data_member_location";
1428     case DW_AT_decl_column:             return "DW_AT_decl_column";
1429     case DW_AT_decl_file:               return "DW_AT_decl_file";
1430     case DW_AT_decl_line:               return "DW_AT_decl_line";
1431     case DW_AT_declaration:             return "DW_AT_declaration";
1432     case DW_AT_discr_list:              return "DW_AT_discr_list";
1433     case DW_AT_encoding:                return "DW_AT_encoding";
1434     case DW_AT_external:                return "DW_AT_external";
1435     case DW_AT_frame_base:              return "DW_AT_frame_base";
1436     case DW_AT_friend:                  return "DW_AT_friend";
1437     case DW_AT_identifier_case:         return "DW_AT_identifier_case";
1438     case DW_AT_macro_info:              return "DW_AT_macro_info";
1439     case DW_AT_namelist_items:          return "DW_AT_namelist_items";
1440     case DW_AT_priority:                return "DW_AT_priority";
1441     case DW_AT_segment:                 return "DW_AT_segment";
1442     case DW_AT_specification:           return "DW_AT_specification";
1443     case DW_AT_static_link:             return "DW_AT_static_link";
1444     case DW_AT_type:                    return "DW_AT_type";
1445     case DW_AT_use_location:            return "DW_AT_use_location";
1446     case DW_AT_variable_parameter:      return "DW_AT_variable_parameter";
1447     case DW_AT_virtuality:              return "DW_AT_virtuality";
1448     case DW_AT_vtable_elem_location:    return "DW_AT_vtable_elem_location";
1449       /* DWARF 2.1 values.  */
1450     case DW_AT_allocated:               return "DW_AT_allocated";
1451     case DW_AT_associated:              return "DW_AT_associated";
1452     case DW_AT_data_location:           return "DW_AT_data_location";
1453     case DW_AT_stride:                  return "DW_AT_stride";
1454     case DW_AT_entry_pc:                return "DW_AT_entry_pc";
1455     case DW_AT_use_UTF8:                return "DW_AT_use_UTF8";
1456     case DW_AT_extension:               return "DW_AT_extension";
1457     case DW_AT_ranges:                  return "DW_AT_ranges";
1458     case DW_AT_trampoline:              return "DW_AT_trampoline";
1459     case DW_AT_call_column:             return "DW_AT_call_column";
1460     case DW_AT_call_file:               return "DW_AT_call_file";
1461     case DW_AT_call_line:               return "DW_AT_call_line";
1462       /* SGI/MIPS extensions.  */
1463     case DW_AT_MIPS_fde:                return "DW_AT_MIPS_fde";
1464     case DW_AT_MIPS_loop_begin:         return "DW_AT_MIPS_loop_begin";
1465     case DW_AT_MIPS_tail_loop_begin:    return "DW_AT_MIPS_tail_loop_begin";
1466     case DW_AT_MIPS_epilog_begin:       return "DW_AT_MIPS_epilog_begin";
1467     case DW_AT_MIPS_loop_unroll_factor: return "DW_AT_MIPS_loop_unroll_factor";
1468     case DW_AT_MIPS_software_pipeline_depth:
1469       return "DW_AT_MIPS_software_pipeline_depth";
1470     case DW_AT_MIPS_linkage_name:       return "DW_AT_MIPS_linkage_name";
1471     case DW_AT_MIPS_stride:             return "DW_AT_MIPS_stride";
1472     case DW_AT_MIPS_abstract_name:      return "DW_AT_MIPS_abstract_name";
1473     case DW_AT_MIPS_clone_origin:       return "DW_AT_MIPS_clone_origin";
1474     case DW_AT_MIPS_has_inlines:        return "DW_AT_MIPS_has_inlines";
1475       /* GNU extensions.  */
1476     case DW_AT_sf_names:                return "DW_AT_sf_names";
1477     case DW_AT_src_info:                return "DW_AT_src_info";
1478     case DW_AT_mac_info:                return "DW_AT_mac_info";
1479     case DW_AT_src_coords:              return "DW_AT_src_coords";
1480     case DW_AT_body_begin:              return "DW_AT_body_begin";
1481     case DW_AT_body_end:                return "DW_AT_body_end";
1482     case DW_AT_GNU_vector:              return "DW_AT_GNU_vector";
1483       /* UPC extension.  */
1484     case DW_AT_upc_threads_scaled:      return "DW_AT_upc_threads_scaled";
1485     default:
1486       {
1487         static char buffer[100];
1488
1489         snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1490                   attribute);
1491         return buffer;
1492       }
1493     }
1494 }
1495
1496 static unsigned char *
1497 read_and_display_attr (unsigned long attribute,
1498                        unsigned long form,
1499                        unsigned char *data,
1500                        unsigned long cu_offset,
1501                        unsigned long pointer_size,
1502                        unsigned long offset_size,
1503                        int dwarf_version,
1504                        debug_info *debug_info_p,
1505                        int do_loc)
1506 {
1507   if (!do_loc)
1508     printf ("   %-18s:", get_AT_name (attribute));
1509   data = read_and_display_attr_value (attribute, form, data, cu_offset,
1510                                       pointer_size, offset_size,
1511                                       dwarf_version, debug_info_p,
1512                                       do_loc);
1513   if (!do_loc)
1514     printf ("\n");
1515   return data;
1516 }
1517
1518
1519 /* Process the contents of a .debug_info section.  If do_loc is non-zero
1520    then we are scanning for location lists and we do not want to display
1521    anything to the user.  */
1522
1523 static int
1524 process_debug_info (struct dwarf_section *section, void *file,
1525                     int do_loc)
1526 {
1527   unsigned char *start = section->start;
1528   unsigned char *end = start + section->size;
1529   unsigned char *section_begin;
1530   unsigned int unit;
1531   unsigned int num_units = 0;
1532
1533   if ((do_loc || do_debug_loc || do_debug_ranges)
1534       && num_debug_info_entries == 0)
1535     {
1536       unsigned long length;
1537
1538       /* First scan the section to get the number of comp units.  */
1539       for (section_begin = start, num_units = 0; section_begin < end;
1540            num_units ++)
1541         {
1542           /* Read the first 4 bytes.  For a 32-bit DWARF section, this
1543              will be the length.  For a 64-bit DWARF section, it'll be
1544              the escape code 0xffffffff followed by an 8 byte length.  */
1545           length = byte_get (section_begin, 4);
1546
1547           if (length == 0xffffffff)
1548             {
1549               length = byte_get (section_begin + 4, 8);
1550               section_begin += length + 12;
1551             }
1552           else
1553             section_begin += length + 4;
1554
1555           /* Negative values are illegal, they may even cause infinite
1556              looping.  This can happen if we can't accurately apply
1557              relocations to an object file.  */
1558           if ((signed long) length <= 0)
1559             {
1560               warn (_("Corrupt unit length (%lx) found in section %s\n"), length, section->name);
1561               return 0;
1562             }
1563         }
1564
1565       if (num_units == 0)
1566         {
1567           error (_("No comp units in %s section ?"), section->name);
1568           return 0;
1569         }
1570
1571       /* Then allocate an array to hold the information.  */
1572       debug_information = cmalloc (num_units,
1573                                    sizeof (* debug_information));
1574       if (debug_information == NULL)
1575         {
1576           error (_("Not enough memory for a debug info array of %u entries"),
1577                  num_units);
1578           return 0;
1579         }
1580     }
1581
1582   if (!do_loc)
1583     {
1584       printf (_("The section %s contains:\n\n"), section->name);
1585
1586       load_debug_section (str, file);
1587     }
1588
1589   load_debug_section (abbrev, file);
1590   if (debug_displays [abbrev].section.start == NULL)
1591     {
1592       warn (_("Unable to locate %s section!\n"),
1593             debug_displays [abbrev].section.name);
1594       return 0;
1595     }
1596
1597   for (section_begin = start, unit = 0; start < end; unit++)
1598     {
1599       DWARF2_Internal_CompUnit compunit;
1600       unsigned char *hdrptr;
1601       unsigned char *cu_abbrev_offset_ptr;
1602       unsigned char *tags;
1603       int level;
1604       unsigned long cu_offset;
1605       int offset_size;
1606       int initial_length_size;
1607
1608       hdrptr = start;
1609
1610       compunit.cu_length = byte_get (hdrptr, 4);
1611       hdrptr += 4;
1612
1613       if (compunit.cu_length == 0xffffffff)
1614         {
1615           compunit.cu_length = byte_get (hdrptr, 8);
1616           hdrptr += 8;
1617           offset_size = 8;
1618           initial_length_size = 12;
1619         }
1620       else
1621         {
1622           offset_size = 4;
1623           initial_length_size = 4;
1624         }
1625
1626       compunit.cu_version = byte_get (hdrptr, 2);
1627       hdrptr += 2;
1628
1629       cu_offset = start - section_begin;
1630
1631       cu_abbrev_offset_ptr = hdrptr;
1632       compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
1633       hdrptr += offset_size;
1634
1635       compunit.cu_pointer_size = byte_get (hdrptr, 1);
1636       hdrptr += 1;
1637       if ((do_loc || do_debug_loc || do_debug_ranges)
1638           && num_debug_info_entries == 0)
1639         {
1640           debug_information [unit].cu_offset = cu_offset;
1641           debug_information [unit].pointer_size
1642             = compunit.cu_pointer_size;
1643           debug_information [unit].base_address = 0;
1644           debug_information [unit].loc_offsets = NULL;
1645           debug_information [unit].have_frame_base = NULL;
1646           debug_information [unit].max_loc_offsets = 0;
1647           debug_information [unit].num_loc_offsets = 0;
1648           debug_information [unit].range_lists = NULL;
1649           debug_information [unit].max_range_lists= 0;
1650           debug_information [unit].num_range_lists = 0;
1651         }
1652
1653       if (!do_loc)
1654         {
1655           printf (_("  Compilation Unit @ offset 0x%lx:\n"), cu_offset);
1656           printf (_("   Length:        %ld\n"), compunit.cu_length);
1657           printf (_("   Version:       %d\n"), compunit.cu_version);
1658           printf (_("   Abbrev Offset: %ld\n"), compunit.cu_abbrev_offset);
1659           printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
1660         }
1661
1662       if (cu_offset + compunit.cu_length + initial_length_size
1663           > section->size)
1664         {
1665           warn (_("Debug info is corrupted, length is invalid (section is %lu bytes)\n"),
1666                 (unsigned long)section->size);
1667           break;
1668         }
1669       tags = hdrptr;
1670       start += compunit.cu_length + initial_length_size;
1671
1672       if (compunit.cu_version != 2 && compunit.cu_version != 3)
1673         {
1674           warn (_("Only version 2 and 3 DWARF debug information is currently supported.\n"));
1675           continue;
1676         }
1677
1678       free_abbrevs ();
1679
1680       /* Process the abbrevs used by this compilation unit. DWARF
1681          sections under Mach-O have non-zero addresses.  */
1682       if (compunit.cu_abbrev_offset >= debug_displays [abbrev].section.size)
1683         warn (_("Debug info is corrupted, abbrev offset is invalid (section is %lu bytes)\n"),
1684               (unsigned long)debug_displays [abbrev].section.size);
1685       else
1686         process_abbrev_section
1687           ((unsigned char *) debug_displays [abbrev].section.start
1688            + compunit.cu_abbrev_offset - debug_displays [abbrev].section.address,
1689            (unsigned char *) debug_displays [abbrev].section.start
1690            + debug_displays [abbrev].section.size);
1691
1692       level = 0;
1693       while (tags < start)
1694         {
1695           unsigned int bytes_read;
1696           unsigned long abbrev_number;
1697           abbrev_entry *entry;
1698           abbrev_attr *attr;
1699
1700           abbrev_number = read_leb128 (tags, & bytes_read, 0);
1701           tags += bytes_read;
1702
1703           /* A null DIE marks the end of a list of children.  */
1704           if (abbrev_number == 0)
1705             {
1706               --level;
1707               continue;
1708             }
1709
1710           if (!do_loc)
1711             printf (_(" <%d><%lx>: Abbrev Number: %lu"),
1712                     level,
1713                     (unsigned long) (tags - section_begin
1714                                      - bytes_read),
1715                     abbrev_number);
1716  
1717           /* Scan through the abbreviation list until we reach the
1718              correct entry.  */
1719           for (entry = first_abbrev;
1720                entry && entry->entry != abbrev_number;
1721                entry = entry->next)
1722             continue;
1723
1724           if (entry == NULL)
1725             {
1726               if (!do_loc)
1727                 {
1728                   printf ("\n");
1729                   fflush (stdout);
1730                 }
1731               warn (_("Unable to locate entry %lu in the abbreviation table\n"),
1732                     abbrev_number);
1733               return 0;
1734             }
1735
1736           if (!do_loc)
1737             printf (_(" (%s)\n"), get_TAG_name (entry->tag));
1738  
1739           switch (entry->tag)
1740             {
1741             default:
1742               need_base_address = 0;
1743               break;
1744             case DW_TAG_compile_unit:
1745               need_base_address = 1;
1746               break;
1747             case DW_TAG_entry_point:
1748             case DW_TAG_subprogram:
1749               need_base_address = 0;
1750               /* Assuming that there is no DW_AT_frame_base.  */
1751               have_frame_base = 0;
1752               break;
1753             }
1754
1755           for (attr = entry->first_attr; attr; attr = attr->next)
1756             {
1757               if (! do_loc)
1758                 /* Show the offset from where the tag was extracted.  */
1759                 printf ("    <%2lx>", (unsigned long)(tags - section_begin));
1760
1761               tags = read_and_display_attr (attr->attribute,
1762                                             attr->form,
1763                                             tags, cu_offset,
1764                                             compunit.cu_pointer_size,
1765                                             offset_size,
1766                                             compunit.cu_version,
1767                                             &debug_information [unit],
1768                                             do_loc);
1769             }
1770  
1771           if (entry->children)
1772             ++level;
1773         }
1774     }
1775  
1776   /* Set num_debug_info_entries here so that it can be used to check if
1777      we need to process .debug_loc and .debug_ranges sections.  */
1778   if ((do_loc || do_debug_loc || do_debug_ranges)
1779       && num_debug_info_entries == 0)
1780     num_debug_info_entries = num_units;
1781       
1782   if (!do_loc)
1783     {
1784       printf ("\n");
1785     }
1786  
1787   return 1;
1788 }
1789
1790 /* Locate and scan the .debug_info section in the file and record the pointer
1791    sizes and offsets for the compilation units in it.  Usually an executable
1792    will have just one pointer size, but this is not guaranteed, and so we try
1793    not to make any assumptions.  Returns zero upon failure, or the number of
1794    compilation units upon success.  */
1795
1796 static unsigned int
1797 load_debug_info (void * file)
1798 {
1799   /* Reset the last pointer size so that we can issue correct error
1800      messages if we are displaying the contents of more than one section.  */
1801   last_pointer_size = 0;
1802   warned_about_missing_comp_units = FALSE;
1803
1804   /* If we already have the information there is nothing else to do.  */
1805   if (num_debug_info_entries > 0)
1806     return num_debug_info_entries;
1807
1808   if (load_debug_section (info, file)
1809       && process_debug_info (&debug_displays [info].section, file, 1))
1810     return num_debug_info_entries;
1811   else
1812     return 0;
1813 }
1814
1815 static int
1816 display_debug_lines (struct dwarf_section *section, void *file)
1817 {
1818   unsigned char *start = section->start;
1819   unsigned char *data = start;
1820   unsigned char *end = start + section->size;
1821
1822   printf (_("\nDump of debug contents of section %s:\n\n"),
1823           section->name);
1824
1825   load_debug_info (file);
1826
1827   while (data < end)
1828     {
1829       DWARF2_Internal_LineInfo info;
1830       unsigned char *standard_opcodes;
1831       unsigned char *end_of_sequence;
1832       unsigned char *hdrptr;
1833       unsigned long hdroff;
1834       int initial_length_size;
1835       int offset_size;
1836       int i;
1837
1838       hdrptr = data;
1839       hdroff = hdrptr - start;
1840
1841       /* Check the length of the block.  */
1842       info.li_length = byte_get (hdrptr, 4);
1843       hdrptr += 4;
1844
1845       if (info.li_length == 0xffffffff)
1846         {
1847           /* This section is 64-bit DWARF 3.  */
1848           info.li_length = byte_get (hdrptr, 8);
1849           hdrptr += 8;
1850           offset_size = 8;
1851           initial_length_size = 12;
1852         }
1853       else
1854         {
1855           offset_size = 4;
1856           initial_length_size = 4;
1857         }
1858
1859       if (info.li_length + initial_length_size > section->size)
1860         {
1861           warn
1862             (_("The line info appears to be corrupt - the section is too small\n"));
1863           return 0;
1864         }
1865
1866       /* Check its version number.  */
1867       info.li_version = byte_get (hdrptr, 2);
1868       hdrptr += 2;
1869       if (info.li_version != 2 && info.li_version != 3)
1870         {
1871           warn (_("Only DWARF version 2 and 3 line info is currently supported.\n"));
1872           return 0;
1873         }
1874
1875       info.li_prologue_length = byte_get (hdrptr, offset_size);
1876       hdrptr += offset_size;
1877       info.li_min_insn_length = byte_get (hdrptr, 1);
1878       hdrptr++;
1879       info.li_default_is_stmt = byte_get (hdrptr, 1);
1880       hdrptr++;
1881       info.li_line_base = byte_get (hdrptr, 1);
1882       hdrptr++;
1883       info.li_line_range = byte_get (hdrptr, 1);
1884       hdrptr++;
1885       info.li_opcode_base = byte_get (hdrptr, 1);
1886       hdrptr++;
1887
1888       /* Sign extend the line base field.  */
1889       info.li_line_base <<= 24;
1890       info.li_line_base >>= 24;
1891
1892       printf (_("  Offset:                      0x%lx\n"), hdroff);
1893       printf (_("  Length:                      %ld\n"), info.li_length);
1894       printf (_("  DWARF Version:               %d\n"), info.li_version);
1895       printf (_("  Prologue Length:             %d\n"), info.li_prologue_length);
1896       printf (_("  Minimum Instruction Length:  %d\n"), info.li_min_insn_length);
1897       printf (_("  Initial value of 'is_stmt':  %d\n"), info.li_default_is_stmt);
1898       printf (_("  Line Base:                   %d\n"), info.li_line_base);
1899       printf (_("  Line Range:                  %d\n"), info.li_line_range);
1900       printf (_("  Opcode Base:                 %d\n"), info.li_opcode_base);
1901
1902       end_of_sequence = data + info.li_length + initial_length_size;
1903
1904       reset_state_machine (info.li_default_is_stmt);
1905
1906       /* Display the contents of the Opcodes table.  */
1907       standard_opcodes = hdrptr;
1908
1909       printf (_("\n Opcodes:\n"));
1910
1911       for (i = 1; i < info.li_opcode_base; i++)
1912         printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
1913
1914       /* Display the contents of the Directory table.  */
1915       data = standard_opcodes + info.li_opcode_base - 1;
1916
1917       if (*data == 0)
1918         printf (_("\n The Directory Table is empty.\n"));
1919       else
1920         {
1921           printf (_("\n The Directory Table:\n"));
1922
1923           while (*data != 0)
1924             {
1925               printf (_("  %s\n"), data);
1926
1927               data += strlen ((char *) data) + 1;
1928             }
1929         }
1930
1931       /* Skip the NUL at the end of the table.  */
1932       data++;
1933
1934       /* Display the contents of the File Name table.  */
1935       if (*data == 0)
1936         printf (_("\n The File Name Table is empty.\n"));
1937       else
1938         {
1939           printf (_("\n The File Name Table:\n"));
1940           printf (_("  Entry\tDir\tTime\tSize\tName\n"));
1941
1942           while (*data != 0)
1943             {
1944               unsigned char *name;
1945               unsigned int bytes_read;
1946
1947               printf (_("  %d\t"), ++state_machine_regs.last_file_entry);
1948               name = data;
1949
1950               data += strlen ((char *) data) + 1;
1951
1952               printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
1953               data += bytes_read;
1954               printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
1955               data += bytes_read;
1956               printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
1957               data += bytes_read;
1958               printf (_("%s\n"), name);
1959             }
1960         }
1961
1962       /* Skip the NUL at the end of the table.  */
1963       data++;
1964
1965       /* Now display the statements.  */
1966       printf (_("\n Line Number Statements:\n"));
1967
1968       while (data < end_of_sequence)
1969         {
1970           unsigned char op_code;
1971           int adv;
1972           unsigned long int uladv;
1973           unsigned int bytes_read;
1974
1975           op_code = *data++;
1976
1977           if (op_code >= info.li_opcode_base)
1978             {
1979               op_code -= info.li_opcode_base;
1980               uladv = (op_code / info.li_line_range) * info.li_min_insn_length;
1981               state_machine_regs.address += uladv;
1982               printf (_("  Special opcode %d: advance Address by %lu to 0x%lx"),
1983                       op_code, uladv, state_machine_regs.address);
1984               adv = (op_code % info.li_line_range) + info.li_line_base;
1985               state_machine_regs.line += adv;
1986               printf (_(" and Line by %d to %d\n"),
1987                       adv, state_machine_regs.line);
1988             }
1989           else switch (op_code)
1990             {
1991             case DW_LNS_extended_op:
1992               data += process_extended_line_op (data, info.li_default_is_stmt);
1993               break;
1994
1995             case DW_LNS_copy:
1996               printf (_("  Copy\n"));
1997               break;
1998
1999             case DW_LNS_advance_pc:
2000               uladv = read_leb128 (data, & bytes_read, 0);
2001               uladv *= info.li_min_insn_length;
2002               data += bytes_read;
2003               state_machine_regs.address += uladv;
2004               printf (_("  Advance PC by %lu to 0x%lx\n"), uladv,
2005                       state_machine_regs.address);
2006               break;
2007
2008             case DW_LNS_advance_line:
2009               adv = read_leb128 (data, & bytes_read, 1);
2010               data += bytes_read;
2011               state_machine_regs.line += adv;
2012               printf (_("  Advance Line by %d to %d\n"), adv,
2013                       state_machine_regs.line);
2014               break;
2015
2016             case DW_LNS_set_file:
2017               adv = read_leb128 (data, & bytes_read, 0);
2018               data += bytes_read;
2019               printf (_("  Set File Name to entry %d in the File Name Table\n"),
2020                       adv);
2021               state_machine_regs.file = adv;
2022               break;
2023
2024             case DW_LNS_set_column:
2025               uladv = read_leb128 (data, & bytes_read, 0);
2026               data += bytes_read;
2027               printf (_("  Set column to %lu\n"), uladv);
2028               state_machine_regs.column = uladv;
2029               break;
2030
2031             case DW_LNS_negate_stmt:
2032               adv = state_machine_regs.is_stmt;
2033               adv = ! adv;
2034               printf (_("  Set is_stmt to %d\n"), adv);
2035               state_machine_regs.is_stmt = adv;
2036               break;
2037
2038             case DW_LNS_set_basic_block:
2039               printf (_("  Set basic block\n"));
2040               state_machine_regs.basic_block = 1;
2041               break;
2042
2043             case DW_LNS_const_add_pc:
2044               uladv = (((255 - info.li_opcode_base) / info.li_line_range)
2045                       * info.li_min_insn_length);
2046               state_machine_regs.address += uladv;
2047               printf (_("  Advance PC by constant %lu to 0x%lx\n"), uladv,
2048                       state_machine_regs.address);
2049               break;
2050
2051             case DW_LNS_fixed_advance_pc:
2052               uladv = byte_get (data, 2);
2053               data += 2;
2054               state_machine_regs.address += uladv;
2055               printf (_("  Advance PC by fixed size amount %lu to 0x%lx\n"),
2056                       uladv, state_machine_regs.address);
2057               break;
2058
2059             case DW_LNS_set_prologue_end:
2060               printf (_("  Set prologue_end to true\n"));
2061               break;
2062
2063             case DW_LNS_set_epilogue_begin:
2064               printf (_("  Set epilogue_begin to true\n"));
2065               break;
2066
2067             case DW_LNS_set_isa:
2068               uladv = read_leb128 (data, & bytes_read, 0);
2069               data += bytes_read;
2070               printf (_("  Set ISA to %lu\n"), uladv);
2071               break;
2072
2073             default:
2074               printf (_("  Unknown opcode %d with operands: "), op_code);
2075
2076               for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2077                 {
2078                   printf ("0x%lx%s", read_leb128 (data, &bytes_read, 0),
2079                           i == 1 ? "" : ", ");
2080                   data += bytes_read;
2081                 }
2082               putchar ('\n');
2083               break;
2084             }
2085         }
2086       putchar ('\n');
2087     }
2088
2089   return 1;
2090 }
2091
2092 static int
2093 display_debug_pubnames (struct dwarf_section *section,
2094                         void *file ATTRIBUTE_UNUSED)
2095 {
2096   DWARF2_Internal_PubNames pubnames;
2097   unsigned char *start = section->start;
2098   unsigned char *end = start + section->size;
2099
2100   printf (_("Contents of the %s section:\n\n"), section->name);
2101
2102   while (start < end)
2103     {
2104       unsigned char *data;
2105       unsigned long offset;
2106       int offset_size, initial_length_size;
2107
2108       data = start;
2109
2110       pubnames.pn_length = byte_get (data, 4);
2111       data += 4;
2112       if (pubnames.pn_length == 0xffffffff)
2113         {
2114           pubnames.pn_length = byte_get (data, 8);
2115           data += 8;
2116           offset_size = 8;
2117           initial_length_size = 12;
2118         }
2119       else
2120         {
2121           offset_size = 4;
2122           initial_length_size = 4;
2123         }
2124
2125       pubnames.pn_version = byte_get (data, 2);
2126       data += 2;
2127       pubnames.pn_offset = byte_get (data, offset_size);
2128       data += offset_size;
2129       pubnames.pn_size = byte_get (data, offset_size);
2130       data += offset_size;
2131
2132       start += pubnames.pn_length + initial_length_size;
2133
2134       if (pubnames.pn_version != 2 && pubnames.pn_version != 3)
2135         {
2136           static int warned = 0;
2137
2138           if (! warned)
2139             {
2140               warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
2141               warned = 1;
2142             }
2143
2144           continue;
2145         }
2146
2147       printf (_("  Length:                              %ld\n"),
2148               pubnames.pn_length);
2149       printf (_("  Version:                             %d\n"),
2150               pubnames.pn_version);
2151       printf (_("  Offset into .debug_info section:     %ld\n"),
2152               pubnames.pn_offset);
2153       printf (_("  Size of area in .debug_info section: %ld\n"),
2154               pubnames.pn_size);
2155
2156       printf (_("\n    Offset\tName\n"));
2157
2158       do
2159         {
2160           offset = byte_get (data, offset_size);
2161
2162           if (offset != 0)
2163             {
2164               data += offset_size;
2165               printf ("    %-6ld\t\t%s\n", offset, data);
2166               data += strlen ((char *) data) + 1;
2167             }
2168         }
2169       while (offset != 0);
2170     }
2171
2172   printf ("\n");
2173   return 1;
2174 }
2175
2176 static int
2177 display_debug_macinfo (struct dwarf_section *section,
2178                        void *file ATTRIBUTE_UNUSED)
2179 {
2180   unsigned char *start = section->start;
2181   unsigned char *end = start + section->size;
2182   unsigned char *curr = start;
2183   unsigned int bytes_read;
2184   enum dwarf_macinfo_record_type op;
2185
2186   printf (_("Contents of the %s section:\n\n"), section->name);
2187
2188   while (curr < end)
2189     {
2190       unsigned int lineno;
2191       const char *string;
2192
2193       op = *curr;
2194       curr++;
2195
2196       switch (op)
2197         {
2198         case DW_MACINFO_start_file:
2199           {
2200             unsigned int filenum;
2201
2202             lineno = read_leb128 (curr, & bytes_read, 0);
2203             curr += bytes_read;
2204             filenum = read_leb128 (curr, & bytes_read, 0);
2205             curr += bytes_read;
2206
2207             printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
2208                     lineno, filenum);
2209           }
2210           break;
2211
2212         case DW_MACINFO_end_file:
2213           printf (_(" DW_MACINFO_end_file\n"));
2214           break;
2215
2216         case DW_MACINFO_define:
2217           lineno = read_leb128 (curr, & bytes_read, 0);
2218           curr += bytes_read;
2219           string = (char *) curr;
2220           curr += strlen (string) + 1;
2221           printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
2222                   lineno, string);
2223           break;
2224
2225         case DW_MACINFO_undef:
2226           lineno = read_leb128 (curr, & bytes_read, 0);
2227           curr += bytes_read;
2228           string = (char *) curr;
2229           curr += strlen (string) + 1;
2230           printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
2231                   lineno, string);
2232           break;
2233
2234         case DW_MACINFO_vendor_ext:
2235           {
2236             unsigned int constant;
2237
2238             constant = read_leb128 (curr, & bytes_read, 0);
2239             curr += bytes_read;
2240             string = (char *) curr;
2241             curr += strlen (string) + 1;
2242             printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
2243                     constant, string);
2244           }
2245           break;
2246         }
2247     }
2248
2249   return 1;
2250 }
2251
2252 static int
2253 display_debug_abbrev (struct dwarf_section *section,
2254                       void *file ATTRIBUTE_UNUSED)
2255 {
2256   abbrev_entry *entry;
2257   unsigned char *start = section->start;
2258   unsigned char *end = start + section->size;
2259
2260   printf (_("Contents of the %s section:\n\n"), section->name);
2261
2262   do
2263     {
2264       free_abbrevs ();
2265
2266       start = process_abbrev_section (start, end);
2267
2268       if (first_abbrev == NULL)
2269         continue;
2270
2271       printf (_("  Number TAG\n"));
2272
2273       for (entry = first_abbrev; entry; entry = entry->next)
2274         {
2275           abbrev_attr *attr;
2276
2277           printf (_("   %ld      %s    [%s]\n"),
2278                   entry->entry,
2279                   get_TAG_name (entry->tag),
2280                   entry->children ? _("has children") : _("no children"));
2281
2282           for (attr = entry->first_attr; attr; attr = attr->next)
2283             printf (_("    %-18s %s\n"),
2284                     get_AT_name (attr->attribute),
2285                     get_FORM_name (attr->form));
2286         }
2287     }
2288   while (start);
2289
2290   printf ("\n");
2291
2292   return 1;
2293 }
2294
2295 static int
2296 display_debug_loc (struct dwarf_section *section, void *file)
2297 {
2298   unsigned char *start = section->start;
2299   unsigned char *section_end;
2300   unsigned long bytes;
2301   unsigned char *section_begin = start;
2302   unsigned int num_loc_list = 0;
2303   unsigned long last_offset = 0;
2304   unsigned int first = 0;
2305   unsigned int i;
2306   unsigned int j;
2307   int seen_first_offset = 0;
2308   int use_debug_info = 1;
2309   unsigned char *next;
2310
2311   bytes = section->size;
2312   section_end = start + bytes;
2313
2314   if (bytes == 0)
2315     {
2316       printf (_("\nThe %s section is empty.\n"), section->name);
2317       return 0;
2318     }
2319
2320   load_debug_info (file);
2321
2322   /* Check the order of location list in .debug_info section. If
2323      offsets of location lists are in the ascending order, we can
2324      use `debug_information' directly.  */
2325   for (i = 0; i < num_debug_info_entries; i++)
2326     {
2327       unsigned int num;
2328
2329       num = debug_information [i].num_loc_offsets;
2330       num_loc_list += num;
2331
2332       /* Check if we can use `debug_information' directly.  */
2333       if (use_debug_info && num != 0)
2334         {
2335           if (!seen_first_offset)
2336             {
2337               /* This is the first location list.  */
2338               last_offset = debug_information [i].loc_offsets [0];
2339               first = i;
2340               seen_first_offset = 1;
2341               j = 1;
2342             }
2343           else
2344             j = 0;
2345
2346           for (; j < num; j++)
2347             {
2348               if (last_offset >
2349                   debug_information [i].loc_offsets [j])
2350                 {
2351                   use_debug_info = 0;
2352                   break;
2353                 }
2354               last_offset = debug_information [i].loc_offsets [j];
2355             }
2356         }
2357     }
2358
2359   if (!use_debug_info)
2360     /* FIXME: Should we handle this case?  */
2361     error (_("Location lists in .debug_info section aren't in ascending order!\n"));
2362
2363   if (!seen_first_offset)
2364     error (_("No location lists in .debug_info section!\n"));
2365
2366   /* DWARF sections under Mach-O have non-zero addresses.  */
2367   if (debug_information [first].num_loc_offsets > 0
2368       && debug_information [first].loc_offsets [0] != section->address)
2369     warn (_("Location lists in %s section start at 0x%lx\n"),
2370           section->name, debug_information [first].loc_offsets [0]);
2371
2372   printf (_("Contents of the %s section:\n\n"), section->name);
2373   printf (_("    Offset   Begin    End      Expression\n"));
2374
2375   seen_first_offset = 0;
2376   for (i = first; i < num_debug_info_entries; i++)
2377     {
2378       unsigned long begin;
2379       unsigned long end;
2380       unsigned short length;
2381       unsigned long offset;
2382       unsigned int pointer_size;
2383       unsigned long cu_offset;
2384       unsigned long base_address;
2385       int need_frame_base;
2386       int has_frame_base;
2387
2388       pointer_size = debug_information [i].pointer_size;
2389       cu_offset = debug_information [i].cu_offset;
2390
2391       for (j = 0; j < debug_information [i].num_loc_offsets; j++)
2392         {
2393           has_frame_base = debug_information [i].have_frame_base [j];
2394           /* DWARF sections under Mach-O have non-zero addresses.  */
2395           offset = debug_information [i].loc_offsets [j] - section->address; 
2396           next = section_begin + offset;
2397           base_address = debug_information [i].base_address;
2398
2399           if (!seen_first_offset)
2400             seen_first_offset = 1;
2401           else
2402             {
2403               if (start < next)
2404                 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
2405                       (long)(start - section_begin), (long)(next - section_begin));
2406               else if (start > next)
2407                 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
2408                       (long)(start - section_begin), (long)(next - section_begin));
2409             }
2410           start = next;
2411
2412           if (offset >= bytes)
2413             {
2414               warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
2415                     offset);
2416               continue;
2417             }
2418
2419           while (1)
2420             {
2421               if (start + 2 * pointer_size > section_end)
2422                 {
2423                   warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2424                         offset);
2425                   break;
2426                 }
2427
2428               begin = byte_get (start, pointer_size);
2429               start += pointer_size;
2430               end = byte_get (start, pointer_size);
2431               start += pointer_size;
2432
2433               if (begin == 0 && end == 0)
2434                 {
2435                   printf (_("    %8.8lx <End of list>\n"), offset);
2436                   break;
2437                 }
2438
2439               /* Check base address specifiers.  */
2440               if (begin == -1UL && end != -1UL)
2441                 {
2442                   base_address = end;
2443                   printf (_("    %8.8lx %8.8lx %8.8lx (base address)\n"),
2444                           offset, begin, end);
2445                   continue;
2446                 }
2447
2448               if (start + 2 > section_end)
2449                 {
2450                   warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2451                         offset);
2452                   break;
2453                 }
2454
2455               length = byte_get (start, 2);
2456               start += 2;
2457
2458               if (start + length > section_end)
2459                 {
2460                   warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2461                         offset);
2462                   break;
2463                 }
2464
2465               printf ("    %8.8lx %8.8lx %8.8lx (",
2466                       offset, begin + base_address, end + base_address);
2467               need_frame_base = decode_location_expression (start,
2468                                                             pointer_size,
2469                                                             length,
2470                                                             cu_offset);
2471               putchar (')');
2472
2473               if (need_frame_base && !has_frame_base)
2474                 printf (_(" [without DW_AT_frame_base]"));
2475
2476               if (begin == end)
2477                 fputs (_(" (start == end)"), stdout);
2478               else if (begin > end)
2479                 fputs (_(" (start > end)"), stdout);
2480
2481               putchar ('\n');
2482
2483               start += length;
2484             }
2485         }
2486     }
2487   return 1;
2488 }
2489
2490 static int
2491 display_debug_str (struct dwarf_section *section,
2492                    void *file ATTRIBUTE_UNUSED)
2493 {
2494   unsigned char *start = section->start;
2495   unsigned long bytes = section->size;
2496   dwarf_vma addr = section->address;
2497
2498   if (bytes == 0)
2499     {
2500       printf (_("\nThe %s section is empty.\n"), section->name);
2501       return 0;
2502     }
2503
2504   printf (_("Contents of the %s section:\n\n"), section->name);
2505
2506   while (bytes)
2507     {
2508       int j;
2509       int k;
2510       int lbytes;
2511
2512       lbytes = (bytes > 16 ? 16 : bytes);
2513
2514       printf ("  0x%8.8lx ", (unsigned long) addr);
2515
2516       for (j = 0; j < 16; j++)
2517         {
2518           if (j < lbytes)
2519             printf ("%2.2x", start[j]);
2520           else
2521             printf ("  ");
2522
2523           if ((j & 3) == 3)
2524             printf (" ");
2525         }
2526
2527       for (j = 0; j < lbytes; j++)
2528         {
2529           k = start[j];
2530           if (k >= ' ' && k < 0x80)
2531             printf ("%c", k);
2532           else
2533             printf (".");
2534         }
2535
2536       putchar ('\n');
2537
2538       start += lbytes;
2539       addr  += lbytes;
2540       bytes -= lbytes;
2541     }
2542
2543   putchar ('\n');
2544
2545   return 1;
2546 }
2547
2548 static int
2549 display_debug_info (struct dwarf_section *section, void *file)
2550 {
2551   return process_debug_info (section, file, 0);
2552 }
2553
2554
2555 static int
2556 display_debug_aranges (struct dwarf_section *section,
2557                        void *file ATTRIBUTE_UNUSED)
2558 {
2559   unsigned char *start = section->start;
2560   unsigned char *end = start + section->size;
2561
2562   printf (_("The section %s contains:\n\n"), section->name);
2563
2564   while (start < end)
2565     {
2566       unsigned char *hdrptr;
2567       DWARF2_Internal_ARange arange;
2568       unsigned char *ranges;
2569       unsigned long length;
2570       unsigned long address;
2571       unsigned char address_size;
2572       int excess;
2573       int offset_size;
2574       int initial_length_size;
2575
2576       hdrptr = start;
2577
2578       arange.ar_length = byte_get (hdrptr, 4);
2579       hdrptr += 4;
2580
2581       if (arange.ar_length == 0xffffffff)
2582         {
2583           arange.ar_length = byte_get (hdrptr, 8);
2584           hdrptr += 8;
2585           offset_size = 8;
2586           initial_length_size = 12;
2587         }
2588       else
2589         {
2590           offset_size = 4;
2591           initial_length_size = 4;
2592         }
2593
2594       arange.ar_version = byte_get (hdrptr, 2);
2595       hdrptr += 2;
2596
2597       arange.ar_info_offset = byte_get (hdrptr, offset_size);
2598       hdrptr += offset_size;
2599
2600       arange.ar_pointer_size = byte_get (hdrptr, 1);
2601       hdrptr += 1;
2602
2603       arange.ar_segment_size = byte_get (hdrptr, 1);
2604       hdrptr += 1;
2605
2606       if (arange.ar_version != 2 && arange.ar_version != 3)
2607         {
2608           warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
2609           break;
2610         }
2611
2612       printf (_("  Length:                   %ld\n"), arange.ar_length);
2613       printf (_("  Version:                  %d\n"), arange.ar_version);
2614       printf (_("  Offset into .debug_info:  %lx\n"), arange.ar_info_offset);
2615       printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
2616       printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
2617
2618       address_size = arange.ar_pointer_size + arange.ar_segment_size;
2619
2620       /* The DWARF spec does not require that the address size be a power
2621          of two, but we do.  This will have to change if we ever encounter
2622          an uneven architecture.  */
2623       if ((address_size & (address_size - 1)) != 0)
2624         {
2625           warn (_("Pointer size + Segment size is not a power of two.\n"));
2626           break;
2627         }
2628       
2629       if (address_size > 4)
2630         printf (_("\n    Address            Length\n"));
2631       else
2632         printf (_("\n    Address    Length\n"));
2633
2634       ranges = hdrptr;
2635
2636       /* Must pad to an alignment boundary that is twice the address size.  */
2637       excess = (hdrptr - start) % (2 * address_size);
2638       if (excess)
2639         ranges += (2 * address_size) - excess;
2640
2641       start += arange.ar_length + initial_length_size;
2642
2643       while (ranges + 2 * address_size <= start)
2644         {
2645           address = byte_get (ranges, address_size);
2646
2647           ranges += address_size;
2648
2649           length  = byte_get (ranges, address_size);
2650
2651           ranges += address_size;
2652
2653           if (address_size > 4)
2654             printf ("    0x%16.16lx 0x%lx\n", address, length);
2655           else
2656             printf ("    0x%8.8lx 0x%lx\n", address, length);       
2657         }
2658     }
2659
2660   printf ("\n");
2661
2662   return 1;
2663 }
2664
2665 static int
2666 display_debug_ranges (struct dwarf_section *section,
2667                       void *file ATTRIBUTE_UNUSED)
2668 {
2669   unsigned char *start = section->start;
2670   unsigned char *section_end;
2671   unsigned long bytes;
2672   unsigned char *section_begin = start;
2673   unsigned int num_range_list = 0;
2674   unsigned long last_offset = 0;
2675   unsigned int first = 0;
2676   unsigned int i;
2677   unsigned int j;
2678   int seen_first_offset = 0;
2679   int use_debug_info = 1;
2680   unsigned char *next;
2681
2682   bytes = section->size;
2683   section_end = start + bytes;
2684
2685   if (bytes == 0)
2686     {
2687       printf (_("\nThe %s section is empty.\n"), section->name);
2688       return 0;
2689     }
2690
2691   load_debug_info (file);
2692
2693   /* Check the order of range list in .debug_info section. If
2694      offsets of range lists are in the ascending order, we can
2695      use `debug_information' directly.  */
2696   for (i = 0; i < num_debug_info_entries; i++)
2697     {
2698       unsigned int num;
2699
2700       num = debug_information [i].num_range_lists;
2701       num_range_list += num;
2702
2703       /* Check if we can use `debug_information' directly.  */
2704       if (use_debug_info && num != 0)
2705         {
2706           if (!seen_first_offset)
2707             {
2708               /* This is the first range list.  */
2709               last_offset = debug_information [i].range_lists [0];
2710               first = i;
2711               seen_first_offset = 1;
2712               j = 1;
2713             }
2714           else
2715             j = 0;
2716
2717           for (; j < num; j++)
2718             {
2719               if (last_offset >
2720                   debug_information [i].range_lists [j])
2721                 {
2722                   use_debug_info = 0;
2723                   break;
2724                 }
2725               last_offset = debug_information [i].range_lists [j];
2726             }
2727         }
2728     }
2729
2730   if (!use_debug_info)
2731     /* FIXME: Should we handle this case?  */
2732     error (_("Range lists in .debug_info section aren't in ascending order!\n"));
2733
2734   if (!seen_first_offset)
2735     error (_("No range lists in .debug_info section!\n"));
2736
2737   /* DWARF sections under Mach-O have non-zero addresses.  */
2738   if (debug_information [first].num_range_lists > 0
2739       && debug_information [first].range_lists [0] != section->address)
2740     warn (_("Range lists in %s section start at 0x%lx\n"),
2741           section->name, debug_information [first].range_lists [0]);
2742
2743   printf (_("Contents of the %s section:\n\n"), section->name);
2744   printf (_("    Offset   Begin    End\n"));
2745
2746   seen_first_offset = 0;
2747   for (i = first; i < num_debug_info_entries; i++)
2748     {
2749       unsigned long begin;
2750       unsigned long end;
2751       unsigned long offset;
2752       unsigned int pointer_size;
2753       unsigned long base_address;
2754
2755       pointer_size = debug_information [i].pointer_size;
2756
2757       for (j = 0; j < debug_information [i].num_range_lists; j++)
2758         {
2759           /* DWARF sections under Mach-O have non-zero addresses.  */
2760           offset = debug_information [i].range_lists [j] - section->address;
2761           next = section_begin + offset;
2762           base_address = debug_information [i].base_address;
2763
2764           if (!seen_first_offset)
2765             seen_first_offset = 1;
2766           else
2767             {
2768               if (start < next)
2769                 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
2770                       (long)(start - section_begin),
2771                       (long)(next - section_begin), section->name);
2772               else if (start > next)
2773                 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
2774                       (long)(start - section_begin),
2775                       (long)(next - section_begin), section->name);
2776             }
2777           start = next;
2778
2779           while (1)
2780             {
2781               begin = byte_get (start, pointer_size);
2782               start += pointer_size;
2783               end = byte_get (start, pointer_size);
2784               start += pointer_size;
2785
2786               if (begin == 0 && end == 0)
2787                 {
2788                   printf (_("    %8.8lx <End of list>\n"), offset);
2789                   break;
2790                 }
2791
2792               /* Check base address specifiers.  */
2793               if (begin == -1UL && end != -1UL)
2794                 {
2795                   base_address = end;
2796                   printf ("    %8.8lx %8.8lx %8.8lx (base address)\n",
2797                           offset, begin, end);
2798                   continue;
2799                 }
2800
2801               printf ("    %8.8lx %8.8lx %8.8lx",
2802                       offset, begin + base_address, end + base_address);
2803
2804               if (begin == end)
2805                 fputs (_(" (start == end)"), stdout);
2806               else if (begin > end)
2807                 fputs (_(" (start > end)"), stdout);
2808
2809               putchar ('\n');
2810             }
2811         }
2812     }
2813   putchar ('\n');
2814   return 1;
2815 }
2816
2817 typedef struct Frame_Chunk
2818 {
2819   struct Frame_Chunk *next;
2820   unsigned char *chunk_start;
2821   int ncols;
2822   /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
2823   short int *col_type;
2824   int *col_offset;
2825   char *augmentation;
2826   unsigned int code_factor;
2827   int data_factor;
2828   unsigned long pc_begin;
2829   unsigned long pc_range;
2830   int cfa_reg;
2831   int cfa_offset;
2832   int ra;
2833   unsigned char fde_encoding;
2834   unsigned char cfa_exp;
2835 }
2836 Frame_Chunk;
2837
2838 /* A marker for a col_type that means this column was never referenced
2839    in the frame info.  */
2840 #define DW_CFA_unreferenced (-1)
2841
2842 static void
2843 frame_need_space (Frame_Chunk *fc, int reg)
2844 {
2845   int prev = fc->ncols;
2846
2847   if (reg < fc->ncols)
2848     return;
2849
2850   fc->ncols = reg + 1;
2851   fc->col_type = xcrealloc (fc->col_type, fc->ncols, sizeof (short int));
2852   fc->col_offset = xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
2853
2854   while (prev < fc->ncols)
2855     {
2856       fc->col_type[prev] = DW_CFA_unreferenced;
2857       fc->col_offset[prev] = 0;
2858       prev++;
2859     }
2860 }
2861
2862 static void
2863 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
2864 {
2865   int r;
2866   char tmp[100];
2867
2868   if (*max_regs < fc->ncols)
2869     *max_regs = fc->ncols;
2870
2871   if (*need_col_headers)
2872     {
2873       *need_col_headers = 0;
2874
2875       printf ("   LOC   CFA      ");
2876
2877       for (r = 0; r < *max_regs; r++)
2878         if (fc->col_type[r] != DW_CFA_unreferenced)
2879           {
2880             if (r == fc->ra)
2881               printf ("ra   ");
2882             else
2883               printf ("r%-4d", r);
2884           }
2885
2886       printf ("\n");
2887     }
2888
2889   printf ("%08lx ", fc->pc_begin);
2890   if (fc->cfa_exp)
2891     strcpy (tmp, "exp");
2892   else
2893     sprintf (tmp, "r%d%+d", fc->cfa_reg, fc->cfa_offset);
2894   printf ("%-8s ", tmp);
2895
2896   for (r = 0; r < fc->ncols; r++)
2897     {
2898       if (fc->col_type[r] != DW_CFA_unreferenced)
2899         {
2900           switch (fc->col_type[r])
2901             {
2902             case DW_CFA_undefined:
2903               strcpy (tmp, "u");
2904               break;
2905             case DW_CFA_same_value:
2906               strcpy (tmp, "s");
2907               break;
2908             case DW_CFA_offset:
2909               sprintf (tmp, "c%+d", fc->col_offset[r]);
2910               break;
2911             case DW_CFA_val_offset:
2912               sprintf (tmp, "v%+d", fc->col_offset[r]);
2913               break;
2914             case DW_CFA_register:
2915               sprintf (tmp, "r%d", fc->col_offset[r]);
2916               break;
2917             case DW_CFA_expression:
2918               strcpy (tmp, "exp");
2919               break;
2920             case DW_CFA_val_expression:
2921               strcpy (tmp, "vexp");
2922               break;
2923             default:
2924               strcpy (tmp, "n/a");
2925               break;
2926             }
2927           printf ("%-5s", tmp);
2928         }
2929     }
2930   printf ("\n");
2931 }
2932
2933 static int
2934 size_of_encoded_value (int encoding)
2935 {
2936   switch (encoding & 0x7)
2937     {
2938     default:    /* ??? */
2939     case 0:     return eh_addr_size;
2940     case 2:     return 2;
2941     case 3:     return 4;
2942     case 4:     return 8;
2943     }
2944 }
2945
2946 static dwarf_vma
2947 get_encoded_value (unsigned char *data, int encoding)
2948 {
2949   int size = size_of_encoded_value (encoding);
2950
2951   if (encoding & DW_EH_PE_signed)
2952     return byte_get_signed (data, size);
2953   else
2954     return byte_get (data, size);
2955 }
2956
2957 #define GET(N)  byte_get (start, N); start += N
2958 #define LEB()   read_leb128 (start, & length_return, 0); start += length_return
2959 #define SLEB()  read_leb128 (start, & length_return, 1); start += length_return
2960
2961 static int
2962 display_debug_frames (struct dwarf_section *section,
2963                       void *file ATTRIBUTE_UNUSED)
2964 {
2965   unsigned char *start = section->start;
2966   unsigned char *end = start + section->size;
2967   unsigned char *section_start = start;
2968   Frame_Chunk *chunks = 0;
2969   Frame_Chunk *remembered_state = 0;
2970   Frame_Chunk *rs;
2971   int is_eh = strcmp (section->name, ".eh_frame") == 0;
2972   unsigned int length_return;
2973   int max_regs = 0;
2974
2975   printf (_("The section %s contains:\n"), section->name);
2976
2977   while (start < end)
2978     {
2979       unsigned char *saved_start;
2980       unsigned char *block_end;
2981       unsigned long length;
2982       unsigned long cie_id;
2983       Frame_Chunk *fc;
2984       Frame_Chunk *cie;
2985       int need_col_headers = 1;
2986       unsigned char *augmentation_data = NULL;
2987       unsigned long augmentation_data_len = 0;
2988       int encoded_ptr_size = eh_addr_size;
2989       int offset_size;
2990       int initial_length_size;
2991
2992       saved_start = start;
2993       length = byte_get (start, 4); start += 4;
2994
2995       if (length == 0)
2996         {
2997           printf ("\n%08lx ZERO terminator\n\n",
2998                     (unsigned long)(saved_start - section_start));
2999           continue;
3000         }
3001
3002       if (length == 0xffffffff)
3003         {
3004           length = byte_get (start, 8);
3005           start += 8;
3006           offset_size = 8;
3007           initial_length_size = 12;
3008         }
3009       else
3010         {
3011           offset_size = 4;
3012           initial_length_size = 4;
3013         }
3014
3015       block_end = saved_start + length + initial_length_size;
3016       if (block_end > end)
3017         {
3018           warn ("Invalid length %#08lx in FDE at %#08lx\n",
3019                 length, (unsigned long)(saved_start - section_start));
3020           block_end = end;
3021         }
3022       cie_id = byte_get (start, offset_size); start += offset_size;
3023
3024       if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
3025         {
3026           int version;
3027
3028           fc = xmalloc (sizeof (Frame_Chunk));
3029           memset (fc, 0, sizeof (Frame_Chunk));
3030
3031           fc->next = chunks;
3032           chunks = fc;
3033           fc->chunk_start = saved_start;
3034           fc->ncols = 0;
3035           fc->col_type = xmalloc (sizeof (short int));
3036           fc->col_offset = xmalloc (sizeof (int));
3037           frame_need_space (fc, max_regs-1);
3038
3039           version = *start++;
3040
3041           fc->augmentation = (char *) start;
3042           start = (unsigned char *) strchr ((char *) start, '\0') + 1;
3043
3044           if (fc->augmentation[0] == 'z')
3045             {
3046               fc->code_factor = LEB ();
3047               fc->data_factor = SLEB ();
3048               if (version == 1)
3049                 {
3050                   fc->ra = GET (1);
3051                 }
3052               else
3053                 {
3054                   fc->ra = LEB ();
3055                 }
3056               augmentation_data_len = LEB ();
3057               augmentation_data = start;
3058               start += augmentation_data_len;
3059             }
3060           else if (strcmp (fc->augmentation, "eh") == 0)
3061             {
3062               start += eh_addr_size;
3063               fc->code_factor = LEB ();
3064               fc->data_factor = SLEB ();
3065               if (version == 1)
3066                 {
3067                   fc->ra = GET (1);
3068                 }
3069               else
3070                 {
3071                   fc->ra = LEB ();
3072                 }
3073             }
3074           else
3075             {
3076               fc->code_factor = LEB ();
3077               fc->data_factor = SLEB ();
3078               if (version == 1)
3079                 {
3080                   fc->ra = GET (1);
3081                 }
3082               else
3083                 {
3084                   fc->ra = LEB ();
3085                 }
3086             }
3087           cie = fc;
3088
3089           if (do_debug_frames_interp)
3090             printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
3091                     (unsigned long)(saved_start - section_start), length, cie_id,
3092                     fc->augmentation, fc->code_factor, fc->data_factor,
3093                     fc->ra);
3094           else
3095             {
3096               printf ("\n%08lx %08lx %08lx CIE\n",
3097                       (unsigned long)(saved_start - section_start), length, cie_id);
3098               printf ("  Version:               %d\n", version);
3099               printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
3100               printf ("  Code alignment factor: %u\n", fc->code_factor);
3101               printf ("  Data alignment factor: %d\n", fc->data_factor);
3102               printf ("  Return address column: %d\n", fc->ra);
3103
3104               if (augmentation_data_len)
3105                 {
3106                   unsigned long i;
3107                   printf ("  Augmentation data:    ");
3108                   for (i = 0; i < augmentation_data_len; ++i)
3109                     printf (" %02x", augmentation_data[i]);
3110                   putchar ('\n');
3111                 }
3112               putchar ('\n');
3113             }
3114
3115           if (augmentation_data_len)
3116             {
3117               unsigned char *p, *q;
3118               p = (unsigned char *) fc->augmentation + 1;
3119               q = augmentation_data;
3120
3121               while (1)
3122                 {
3123                   if (*p == 'L')
3124                     q++;
3125                   else if (*p == 'P')
3126                     q += 1 + size_of_encoded_value (*q);
3127                   else if (*p == 'R')
3128                     fc->fde_encoding = *q++;
3129                   else
3130                     break;
3131                   p++;
3132                 }
3133
3134               if (fc->fde_encoding)
3135                 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
3136             }
3137
3138           frame_need_space (fc, fc->ra);
3139         }
3140       else
3141         {
3142           unsigned char *look_for;
3143           static Frame_Chunk fde_fc;
3144
3145           fc = & fde_fc;
3146           memset (fc, 0, sizeof (Frame_Chunk));
3147
3148           look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
3149
3150           for (cie = chunks; cie ; cie = cie->next)
3151             if (cie->chunk_start == look_for)
3152               break;
3153
3154           if (!cie)
3155             {
3156               warn ("Invalid CIE pointer %#08lx in FDE at %#08lx\n",
3157                     cie_id, (unsigned long)(saved_start - section_start));
3158               fc->ncols = 0;
3159               fc->col_type = xmalloc (sizeof (short int));
3160               fc->col_offset = xmalloc (sizeof (int));
3161               frame_need_space (fc, max_regs - 1);
3162               cie = fc;
3163               fc->augmentation = "";
3164               fc->fde_encoding = 0;
3165             }
3166           else
3167             {
3168               fc->ncols = cie->ncols;
3169               fc->col_type = xcmalloc (fc->ncols, sizeof (short int));
3170               fc->col_offset = xcmalloc (fc->ncols, sizeof (int));
3171               memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
3172               memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
3173               fc->augmentation = cie->augmentation;
3174               fc->code_factor = cie->code_factor;
3175               fc->data_factor = cie->data_factor;
3176               fc->cfa_reg = cie->cfa_reg;
3177               fc->cfa_offset = cie->cfa_offset;
3178               fc->ra = cie->ra;
3179               frame_need_space (fc, max_regs-1);
3180               fc->fde_encoding = cie->fde_encoding;
3181             }
3182
3183           if (fc->fde_encoding)
3184             encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
3185
3186           fc->pc_begin = get_encoded_value (start, fc->fde_encoding);
3187           if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel
3188               /* Don't adjust for relocatable file since there's
3189                  invariably a pcrel reloc here, which we haven't
3190                  applied.  */
3191               && !is_relocatable)
3192             fc->pc_begin += section->address + (start - section_start);
3193           start += encoded_ptr_size;
3194           fc->pc_range = byte_get (start, encoded_ptr_size);
3195           start += encoded_ptr_size;
3196
3197           if (cie->augmentation[0] == 'z')
3198             {
3199               augmentation_data_len = LEB ();
3200               augmentation_data = start;
3201               start += augmentation_data_len;
3202             }
3203
3204           printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
3205                   (unsigned long)(saved_start - section_start), length, cie_id,
3206                   (unsigned long)(cie->chunk_start - section_start),
3207                   fc->pc_begin, fc->pc_begin + fc->pc_range);
3208           if (! do_debug_frames_interp && augmentation_data_len)
3209             {
3210               unsigned long i;
3211
3212               printf ("  Augmentation data:    ");
3213               for (i = 0; i < augmentation_data_len; ++i)
3214                 printf (" %02x", augmentation_data[i]);
3215               putchar ('\n');
3216               putchar ('\n');
3217             }
3218         }
3219
3220       /* At this point, fc is the current chunk, cie (if any) is set, and
3221          we're about to interpret instructions for the chunk.  */
3222       /* ??? At present we need to do this always, since this sizes the
3223          fc->col_type and fc->col_offset arrays, which we write into always.
3224          We should probably split the interpreted and non-interpreted bits
3225          into two different routines, since there's so much that doesn't
3226          really overlap between them.  */
3227       if (1 || do_debug_frames_interp)
3228         {
3229           /* Start by making a pass over the chunk, allocating storage
3230              and taking note of what registers are used.  */
3231           unsigned char *tmp = start;
3232
3233           while (start < block_end)
3234             {
3235               unsigned op, opa;
3236               unsigned long reg, tmp;
3237
3238               op = *start++;
3239               opa = op & 0x3f;
3240               if (op & 0xc0)
3241                 op &= 0xc0;
3242
3243               /* Warning: if you add any more cases to this switch, be
3244                  sure to add them to the corresponding switch below.  */
3245               switch (op)
3246                 {
3247                 case DW_CFA_advance_loc:
3248                   break;
3249                 case DW_CFA_offset:
3250                   LEB ();
3251                   frame_need_space (fc, opa);
3252                   fc->col_type[opa] = DW_CFA_undefined;
3253                   break;
3254                 case DW_CFA_restore:
3255                   frame_need_space (fc, opa);
3256                   fc->col_type[opa] = DW_CFA_undefined;
3257                   break;
3258                 case DW_CFA_set_loc:
3259                   start += encoded_ptr_size;
3260                   break;
3261                 case DW_CFA_advance_loc1:
3262                   start += 1;
3263                   break;
3264                 case DW_CFA_advance_loc2:
3265                   start += 2;
3266                   break;
3267                 case DW_CFA_advance_loc4:
3268                   start += 4;
3269                   break;
3270                 case DW_CFA_offset_extended:
3271                 case DW_CFA_val_offset:
3272                   reg = LEB (); LEB ();
3273                   frame_need_space (fc, reg);
3274                   fc->col_type[reg] = DW_CFA_undefined;
3275                   break;
3276                 case DW_CFA_restore_extended:
3277                   reg = LEB ();
3278                   frame_need_space (fc, reg);
3279                   fc->col_type[reg] = DW_CFA_undefined;
3280                   break;
3281                 case DW_CFA_undefined:
3282                   reg = LEB ();
3283                   frame_need_space (fc, reg);
3284                   fc->col_type[reg] = DW_CFA_undefined;
3285                   break;
3286                 case DW_CFA_same_value:
3287                   reg = LEB ();
3288                   frame_need_space (fc, reg);
3289                   fc->col_type[reg] = DW_CFA_undefined;
3290                   break;
3291                 case DW_CFA_register:
3292                   reg = LEB (); LEB ();
3293                   frame_need_space (fc, reg);
3294                   fc->col_type[reg] = DW_CFA_undefined;
3295                   break;
3296                 case DW_CFA_def_cfa:
3297                   LEB (); LEB ();
3298                   break;
3299                 case DW_CFA_def_cfa_register:
3300                   LEB ();
3301                   break;
3302                 case DW_CFA_def_cfa_offset:
3303                   LEB ();
3304                   break;
3305                 case DW_CFA_def_cfa_expression:
3306                   tmp = LEB ();
3307                   start += tmp;
3308                   break;
3309                 case DW_CFA_expression:
3310                 case DW_CFA_val_expression:
3311                   reg = LEB ();
3312                   tmp = LEB ();
3313                   start += tmp;
3314                   frame_need_space (fc, reg);
3315                   fc->col_type[reg] = DW_CFA_undefined;
3316                   break;
3317                 case DW_CFA_offset_extended_sf:
3318                 case DW_CFA_val_offset_sf:
3319                   reg = LEB (); SLEB ();
3320                   frame_need_space (fc, reg);
3321                   fc->col_type[reg] = DW_CFA_undefined;
3322                   break;
3323                 case DW_CFA_def_cfa_sf:
3324                   LEB (); SLEB ();
3325                   break;
3326                 case DW_CFA_def_cfa_offset_sf:
3327                   SLEB ();
3328                   break;
3329                 case DW_CFA_MIPS_advance_loc8:
3330                   start += 8;
3331                   break;
3332                 case DW_CFA_GNU_args_size:
3333                   LEB ();
3334                   break;
3335                 case DW_CFA_GNU_negative_offset_extended:
3336                   reg = LEB (); LEB ();
3337                   frame_need_space (fc, reg);
3338                   fc->col_type[reg] = DW_CFA_undefined;
3339
3340                 default:
3341                   break;
3342                 }
3343             }
3344           start = tmp;
3345         }
3346
3347       /* Now we know what registers are used, make a second pass over
3348          the chunk, this time actually printing out the info.  */
3349
3350       while (start < block_end)
3351         {
3352           unsigned op, opa;
3353           unsigned long ul, reg, roffs;
3354           long l, ofs;
3355           dwarf_vma vma;
3356
3357           op = *start++;
3358           opa = op & 0x3f;
3359           if (op & 0xc0)
3360             op &= 0xc0;
3361
3362           /* Warning: if you add any more cases to this switch, be
3363              sure to add them to the corresponding switch above.  */
3364           switch (op)
3365             {
3366             case DW_CFA_advance_loc:
3367               if (do_debug_frames_interp)
3368                 frame_display_row (fc, &need_col_headers, &max_regs);
3369               else
3370                 printf ("  DW_CFA_advance_loc: %d to %08lx\n",
3371                         opa * fc->code_factor,
3372                         fc->pc_begin + opa * fc->code_factor);
3373               fc->pc_begin += opa * fc->code_factor;
3374               break;
3375
3376             case DW_CFA_offset:
3377               roffs = LEB ();
3378               if (! do_debug_frames_interp)
3379                 printf ("  DW_CFA_offset: r%d at cfa%+ld\n",
3380                         opa, roffs * fc->data_factor);
3381               fc->col_type[opa] = DW_CFA_offset;
3382               fc->col_offset[opa] = roffs * fc->data_factor;
3383               break;
3384
3385             case DW_CFA_restore:
3386               if (! do_debug_frames_interp)
3387                 printf ("  DW_CFA_restore: r%d\n", opa);
3388               fc->col_type[opa] = cie->col_type[opa];
3389               fc->col_offset[opa] = cie->col_offset[opa];
3390               break;
3391
3392             case DW_CFA_set_loc:
3393               vma = get_encoded_value (start, fc->fde_encoding);
3394               if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel
3395                   && !is_relocatable)
3396                 vma += section->address + (start - section_start);
3397               start += encoded_ptr_size;
3398               if (do_debug_frames_interp)
3399                 frame_display_row (fc, &need_col_headers, &max_regs);
3400               else
3401                 printf ("  DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
3402               fc->pc_begin = vma;
3403               break;
3404
3405             case DW_CFA_advance_loc1:
3406               ofs = byte_get (start, 1); start += 1;
3407               if (do_debug_frames_interp)
3408                 frame_display_row (fc, &need_col_headers, &max_regs);
3409               else
3410                 printf ("  DW_CFA_advance_loc1: %ld to %08lx\n",
3411                         ofs * fc->code_factor,
3412                         fc->pc_begin + ofs * fc->code_factor);
3413               fc->pc_begin += ofs * fc->code_factor;
3414               break;
3415
3416             case DW_CFA_advance_loc2:
3417               ofs = byte_get (start, 2); start += 2;
3418               if (do_debug_frames_interp)
3419                 frame_display_row (fc, &need_col_headers, &max_regs);
3420               else
3421                 printf ("  DW_CFA_advance_loc2: %ld to %08lx\n",
3422                         ofs * fc->code_factor,
3423                         fc->pc_begin + ofs * fc->code_factor);
3424               fc->pc_begin += ofs * fc->code_factor;
3425               break;
3426
3427             case DW_CFA_advance_loc4:
3428               ofs = byte_get (start, 4); start += 4;
3429               if (do_debug_frames_interp)
3430                 frame_display_row (fc, &need_col_headers, &max_regs);
3431               else
3432                 printf ("  DW_CFA_advance_loc4: %ld to %08lx\n",
3433                         ofs * fc->code_factor,
3434                         fc->pc_begin + ofs * fc->code_factor);
3435               fc->pc_begin += ofs * fc->code_factor;
3436               break;
3437
3438             case DW_CFA_offset_extended:
3439               reg = LEB ();
3440               roffs = LEB ();
3441               if (! do_debug_frames_interp)
3442                 printf ("  DW_CFA_offset_extended: r%ld at cfa%+ld\n",
3443                         reg, roffs * fc->data_factor);
3444               fc->col_type[reg] = DW_CFA_offset;
3445               fc->col_offset[reg] = roffs * fc->data_factor;
3446               break;
3447
3448             case DW_CFA_val_offset:
3449               reg = LEB ();
3450               roffs = LEB ();
3451               if (! do_debug_frames_interp)
3452                 printf ("  DW_CFA_val_offset: r%ld at cfa%+ld\n",
3453                         reg, roffs * fc->data_factor);
3454               fc->col_type[reg] = DW_CFA_val_offset;
3455               fc->col_offset[reg] = roffs * fc->data_factor;
3456               break;
3457
3458             case DW_CFA_restore_extended:
3459               reg = LEB ();
3460               if (! do_debug_frames_interp)
3461                 printf ("  DW_CFA_restore_extended: r%ld\n", reg);
3462               fc->col_type[reg] = cie->col_type[reg];
3463               fc->col_offset[reg] = cie->col_offset[reg];
3464               break;
3465
3466             case DW_CFA_undefined:
3467               reg = LEB ();
3468               if (! do_debug_frames_interp)
3469                 printf ("  DW_CFA_undefined: r%ld\n", reg);
3470               fc->col_type[reg] = DW_CFA_undefined;
3471               fc->col_offset[reg] = 0;
3472               break;
3473
3474             case DW_CFA_same_value:
3475               reg = LEB ();
3476               if (! do_debug_frames_interp)
3477                 printf ("  DW_CFA_same_value: r%ld\n", reg);
3478               fc->col_type[reg] = DW_CFA_same_value;
3479               fc->col_offset[reg] = 0;
3480               break;
3481
3482             case DW_CFA_register:
3483               reg = LEB ();
3484               roffs = LEB ();
3485               if (! do_debug_frames_interp)
3486                 printf ("  DW_CFA_register: r%ld in r%ld\n", reg, roffs);
3487               fc->col_type[reg] = DW_CFA_register;
3488               fc->col_offset[reg] = roffs;
3489               break;
3490
3491             case DW_CFA_remember_state:
3492               if (! do_debug_frames_interp)
3493                 printf ("  DW_CFA_remember_state\n");
3494               rs = xmalloc (sizeof (Frame_Chunk));
3495               rs->ncols = fc->ncols;
3496               rs->col_type = xcmalloc (rs->ncols, sizeof (short int));
3497               rs->col_offset = xcmalloc (rs->ncols, sizeof (int));
3498               memcpy (rs->col_type, fc->col_type, rs->ncols);
3499               memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
3500               rs->next = remembered_state;
3501               remembered_state = rs;
3502               break;
3503
3504             case DW_CFA_restore_state:
3505               if (! do_debug_frames_interp)
3506                 printf ("  DW_CFA_restore_state\n");
3507               rs = remembered_state;
3508               if (rs)
3509                 {
3510                   remembered_state = rs->next;
3511                   frame_need_space (fc, rs->ncols-1);
3512                   memcpy (fc->col_type, rs->col_type, rs->ncols);
3513                   memcpy (fc->col_offset, rs->col_offset,
3514                           rs->ncols * sizeof (int));
3515                   free (rs->col_type);
3516                   free (rs->col_offset);
3517                   free (rs);
3518                 }
3519               else if (do_debug_frames_interp)
3520                 printf ("Mismatched DW_CFA_restore_state\n");
3521               break;
3522
3523             case DW_CFA_def_cfa:
3524               fc->cfa_reg = LEB ();
3525               fc->cfa_offset = LEB ();
3526               fc->cfa_exp = 0;
3527               if (! do_debug_frames_interp)
3528                 printf ("  DW_CFA_def_cfa: r%d ofs %d\n",
3529                         fc->cfa_reg, fc->cfa_offset);
3530               break;
3531
3532             case DW_CFA_def_cfa_register:
3533               fc->cfa_reg = LEB ();
3534               fc->cfa_exp = 0;
3535               if (! do_debug_frames_interp)
3536                 printf ("  DW_CFA_def_cfa_reg: r%d\n", fc->cfa_reg);
3537               break;
3538
3539             case DW_CFA_def_cfa_offset:
3540               fc->cfa_offset = LEB ();
3541               if (! do_debug_frames_interp)
3542                 printf ("  DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
3543               break;
3544
3545             case DW_CFA_nop:
3546               if (! do_debug_frames_interp)
3547                 printf ("  DW_CFA_nop\n");
3548               break;
3549
3550             case DW_CFA_def_cfa_expression:
3551               ul = LEB ();
3552               if (! do_debug_frames_interp)
3553                 {
3554                   printf ("  DW_CFA_def_cfa_expression (");
3555                   decode_location_expression (start, eh_addr_size, ul, 0);
3556                   printf (")\n");
3557                 }
3558               fc->cfa_exp = 1;
3559               start += ul;
3560               break;
3561
3562             case DW_CFA_expression:
3563               reg = LEB ();
3564               ul = LEB ();
3565               if (! do_debug_frames_interp)
3566                 {
3567                   printf ("  DW_CFA_expression: r%ld (", reg);
3568                   decode_location_expression (start, eh_addr_size, ul, 0);
3569                   printf (")\n");
3570                 }
3571               fc->col_type[reg] = DW_CFA_expression;
3572               start += ul;
3573               break;
3574
3575             case DW_CFA_val_expression:
3576               reg = LEB ();
3577               ul = LEB ();
3578               if (! do_debug_frames_interp)
3579                 {
3580                   printf ("  DW_CFA_val_expression: r%ld (", reg);
3581                   decode_location_expression (start, eh_addr_size, ul, 0);
3582                   printf (")\n");
3583                 }
3584               fc->col_type[reg] = DW_CFA_val_expression;
3585               start += ul;
3586               break;
3587
3588             case DW_CFA_offset_extended_sf:
3589               reg = LEB ();
3590               l = SLEB ();
3591               frame_need_space (fc, reg);
3592               if (! do_debug_frames_interp)
3593                 printf ("  DW_CFA_offset_extended_sf: r%ld at cfa%+ld\n",
3594                         reg, l * fc->data_factor);
3595               fc->col_type[reg] = DW_CFA_offset;
3596               fc->col_offset[reg] = l * fc->data_factor;
3597               break;
3598
3599             case DW_CFA_val_offset_sf:
3600               reg = LEB ();
3601               l = SLEB ();
3602               frame_need_space (fc, reg);
3603               if (! do_debug_frames_interp)
3604                 printf ("  DW_CFA_val_offset_sf: r%ld at cfa%+ld\n",
3605                         reg, l * fc->data_factor);
3606               fc->col_type[reg] = DW_CFA_val_offset;
3607               fc->col_offset[reg] = l * fc->data_factor;
3608               break;
3609
3610             case DW_CFA_def_cfa_sf:
3611               fc->cfa_reg = LEB ();
3612               fc->cfa_offset = SLEB ();
3613               fc->cfa_offset = fc->cfa_offset * fc->data_factor;
3614               fc->cfa_exp = 0;
3615               if (! do_debug_frames_interp)
3616                 printf ("  DW_CFA_def_cfa_sf: r%d ofs %d\n",
3617                         fc->cfa_reg, fc->cfa_offset);
3618               break;
3619
3620             case DW_CFA_def_cfa_offset_sf:
3621               fc->cfa_offset = SLEB ();
3622               fc->cfa_offset = fc->cfa_offset * fc->data_factor;
3623               if (! do_debug_frames_interp)
3624                 printf ("  DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
3625               break;
3626
3627             case DW_CFA_MIPS_advance_loc8:
3628               ofs = byte_get (start, 8); start += 8;
3629               if (do_debug_frames_interp)
3630                 frame_display_row (fc, &need_col_headers, &max_regs);
3631               else
3632                 printf ("  DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
3633                         ofs * fc->code_factor,
3634                         fc->pc_begin + ofs * fc->code_factor);
3635               fc->pc_begin += ofs * fc->code_factor;
3636               break;
3637
3638             case DW_CFA_GNU_window_save:
3639               if (! do_debug_frames_interp)
3640                 printf ("  DW_CFA_GNU_window_save\n");
3641               break;
3642
3643             case DW_CFA_GNU_args_size:
3644               ul = LEB ();
3645               if (! do_debug_frames_interp)
3646                 printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
3647               break;
3648
3649             case DW_CFA_GNU_negative_offset_extended:
3650               reg = LEB ();
3651               l = - LEB ();
3652               frame_need_space (fc, reg);
3653               if (! do_debug_frames_interp)
3654                 printf ("  DW_CFA_GNU_negative_offset_extended: r%ld at cfa%+ld\n",
3655                         reg, l * fc->data_factor);
3656               fc->col_type[reg] = DW_CFA_offset;
3657               fc->col_offset[reg] = l * fc->data_factor;
3658               break;
3659
3660             default:
3661               if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
3662                 printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
3663               else
3664                 warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);              
3665               start = block_end;
3666             }
3667         }
3668
3669       if (do_debug_frames_interp)
3670         frame_display_row (fc, &need_col_headers, &max_regs);
3671
3672       start = block_end;
3673     }
3674
3675   printf ("\n");
3676
3677   return 1;
3678 }
3679
3680 #undef GET
3681 #undef LEB
3682 #undef SLEB
3683
3684 static int
3685 display_debug_not_supported (struct dwarf_section *section,
3686                              void *file ATTRIBUTE_UNUSED)
3687 {
3688   printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
3689             section->name);
3690
3691   return 1;
3692 }
3693
3694 void *
3695 cmalloc (size_t nmemb, size_t size)
3696 {
3697   /* Check for overflow.  */
3698   if (nmemb >= ~(size_t) 0 / size)
3699     return NULL;
3700   else
3701     return malloc (nmemb * size);
3702 }
3703
3704 void *
3705 xcmalloc (size_t nmemb, size_t size)
3706 {
3707   /* Check for overflow.  */
3708   if (nmemb >= ~(size_t) 0 / size)
3709     return NULL;
3710   else
3711     return xmalloc (nmemb * size);
3712 }
3713
3714 void *
3715 xcrealloc (void *ptr, size_t nmemb, size_t size)
3716 {
3717   /* Check for overflow.  */
3718   if (nmemb >= ~(size_t) 0 / size)
3719     return NULL;
3720   else
3721     return xrealloc (ptr, nmemb * size);
3722 }
3723
3724 void
3725 error (const char *message, ...)
3726 {
3727   va_list args;
3728
3729   va_start (args, message);
3730   fprintf (stderr, _("%s: Error: "), program_name);
3731   vfprintf (stderr, message, args);
3732   va_end (args);
3733 }
3734
3735 void
3736 warn (const char *message, ...)
3737 {
3738   va_list args;
3739
3740   va_start (args, message);
3741   fprintf (stderr, _("%s: Warning: "), program_name);
3742   vfprintf (stderr, message, args);
3743   va_end (args);
3744 }
3745
3746 void
3747 free_debug_memory (void)
3748 {
3749   enum dwarf_section_display_enum i;
3750
3751   free_abbrevs ();
3752
3753   for (i = 0; i < max; i++)
3754     free_debug_section (i);
3755
3756   if (debug_information)
3757     {
3758       for (i = 0; i < num_debug_info_entries; i++)
3759         {
3760           if (!debug_information [i].max_loc_offsets)
3761             {
3762               free (debug_information [i].loc_offsets);
3763               free (debug_information [i].have_frame_base);
3764             }
3765           if (!debug_information [i].max_range_lists)
3766             free (debug_information [i].range_lists);
3767         }
3768       free (debug_information);
3769       debug_information = NULL;
3770       num_debug_info_entries = 0;
3771     }
3772
3773 }
3774
3775 struct dwarf_section_display debug_displays[] =
3776 {
3777   { { ".debug_abbrev",          NULL,   0,      0 },
3778     display_debug_abbrev,               0,      0 },
3779   { { ".debug_aranges",         NULL,   0,      0 },
3780     display_debug_aranges,              0,      0 },
3781   { { ".debug_frame",           NULL,   0,      0 },
3782     display_debug_frames,               1,      0 },
3783   { { ".debug_info",            NULL,   0,      0 },
3784     display_debug_info,                 1,      0 },
3785   { { ".debug_line",            NULL,   0,      0 },
3786     display_debug_lines,                0,      0 },
3787   { { ".debug_pubnames",        NULL,   0,      0 },
3788     display_debug_pubnames,             0,      0 },
3789   { { ".eh_frame",              NULL,   0,      0 },
3790     display_debug_frames,               1,      1 },
3791   { { ".debug_macinfo",         NULL,   0,      0 },
3792     display_debug_macinfo,              0,      0 },
3793   { { ".debug_str",             NULL,   0,      0 },
3794     display_debug_str,                  0,      0 },
3795   { { ".debug_loc",             NULL,   0,      0 },
3796     display_debug_loc,                  0,      0 },
3797   { { ".debug_pubtypes",        NULL,   0,      0 },
3798     display_debug_pubnames,             0,      0 },
3799   { { ".debug_ranges",          NULL,   0,      0 },
3800     display_debug_ranges,               0,      0 },
3801   { { ".debug_static_func",     NULL,   0,      0 },
3802     display_debug_not_supported,        0,      0 },
3803   { { ".debug_static_vars",     NULL,   0,      0 },
3804     display_debug_not_supported,        0,      0 },
3805   { { ".debug_types",           NULL,   0,      0 },
3806     display_debug_not_supported,        0,      0 },
3807   { { ".debug_weaknames",       NULL,   0,      0 },
3808     display_debug_not_supported,        0,      0 }
3809 };