OSDN Git Service

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