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