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