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