OSDN Git Service

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