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