OSDN Git Service

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