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