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