OSDN Git Service

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