xref: /trueos/contrib/binutils/binutils/dwarf.c (revision 0e6638027ddc49e54fd6a31fb4629766a5992959)
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2    Copyright 2005, 2006, 2007
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 2 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/dwarf2.h"
27 #include "dwarf.h"
28 
29 static int have_frame_base;
30 static int need_base_address;
31 
32 static unsigned int last_pointer_size = 0;
33 static int warned_about_missing_comp_units = FALSE;
34 
35 static unsigned int num_debug_info_entries = 0;
36 static debug_info *debug_information = NULL;
37 
38 dwarf_vma eh_addr_size;
39 int is_relocatable;
40 
41 int do_debug_info;
42 int do_debug_abbrevs;
43 int do_debug_lines;
44 int do_debug_pubnames;
45 int do_debug_aranges;
46 int do_debug_ranges;
47 int do_debug_frames;
48 int do_debug_frames_interp;
49 int do_debug_macinfo;
50 int do_debug_str;
51 int do_debug_loc;
52 
53 dwarf_vma (*byte_get) (unsigned char *, int);
54 
55 dwarf_vma
byte_get_little_endian(unsigned char * field,int size)56 byte_get_little_endian (unsigned char *field, int size)
57 {
58   switch (size)
59     {
60     case 1:
61       return *field;
62 
63     case 2:
64       return  ((unsigned int) (field[0]))
65 	|    (((unsigned int) (field[1])) << 8);
66 
67     case 4:
68       return  ((unsigned long) (field[0]))
69 	|    (((unsigned long) (field[1])) << 8)
70 	|    (((unsigned long) (field[2])) << 16)
71 	|    (((unsigned long) (field[3])) << 24);
72 
73     case 8:
74       if (sizeof (dwarf_vma) == 8)
75 	return  ((dwarf_vma) (field[0]))
76 	  |    (((dwarf_vma) (field[1])) << 8)
77 	  |    (((dwarf_vma) (field[2])) << 16)
78 	  |    (((dwarf_vma) (field[3])) << 24)
79 	  |    (((dwarf_vma) (field[4])) << 32)
80 	  |    (((dwarf_vma) (field[5])) << 40)
81 	  |    (((dwarf_vma) (field[6])) << 48)
82 	  |    (((dwarf_vma) (field[7])) << 56);
83       else if (sizeof (dwarf_vma) == 4)
84 	/* We want to extract data from an 8 byte wide field and
85 	   place it into a 4 byte wide field.  Since this is a little
86 	   endian source we can just use the 4 byte extraction code.  */
87 	return  ((unsigned long) (field[0]))
88 	  |    (((unsigned long) (field[1])) << 8)
89 	  |    (((unsigned long) (field[2])) << 16)
90 	  |    (((unsigned long) (field[3])) << 24);
91 
92     default:
93       error (_("Unhandled data length: %d\n"), size);
94       abort ();
95     }
96 }
97 
98 dwarf_vma
byte_get_big_endian(unsigned char * field,int size)99 byte_get_big_endian (unsigned char *field, int size)
100 {
101   switch (size)
102     {
103     case 1:
104       return *field;
105 
106     case 2:
107       return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
108 
109     case 4:
110       return ((unsigned long) (field[3]))
111 	|   (((unsigned long) (field[2])) << 8)
112 	|   (((unsigned long) (field[1])) << 16)
113 	|   (((unsigned long) (field[0])) << 24);
114 
115     case 8:
116       if (sizeof (dwarf_vma) == 8)
117 	return ((dwarf_vma) (field[7]))
118 	  |   (((dwarf_vma) (field[6])) << 8)
119 	  |   (((dwarf_vma) (field[5])) << 16)
120 	  |   (((dwarf_vma) (field[4])) << 24)
121 	  |   (((dwarf_vma) (field[3])) << 32)
122 	  |   (((dwarf_vma) (field[2])) << 40)
123 	  |   (((dwarf_vma) (field[1])) << 48)
124 	  |   (((dwarf_vma) (field[0])) << 56);
125       else if (sizeof (dwarf_vma) == 4)
126 	{
127 	  /* Although we are extracing data from an 8 byte wide field,
128 	     we are returning only 4 bytes of data.  */
129 	  field += 4;
130 	  return ((unsigned long) (field[3]))
131 	    |   (((unsigned long) (field[2])) << 8)
132 	    |   (((unsigned long) (field[1])) << 16)
133 	    |   (((unsigned long) (field[0])) << 24);
134 	}
135 
136     default:
137       error (_("Unhandled data length: %d\n"), size);
138       abort ();
139     }
140 }
141 
142 static dwarf_vma
byte_get_signed(unsigned char * field,int size)143 byte_get_signed (unsigned char *field, int size)
144 {
145   dwarf_vma x = byte_get (field, size);
146 
147   switch (size)
148     {
149     case 1:
150       return (x ^ 0x80) - 0x80;
151     case 2:
152       return (x ^ 0x8000) - 0x8000;
153     case 4:
154       return (x ^ 0x80000000) - 0x80000000;
155     case 8:
156       return x;
157     default:
158       abort ();
159     }
160 }
161 
162 static unsigned long int
read_leb128(unsigned char * data,unsigned int * length_return,int sign)163 read_leb128 (unsigned char *data, unsigned int *length_return, int sign)
164 {
165   unsigned long int result = 0;
166   unsigned int num_read = 0;
167   unsigned int shift = 0;
168   unsigned char byte;
169 
170   do
171     {
172       byte = *data++;
173       num_read++;
174 
175       result |= ((unsigned long int) (byte & 0x7f)) << shift;
176 
177       shift += 7;
178 
179     }
180   while (byte & 0x80);
181 
182   if (length_return != NULL)
183     *length_return = num_read;
184 
185   if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
186     result |= -1L << shift;
187 
188   return result;
189 }
190 
191 typedef struct State_Machine_Registers
192 {
193   unsigned long address;
194   unsigned int file;
195   unsigned int line;
196   unsigned int column;
197   int is_stmt;
198   int basic_block;
199   int end_sequence;
200 /* This variable hold the number of the last entry seen
201    in the File Table.  */
202   unsigned int last_file_entry;
203 } SMR;
204 
205 static SMR state_machine_regs;
206 
207 static void
reset_state_machine(int is_stmt)208 reset_state_machine (int is_stmt)
209 {
210   state_machine_regs.address = 0;
211   state_machine_regs.file = 1;
212   state_machine_regs.line = 1;
213   state_machine_regs.column = 0;
214   state_machine_regs.is_stmt = is_stmt;
215   state_machine_regs.basic_block = 0;
216   state_machine_regs.end_sequence = 0;
217   state_machine_regs.last_file_entry = 0;
218 }
219 
220 /* Handled an extend line op.
221    Returns the number of bytes read.  */
222 
223 static int
process_extended_line_op(unsigned char * data,int is_stmt)224 process_extended_line_op (unsigned char *data, int is_stmt)
225 {
226   unsigned char op_code;
227   unsigned int bytes_read;
228   unsigned int len;
229   unsigned char *name;
230   unsigned long adr;
231 
232   len = read_leb128 (data, & bytes_read, 0);
233   data += bytes_read;
234 
235   if (len == 0)
236     {
237       warn (_("badly formed extended line op encountered!\n"));
238       return bytes_read;
239     }
240 
241   len += bytes_read;
242   op_code = *data++;
243 
244   printf (_("  Extended opcode %d: "), op_code);
245 
246   switch (op_code)
247     {
248     case DW_LNE_end_sequence:
249       printf (_("End of Sequence\n\n"));
250       reset_state_machine (is_stmt);
251       break;
252 
253     case DW_LNE_set_address:
254       adr = byte_get (data, len - bytes_read - 1);
255       printf (_("set Address to 0x%lx\n"), adr);
256       state_machine_regs.address = adr;
257       break;
258 
259     case DW_LNE_define_file:
260       printf (_("  define new File Table entry\n"));
261       printf (_("  Entry\tDir\tTime\tSize\tName\n"));
262 
263       printf (_("   %d\t"), ++state_machine_regs.last_file_entry);
264       name = data;
265       data += strlen ((char *) data) + 1;
266       printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
267       data += bytes_read;
268       printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
269       data += bytes_read;
270       printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
271       printf (_("%s\n\n"), name);
272       break;
273 
274     default:
275       printf (_("UNKNOWN: length %d\n"), len - bytes_read);
276       break;
277     }
278 
279   return len;
280 }
281 
282 static const char *
fetch_indirect_string(unsigned long offset)283 fetch_indirect_string (unsigned long offset)
284 {
285   struct dwarf_section *section = &debug_displays [str].section;
286 
287   if (section->start == NULL)
288     return _("<no .debug_str section>");
289 
290   /* DWARF sections under Mach-O have non-zero addresses.  */
291   offset -= section->address;
292   if (offset > section->size)
293     {
294       warn (_("DW_FORM_strp offset too big: %lx\n"), offset);
295       return _("<offset is too big>");
296     }
297 
298   return (const char *) section->start + offset;
299 }
300 
301 /* FIXME:  There are better and more efficient ways to handle
302    these structures.  For now though, I just want something that
303    is simple to implement.  */
304 typedef struct abbrev_attr
305 {
306   unsigned long attribute;
307   unsigned long form;
308   struct abbrev_attr *next;
309 }
310 abbrev_attr;
311 
312 typedef struct abbrev_entry
313 {
314   unsigned long entry;
315   unsigned long tag;
316   int children;
317   struct abbrev_attr *first_attr;
318   struct abbrev_attr *last_attr;
319   struct abbrev_entry *next;
320 }
321 abbrev_entry;
322 
323 static abbrev_entry *first_abbrev = NULL;
324 static abbrev_entry *last_abbrev = NULL;
325 
326 static void
free_abbrevs(void)327 free_abbrevs (void)
328 {
329   abbrev_entry *abbrev;
330 
331   for (abbrev = first_abbrev; abbrev;)
332     {
333       abbrev_entry *next = abbrev->next;
334       abbrev_attr *attr;
335 
336       for (attr = abbrev->first_attr; attr;)
337 	{
338 	  abbrev_attr *next = attr->next;
339 
340 	  free (attr);
341 	  attr = next;
342 	}
343 
344       free (abbrev);
345       abbrev = next;
346     }
347 
348   last_abbrev = first_abbrev = NULL;
349 }
350 
351 static void
add_abbrev(unsigned long number,unsigned long tag,int children)352 add_abbrev (unsigned long number, unsigned long tag, int children)
353 {
354   abbrev_entry *entry;
355 
356   entry = malloc (sizeof (*entry));
357 
358   if (entry == NULL)
359     /* ugg */
360     return;
361 
362   entry->entry      = number;
363   entry->tag        = tag;
364   entry->children   = children;
365   entry->first_attr = NULL;
366   entry->last_attr  = NULL;
367   entry->next       = NULL;
368 
369   if (first_abbrev == NULL)
370     first_abbrev = entry;
371   else
372     last_abbrev->next = entry;
373 
374   last_abbrev = entry;
375 }
376 
377 static void
add_abbrev_attr(unsigned long attribute,unsigned long form)378 add_abbrev_attr (unsigned long attribute, unsigned long form)
379 {
380   abbrev_attr *attr;
381 
382   attr = malloc (sizeof (*attr));
383 
384   if (attr == NULL)
385     /* ugg */
386     return;
387 
388   attr->attribute = attribute;
389   attr->form      = form;
390   attr->next      = NULL;
391 
392   if (last_abbrev->first_attr == NULL)
393     last_abbrev->first_attr = attr;
394   else
395     last_abbrev->last_attr->next = attr;
396 
397   last_abbrev->last_attr = attr;
398 }
399 
400 /* Processes the (partial) contents of a .debug_abbrev section.
401    Returns NULL if the end of the section was encountered.
402    Returns the address after the last byte read if the end of
403    an abbreviation set was found.  */
404 
405 static unsigned char *
process_abbrev_section(unsigned char * start,unsigned char * end)406 process_abbrev_section (unsigned char *start, unsigned char *end)
407 {
408   if (first_abbrev != NULL)
409     return NULL;
410 
411   while (start < end)
412     {
413       unsigned int bytes_read;
414       unsigned long entry;
415       unsigned long tag;
416       unsigned long attribute;
417       int children;
418 
419       entry = read_leb128 (start, & bytes_read, 0);
420       start += bytes_read;
421 
422       /* A single zero is supposed to end the section according
423 	 to the standard.  If there's more, then signal that to
424 	 the caller.  */
425       if (entry == 0)
426 	return start == end ? NULL : start;
427 
428       tag = read_leb128 (start, & bytes_read, 0);
429       start += bytes_read;
430 
431       children = *start++;
432 
433       add_abbrev (entry, tag, children);
434 
435       do
436 	{
437 	  unsigned long form;
438 
439 	  attribute = read_leb128 (start, & bytes_read, 0);
440 	  start += bytes_read;
441 
442 	  form = read_leb128 (start, & bytes_read, 0);
443 	  start += bytes_read;
444 
445 	  if (attribute != 0)
446 	    add_abbrev_attr (attribute, form);
447 	}
448       while (attribute != 0);
449     }
450 
451   return NULL;
452 }
453 
454 static char *
get_TAG_name(unsigned long tag)455 get_TAG_name (unsigned long tag)
456 {
457   switch (tag)
458     {
459     case DW_TAG_padding:		return "DW_TAG_padding";
460     case DW_TAG_array_type:		return "DW_TAG_array_type";
461     case DW_TAG_class_type:		return "DW_TAG_class_type";
462     case DW_TAG_entry_point:		return "DW_TAG_entry_point";
463     case DW_TAG_enumeration_type:	return "DW_TAG_enumeration_type";
464     case DW_TAG_formal_parameter:	return "DW_TAG_formal_parameter";
465     case DW_TAG_imported_declaration:	return "DW_TAG_imported_declaration";
466     case DW_TAG_label:			return "DW_TAG_label";
467     case DW_TAG_lexical_block:		return "DW_TAG_lexical_block";
468     case DW_TAG_member:			return "DW_TAG_member";
469     case DW_TAG_pointer_type:		return "DW_TAG_pointer_type";
470     case DW_TAG_reference_type:		return "DW_TAG_reference_type";
471     case DW_TAG_compile_unit:		return "DW_TAG_compile_unit";
472     case DW_TAG_string_type:		return "DW_TAG_string_type";
473     case DW_TAG_structure_type:		return "DW_TAG_structure_type";
474     case DW_TAG_subroutine_type:	return "DW_TAG_subroutine_type";
475     case DW_TAG_typedef:		return "DW_TAG_typedef";
476     case DW_TAG_union_type:		return "DW_TAG_union_type";
477     case DW_TAG_unspecified_parameters: return "DW_TAG_unspecified_parameters";
478     case DW_TAG_variant:		return "DW_TAG_variant";
479     case DW_TAG_common_block:		return "DW_TAG_common_block";
480     case DW_TAG_common_inclusion:	return "DW_TAG_common_inclusion";
481     case DW_TAG_inheritance:		return "DW_TAG_inheritance";
482     case DW_TAG_inlined_subroutine:	return "DW_TAG_inlined_subroutine";
483     case DW_TAG_module:			return "DW_TAG_module";
484     case DW_TAG_ptr_to_member_type:	return "DW_TAG_ptr_to_member_type";
485     case DW_TAG_set_type:		return "DW_TAG_set_type";
486     case DW_TAG_subrange_type:		return "DW_TAG_subrange_type";
487     case DW_TAG_with_stmt:		return "DW_TAG_with_stmt";
488     case DW_TAG_access_declaration:	return "DW_TAG_access_declaration";
489     case DW_TAG_base_type:		return "DW_TAG_base_type";
490     case DW_TAG_catch_block:		return "DW_TAG_catch_block";
491     case DW_TAG_const_type:		return "DW_TAG_const_type";
492     case DW_TAG_constant:		return "DW_TAG_constant";
493     case DW_TAG_enumerator:		return "DW_TAG_enumerator";
494     case DW_TAG_file_type:		return "DW_TAG_file_type";
495     case DW_TAG_friend:			return "DW_TAG_friend";
496     case DW_TAG_namelist:		return "DW_TAG_namelist";
497     case DW_TAG_namelist_item:		return "DW_TAG_namelist_item";
498     case DW_TAG_packed_type:		return "DW_TAG_packed_type";
499     case DW_TAG_subprogram:		return "DW_TAG_subprogram";
500     case DW_TAG_template_type_param:	return "DW_TAG_template_type_param";
501     case DW_TAG_template_value_param:	return "DW_TAG_template_value_param";
502     case DW_TAG_thrown_type:		return "DW_TAG_thrown_type";
503     case DW_TAG_try_block:		return "DW_TAG_try_block";
504     case DW_TAG_variant_part:		return "DW_TAG_variant_part";
505     case DW_TAG_variable:		return "DW_TAG_variable";
506     case DW_TAG_volatile_type:		return "DW_TAG_volatile_type";
507     case DW_TAG_MIPS_loop:		return "DW_TAG_MIPS_loop";
508     case DW_TAG_format_label:		return "DW_TAG_format_label";
509     case DW_TAG_function_template:	return "DW_TAG_function_template";
510     case DW_TAG_class_template:		return "DW_TAG_class_template";
511       /* DWARF 2.1 values.  */
512     case DW_TAG_dwarf_procedure:	return "DW_TAG_dwarf_procedure";
513     case DW_TAG_restrict_type:		return "DW_TAG_restrict_type";
514     case DW_TAG_interface_type:		return "DW_TAG_interface_type";
515     case DW_TAG_namespace:		return "DW_TAG_namespace";
516     case DW_TAG_imported_module:	return "DW_TAG_imported_module";
517     case DW_TAG_unspecified_type:	return "DW_TAG_unspecified_type";
518     case DW_TAG_partial_unit:		return "DW_TAG_partial_unit";
519     case DW_TAG_imported_unit:		return "DW_TAG_imported_unit";
520       /* UPC values.  */
521     case DW_TAG_upc_shared_type:	return "DW_TAG_upc_shared_type";
522     case DW_TAG_upc_strict_type:	return "DW_TAG_upc_strict_type";
523     case DW_TAG_upc_relaxed_type:	return "DW_TAG_upc_relaxed_type";
524     default:
525       {
526 	static char buffer[100];
527 
528 	snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
529 	return buffer;
530       }
531     }
532 }
533 
534 static char *
get_FORM_name(unsigned long form)535 get_FORM_name (unsigned long form)
536 {
537   switch (form)
538     {
539     case DW_FORM_addr:		return "DW_FORM_addr";
540     case DW_FORM_block2:	return "DW_FORM_block2";
541     case DW_FORM_block4:	return "DW_FORM_block4";
542     case DW_FORM_data2:		return "DW_FORM_data2";
543     case DW_FORM_data4:		return "DW_FORM_data4";
544     case DW_FORM_data8:		return "DW_FORM_data8";
545     case DW_FORM_string:	return "DW_FORM_string";
546     case DW_FORM_block:		return "DW_FORM_block";
547     case DW_FORM_block1:	return "DW_FORM_block1";
548     case DW_FORM_data1:		return "DW_FORM_data1";
549     case DW_FORM_flag:		return "DW_FORM_flag";
550     case DW_FORM_sdata:		return "DW_FORM_sdata";
551     case DW_FORM_strp:		return "DW_FORM_strp";
552     case DW_FORM_udata:		return "DW_FORM_udata";
553     case DW_FORM_ref_addr:	return "DW_FORM_ref_addr";
554     case DW_FORM_ref1:		return "DW_FORM_ref1";
555     case DW_FORM_ref2:		return "DW_FORM_ref2";
556     case DW_FORM_ref4:		return "DW_FORM_ref4";
557     case DW_FORM_ref8:		return "DW_FORM_ref8";
558     case DW_FORM_ref_udata:	return "DW_FORM_ref_udata";
559     case DW_FORM_indirect:	return "DW_FORM_indirect";
560     case DW_FORM_flag_present:	return "DW_FORM_flag_present";
561     default:
562       {
563 	static char buffer[100];
564 
565 	snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
566 	return buffer;
567       }
568     }
569 }
570 
571 static unsigned char *
display_block(unsigned char * data,unsigned long length)572 display_block (unsigned char *data, unsigned long length)
573 {
574   printf (_(" %lu byte block: "), length);
575 
576   while (length --)
577     printf ("%lx ", (unsigned long) byte_get (data++, 1));
578 
579   return data;
580 }
581 
582 static int
decode_location_expression(unsigned char * data,unsigned int pointer_size,unsigned long length,unsigned long cu_offset)583 decode_location_expression (unsigned char * data,
584 			    unsigned int pointer_size,
585 			    unsigned long length,
586 			    unsigned long cu_offset)
587 {
588   unsigned op;
589   unsigned int bytes_read;
590   unsigned long uvalue;
591   unsigned char *end = data + length;
592   int need_frame_base = 0;
593 
594   while (data < end)
595     {
596       op = *data++;
597 
598       switch (op)
599 	{
600 	case DW_OP_addr:
601 	  printf ("DW_OP_addr: %lx",
602 		  (unsigned long) byte_get (data, pointer_size));
603 	  data += pointer_size;
604 	  break;
605 	case DW_OP_deref:
606 	  printf ("DW_OP_deref");
607 	  break;
608 	case DW_OP_const1u:
609 	  printf ("DW_OP_const1u: %lu", (unsigned long) byte_get (data++, 1));
610 	  break;
611 	case DW_OP_const1s:
612 	  printf ("DW_OP_const1s: %ld", (long) byte_get_signed (data++, 1));
613 	  break;
614 	case DW_OP_const2u:
615 	  printf ("DW_OP_const2u: %lu", (unsigned long) byte_get (data, 2));
616 	  data += 2;
617 	  break;
618 	case DW_OP_const2s:
619 	  printf ("DW_OP_const2s: %ld", (long) byte_get_signed (data, 2));
620 	  data += 2;
621 	  break;
622 	case DW_OP_const4u:
623 	  printf ("DW_OP_const4u: %lu", (unsigned long) byte_get (data, 4));
624 	  data += 4;
625 	  break;
626 	case DW_OP_const4s:
627 	  printf ("DW_OP_const4s: %ld", (long) byte_get_signed (data, 4));
628 	  data += 4;
629 	  break;
630 	case DW_OP_const8u:
631 	  printf ("DW_OP_const8u: %lu %lu", (unsigned long) byte_get (data, 4),
632 		  (unsigned long) byte_get (data + 4, 4));
633 	  data += 8;
634 	  break;
635 	case DW_OP_const8s:
636 	  printf ("DW_OP_const8s: %ld %ld", (long) byte_get (data, 4),
637 		  (long) byte_get (data + 4, 4));
638 	  data += 8;
639 	  break;
640 	case DW_OP_constu:
641 	  printf ("DW_OP_constu: %lu", read_leb128 (data, &bytes_read, 0));
642 	  data += bytes_read;
643 	  break;
644 	case DW_OP_consts:
645 	  printf ("DW_OP_consts: %ld", read_leb128 (data, &bytes_read, 1));
646 	  data += bytes_read;
647 	  break;
648 	case DW_OP_dup:
649 	  printf ("DW_OP_dup");
650 	  break;
651 	case DW_OP_drop:
652 	  printf ("DW_OP_drop");
653 	  break;
654 	case DW_OP_over:
655 	  printf ("DW_OP_over");
656 	  break;
657 	case DW_OP_pick:
658 	  printf ("DW_OP_pick: %ld", (unsigned long) byte_get (data++, 1));
659 	  break;
660 	case DW_OP_swap:
661 	  printf ("DW_OP_swap");
662 	  break;
663 	case DW_OP_rot:
664 	  printf ("DW_OP_rot");
665 	  break;
666 	case DW_OP_xderef:
667 	  printf ("DW_OP_xderef");
668 	  break;
669 	case DW_OP_abs:
670 	  printf ("DW_OP_abs");
671 	  break;
672 	case DW_OP_and:
673 	  printf ("DW_OP_and");
674 	  break;
675 	case DW_OP_div:
676 	  printf ("DW_OP_div");
677 	  break;
678 	case DW_OP_minus:
679 	  printf ("DW_OP_minus");
680 	  break;
681 	case DW_OP_mod:
682 	  printf ("DW_OP_mod");
683 	  break;
684 	case DW_OP_mul:
685 	  printf ("DW_OP_mul");
686 	  break;
687 	case DW_OP_neg:
688 	  printf ("DW_OP_neg");
689 	  break;
690 	case DW_OP_not:
691 	  printf ("DW_OP_not");
692 	  break;
693 	case DW_OP_or:
694 	  printf ("DW_OP_or");
695 	  break;
696 	case DW_OP_plus:
697 	  printf ("DW_OP_plus");
698 	  break;
699 	case DW_OP_plus_uconst:
700 	  printf ("DW_OP_plus_uconst: %lu",
701 		  read_leb128 (data, &bytes_read, 0));
702 	  data += bytes_read;
703 	  break;
704 	case DW_OP_shl:
705 	  printf ("DW_OP_shl");
706 	  break;
707 	case DW_OP_shr:
708 	  printf ("DW_OP_shr");
709 	  break;
710 	case DW_OP_shra:
711 	  printf ("DW_OP_shra");
712 	  break;
713 	case DW_OP_xor:
714 	  printf ("DW_OP_xor");
715 	  break;
716 	case DW_OP_bra:
717 	  printf ("DW_OP_bra: %ld", (long) byte_get_signed (data, 2));
718 	  data += 2;
719 	  break;
720 	case DW_OP_eq:
721 	  printf ("DW_OP_eq");
722 	  break;
723 	case DW_OP_ge:
724 	  printf ("DW_OP_ge");
725 	  break;
726 	case DW_OP_gt:
727 	  printf ("DW_OP_gt");
728 	  break;
729 	case DW_OP_le:
730 	  printf ("DW_OP_le");
731 	  break;
732 	case DW_OP_lt:
733 	  printf ("DW_OP_lt");
734 	  break;
735 	case DW_OP_ne:
736 	  printf ("DW_OP_ne");
737 	  break;
738 	case DW_OP_skip:
739 	  printf ("DW_OP_skip: %ld", (long) byte_get_signed (data, 2));
740 	  data += 2;
741 	  break;
742 
743 	case DW_OP_lit0:
744 	case DW_OP_lit1:
745 	case DW_OP_lit2:
746 	case DW_OP_lit3:
747 	case DW_OP_lit4:
748 	case DW_OP_lit5:
749 	case DW_OP_lit6:
750 	case DW_OP_lit7:
751 	case DW_OP_lit8:
752 	case DW_OP_lit9:
753 	case DW_OP_lit10:
754 	case DW_OP_lit11:
755 	case DW_OP_lit12:
756 	case DW_OP_lit13:
757 	case DW_OP_lit14:
758 	case DW_OP_lit15:
759 	case DW_OP_lit16:
760 	case DW_OP_lit17:
761 	case DW_OP_lit18:
762 	case DW_OP_lit19:
763 	case DW_OP_lit20:
764 	case DW_OP_lit21:
765 	case DW_OP_lit22:
766 	case DW_OP_lit23:
767 	case DW_OP_lit24:
768 	case DW_OP_lit25:
769 	case DW_OP_lit26:
770 	case DW_OP_lit27:
771 	case DW_OP_lit28:
772 	case DW_OP_lit29:
773 	case DW_OP_lit30:
774 	case DW_OP_lit31:
775 	  printf ("DW_OP_lit%d", op - DW_OP_lit0);
776 	  break;
777 
778 	case DW_OP_reg0:
779 	case DW_OP_reg1:
780 	case DW_OP_reg2:
781 	case DW_OP_reg3:
782 	case DW_OP_reg4:
783 	case DW_OP_reg5:
784 	case DW_OP_reg6:
785 	case DW_OP_reg7:
786 	case DW_OP_reg8:
787 	case DW_OP_reg9:
788 	case DW_OP_reg10:
789 	case DW_OP_reg11:
790 	case DW_OP_reg12:
791 	case DW_OP_reg13:
792 	case DW_OP_reg14:
793 	case DW_OP_reg15:
794 	case DW_OP_reg16:
795 	case DW_OP_reg17:
796 	case DW_OP_reg18:
797 	case DW_OP_reg19:
798 	case DW_OP_reg20:
799 	case DW_OP_reg21:
800 	case DW_OP_reg22:
801 	case DW_OP_reg23:
802 	case DW_OP_reg24:
803 	case DW_OP_reg25:
804 	case DW_OP_reg26:
805 	case DW_OP_reg27:
806 	case DW_OP_reg28:
807 	case DW_OP_reg29:
808 	case DW_OP_reg30:
809 	case DW_OP_reg31:
810 	  printf ("DW_OP_reg%d", op - DW_OP_reg0);
811 	  break;
812 
813 	case DW_OP_breg0:
814 	case DW_OP_breg1:
815 	case DW_OP_breg2:
816 	case DW_OP_breg3:
817 	case DW_OP_breg4:
818 	case DW_OP_breg5:
819 	case DW_OP_breg6:
820 	case DW_OP_breg7:
821 	case DW_OP_breg8:
822 	case DW_OP_breg9:
823 	case DW_OP_breg10:
824 	case DW_OP_breg11:
825 	case DW_OP_breg12:
826 	case DW_OP_breg13:
827 	case DW_OP_breg14:
828 	case DW_OP_breg15:
829 	case DW_OP_breg16:
830 	case DW_OP_breg17:
831 	case DW_OP_breg18:
832 	case DW_OP_breg19:
833 	case DW_OP_breg20:
834 	case DW_OP_breg21:
835 	case DW_OP_breg22:
836 	case DW_OP_breg23:
837 	case DW_OP_breg24:
838 	case DW_OP_breg25:
839 	case DW_OP_breg26:
840 	case DW_OP_breg27:
841 	case DW_OP_breg28:
842 	case DW_OP_breg29:
843 	case DW_OP_breg30:
844 	case DW_OP_breg31:
845 	  printf ("DW_OP_breg%d: %ld", op - DW_OP_breg0,
846 		  read_leb128 (data, &bytes_read, 1));
847 	  data += bytes_read;
848 	  break;
849 
850 	case DW_OP_regx:
851 	  printf ("DW_OP_regx: %lu", read_leb128 (data, &bytes_read, 0));
852 	  data += bytes_read;
853 	  break;
854 	case DW_OP_fbreg:
855 	  need_frame_base = 1;
856 	  printf ("DW_OP_fbreg: %ld", read_leb128 (data, &bytes_read, 1));
857 	  data += bytes_read;
858 	  break;
859 	case DW_OP_bregx:
860 	  uvalue = read_leb128 (data, &bytes_read, 0);
861 	  data += bytes_read;
862 	  printf ("DW_OP_bregx: %lu %ld", uvalue,
863 		  read_leb128 (data, &bytes_read, 1));
864 	  data += bytes_read;
865 	  break;
866 	case DW_OP_piece:
867 	  printf ("DW_OP_piece: %lu", read_leb128 (data, &bytes_read, 0));
868 	  data += bytes_read;
869 	  break;
870 	case DW_OP_deref_size:
871 	  printf ("DW_OP_deref_size: %ld", (long) byte_get (data++, 1));
872 	  break;
873 	case DW_OP_xderef_size:
874 	  printf ("DW_OP_xderef_size: %ld", (long) byte_get (data++, 1));
875 	  break;
876 	case DW_OP_nop:
877 	  printf ("DW_OP_nop");
878 	  break;
879 
880 	  /* DWARF 3 extensions.  */
881 	case DW_OP_push_object_address:
882 	  printf ("DW_OP_push_object_address");
883 	  break;
884 	case DW_OP_call2:
885 	  /* XXX: Strictly speaking for 64-bit DWARF3 files
886 	     this ought to be an 8-byte wide computation.  */
887 	  printf ("DW_OP_call2: <%lx>", (long) byte_get (data, 2) + cu_offset);
888 	  data += 2;
889 	  break;
890 	case DW_OP_call4:
891 	  /* XXX: Strictly speaking for 64-bit DWARF3 files
892 	     this ought to be an 8-byte wide computation.  */
893 	  printf ("DW_OP_call4: <%lx>", (long) byte_get (data, 4) + cu_offset);
894 	  data += 4;
895 	  break;
896 	case DW_OP_call_ref:
897 	  printf ("DW_OP_call_ref");
898 	  break;
899 	case DW_OP_form_tls_address:
900 	  printf ("DW_OP_form_tls_address");
901 	  break;
902 
903 	  /* GNU extensions.  */
904 	case DW_OP_GNU_push_tls_address:
905 	  printf ("DW_OP_GNU_push_tls_address");
906 	  break;
907 
908 	default:
909 	  if (op >= DW_OP_lo_user
910 	      && op <= DW_OP_hi_user)
911 	    printf (_("(User defined location op)"));
912 	  else
913 	    printf (_("(Unknown location op)"));
914 	  /* No way to tell where the next op is, so just bail.  */
915 	  return need_frame_base;
916 	}
917 
918       /* Separate the ops.  */
919       if (data < end)
920 	printf ("; ");
921     }
922 
923   return need_frame_base;
924 }
925 
926 static unsigned char *
read_and_display_attr_value(unsigned long attribute,unsigned long form,unsigned char * data,unsigned long cu_offset,unsigned long pointer_size,unsigned long offset_size,int dwarf_version,debug_info * debug_info_p,int do_loc)927 read_and_display_attr_value (unsigned long attribute,
928 			     unsigned long form,
929 			     unsigned char *data,
930 			     unsigned long cu_offset,
931 			     unsigned long pointer_size,
932 			     unsigned long offset_size,
933 			     int dwarf_version,
934 			     debug_info *debug_info_p,
935 			     int do_loc)
936 {
937   unsigned long uvalue = 0;
938   unsigned char *block_start = NULL;
939   unsigned int bytes_read;
940 
941   switch (form)
942     {
943     default:
944       break;
945 
946     case DW_FORM_ref_addr:
947       if (dwarf_version == 2)
948 	{
949 	  uvalue = byte_get (data, pointer_size);
950 	  data += pointer_size;
951 	}
952       else if (dwarf_version == 3)
953 	{
954 	  uvalue = byte_get (data, offset_size);
955 	  data += offset_size;
956 	}
957       else
958 	{
959 	  error (_("Internal error: DWARF version is not 2 or 3.\n"));
960 	}
961       break;
962 
963     case DW_FORM_addr:
964       uvalue = byte_get (data, pointer_size);
965       data += pointer_size;
966       break;
967 
968     case DW_FORM_strp:
969       uvalue = byte_get (data, offset_size);
970       data += offset_size;
971       break;
972 
973     case DW_FORM_flag_present:
974       uvalue = 1;
975       break;
976 
977     case DW_FORM_ref1:
978     case DW_FORM_flag:
979     case DW_FORM_data1:
980       uvalue = byte_get (data++, 1);
981       break;
982 
983     case DW_FORM_ref2:
984     case DW_FORM_data2:
985       uvalue = byte_get (data, 2);
986       data += 2;
987       break;
988 
989     case DW_FORM_ref4:
990     case DW_FORM_data4:
991       uvalue = byte_get (data, 4);
992       data += 4;
993       break;
994 
995     case DW_FORM_sdata:
996       uvalue = read_leb128 (data, & bytes_read, 1);
997       data += bytes_read;
998       break;
999 
1000     case DW_FORM_ref_udata:
1001     case DW_FORM_udata:
1002       uvalue = read_leb128 (data, & bytes_read, 0);
1003       data += bytes_read;
1004       break;
1005 
1006     case DW_FORM_indirect:
1007       form = read_leb128 (data, & bytes_read, 0);
1008       data += bytes_read;
1009       if (!do_loc)
1010 	printf (" %s", get_FORM_name (form));
1011       return read_and_display_attr_value (attribute, form, data,
1012 					  cu_offset, pointer_size,
1013 					  offset_size, dwarf_version,
1014 					  debug_info_p, do_loc);
1015     }
1016 
1017   switch (form)
1018     {
1019     case DW_FORM_ref_addr:
1020       if (!do_loc)
1021 	printf (" <#%lx>", uvalue);
1022       break;
1023 
1024     case DW_FORM_ref1:
1025     case DW_FORM_ref2:
1026     case DW_FORM_ref4:
1027     case DW_FORM_ref_udata:
1028       if (!do_loc)
1029 	printf (" <%lx>", uvalue + cu_offset);
1030       break;
1031 
1032     case DW_FORM_data4:
1033     case DW_FORM_addr:
1034       if (!do_loc)
1035 	printf (" %#lx", uvalue);
1036       break;
1037 
1038     case DW_FORM_flag_present:
1039     case DW_FORM_flag:
1040     case DW_FORM_data1:
1041     case DW_FORM_data2:
1042     case DW_FORM_sdata:
1043     case DW_FORM_udata:
1044       if (!do_loc)
1045 	printf (" %ld", uvalue);
1046       break;
1047 
1048     case DW_FORM_ref8:
1049     case DW_FORM_data8:
1050       if (!do_loc)
1051 	{
1052 	  uvalue = byte_get (data, 4);
1053 	  printf (" %lx", uvalue);
1054 	  printf (" %lx", (unsigned long) byte_get (data + 4, 4));
1055 	}
1056       if ((do_loc || do_debug_loc || do_debug_ranges)
1057 	  && num_debug_info_entries == 0)
1058 	{
1059 	  if (sizeof (uvalue) == 8)
1060 	    uvalue = byte_get (data, 8);
1061 	  else
1062 	    error (_("DW_FORM_data8 is unsupported when sizeof (unsigned long) != 8\n"));
1063 	}
1064       data += 8;
1065       break;
1066 
1067     case DW_FORM_string:
1068       if (!do_loc)
1069 	printf (" %s", data);
1070       data += strlen ((char *) data) + 1;
1071       break;
1072 
1073     case DW_FORM_block:
1074       uvalue = read_leb128 (data, & bytes_read, 0);
1075       block_start = data + bytes_read;
1076       if (do_loc)
1077 	data = block_start + uvalue;
1078       else
1079 	data = display_block (block_start, uvalue);
1080       break;
1081 
1082     case DW_FORM_block1:
1083       uvalue = byte_get (data, 1);
1084       block_start = data + 1;
1085       if (do_loc)
1086 	data = block_start + uvalue;
1087       else
1088 	data = display_block (block_start, uvalue);
1089       break;
1090 
1091     case DW_FORM_block2:
1092       uvalue = byte_get (data, 2);
1093       block_start = data + 2;
1094       if (do_loc)
1095 	data = block_start + uvalue;
1096       else
1097 	data = display_block (block_start, uvalue);
1098       break;
1099 
1100     case DW_FORM_block4:
1101       uvalue = byte_get (data, 4);
1102       block_start = data + 4;
1103       if (do_loc)
1104 	data = block_start + uvalue;
1105       else
1106 	data = display_block (block_start, uvalue);
1107       break;
1108 
1109     case DW_FORM_strp:
1110       if (!do_loc)
1111 	printf (_(" (indirect string, offset: 0x%lx): %s"),
1112 		uvalue, fetch_indirect_string (uvalue));
1113       break;
1114 
1115     case DW_FORM_indirect:
1116       /* Handled above.  */
1117       break;
1118 
1119     default:
1120       warn (_("Unrecognized form: %lu\n"), form);
1121       break;
1122     }
1123 
1124   /* For some attributes we can display further information.  */
1125   if ((do_loc || do_debug_loc || do_debug_ranges)
1126       && num_debug_info_entries == 0)
1127     {
1128       switch (attribute)
1129 	{
1130 	case DW_AT_frame_base:
1131 	  have_frame_base = 1;
1132 	case DW_AT_location:
1133 	case DW_AT_data_member_location:
1134 	case DW_AT_vtable_elem_location:
1135 	case DW_AT_allocated:
1136 	case DW_AT_associated:
1137 	case DW_AT_data_location:
1138 	case DW_AT_stride:
1139 	case DW_AT_upper_bound:
1140 	case DW_AT_lower_bound:
1141 	  if (form == DW_FORM_data4 || form == DW_FORM_data8)
1142 	    {
1143 	      /* Process location list.  */
1144 	      unsigned int max = debug_info_p->max_loc_offsets;
1145 	      unsigned int num = debug_info_p->num_loc_offsets;
1146 
1147 	      if (max == 0 || num >= max)
1148 		{
1149 		  max += 1024;
1150 		  debug_info_p->loc_offsets
1151 		    = xcrealloc (debug_info_p->loc_offsets,
1152 				 max, sizeof (*debug_info_p->loc_offsets));
1153 		  debug_info_p->have_frame_base
1154 		    = xcrealloc (debug_info_p->have_frame_base,
1155 				 max, sizeof (*debug_info_p->have_frame_base));
1156 		  debug_info_p->max_loc_offsets = max;
1157 		}
1158 	      debug_info_p->loc_offsets [num] = uvalue;
1159 	      debug_info_p->have_frame_base [num] = have_frame_base;
1160 	      debug_info_p->num_loc_offsets++;
1161 	    }
1162 	  break;
1163 
1164 	case DW_AT_low_pc:
1165 	  if (need_base_address)
1166 	    debug_info_p->base_address = uvalue;
1167 	  break;
1168 
1169 	case DW_AT_ranges:
1170 	  if (form == DW_FORM_data4 || form == DW_FORM_data8)
1171 	    {
1172 	      /* Process range list.  */
1173 	      unsigned int max = debug_info_p->max_range_lists;
1174 	      unsigned int num = debug_info_p->num_range_lists;
1175 
1176 	      if (max == 0 || num >= max)
1177 		{
1178 		  max += 1024;
1179 		  debug_info_p->range_lists
1180 		    = xcrealloc (debug_info_p->range_lists,
1181 				 max, sizeof (*debug_info_p->range_lists));
1182 		  debug_info_p->max_range_lists = max;
1183 		}
1184 	      debug_info_p->range_lists [num] = uvalue;
1185 	      debug_info_p->num_range_lists++;
1186 	    }
1187 	  break;
1188 
1189 	default:
1190 	  break;
1191 	}
1192     }
1193 
1194   if (do_loc)
1195     return data;
1196 
1197   printf ("\t");
1198 
1199   switch (attribute)
1200     {
1201     case DW_AT_inline:
1202       switch (uvalue)
1203 	{
1204 	case DW_INL_not_inlined:
1205 	  printf (_("(not inlined)"));
1206 	  break;
1207 	case DW_INL_inlined:
1208 	  printf (_("(inlined)"));
1209 	  break;
1210 	case DW_INL_declared_not_inlined:
1211 	  printf (_("(declared as inline but ignored)"));
1212 	  break;
1213 	case DW_INL_declared_inlined:
1214 	  printf (_("(declared as inline and inlined)"));
1215 	  break;
1216 	default:
1217 	  printf (_("  (Unknown inline attribute value: %lx)"), uvalue);
1218 	  break;
1219 	}
1220       break;
1221 
1222     case DW_AT_language:
1223       switch (uvalue)
1224 	{
1225 	  /* Ordered by the numeric value of these constants.  */
1226 	case DW_LANG_C89:		printf ("(ANSI C)"); break;
1227 	case DW_LANG_C:			printf ("(non-ANSI C)"); break;
1228 	case DW_LANG_Ada83:		printf ("(Ada)"); break;
1229 	case DW_LANG_C_plus_plus:	printf ("(C++)"); break;
1230 	case DW_LANG_Cobol74:		printf ("(Cobol 74)"); break;
1231 	case DW_LANG_Cobol85:		printf ("(Cobol 85)"); break;
1232 	case DW_LANG_Fortran77:		printf ("(FORTRAN 77)"); break;
1233 	case DW_LANG_Fortran90:		printf ("(Fortran 90)"); break;
1234 	case DW_LANG_Pascal83:		printf ("(ANSI Pascal)"); break;
1235 	case DW_LANG_Modula2:		printf ("(Modula 2)"); break;
1236 	  /* DWARF 2.1 values.	*/
1237 	case DW_LANG_Java:		printf ("(Java)"); break;
1238 	case DW_LANG_C99:		printf ("(ANSI C99)"); break;
1239 	case DW_LANG_Ada95:		printf ("(ADA 95)"); break;
1240 	case DW_LANG_Fortran95:		printf ("(Fortran 95)"); break;
1241 	  /* DWARF 3 values.  */
1242 	case DW_LANG_PLI:		printf ("(PLI)"); break;
1243 	case DW_LANG_ObjC:		printf ("(Objective C)"); break;
1244 	case DW_LANG_ObjC_plus_plus:	printf ("(Objective C++)"); break;
1245 	case DW_LANG_UPC:		printf ("(Unified Parallel C)"); break;
1246 	case DW_LANG_D:			printf ("(D)"); break;
1247 	  /* MIPS extension.  */
1248 	case DW_LANG_Mips_Assembler:	printf ("(MIPS assembler)"); break;
1249 	  /* UPC extension.  */
1250 	case DW_LANG_Upc:		printf ("(Unified Parallel C)"); break;
1251 	default:
1252 	  if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1253 	    printf ("(implementation defined: %lx)", uvalue);
1254 	  else
1255 	    printf ("(Unknown: %lx)", uvalue);
1256 	  break;
1257 	}
1258       break;
1259 
1260     case DW_AT_encoding:
1261       switch (uvalue)
1262 	{
1263 	case DW_ATE_void:		printf ("(void)"); break;
1264 	case DW_ATE_address:		printf ("(machine address)"); break;
1265 	case DW_ATE_boolean:		printf ("(boolean)"); break;
1266 	case DW_ATE_complex_float:	printf ("(complex float)"); break;
1267 	case DW_ATE_float:		printf ("(float)"); break;
1268 	case DW_ATE_signed:		printf ("(signed)"); break;
1269 	case DW_ATE_signed_char:	printf ("(signed char)"); break;
1270 	case DW_ATE_unsigned:		printf ("(unsigned)"); break;
1271 	case DW_ATE_unsigned_char:	printf ("(unsigned char)"); break;
1272 	  /* DWARF 2.1 value.  */
1273 	case DW_ATE_imaginary_float:	printf ("(imaginary float)"); break;
1274 	case DW_ATE_decimal_float:	printf ("(decimal float)"); break;
1275 	default:
1276 	  if (uvalue >= DW_ATE_lo_user
1277 	      && uvalue <= DW_ATE_hi_user)
1278 	    printf ("(user defined type)");
1279 	  else
1280 	    printf ("(unknown type)");
1281 	  break;
1282 	}
1283       break;
1284 
1285     case DW_AT_accessibility:
1286       switch (uvalue)
1287 	{
1288 	case DW_ACCESS_public:		printf ("(public)"); break;
1289 	case DW_ACCESS_protected:	printf ("(protected)"); break;
1290 	case DW_ACCESS_private:		printf ("(private)"); break;
1291 	default:
1292 	  printf ("(unknown accessibility)");
1293 	  break;
1294 	}
1295       break;
1296 
1297     case DW_AT_visibility:
1298       switch (uvalue)
1299 	{
1300 	case DW_VIS_local:		printf ("(local)"); break;
1301 	case DW_VIS_exported:		printf ("(exported)"); break;
1302 	case DW_VIS_qualified:		printf ("(qualified)"); break;
1303 	default:			printf ("(unknown visibility)"); break;
1304 	}
1305       break;
1306 
1307     case DW_AT_virtuality:
1308       switch (uvalue)
1309 	{
1310 	case DW_VIRTUALITY_none:	printf ("(none)"); break;
1311 	case DW_VIRTUALITY_virtual:	printf ("(virtual)"); break;
1312 	case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1313 	default:			printf ("(unknown virtuality)"); break;
1314 	}
1315       break;
1316 
1317     case DW_AT_identifier_case:
1318       switch (uvalue)
1319 	{
1320 	case DW_ID_case_sensitive:	printf ("(case_sensitive)"); break;
1321 	case DW_ID_up_case:		printf ("(up_case)"); break;
1322 	case DW_ID_down_case:		printf ("(down_case)"); break;
1323 	case DW_ID_case_insensitive:	printf ("(case_insensitive)"); break;
1324 	default:			printf ("(unknown case)"); break;
1325 	}
1326       break;
1327 
1328     case DW_AT_calling_convention:
1329       switch (uvalue)
1330 	{
1331 	case DW_CC_normal:	printf ("(normal)"); break;
1332 	case DW_CC_program:	printf ("(program)"); break;
1333 	case DW_CC_nocall:	printf ("(nocall)"); break;
1334 	default:
1335 	  if (uvalue >= DW_CC_lo_user
1336 	      && uvalue <= DW_CC_hi_user)
1337 	    printf ("(user defined)");
1338 	  else
1339 	    printf ("(unknown convention)");
1340 	}
1341       break;
1342 
1343     case DW_AT_ordering:
1344       switch (uvalue)
1345 	{
1346 	case -1: printf ("(undefined)"); break;
1347 	case 0:  printf ("(row major)"); break;
1348 	case 1:  printf ("(column major)"); break;
1349 	}
1350       break;
1351 
1352     case DW_AT_frame_base:
1353       have_frame_base = 1;
1354     case DW_AT_location:
1355     case DW_AT_data_member_location:
1356     case DW_AT_vtable_elem_location:
1357     case DW_AT_allocated:
1358     case DW_AT_associated:
1359     case DW_AT_data_location:
1360     case DW_AT_stride:
1361     case DW_AT_upper_bound:
1362     case DW_AT_lower_bound:
1363       if (block_start)
1364 	{
1365 	  int need_frame_base;
1366 
1367 	  printf ("(");
1368 	  need_frame_base = decode_location_expression (block_start,
1369 							pointer_size,
1370 							uvalue,
1371 							cu_offset);
1372 	  printf (")");
1373 	  if (need_frame_base && !have_frame_base)
1374 	    printf (_(" [without DW_AT_frame_base]"));
1375 	}
1376       else if (form == DW_FORM_data4 || form == DW_FORM_data8)
1377 	printf (_("(location list)"));
1378 
1379       break;
1380 
1381     default:
1382       break;
1383     }
1384 
1385   return data;
1386 }
1387 
1388 static char *
get_AT_name(unsigned long attribute)1389 get_AT_name (unsigned long attribute)
1390 {
1391   switch (attribute)
1392     {
1393     case DW_AT_sibling:			return "DW_AT_sibling";
1394     case DW_AT_location:		return "DW_AT_location";
1395     case DW_AT_name:			return "DW_AT_name";
1396     case DW_AT_ordering:		return "DW_AT_ordering";
1397     case DW_AT_subscr_data:		return "DW_AT_subscr_data";
1398     case DW_AT_byte_size:		return "DW_AT_byte_size";
1399     case DW_AT_bit_offset:		return "DW_AT_bit_offset";
1400     case DW_AT_bit_size:		return "DW_AT_bit_size";
1401     case DW_AT_element_list:		return "DW_AT_element_list";
1402     case DW_AT_stmt_list:		return "DW_AT_stmt_list";
1403     case DW_AT_low_pc:			return "DW_AT_low_pc";
1404     case DW_AT_high_pc:			return "DW_AT_high_pc";
1405     case DW_AT_language:		return "DW_AT_language";
1406     case DW_AT_member:			return "DW_AT_member";
1407     case DW_AT_discr:			return "DW_AT_discr";
1408     case DW_AT_discr_value:		return "DW_AT_discr_value";
1409     case DW_AT_visibility:		return "DW_AT_visibility";
1410     case DW_AT_import:			return "DW_AT_import";
1411     case DW_AT_string_length:		return "DW_AT_string_length";
1412     case DW_AT_common_reference:	return "DW_AT_common_reference";
1413     case DW_AT_comp_dir:		return "DW_AT_comp_dir";
1414     case DW_AT_const_value:		return "DW_AT_const_value";
1415     case DW_AT_containing_type:		return "DW_AT_containing_type";
1416     case DW_AT_default_value:		return "DW_AT_default_value";
1417     case DW_AT_inline:			return "DW_AT_inline";
1418     case DW_AT_is_optional:		return "DW_AT_is_optional";
1419     case DW_AT_lower_bound:		return "DW_AT_lower_bound";
1420     case DW_AT_producer:		return "DW_AT_producer";
1421     case DW_AT_prototyped:		return "DW_AT_prototyped";
1422     case DW_AT_return_addr:		return "DW_AT_return_addr";
1423     case DW_AT_start_scope:		return "DW_AT_start_scope";
1424     case DW_AT_stride_size:		return "DW_AT_stride_size";
1425     case DW_AT_upper_bound:		return "DW_AT_upper_bound";
1426     case DW_AT_abstract_origin:		return "DW_AT_abstract_origin";
1427     case DW_AT_accessibility:		return "DW_AT_accessibility";
1428     case DW_AT_address_class:		return "DW_AT_address_class";
1429     case DW_AT_artificial:		return "DW_AT_artificial";
1430     case DW_AT_base_types:		return "DW_AT_base_types";
1431     case DW_AT_calling_convention:	return "DW_AT_calling_convention";
1432     case DW_AT_count:			return "DW_AT_count";
1433     case DW_AT_data_member_location:	return "DW_AT_data_member_location";
1434     case DW_AT_decl_column:		return "DW_AT_decl_column";
1435     case DW_AT_decl_file:		return "DW_AT_decl_file";
1436     case DW_AT_decl_line:		return "DW_AT_decl_line";
1437     case DW_AT_declaration:		return "DW_AT_declaration";
1438     case DW_AT_discr_list:		return "DW_AT_discr_list";
1439     case DW_AT_encoding:		return "DW_AT_encoding";
1440     case DW_AT_external:		return "DW_AT_external";
1441     case DW_AT_frame_base:		return "DW_AT_frame_base";
1442     case DW_AT_friend:			return "DW_AT_friend";
1443     case DW_AT_identifier_case:		return "DW_AT_identifier_case";
1444     case DW_AT_macro_info:		return "DW_AT_macro_info";
1445     case DW_AT_namelist_items:		return "DW_AT_namelist_items";
1446     case DW_AT_priority:		return "DW_AT_priority";
1447     case DW_AT_segment:			return "DW_AT_segment";
1448     case DW_AT_specification:		return "DW_AT_specification";
1449     case DW_AT_static_link:		return "DW_AT_static_link";
1450     case DW_AT_type:			return "DW_AT_type";
1451     case DW_AT_use_location:		return "DW_AT_use_location";
1452     case DW_AT_variable_parameter:	return "DW_AT_variable_parameter";
1453     case DW_AT_virtuality:		return "DW_AT_virtuality";
1454     case DW_AT_vtable_elem_location:	return "DW_AT_vtable_elem_location";
1455       /* DWARF 2.1 values.  */
1456     case DW_AT_allocated:		return "DW_AT_allocated";
1457     case DW_AT_associated:		return "DW_AT_associated";
1458     case DW_AT_data_location:		return "DW_AT_data_location";
1459     case DW_AT_stride:			return "DW_AT_stride";
1460     case DW_AT_entry_pc:		return "DW_AT_entry_pc";
1461     case DW_AT_use_UTF8:		return "DW_AT_use_UTF8";
1462     case DW_AT_extension:		return "DW_AT_extension";
1463     case DW_AT_ranges:			return "DW_AT_ranges";
1464     case DW_AT_trampoline:		return "DW_AT_trampoline";
1465     case DW_AT_call_column:		return "DW_AT_call_column";
1466     case DW_AT_call_file:		return "DW_AT_call_file";
1467     case DW_AT_call_line:		return "DW_AT_call_line";
1468       /* SGI/MIPS extensions.  */
1469     case DW_AT_MIPS_fde:		return "DW_AT_MIPS_fde";
1470     case DW_AT_MIPS_loop_begin:		return "DW_AT_MIPS_loop_begin";
1471     case DW_AT_MIPS_tail_loop_begin:	return "DW_AT_MIPS_tail_loop_begin";
1472     case DW_AT_MIPS_epilog_begin:	return "DW_AT_MIPS_epilog_begin";
1473     case DW_AT_MIPS_loop_unroll_factor: return "DW_AT_MIPS_loop_unroll_factor";
1474     case DW_AT_MIPS_software_pipeline_depth:
1475       return "DW_AT_MIPS_software_pipeline_depth";
1476     case DW_AT_MIPS_linkage_name:	return "DW_AT_MIPS_linkage_name";
1477     case DW_AT_MIPS_stride:		return "DW_AT_MIPS_stride";
1478     case DW_AT_MIPS_abstract_name:	return "DW_AT_MIPS_abstract_name";
1479     case DW_AT_MIPS_clone_origin:	return "DW_AT_MIPS_clone_origin";
1480     case DW_AT_MIPS_has_inlines:	return "DW_AT_MIPS_has_inlines";
1481       /* GNU extensions.  */
1482     case DW_AT_sf_names:		return "DW_AT_sf_names";
1483     case DW_AT_src_info:		return "DW_AT_src_info";
1484     case DW_AT_mac_info:		return "DW_AT_mac_info";
1485     case DW_AT_src_coords:		return "DW_AT_src_coords";
1486     case DW_AT_body_begin:		return "DW_AT_body_begin";
1487     case DW_AT_body_end:		return "DW_AT_body_end";
1488     case DW_AT_GNU_vector:		return "DW_AT_GNU_vector";
1489       /* UPC extension.  */
1490     case DW_AT_upc_threads_scaled:	return "DW_AT_upc_threads_scaled";
1491     default:
1492       {
1493 	static char buffer[100];
1494 
1495 	snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1496 		  attribute);
1497 	return buffer;
1498       }
1499     }
1500 }
1501 
1502 static unsigned char *
read_and_display_attr(unsigned long attribute,unsigned long form,unsigned char * data,unsigned long cu_offset,unsigned long pointer_size,unsigned long offset_size,int dwarf_version,debug_info * debug_info_p,int do_loc)1503 read_and_display_attr (unsigned long attribute,
1504 		       unsigned long form,
1505 		       unsigned char *data,
1506 		       unsigned long cu_offset,
1507 		       unsigned long pointer_size,
1508 		       unsigned long offset_size,
1509 		       int dwarf_version,
1510 		       debug_info *debug_info_p,
1511 		       int do_loc)
1512 {
1513   if (!do_loc)
1514     printf ("     %-18s:", get_AT_name (attribute));
1515   data = read_and_display_attr_value (attribute, form, data, cu_offset,
1516 				      pointer_size, offset_size,
1517 				      dwarf_version, debug_info_p,
1518 				      do_loc);
1519   if (!do_loc)
1520     printf ("\n");
1521   return data;
1522 }
1523 
1524 
1525 /* Process the contents of a .debug_info section.  If do_loc is non-zero
1526    then we are scanning for location lists and we do not want to display
1527    anything to the user.  */
1528 
1529 static int
process_debug_info(struct dwarf_section * section,void * file,int do_loc)1530 process_debug_info (struct dwarf_section *section, void *file,
1531 		    int do_loc)
1532 {
1533   unsigned char *start = section->start;
1534   unsigned char *end = start + section->size;
1535   unsigned char *section_begin;
1536   unsigned int unit;
1537   unsigned int num_units = 0;
1538 
1539   if ((do_loc || do_debug_loc || do_debug_ranges)
1540       && num_debug_info_entries == 0)
1541     {
1542       unsigned long length;
1543 
1544       /* First scan the section to get the number of comp units.  */
1545       for (section_begin = start, num_units = 0; section_begin < end;
1546 	   num_units ++)
1547 	{
1548 	  /* Read the first 4 bytes.  For a 32-bit DWARF section, this
1549 	     will be the length.  For a 64-bit DWARF section, it'll be
1550 	     the escape code 0xffffffff followed by an 8 byte length.  */
1551 	  length = byte_get (section_begin, 4);
1552 
1553 	  if (length == 0xffffffff)
1554 	    {
1555 	      length = byte_get (section_begin + 4, 8);
1556 	      section_begin += length + 12;
1557 	    }
1558 	  else
1559 	    section_begin += length + 4;
1560 	}
1561 
1562       if (num_units == 0)
1563 	{
1564 	  error (_("No comp units in %s section ?"), section->name);
1565 	  return 0;
1566 	}
1567 
1568       /* Then allocate an array to hold the information.  */
1569       debug_information = cmalloc (num_units,
1570 				   sizeof (* debug_information));
1571       if (debug_information == NULL)
1572 	{
1573 	  error (_("Not enough memory for a debug info array of %u entries"),
1574 		 num_units);
1575 	  return 0;
1576 	}
1577     }
1578 
1579   if (!do_loc)
1580     {
1581       printf (_("The section %s contains:\n\n"), section->name);
1582 
1583       load_debug_section (str, file);
1584     }
1585 
1586   load_debug_section (abbrev, file);
1587   if (debug_displays [abbrev].section.start == NULL)
1588     {
1589       warn (_("Unable to locate %s section!\n"),
1590 	    debug_displays [abbrev].section.name);
1591       return 0;
1592     }
1593 
1594   for (section_begin = start, unit = 0; start < end; unit++)
1595     {
1596       DWARF2_Internal_CompUnit compunit;
1597       unsigned char *hdrptr;
1598       unsigned char *cu_abbrev_offset_ptr;
1599       unsigned char *tags;
1600       int level;
1601       unsigned long cu_offset;
1602       int offset_size;
1603       int initial_length_size;
1604 
1605       hdrptr = start;
1606 
1607       compunit.cu_length = byte_get (hdrptr, 4);
1608       hdrptr += 4;
1609 
1610       if (compunit.cu_length == 0xffffffff)
1611 	{
1612 	  compunit.cu_length = byte_get (hdrptr, 8);
1613 	  hdrptr += 8;
1614 	  offset_size = 8;
1615 	  initial_length_size = 12;
1616 	}
1617       else
1618 	{
1619 	  offset_size = 4;
1620 	  initial_length_size = 4;
1621 	}
1622 
1623       compunit.cu_version = byte_get (hdrptr, 2);
1624       hdrptr += 2;
1625 
1626       cu_offset = start - section_begin;
1627 
1628       cu_abbrev_offset_ptr = hdrptr;
1629       compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
1630       hdrptr += offset_size;
1631 
1632       compunit.cu_pointer_size = byte_get (hdrptr, 1);
1633       hdrptr += 1;
1634       if ((do_loc || do_debug_loc || do_debug_ranges)
1635 	  && num_debug_info_entries == 0)
1636 	{
1637 	  debug_information [unit].cu_offset = cu_offset;
1638 	  debug_information [unit].pointer_size
1639 	    = compunit.cu_pointer_size;
1640 	  debug_information [unit].base_address = 0;
1641 	  debug_information [unit].loc_offsets = NULL;
1642 	  debug_information [unit].have_frame_base = NULL;
1643 	  debug_information [unit].max_loc_offsets = 0;
1644 	  debug_information [unit].num_loc_offsets = 0;
1645 	  debug_information [unit].range_lists = NULL;
1646 	  debug_information [unit].max_range_lists= 0;
1647 	  debug_information [unit].num_range_lists = 0;
1648 	}
1649 
1650       if (!do_loc)
1651 	{
1652 	  printf (_("  Compilation Unit @ offset 0x%lx:\n"), cu_offset);
1653 	  printf (_("   Length:        %ld\n"), compunit.cu_length);
1654 	  printf (_("   Version:       %d\n"), compunit.cu_version);
1655 	  printf (_("   Abbrev Offset: %ld\n"), compunit.cu_abbrev_offset);
1656 	  printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
1657 	}
1658 
1659       if (cu_offset + compunit.cu_length + initial_length_size
1660 	  > section->size)
1661 	{
1662 	  warn (_("Debug info is corrupted, length is invalid (section is %lu bytes)\n"),
1663 		(unsigned long)section->size);
1664 	  break;
1665 	}
1666       tags = hdrptr;
1667       start += compunit.cu_length + initial_length_size;
1668 
1669       if (compunit.cu_version != 2 && compunit.cu_version != 3)
1670 	{
1671 	  warn (_("Only version 2 and 3 DWARF debug information is currently supported.\n"));
1672 	  continue;
1673 	}
1674 
1675       free_abbrevs ();
1676 
1677       /* Process the abbrevs used by this compilation unit. DWARF
1678 	 sections under Mach-O have non-zero addresses.  */
1679       if (compunit.cu_abbrev_offset >= debug_displays [abbrev].section.size)
1680 	warn (_("Debug info is corrupted, abbrev offset is invalid (section is %lu bytes)\n"),
1681 	      (unsigned long)debug_displays [abbrev].section.size);
1682       else
1683 	process_abbrev_section
1684 	  ((unsigned char *) debug_displays [abbrev].section.start
1685 	   + compunit.cu_abbrev_offset - debug_displays [abbrev].section.address,
1686 	   (unsigned char *) debug_displays [abbrev].section.start
1687 	   + debug_displays [abbrev].section.size);
1688 
1689       level = 0;
1690       while (tags < start)
1691 	{
1692 	  unsigned int bytes_read;
1693 	  unsigned long abbrev_number;
1694 	  abbrev_entry *entry;
1695 	  abbrev_attr *attr;
1696 
1697 	  abbrev_number = read_leb128 (tags, & bytes_read, 0);
1698 	  tags += bytes_read;
1699 
1700 	  /* A null DIE marks the end of a list of children.  */
1701 	  if (abbrev_number == 0)
1702 	    {
1703 	      --level;
1704 	      continue;
1705 	    }
1706 
1707 	  if (!do_loc)
1708 	    printf (_(" <%d><%lx>: Abbrev Number: %lu"),
1709 		    level,
1710 		    (unsigned long) (tags - section_begin
1711 				     - bytes_read),
1712 		    abbrev_number);
1713 
1714 	  /* Scan through the abbreviation list until we reach the
1715 	     correct entry.  */
1716 	  for (entry = first_abbrev;
1717 	       entry && entry->entry != abbrev_number;
1718 	       entry = entry->next)
1719 	    continue;
1720 
1721 	  if (entry == NULL)
1722 	    {
1723 	      if (!do_loc)
1724 		{
1725 		  printf ("\n");
1726 		  fflush (stdout);
1727 		}
1728 	      warn (_("Unable to locate entry %lu in the abbreviation table\n"),
1729 		    abbrev_number);
1730 	      return 0;
1731 	    }
1732 
1733 	  if (!do_loc)
1734 	    printf (_(" (%s)\n"), get_TAG_name (entry->tag));
1735 
1736 	  switch (entry->tag)
1737 	    {
1738 	    default:
1739 	      need_base_address = 0;
1740 	      break;
1741 	    case DW_TAG_compile_unit:
1742 	      need_base_address = 1;
1743 	      break;
1744 	    case DW_TAG_entry_point:
1745 	    case DW_TAG_subprogram:
1746 	      need_base_address = 0;
1747 	      /* Assuming that there is no DW_AT_frame_base.  */
1748 	      have_frame_base = 0;
1749 	      break;
1750 	    }
1751 
1752 	  for (attr = entry->first_attr; attr; attr = attr->next)
1753 	    {
1754 	      if (! do_loc)
1755 		/* Show the offset from where the tag was extracted.  */
1756 		printf ("  <%2lx>", (unsigned long)(tags - section_begin));
1757 
1758 	      tags = read_and_display_attr (attr->attribute,
1759 					    attr->form,
1760 					    tags, cu_offset,
1761 					    compunit.cu_pointer_size,
1762 					    offset_size,
1763 					    compunit.cu_version,
1764 					    &debug_information [unit],
1765 					    do_loc);
1766 	    }
1767 
1768  	  if (entry->children)
1769  	    ++level;
1770  	}
1771     }
1772 
1773   /* Set num_debug_info_entries here so that it can be used to check if
1774      we need to process .debug_loc and .debug_ranges sections.  */
1775   if ((do_loc || do_debug_loc || do_debug_ranges)
1776       && num_debug_info_entries == 0)
1777     num_debug_info_entries = num_units;
1778 
1779   if (!do_loc)
1780     {
1781       printf ("\n");
1782     }
1783 
1784   return 1;
1785 }
1786 
1787 /* Locate and scan the .debug_info section in the file and record the pointer
1788    sizes and offsets for the compilation units in it.  Usually an executable
1789    will have just one pointer size, but this is not guaranteed, and so we try
1790    not to make any assumptions.  Returns zero upon failure, or the number of
1791    compilation units upon success.  */
1792 
1793 static unsigned int
load_debug_info(void * file)1794 load_debug_info (void * file)
1795 {
1796   /* Reset the last pointer size so that we can issue correct error
1797      messages if we are displaying the contents of more than one section.  */
1798   last_pointer_size = 0;
1799   warned_about_missing_comp_units = FALSE;
1800 
1801   /* If we already have the information there is nothing else to do.  */
1802   if (num_debug_info_entries > 0)
1803     return num_debug_info_entries;
1804 
1805   if (load_debug_section (info, file)
1806       && process_debug_info (&debug_displays [info].section, file, 1))
1807     return num_debug_info_entries;
1808   else
1809     return 0;
1810 }
1811 
1812 static int
display_debug_lines(struct dwarf_section * section,void * file)1813 display_debug_lines (struct dwarf_section *section, void *file)
1814 {
1815   unsigned char *start = section->start;
1816   unsigned char *data = start;
1817   unsigned char *end = start + section->size;
1818 
1819   printf (_("\nDump of debug contents of section %s:\n\n"),
1820 	  section->name);
1821 
1822   load_debug_info (file);
1823 
1824   while (data < end)
1825     {
1826       DWARF2_Internal_LineInfo info;
1827       unsigned char *standard_opcodes;
1828       unsigned char *end_of_sequence;
1829       unsigned char *hdrptr;
1830       int initial_length_size;
1831       int offset_size;
1832       int i;
1833 
1834       hdrptr = data;
1835 
1836       /* Check the length of the block.  */
1837       info.li_length = byte_get (hdrptr, 4);
1838       hdrptr += 4;
1839 
1840       if (info.li_length == 0xffffffff)
1841 	{
1842 	  /* This section is 64-bit DWARF 3.  */
1843 	  info.li_length = byte_get (hdrptr, 8);
1844 	  hdrptr += 8;
1845 	  offset_size = 8;
1846 	  initial_length_size = 12;
1847 	}
1848       else
1849 	{
1850 	  offset_size = 4;
1851 	  initial_length_size = 4;
1852 	}
1853 
1854       if (info.li_length + initial_length_size > section->size)
1855 	{
1856 	  warn
1857 	    (_("The line info appears to be corrupt - the section is too small\n"));
1858 	  return 0;
1859 	}
1860 
1861       /* Check its version number.  */
1862       info.li_version = byte_get (hdrptr, 2);
1863       hdrptr += 2;
1864       if (info.li_version != 2 && info.li_version != 3)
1865 	{
1866 	  warn (_("Only DWARF version 2 and 3 line info is currently supported.\n"));
1867 	  return 0;
1868 	}
1869 
1870       info.li_prologue_length = byte_get (hdrptr, offset_size);
1871       hdrptr += offset_size;
1872       info.li_min_insn_length = byte_get (hdrptr, 1);
1873       hdrptr++;
1874       info.li_default_is_stmt = byte_get (hdrptr, 1);
1875       hdrptr++;
1876       info.li_line_base = byte_get (hdrptr, 1);
1877       hdrptr++;
1878       info.li_line_range = byte_get (hdrptr, 1);
1879       hdrptr++;
1880       info.li_opcode_base = byte_get (hdrptr, 1);
1881       hdrptr++;
1882 
1883       /* Sign extend the line base field.  */
1884       info.li_line_base <<= 24;
1885       info.li_line_base >>= 24;
1886 
1887       printf (_("  Length:                      %ld\n"), info.li_length);
1888       printf (_("  DWARF Version:               %d\n"), info.li_version);
1889       printf (_("  Prologue Length:             %d\n"), info.li_prologue_length);
1890       printf (_("  Minimum Instruction Length:  %d\n"), info.li_min_insn_length);
1891       printf (_("  Initial value of 'is_stmt':  %d\n"), info.li_default_is_stmt);
1892       printf (_("  Line Base:                   %d\n"), info.li_line_base);
1893       printf (_("  Line Range:                  %d\n"), info.li_line_range);
1894       printf (_("  Opcode Base:                 %d\n"), info.li_opcode_base);
1895 
1896       end_of_sequence = data + info.li_length + initial_length_size;
1897 
1898       reset_state_machine (info.li_default_is_stmt);
1899 
1900       /* Display the contents of the Opcodes table.  */
1901       standard_opcodes = hdrptr;
1902 
1903       printf (_("\n Opcodes:\n"));
1904 
1905       for (i = 1; i < info.li_opcode_base; i++)
1906 	printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
1907 
1908       /* Display the contents of the Directory table.  */
1909       data = standard_opcodes + info.li_opcode_base - 1;
1910 
1911       if (*data == 0)
1912 	printf (_("\n The Directory Table is empty.\n"));
1913       else
1914 	{
1915 	  printf (_("\n The Directory Table:\n"));
1916 
1917 	  while (*data != 0)
1918 	    {
1919 	      printf (_("  %s\n"), data);
1920 
1921 	      data += strlen ((char *) data) + 1;
1922 	    }
1923 	}
1924 
1925       /* Skip the NUL at the end of the table.  */
1926       data++;
1927 
1928       /* Display the contents of the File Name table.  */
1929       if (*data == 0)
1930 	printf (_("\n The File Name Table is empty.\n"));
1931       else
1932 	{
1933 	  printf (_("\n The File Name Table:\n"));
1934 	  printf (_("  Entry\tDir\tTime\tSize\tName\n"));
1935 
1936 	  while (*data != 0)
1937 	    {
1938 	      unsigned char *name;
1939 	      unsigned int bytes_read;
1940 
1941 	      printf (_("  %d\t"), ++state_machine_regs.last_file_entry);
1942 	      name = data;
1943 
1944 	      data += strlen ((char *) data) + 1;
1945 
1946 	      printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
1947 	      data += bytes_read;
1948 	      printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
1949 	      data += bytes_read;
1950 	      printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
1951 	      data += bytes_read;
1952 	      printf (_("%s\n"), name);
1953 	    }
1954 	}
1955 
1956       /* Skip the NUL at the end of the table.  */
1957       data++;
1958 
1959       /* Now display the statements.  */
1960       printf (_("\n Line Number Statements:\n"));
1961 
1962       while (data < end_of_sequence)
1963 	{
1964 	  unsigned char op_code;
1965 	  int adv;
1966 	  unsigned long int uladv;
1967 	  unsigned int bytes_read;
1968 
1969 	  op_code = *data++;
1970 
1971 	  if (op_code >= info.li_opcode_base)
1972 	    {
1973 	      op_code -= info.li_opcode_base;
1974 	      uladv = (op_code / info.li_line_range) * info.li_min_insn_length;
1975 	      state_machine_regs.address += uladv;
1976 	      printf (_("  Special opcode %d: advance Address by %lu to 0x%lx"),
1977 		      op_code, uladv, state_machine_regs.address);
1978 	      adv = (op_code % info.li_line_range) + info.li_line_base;
1979 	      state_machine_regs.line += adv;
1980 	      printf (_(" and Line by %d to %d\n"),
1981 		      adv, state_machine_regs.line);
1982 	    }
1983 	  else switch (op_code)
1984 	    {
1985 	    case DW_LNS_extended_op:
1986 	      data += process_extended_line_op (data, info.li_default_is_stmt);
1987 	      break;
1988 
1989 	    case DW_LNS_copy:
1990 	      printf (_("  Copy\n"));
1991 	      break;
1992 
1993 	    case DW_LNS_advance_pc:
1994 	      uladv = read_leb128 (data, & bytes_read, 0);
1995 	      uladv *= info.li_min_insn_length;
1996 	      data += bytes_read;
1997 	      state_machine_regs.address += uladv;
1998 	      printf (_("  Advance PC by %lu to 0x%lx\n"), uladv,
1999 		      state_machine_regs.address);
2000 	      break;
2001 
2002 	    case DW_LNS_advance_line:
2003 	      adv = read_leb128 (data, & bytes_read, 1);
2004 	      data += bytes_read;
2005 	      state_machine_regs.line += adv;
2006 	      printf (_("  Advance Line by %d to %d\n"), adv,
2007 		      state_machine_regs.line);
2008 	      break;
2009 
2010 	    case DW_LNS_set_file:
2011 	      adv = read_leb128 (data, & bytes_read, 0);
2012 	      data += bytes_read;
2013 	      printf (_("  Set File Name to entry %d in the File Name Table\n"),
2014 		      adv);
2015 	      state_machine_regs.file = adv;
2016 	      break;
2017 
2018 	    case DW_LNS_set_column:
2019 	      uladv = read_leb128 (data, & bytes_read, 0);
2020 	      data += bytes_read;
2021 	      printf (_("  Set column to %lu\n"), uladv);
2022 	      state_machine_regs.column = uladv;
2023 	      break;
2024 
2025 	    case DW_LNS_negate_stmt:
2026 	      adv = state_machine_regs.is_stmt;
2027 	      adv = ! adv;
2028 	      printf (_("  Set is_stmt to %d\n"), adv);
2029 	      state_machine_regs.is_stmt = adv;
2030 	      break;
2031 
2032 	    case DW_LNS_set_basic_block:
2033 	      printf (_("  Set basic block\n"));
2034 	      state_machine_regs.basic_block = 1;
2035 	      break;
2036 
2037 	    case DW_LNS_const_add_pc:
2038 	      uladv = (((255 - info.li_opcode_base) / info.li_line_range)
2039 		      * info.li_min_insn_length);
2040 	      state_machine_regs.address += uladv;
2041 	      printf (_("  Advance PC by constant %lu to 0x%lx\n"), uladv,
2042 		      state_machine_regs.address);
2043 	      break;
2044 
2045 	    case DW_LNS_fixed_advance_pc:
2046 	      uladv = byte_get (data, 2);
2047 	      data += 2;
2048 	      state_machine_regs.address += uladv;
2049 	      printf (_("  Advance PC by fixed size amount %lu to 0x%lx\n"),
2050 		      uladv, state_machine_regs.address);
2051 	      break;
2052 
2053 	    case DW_LNS_set_prologue_end:
2054 	      printf (_("  Set prologue_end to true\n"));
2055 	      break;
2056 
2057 	    case DW_LNS_set_epilogue_begin:
2058 	      printf (_("  Set epilogue_begin to true\n"));
2059 	      break;
2060 
2061 	    case DW_LNS_set_isa:
2062 	      uladv = read_leb128 (data, & bytes_read, 0);
2063 	      data += bytes_read;
2064 	      printf (_("  Set ISA to %lu\n"), uladv);
2065 	      break;
2066 
2067 	    default:
2068 	      printf (_("  Unknown opcode %d with operands: "), op_code);
2069 
2070 	      for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2071 		{
2072 		  printf ("0x%lx%s", read_leb128 (data, &bytes_read, 0),
2073 			  i == 1 ? "" : ", ");
2074 		  data += bytes_read;
2075 		}
2076 	      putchar ('\n');
2077 	      break;
2078 	    }
2079 	}
2080       putchar ('\n');
2081     }
2082 
2083   return 1;
2084 }
2085 
2086 static int
display_debug_pubnames(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)2087 display_debug_pubnames (struct dwarf_section *section,
2088 			void *file ATTRIBUTE_UNUSED)
2089 {
2090   DWARF2_Internal_PubNames pubnames;
2091   unsigned char *start = section->start;
2092   unsigned char *end = start + section->size;
2093 
2094   printf (_("Contents of the %s section:\n\n"), section->name);
2095 
2096   while (start < end)
2097     {
2098       unsigned char *data;
2099       unsigned long offset;
2100       int offset_size, initial_length_size;
2101 
2102       data = start;
2103 
2104       pubnames.pn_length = byte_get (data, 4);
2105       data += 4;
2106       if (pubnames.pn_length == 0xffffffff)
2107 	{
2108 	  pubnames.pn_length = byte_get (data, 8);
2109 	  data += 8;
2110 	  offset_size = 8;
2111 	  initial_length_size = 12;
2112 	}
2113       else
2114 	{
2115 	  offset_size = 4;
2116 	  initial_length_size = 4;
2117 	}
2118 
2119       pubnames.pn_version = byte_get (data, 2);
2120       data += 2;
2121       pubnames.pn_offset = byte_get (data, offset_size);
2122       data += offset_size;
2123       pubnames.pn_size = byte_get (data, offset_size);
2124       data += offset_size;
2125 
2126       start += pubnames.pn_length + initial_length_size;
2127 
2128       if (pubnames.pn_version != 2 && pubnames.pn_version != 3)
2129 	{
2130 	  static int warned = 0;
2131 
2132 	  if (! warned)
2133 	    {
2134 	      warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
2135 	      warned = 1;
2136 	    }
2137 
2138 	  continue;
2139 	}
2140 
2141       printf (_("  Length:                              %ld\n"),
2142 	      pubnames.pn_length);
2143       printf (_("  Version:                             %d\n"),
2144 	      pubnames.pn_version);
2145       printf (_("  Offset into .debug_info section:     %ld\n"),
2146 	      pubnames.pn_offset);
2147       printf (_("  Size of area in .debug_info section: %ld\n"),
2148 	      pubnames.pn_size);
2149 
2150       printf (_("\n    Offset\tName\n"));
2151 
2152       do
2153 	{
2154 	  offset = byte_get (data, offset_size);
2155 
2156 	  if (offset != 0)
2157 	    {
2158 	      data += offset_size;
2159 	      printf ("    %-6ld\t\t%s\n", offset, data);
2160 	      data += strlen ((char *) data) + 1;
2161 	    }
2162 	}
2163       while (offset != 0);
2164     }
2165 
2166   printf ("\n");
2167   return 1;
2168 }
2169 
2170 static int
display_debug_macinfo(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)2171 display_debug_macinfo (struct dwarf_section *section,
2172 		       void *file ATTRIBUTE_UNUSED)
2173 {
2174   unsigned char *start = section->start;
2175   unsigned char *end = start + section->size;
2176   unsigned char *curr = start;
2177   unsigned int bytes_read;
2178   enum dwarf_macinfo_record_type op;
2179 
2180   printf (_("Contents of the %s section:\n\n"), section->name);
2181 
2182   while (curr < end)
2183     {
2184       unsigned int lineno;
2185       const char *string;
2186 
2187       op = *curr;
2188       curr++;
2189 
2190       switch (op)
2191 	{
2192 	case DW_MACINFO_start_file:
2193 	  {
2194 	    unsigned int filenum;
2195 
2196 	    lineno = read_leb128 (curr, & bytes_read, 0);
2197 	    curr += bytes_read;
2198 	    filenum = read_leb128 (curr, & bytes_read, 0);
2199 	    curr += bytes_read;
2200 
2201 	    printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
2202 		    lineno, filenum);
2203 	  }
2204 	  break;
2205 
2206 	case DW_MACINFO_end_file:
2207 	  printf (_(" DW_MACINFO_end_file\n"));
2208 	  break;
2209 
2210 	case DW_MACINFO_define:
2211 	  lineno = read_leb128 (curr, & bytes_read, 0);
2212 	  curr += bytes_read;
2213 	  string = (char *) curr;
2214 	  curr += strlen (string) + 1;
2215 	  printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
2216 		  lineno, string);
2217 	  break;
2218 
2219 	case DW_MACINFO_undef:
2220 	  lineno = read_leb128 (curr, & bytes_read, 0);
2221 	  curr += bytes_read;
2222 	  string = (char *) curr;
2223 	  curr += strlen (string) + 1;
2224 	  printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
2225 		  lineno, string);
2226 	  break;
2227 
2228 	case DW_MACINFO_vendor_ext:
2229 	  {
2230 	    unsigned int constant;
2231 
2232 	    constant = read_leb128 (curr, & bytes_read, 0);
2233 	    curr += bytes_read;
2234 	    string = (char *) curr;
2235 	    curr += strlen (string) + 1;
2236 	    printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
2237 		    constant, string);
2238 	  }
2239 	  break;
2240 	}
2241     }
2242 
2243   return 1;
2244 }
2245 
2246 static int
display_debug_abbrev(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)2247 display_debug_abbrev (struct dwarf_section *section,
2248 		      void *file ATTRIBUTE_UNUSED)
2249 {
2250   abbrev_entry *entry;
2251   unsigned char *start = section->start;
2252   unsigned char *end = start + section->size;
2253 
2254   printf (_("Contents of the %s section:\n\n"), section->name);
2255 
2256   do
2257     {
2258       free_abbrevs ();
2259 
2260       start = process_abbrev_section (start, end);
2261 
2262       if (first_abbrev == NULL)
2263 	continue;
2264 
2265       printf (_("  Number TAG\n"));
2266 
2267       for (entry = first_abbrev; entry; entry = entry->next)
2268 	{
2269 	  abbrev_attr *attr;
2270 
2271 	  printf (_("   %ld      %s    [%s]\n"),
2272 		  entry->entry,
2273 		  get_TAG_name (entry->tag),
2274 		  entry->children ? _("has children") : _("no children"));
2275 
2276 	  for (attr = entry->first_attr; attr; attr = attr->next)
2277 	    printf (_("    %-18s %s\n"),
2278 		    get_AT_name (attr->attribute),
2279 		    get_FORM_name (attr->form));
2280 	}
2281     }
2282   while (start);
2283 
2284   printf ("\n");
2285 
2286   return 1;
2287 }
2288 
2289 static int
display_debug_loc(struct dwarf_section * section,void * file)2290 display_debug_loc (struct dwarf_section *section, void *file)
2291 {
2292   unsigned char *start = section->start;
2293   unsigned char *section_end;
2294   unsigned long bytes;
2295   unsigned char *section_begin = start;
2296   unsigned int num_loc_list = 0;
2297   unsigned long last_offset = 0;
2298   unsigned int first = 0;
2299   unsigned int i;
2300   unsigned int j;
2301   int seen_first_offset = 0;
2302   int use_debug_info = 1;
2303   unsigned char *next;
2304 
2305   bytes = section->size;
2306   section_end = start + bytes;
2307 
2308   if (bytes == 0)
2309     {
2310       printf (_("\nThe %s section is empty.\n"), section->name);
2311       return 0;
2312     }
2313 
2314   load_debug_info (file);
2315 
2316   /* Check the order of location list in .debug_info section. If
2317      offsets of location lists are in the ascending order, we can
2318      use `debug_information' directly.  */
2319   for (i = 0; i < num_debug_info_entries; i++)
2320     {
2321       unsigned int num;
2322 
2323       num = debug_information [i].num_loc_offsets;
2324       num_loc_list += num;
2325 
2326       /* Check if we can use `debug_information' directly.  */
2327       if (use_debug_info && num != 0)
2328 	{
2329 	  if (!seen_first_offset)
2330 	    {
2331 	      /* This is the first location list.  */
2332 	      last_offset = debug_information [i].loc_offsets [0];
2333 	      first = i;
2334 	      seen_first_offset = 1;
2335 	      j = 1;
2336 	    }
2337 	  else
2338 	    j = 0;
2339 
2340 	  for (; j < num; j++)
2341 	    {
2342 	      if (last_offset >
2343 		  debug_information [i].loc_offsets [j])
2344 		{
2345 		  use_debug_info = 0;
2346 		  break;
2347 		}
2348 	      last_offset = debug_information [i].loc_offsets [j];
2349 	    }
2350 	}
2351     }
2352 
2353   if (!use_debug_info)
2354     /* FIXME: Should we handle this case?  */
2355     error (_("Location lists in .debug_info section aren't in ascending order!\n"));
2356 
2357   if (!seen_first_offset)
2358     error (_("No location lists in .debug_info section!\n"));
2359 
2360   /* DWARF sections under Mach-O have non-zero addresses.  */
2361   if (debug_information [first].num_loc_offsets > 0
2362       && debug_information [first].loc_offsets [0] != section->address)
2363     warn (_("Location lists in %s section start at 0x%lx\n"),
2364 	  section->name, debug_information [first].loc_offsets [0]);
2365 
2366   printf (_("Contents of the %s section:\n\n"), section->name);
2367   printf (_("    Offset   Begin    End      Expression\n"));
2368 
2369   seen_first_offset = 0;
2370   for (i = first; i < num_debug_info_entries; i++)
2371     {
2372       unsigned long begin;
2373       unsigned long end;
2374       unsigned short length;
2375       unsigned long offset;
2376       unsigned int pointer_size;
2377       unsigned long cu_offset;
2378       unsigned long base_address;
2379       int need_frame_base;
2380       int has_frame_base;
2381 
2382       pointer_size = debug_information [i].pointer_size;
2383       cu_offset = debug_information [i].cu_offset;
2384 
2385       for (j = 0; j < debug_information [i].num_loc_offsets; j++)
2386 	{
2387 	  has_frame_base = debug_information [i].have_frame_base [j];
2388 	  /* DWARF sections under Mach-O have non-zero addresses.  */
2389 	  offset = debug_information [i].loc_offsets [j] - section->address;
2390 	  next = section_begin + offset;
2391 	  base_address = debug_information [i].base_address;
2392 
2393 	  if (!seen_first_offset)
2394 	    seen_first_offset = 1;
2395 	  else
2396 	    {
2397 	      if (start < next)
2398 		warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
2399 		      (long)(start - section_begin), (long)(next - section_begin));
2400 	      else if (start > next)
2401 		warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
2402 		      (long)(start - section_begin), (long)(next - section_begin));
2403 	    }
2404 	  start = next;
2405 
2406 	  if (offset >= bytes)
2407 	    {
2408 	      warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
2409 		    offset);
2410 	      continue;
2411 	    }
2412 
2413 	  while (1)
2414 	    {
2415 	      if (start + 2 * pointer_size > section_end)
2416 		{
2417 		  warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2418 			offset);
2419 		  break;
2420 		}
2421 
2422 	      begin = byte_get (start, pointer_size);
2423 	      start += pointer_size;
2424 	      end = byte_get (start, pointer_size);
2425 	      start += pointer_size;
2426 
2427 	      if (begin == 0 && end == 0)
2428 		{
2429 		  printf (_("    %8.8lx <End of list>\n"), offset);
2430 		  break;
2431 		}
2432 
2433 	      /* Check base address specifiers.  */
2434 	      if (begin == -1UL && end != -1UL)
2435 		{
2436 		  base_address = end;
2437 		  printf (_("    %8.8lx %8.8lx %8.8lx (base address)\n"),
2438 			  offset, begin, end);
2439 		  continue;
2440 		}
2441 
2442 	      if (start + 2 > section_end)
2443 		{
2444 		  warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2445 			offset);
2446 		  break;
2447 		}
2448 
2449 	      length = byte_get (start, 2);
2450 	      start += 2;
2451 
2452 	      if (start + length > section_end)
2453 		{
2454 		  warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2455 			offset);
2456 		  break;
2457 		}
2458 
2459 	      printf ("    %8.8lx %8.8lx %8.8lx (",
2460 		      offset, begin + base_address, end + base_address);
2461 	      need_frame_base = decode_location_expression (start,
2462 							    pointer_size,
2463 							    length,
2464 							    cu_offset);
2465 	      putchar (')');
2466 
2467 	      if (need_frame_base && !has_frame_base)
2468 		printf (_(" [without DW_AT_frame_base]"));
2469 
2470 	      if (begin == end)
2471 		fputs (_(" (start == end)"), stdout);
2472 	      else if (begin > end)
2473 		fputs (_(" (start > end)"), stdout);
2474 
2475 	      putchar ('\n');
2476 
2477 	      start += length;
2478 	    }
2479 	}
2480     }
2481   return 1;
2482 }
2483 
2484 static int
display_debug_str(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)2485 display_debug_str (struct dwarf_section *section,
2486 		   void *file ATTRIBUTE_UNUSED)
2487 {
2488   unsigned char *start = section->start;
2489   unsigned long bytes = section->size;
2490   dwarf_vma addr = section->address;
2491 
2492   if (bytes == 0)
2493     {
2494       printf (_("\nThe %s section is empty.\n"), section->name);
2495       return 0;
2496     }
2497 
2498   printf (_("Contents of the %s section:\n\n"), section->name);
2499 
2500   while (bytes)
2501     {
2502       int j;
2503       int k;
2504       int lbytes;
2505 
2506       lbytes = (bytes > 16 ? 16 : bytes);
2507 
2508       printf ("  0x%8.8lx ", (unsigned long) addr);
2509 
2510       for (j = 0; j < 16; j++)
2511 	{
2512 	  if (j < lbytes)
2513 	    printf ("%2.2x", start[j]);
2514 	  else
2515 	    printf ("  ");
2516 
2517 	  if ((j & 3) == 3)
2518 	    printf (" ");
2519 	}
2520 
2521       for (j = 0; j < lbytes; j++)
2522 	{
2523 	  k = start[j];
2524 	  if (k >= ' ' && k < 0x80)
2525 	    printf ("%c", k);
2526 	  else
2527 	    printf (".");
2528 	}
2529 
2530       putchar ('\n');
2531 
2532       start += lbytes;
2533       addr  += lbytes;
2534       bytes -= lbytes;
2535     }
2536 
2537   putchar ('\n');
2538 
2539   return 1;
2540 }
2541 
2542 static int
display_debug_info(struct dwarf_section * section,void * file)2543 display_debug_info (struct dwarf_section *section, void *file)
2544 {
2545   return process_debug_info (section, file, 0);
2546 }
2547 
2548 
2549 static int
display_debug_aranges(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)2550 display_debug_aranges (struct dwarf_section *section,
2551 		       void *file ATTRIBUTE_UNUSED)
2552 {
2553   unsigned char *start = section->start;
2554   unsigned char *end = start + section->size;
2555 
2556   printf (_("The section %s contains:\n\n"), section->name);
2557 
2558   while (start < end)
2559     {
2560       unsigned char *hdrptr;
2561       DWARF2_Internal_ARange arange;
2562       unsigned char *ranges;
2563       unsigned long length;
2564       unsigned long address;
2565       unsigned char address_size;
2566       int excess;
2567       int offset_size;
2568       int initial_length_size;
2569 
2570       hdrptr = start;
2571 
2572       arange.ar_length = byte_get (hdrptr, 4);
2573       hdrptr += 4;
2574 
2575       if (arange.ar_length == 0xffffffff)
2576 	{
2577 	  arange.ar_length = byte_get (hdrptr, 8);
2578 	  hdrptr += 8;
2579 	  offset_size = 8;
2580 	  initial_length_size = 12;
2581 	}
2582       else
2583 	{
2584 	  offset_size = 4;
2585 	  initial_length_size = 4;
2586 	}
2587 
2588       arange.ar_version = byte_get (hdrptr, 2);
2589       hdrptr += 2;
2590 
2591       arange.ar_info_offset = byte_get (hdrptr, offset_size);
2592       hdrptr += offset_size;
2593 
2594       arange.ar_pointer_size = byte_get (hdrptr, 1);
2595       hdrptr += 1;
2596 
2597       arange.ar_segment_size = byte_get (hdrptr, 1);
2598       hdrptr += 1;
2599 
2600       if (arange.ar_version != 2 && arange.ar_version != 3)
2601 	{
2602 	  warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
2603 	  break;
2604 	}
2605 
2606       printf (_("  Length:                   %ld\n"), arange.ar_length);
2607       printf (_("  Version:                  %d\n"), arange.ar_version);
2608       printf (_("  Offset into .debug_info:  %lx\n"), arange.ar_info_offset);
2609       printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
2610       printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
2611 
2612       address_size = arange.ar_pointer_size + arange.ar_segment_size;
2613 
2614       /* The DWARF spec does not require that the address size be a power
2615 	 of two, but we do.  This will have to change if we ever encounter
2616 	 an uneven architecture.  */
2617       if ((address_size & (address_size - 1)) != 0)
2618 	{
2619 	  warn (_("Pointer size + Segment size is not a power of two.\n"));
2620 	  break;
2621 	}
2622 
2623       if (address_size > 4)
2624 	printf (_("\n    Address            Length\n"));
2625       else
2626 	printf (_("\n    Address    Length\n"));
2627 
2628       ranges = hdrptr;
2629 
2630       /* Must pad to an alignment boundary that is twice the address size.  */
2631       excess = (hdrptr - start) % (2 * address_size);
2632       if (excess)
2633 	ranges += (2 * address_size) - excess;
2634 
2635       start += arange.ar_length + initial_length_size;
2636 
2637       while (ranges + 2 * address_size <= start)
2638 	{
2639 	  address = byte_get (ranges, address_size);
2640 
2641 	  ranges += address_size;
2642 
2643 	  length  = byte_get (ranges, address_size);
2644 
2645 	  ranges += address_size;
2646 
2647 	  if (address_size > 4)
2648 	    printf ("    0x%16.16lx 0x%lx\n", address, length);
2649 	  else
2650 	    printf ("    0x%8.8lx 0x%lx\n", address, length);
2651 	}
2652     }
2653 
2654   printf ("\n");
2655 
2656   return 1;
2657 }
2658 
2659 static int
display_debug_ranges(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)2660 display_debug_ranges (struct dwarf_section *section,
2661 		      void *file ATTRIBUTE_UNUSED)
2662 {
2663   unsigned char *start = section->start;
2664   unsigned char *section_end;
2665   unsigned long bytes;
2666   unsigned char *section_begin = start;
2667   unsigned int num_range_list = 0;
2668   unsigned long last_offset = 0;
2669   unsigned int first = 0;
2670   unsigned int i;
2671   unsigned int j;
2672   int seen_first_offset = 0;
2673   int use_debug_info = 1;
2674   unsigned char *next;
2675 
2676   bytes = section->size;
2677   section_end = start + bytes;
2678 
2679   if (bytes == 0)
2680     {
2681       printf (_("\nThe %s section is empty.\n"), section->name);
2682       return 0;
2683     }
2684 
2685   load_debug_info (file);
2686 
2687   /* Check the order of range list in .debug_info section. If
2688      offsets of range lists are in the ascending order, we can
2689      use `debug_information' directly.  */
2690   for (i = 0; i < num_debug_info_entries; i++)
2691     {
2692       unsigned int num;
2693 
2694       num = debug_information [i].num_range_lists;
2695       num_range_list += num;
2696 
2697       /* Check if we can use `debug_information' directly.  */
2698       if (use_debug_info && num != 0)
2699 	{
2700 	  if (!seen_first_offset)
2701 	    {
2702 	      /* This is the first range list.  */
2703 	      last_offset = debug_information [i].range_lists [0];
2704 	      first = i;
2705 	      seen_first_offset = 1;
2706 	      j = 1;
2707 	    }
2708 	  else
2709 	    j = 0;
2710 
2711 	  for (; j < num; j++)
2712 	    {
2713 	      if (last_offset >
2714 		  debug_information [i].range_lists [j])
2715 		{
2716 		  use_debug_info = 0;
2717 		  break;
2718 		}
2719 	      last_offset = debug_information [i].range_lists [j];
2720 	    }
2721 	}
2722     }
2723 
2724   if (!use_debug_info)
2725     /* FIXME: Should we handle this case?  */
2726     error (_("Range lists in .debug_info section aren't in ascending order!\n"));
2727 
2728   if (!seen_first_offset)
2729     error (_("No range lists in .debug_info section!\n"));
2730 
2731   /* DWARF sections under Mach-O have non-zero addresses.  */
2732   if (debug_information [first].num_range_lists > 0
2733       && debug_information [first].range_lists [0] != section->address)
2734     warn (_("Range lists in %s section start at 0x%lx\n"),
2735 	  section->name, debug_information [first].range_lists [0]);
2736 
2737   printf (_("Contents of the %s section:\n\n"), section->name);
2738   printf (_("    Offset   Begin    End\n"));
2739 
2740   seen_first_offset = 0;
2741   for (i = first; i < num_debug_info_entries; i++)
2742     {
2743       unsigned long begin;
2744       unsigned long end;
2745       unsigned long offset;
2746       unsigned int pointer_size;
2747       unsigned long base_address;
2748 
2749       pointer_size = debug_information [i].pointer_size;
2750 
2751       for (j = 0; j < debug_information [i].num_range_lists; j++)
2752 	{
2753 	  /* DWARF sections under Mach-O have non-zero addresses.  */
2754 	  offset = debug_information [i].range_lists [j] - section->address;
2755 	  next = section_begin + offset;
2756 	  base_address = debug_information [i].base_address;
2757 
2758 	  if (!seen_first_offset)
2759 	    seen_first_offset = 1;
2760 	  else
2761 	    {
2762 	      if (start < next)
2763 		warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
2764 		      (long)(start - section_begin),
2765 		      (long)(next - section_begin), section->name);
2766 	      else if (start > next)
2767 		warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
2768 		      (long)(start - section_begin),
2769 		      (long)(next - section_begin), section->name);
2770 	    }
2771 	  start = next;
2772 
2773 	  while (1)
2774 	    {
2775 	      begin = byte_get (start, pointer_size);
2776 	      start += pointer_size;
2777 	      end = byte_get (start, pointer_size);
2778 	      start += pointer_size;
2779 
2780 	      if (begin == 0 && end == 0)
2781 		{
2782 		  printf (_("    %8.8lx <End of list>\n"), offset);
2783 		  break;
2784 		}
2785 
2786 	      /* Check base address specifiers.  */
2787 	      if (begin == -1UL && end != -1UL)
2788 		{
2789 		  base_address = end;
2790 		  printf ("    %8.8lx %8.8lx %8.8lx (base address)\n",
2791 			  offset, begin, end);
2792 		  continue;
2793 		}
2794 
2795 	      printf ("    %8.8lx %8.8lx %8.8lx",
2796 		      offset, begin + base_address, end + base_address);
2797 
2798 	      if (begin == end)
2799 		fputs (_(" (start == end)"), stdout);
2800 	      else if (begin > end)
2801 		fputs (_(" (start > end)"), stdout);
2802 
2803 	      putchar ('\n');
2804 	    }
2805 	}
2806     }
2807   putchar ('\n');
2808   return 1;
2809 }
2810 
2811 typedef struct Frame_Chunk
2812 {
2813   struct Frame_Chunk *next;
2814   unsigned char *chunk_start;
2815   int ncols;
2816   /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
2817   short int *col_type;
2818   int *col_offset;
2819   char *augmentation;
2820   unsigned int code_factor;
2821   int data_factor;
2822   unsigned long pc_begin;
2823   unsigned long pc_range;
2824   int cfa_reg;
2825   int cfa_offset;
2826   int ra;
2827   unsigned char fde_encoding;
2828   unsigned char cfa_exp;
2829 }
2830 Frame_Chunk;
2831 
2832 /* A marker for a col_type that means this column was never referenced
2833    in the frame info.  */
2834 #define DW_CFA_unreferenced (-1)
2835 
2836 static void
frame_need_space(Frame_Chunk * fc,int reg)2837 frame_need_space (Frame_Chunk *fc, int reg)
2838 {
2839   int prev = fc->ncols;
2840 
2841   if (reg < fc->ncols)
2842     return;
2843 
2844   fc->ncols = reg + 1;
2845   fc->col_type = xcrealloc (fc->col_type, fc->ncols, sizeof (short int));
2846   fc->col_offset = xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
2847 
2848   while (prev < fc->ncols)
2849     {
2850       fc->col_type[prev] = DW_CFA_unreferenced;
2851       fc->col_offset[prev] = 0;
2852       prev++;
2853     }
2854 }
2855 
2856 static void
frame_display_row(Frame_Chunk * fc,int * need_col_headers,int * max_regs)2857 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
2858 {
2859   int r;
2860   char tmp[100];
2861 
2862   if (*max_regs < fc->ncols)
2863     *max_regs = fc->ncols;
2864 
2865   if (*need_col_headers)
2866     {
2867       *need_col_headers = 0;
2868 
2869       printf ("   LOC   CFA      ");
2870 
2871       for (r = 0; r < *max_regs; r++)
2872 	if (fc->col_type[r] != DW_CFA_unreferenced)
2873 	  {
2874 	    if (r == fc->ra)
2875 	      printf ("ra   ");
2876 	    else
2877 	      printf ("r%-4d", r);
2878 	  }
2879 
2880       printf ("\n");
2881     }
2882 
2883   printf ("%08lx ", fc->pc_begin);
2884   if (fc->cfa_exp)
2885     strcpy (tmp, "exp");
2886   else
2887     sprintf (tmp, "r%d%+d", fc->cfa_reg, fc->cfa_offset);
2888   printf ("%-8s ", tmp);
2889 
2890   for (r = 0; r < fc->ncols; r++)
2891     {
2892       if (fc->col_type[r] != DW_CFA_unreferenced)
2893 	{
2894 	  switch (fc->col_type[r])
2895 	    {
2896 	    case DW_CFA_undefined:
2897 	      strcpy (tmp, "u");
2898 	      break;
2899 	    case DW_CFA_same_value:
2900 	      strcpy (tmp, "s");
2901 	      break;
2902 	    case DW_CFA_offset:
2903 	      sprintf (tmp, "c%+d", fc->col_offset[r]);
2904 	      break;
2905 	    case DW_CFA_val_offset:
2906 	      sprintf (tmp, "v%+d", fc->col_offset[r]);
2907 	      break;
2908 	    case DW_CFA_register:
2909 	      sprintf (tmp, "r%d", fc->col_offset[r]);
2910 	      break;
2911 	    case DW_CFA_expression:
2912 	      strcpy (tmp, "exp");
2913 	      break;
2914 	    case DW_CFA_val_expression:
2915 	      strcpy (tmp, "vexp");
2916 	      break;
2917 	    default:
2918 	      strcpy (tmp, "n/a");
2919 	      break;
2920 	    }
2921 	  printf ("%-5s", tmp);
2922 	}
2923     }
2924   printf ("\n");
2925 }
2926 
2927 static int
size_of_encoded_value(int encoding)2928 size_of_encoded_value (int encoding)
2929 {
2930   switch (encoding & 0x7)
2931     {
2932     default:	/* ??? */
2933     case 0:	return eh_addr_size;
2934     case 2:	return 2;
2935     case 3:	return 4;
2936     case 4:	return 8;
2937     }
2938 }
2939 
2940 static dwarf_vma
get_encoded_value(unsigned char * data,int encoding)2941 get_encoded_value (unsigned char *data, int encoding)
2942 {
2943   int size = size_of_encoded_value (encoding);
2944 
2945   if (encoding & DW_EH_PE_signed)
2946     return byte_get_signed (data, size);
2947   else
2948     return byte_get (data, size);
2949 }
2950 
2951 #define GET(N)	byte_get (start, N); start += N
2952 #define LEB()	read_leb128 (start, & length_return, 0); start += length_return
2953 #define SLEB()	read_leb128 (start, & length_return, 1); start += length_return
2954 
2955 static int
display_debug_frames(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)2956 display_debug_frames (struct dwarf_section *section,
2957 		      void *file ATTRIBUTE_UNUSED)
2958 {
2959   unsigned char *start = section->start;
2960   unsigned char *end = start + section->size;
2961   unsigned char *section_start = start;
2962   Frame_Chunk *chunks = 0;
2963   Frame_Chunk *remembered_state = 0;
2964   Frame_Chunk *rs;
2965   int is_eh = strcmp (section->name, ".eh_frame") == 0;
2966   unsigned int length_return;
2967   int max_regs = 0;
2968 
2969   printf (_("The section %s contains:\n"), section->name);
2970 
2971   while (start < end)
2972     {
2973       unsigned char *saved_start;
2974       unsigned char *block_end;
2975       unsigned long length;
2976       unsigned long cie_id;
2977       Frame_Chunk *fc;
2978       Frame_Chunk *cie;
2979       int need_col_headers = 1;
2980       unsigned char *augmentation_data = NULL;
2981       unsigned long augmentation_data_len = 0;
2982       int encoded_ptr_size = eh_addr_size;
2983       int offset_size;
2984       int initial_length_size;
2985 
2986       saved_start = start;
2987       length = byte_get (start, 4); start += 4;
2988 
2989       if (length == 0)
2990 	{
2991 	  printf ("\n%08lx ZERO terminator\n\n",
2992 		    (unsigned long)(saved_start - section_start));
2993 	  continue;
2994 	}
2995 
2996       if (length == 0xffffffff)
2997 	{
2998 	  length = byte_get (start, 8);
2999 	  start += 8;
3000 	  offset_size = 8;
3001 	  initial_length_size = 12;
3002 	}
3003       else
3004 	{
3005 	  offset_size = 4;
3006 	  initial_length_size = 4;
3007 	}
3008 
3009       block_end = saved_start + length + initial_length_size;
3010       if (block_end > end)
3011 	{
3012 	  warn ("Invalid length %#08lx in FDE at %#08lx\n",
3013 		length, (unsigned long)(saved_start - section_start));
3014 	  block_end = end;
3015 	}
3016       cie_id = byte_get (start, offset_size); start += offset_size;
3017 
3018       if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
3019 	{
3020 	  int version;
3021 
3022 	  fc = xmalloc (sizeof (Frame_Chunk));
3023 	  memset (fc, 0, sizeof (Frame_Chunk));
3024 
3025 	  fc->next = chunks;
3026 	  chunks = fc;
3027 	  fc->chunk_start = saved_start;
3028 	  fc->ncols = 0;
3029 	  fc->col_type = xmalloc (sizeof (short int));
3030 	  fc->col_offset = xmalloc (sizeof (int));
3031 	  frame_need_space (fc, max_regs-1);
3032 
3033 	  version = *start++;
3034 
3035 	  fc->augmentation = (char *) start;
3036 	  start = (unsigned char *) strchr ((char *) start, '\0') + 1;
3037 
3038 	  if (fc->augmentation[0] == 'z')
3039 	    {
3040 	      fc->code_factor = LEB ();
3041 	      fc->data_factor = SLEB ();
3042 	      if (version == 1)
3043 		{
3044 		  fc->ra = GET (1);
3045 		}
3046 	      else
3047 		{
3048 		  fc->ra = LEB ();
3049 		}
3050 	      augmentation_data_len = LEB ();
3051 	      augmentation_data = start;
3052 	      start += augmentation_data_len;
3053 	    }
3054 	  else if (strcmp (fc->augmentation, "eh") == 0)
3055 	    {
3056 	      start += eh_addr_size;
3057 	      fc->code_factor = LEB ();
3058 	      fc->data_factor = SLEB ();
3059 	      if (version == 1)
3060 		{
3061 		  fc->ra = GET (1);
3062 		}
3063 	      else
3064 		{
3065 		  fc->ra = LEB ();
3066 		}
3067 	    }
3068 	  else
3069 	    {
3070 	      fc->code_factor = LEB ();
3071 	      fc->data_factor = SLEB ();
3072 	      if (version == 1)
3073 		{
3074 		  fc->ra = GET (1);
3075 		}
3076 	      else
3077 		{
3078 		  fc->ra = LEB ();
3079 		}
3080 	    }
3081 	  cie = fc;
3082 
3083 	  if (do_debug_frames_interp)
3084 	    printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
3085 		    (unsigned long)(saved_start - section_start), length, cie_id,
3086 		    fc->augmentation, fc->code_factor, fc->data_factor,
3087 		    fc->ra);
3088 	  else
3089 	    {
3090 	      printf ("\n%08lx %08lx %08lx CIE\n",
3091 		      (unsigned long)(saved_start - section_start), length, cie_id);
3092 	      printf ("  Version:               %d\n", version);
3093 	      printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
3094 	      printf ("  Code alignment factor: %u\n", fc->code_factor);
3095 	      printf ("  Data alignment factor: %d\n", fc->data_factor);
3096 	      printf ("  Return address column: %d\n", fc->ra);
3097 
3098 	      if (augmentation_data_len)
3099 		{
3100 		  unsigned long i;
3101 		  printf ("  Augmentation data:    ");
3102 		  for (i = 0; i < augmentation_data_len; ++i)
3103 		    printf (" %02x", augmentation_data[i]);
3104 		  putchar ('\n');
3105 		}
3106 	      putchar ('\n');
3107 	    }
3108 
3109 	  if (augmentation_data_len)
3110 	    {
3111 	      unsigned char *p, *q;
3112 	      p = (unsigned char *) fc->augmentation + 1;
3113 	      q = augmentation_data;
3114 
3115 	      while (1)
3116 		{
3117 		  if (*p == 'L')
3118 		    q++;
3119 		  else if (*p == 'P')
3120 		    q += 1 + size_of_encoded_value (*q);
3121 		  else if (*p == 'R')
3122 		    fc->fde_encoding = *q++;
3123 		  else
3124 		    break;
3125 		  p++;
3126 		}
3127 
3128 	      if (fc->fde_encoding)
3129 		encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
3130 	    }
3131 
3132 	  frame_need_space (fc, fc->ra);
3133 	}
3134       else
3135 	{
3136 	  unsigned char *look_for;
3137 	  static Frame_Chunk fde_fc;
3138 
3139 	  fc = & fde_fc;
3140 	  memset (fc, 0, sizeof (Frame_Chunk));
3141 
3142 	  look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
3143 
3144 	  for (cie = chunks; cie ; cie = cie->next)
3145 	    if (cie->chunk_start == look_for)
3146 	      break;
3147 
3148 	  if (!cie)
3149 	    {
3150 	      warn ("Invalid CIE pointer %#08lx in FDE at %#08lx\n",
3151 		    cie_id, (unsigned long)(saved_start - section_start));
3152 	      fc->ncols = 0;
3153 	      fc->col_type = xmalloc (sizeof (short int));
3154 	      fc->col_offset = xmalloc (sizeof (int));
3155 	      frame_need_space (fc, max_regs - 1);
3156 	      cie = fc;
3157 	      fc->augmentation = "";
3158 	      fc->fde_encoding = 0;
3159 	    }
3160 	  else
3161 	    {
3162 	      fc->ncols = cie->ncols;
3163 	      fc->col_type = xcmalloc (fc->ncols, sizeof (short int));
3164 	      fc->col_offset = xcmalloc (fc->ncols, sizeof (int));
3165 	      memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
3166 	      memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
3167 	      fc->augmentation = cie->augmentation;
3168 	      fc->code_factor = cie->code_factor;
3169 	      fc->data_factor = cie->data_factor;
3170 	      fc->cfa_reg = cie->cfa_reg;
3171 	      fc->cfa_offset = cie->cfa_offset;
3172 	      fc->ra = cie->ra;
3173 	      frame_need_space (fc, max_regs-1);
3174 	      fc->fde_encoding = cie->fde_encoding;
3175 	    }
3176 
3177 	  if (fc->fde_encoding)
3178 	    encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
3179 
3180 	  fc->pc_begin = get_encoded_value (start, fc->fde_encoding);
3181 	  if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel
3182 	      /* Don't adjust for relocatable file since there's
3183 		 invariably a pcrel reloc here, which we haven't
3184 		 applied.  */
3185 	      && !is_relocatable)
3186 	    fc->pc_begin += section->address + (start - section_start);
3187 	  start += encoded_ptr_size;
3188 	  fc->pc_range = byte_get (start, encoded_ptr_size);
3189 	  start += encoded_ptr_size;
3190 
3191 	  if (cie->augmentation[0] == 'z')
3192 	    {
3193 	      augmentation_data_len = LEB ();
3194 	      augmentation_data = start;
3195 	      start += augmentation_data_len;
3196 	    }
3197 
3198 	  printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
3199 		  (unsigned long)(saved_start - section_start), length, cie_id,
3200 		  (unsigned long)(cie->chunk_start - section_start),
3201 		  fc->pc_begin, fc->pc_begin + fc->pc_range);
3202 	  if (! do_debug_frames_interp && augmentation_data_len)
3203 	    {
3204 	      unsigned long i;
3205 
3206 	      printf ("  Augmentation data:    ");
3207 	      for (i = 0; i < augmentation_data_len; ++i)
3208 		printf (" %02x", augmentation_data[i]);
3209 	      putchar ('\n');
3210 	      putchar ('\n');
3211 	    }
3212 	}
3213 
3214       /* At this point, fc is the current chunk, cie (if any) is set, and
3215 	 we're about to interpret instructions for the chunk.  */
3216       /* ??? At present we need to do this always, since this sizes the
3217 	 fc->col_type and fc->col_offset arrays, which we write into always.
3218 	 We should probably split the interpreted and non-interpreted bits
3219 	 into two different routines, since there's so much that doesn't
3220 	 really overlap between them.  */
3221       if (1 || do_debug_frames_interp)
3222 	{
3223 	  /* Start by making a pass over the chunk, allocating storage
3224 	     and taking note of what registers are used.  */
3225 	  unsigned char *tmp = start;
3226 
3227 	  while (start < block_end)
3228 	    {
3229 	      unsigned op, opa;
3230 	      unsigned long reg, tmp;
3231 
3232 	      op = *start++;
3233 	      opa = op & 0x3f;
3234 	      if (op & 0xc0)
3235 		op &= 0xc0;
3236 
3237 	      /* Warning: if you add any more cases to this switch, be
3238 		 sure to add them to the corresponding switch below.  */
3239 	      switch (op)
3240 		{
3241 		case DW_CFA_advance_loc:
3242 		  break;
3243 		case DW_CFA_offset:
3244 		  LEB ();
3245 		  frame_need_space (fc, opa);
3246 		  fc->col_type[opa] = DW_CFA_undefined;
3247 		  break;
3248 		case DW_CFA_restore:
3249 		  frame_need_space (fc, opa);
3250 		  fc->col_type[opa] = DW_CFA_undefined;
3251 		  break;
3252 		case DW_CFA_set_loc:
3253 		  start += encoded_ptr_size;
3254 		  break;
3255 		case DW_CFA_advance_loc1:
3256 		  start += 1;
3257 		  break;
3258 		case DW_CFA_advance_loc2:
3259 		  start += 2;
3260 		  break;
3261 		case DW_CFA_advance_loc4:
3262 		  start += 4;
3263 		  break;
3264 		case DW_CFA_offset_extended:
3265 		case DW_CFA_val_offset:
3266 		  reg = LEB (); LEB ();
3267 		  frame_need_space (fc, reg);
3268 		  fc->col_type[reg] = DW_CFA_undefined;
3269 		  break;
3270 		case DW_CFA_restore_extended:
3271 		  reg = LEB ();
3272 		  frame_need_space (fc, reg);
3273 		  fc->col_type[reg] = DW_CFA_undefined;
3274 		  break;
3275 		case DW_CFA_undefined:
3276 		  reg = LEB ();
3277 		  frame_need_space (fc, reg);
3278 		  fc->col_type[reg] = DW_CFA_undefined;
3279 		  break;
3280 		case DW_CFA_same_value:
3281 		  reg = LEB ();
3282 		  frame_need_space (fc, reg);
3283 		  fc->col_type[reg] = DW_CFA_undefined;
3284 		  break;
3285 		case DW_CFA_register:
3286 		  reg = LEB (); LEB ();
3287 		  frame_need_space (fc, reg);
3288 		  fc->col_type[reg] = DW_CFA_undefined;
3289 		  break;
3290 		case DW_CFA_def_cfa:
3291 		  LEB (); LEB ();
3292 		  break;
3293 		case DW_CFA_def_cfa_register:
3294 		  LEB ();
3295 		  break;
3296 		case DW_CFA_def_cfa_offset:
3297 		  LEB ();
3298 		  break;
3299 		case DW_CFA_def_cfa_expression:
3300 		  tmp = LEB ();
3301 		  start += tmp;
3302 		  break;
3303 		case DW_CFA_expression:
3304 		case DW_CFA_val_expression:
3305 		  reg = LEB ();
3306 		  tmp = LEB ();
3307 		  start += tmp;
3308 		  frame_need_space (fc, reg);
3309 		  fc->col_type[reg] = DW_CFA_undefined;
3310 		  break;
3311 		case DW_CFA_offset_extended_sf:
3312 		case DW_CFA_val_offset_sf:
3313 		  reg = LEB (); SLEB ();
3314 		  frame_need_space (fc, reg);
3315 		  fc->col_type[reg] = DW_CFA_undefined;
3316 		  break;
3317 		case DW_CFA_def_cfa_sf:
3318 		  LEB (); SLEB ();
3319 		  break;
3320 		case DW_CFA_def_cfa_offset_sf:
3321 		  SLEB ();
3322 		  break;
3323 		case DW_CFA_MIPS_advance_loc8:
3324 		  start += 8;
3325 		  break;
3326 		case DW_CFA_GNU_args_size:
3327 		  LEB ();
3328 		  break;
3329 		case DW_CFA_GNU_negative_offset_extended:
3330 		  reg = LEB (); LEB ();
3331 		  frame_need_space (fc, reg);
3332 		  fc->col_type[reg] = DW_CFA_undefined;
3333 
3334 		default:
3335 		  break;
3336 		}
3337 	    }
3338 	  start = tmp;
3339 	}
3340 
3341       /* Now we know what registers are used, make a second pass over
3342 	 the chunk, this time actually printing out the info.  */
3343 
3344       while (start < block_end)
3345 	{
3346 	  unsigned op, opa;
3347 	  unsigned long ul, reg, roffs;
3348 	  long l, ofs;
3349 	  dwarf_vma vma;
3350 
3351 	  op = *start++;
3352 	  opa = op & 0x3f;
3353 	  if (op & 0xc0)
3354 	    op &= 0xc0;
3355 
3356 	  /* Warning: if you add any more cases to this switch, be
3357 	     sure to add them to the corresponding switch above.  */
3358 	  switch (op)
3359 	    {
3360 	    case DW_CFA_advance_loc:
3361 	      if (do_debug_frames_interp)
3362 		frame_display_row (fc, &need_col_headers, &max_regs);
3363 	      else
3364 		printf ("  DW_CFA_advance_loc: %d to %08lx\n",
3365 			opa * fc->code_factor,
3366 			fc->pc_begin + opa * fc->code_factor);
3367 	      fc->pc_begin += opa * fc->code_factor;
3368 	      break;
3369 
3370 	    case DW_CFA_offset:
3371 	      roffs = LEB ();
3372 	      if (! do_debug_frames_interp)
3373 		printf ("  DW_CFA_offset: r%d at cfa%+ld\n",
3374 			opa, roffs * fc->data_factor);
3375 	      fc->col_type[opa] = DW_CFA_offset;
3376 	      fc->col_offset[opa] = roffs * fc->data_factor;
3377 	      break;
3378 
3379 	    case DW_CFA_restore:
3380 	      if (! do_debug_frames_interp)
3381 		printf ("  DW_CFA_restore: r%d\n", opa);
3382 	      fc->col_type[opa] = cie->col_type[opa];
3383 	      fc->col_offset[opa] = cie->col_offset[opa];
3384 	      break;
3385 
3386 	    case DW_CFA_set_loc:
3387 	      vma = get_encoded_value (start, fc->fde_encoding);
3388 	      if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel
3389 		  && !is_relocatable)
3390 		vma += section->address + (start - section_start);
3391 	      start += encoded_ptr_size;
3392 	      if (do_debug_frames_interp)
3393 		frame_display_row (fc, &need_col_headers, &max_regs);
3394 	      else
3395 		printf ("  DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
3396 	      fc->pc_begin = vma;
3397 	      break;
3398 
3399 	    case DW_CFA_advance_loc1:
3400 	      ofs = byte_get (start, 1); start += 1;
3401 	      if (do_debug_frames_interp)
3402 		frame_display_row (fc, &need_col_headers, &max_regs);
3403 	      else
3404 		printf ("  DW_CFA_advance_loc1: %ld to %08lx\n",
3405 			ofs * fc->code_factor,
3406 			fc->pc_begin + ofs * fc->code_factor);
3407 	      fc->pc_begin += ofs * fc->code_factor;
3408 	      break;
3409 
3410 	    case DW_CFA_advance_loc2:
3411 	      ofs = byte_get (start, 2); start += 2;
3412 	      if (do_debug_frames_interp)
3413 		frame_display_row (fc, &need_col_headers, &max_regs);
3414 	      else
3415 		printf ("  DW_CFA_advance_loc2: %ld to %08lx\n",
3416 			ofs * fc->code_factor,
3417 			fc->pc_begin + ofs * fc->code_factor);
3418 	      fc->pc_begin += ofs * fc->code_factor;
3419 	      break;
3420 
3421 	    case DW_CFA_advance_loc4:
3422 	      ofs = byte_get (start, 4); start += 4;
3423 	      if (do_debug_frames_interp)
3424 		frame_display_row (fc, &need_col_headers, &max_regs);
3425 	      else
3426 		printf ("  DW_CFA_advance_loc4: %ld to %08lx\n",
3427 			ofs * fc->code_factor,
3428 			fc->pc_begin + ofs * fc->code_factor);
3429 	      fc->pc_begin += ofs * fc->code_factor;
3430 	      break;
3431 
3432 	    case DW_CFA_offset_extended:
3433 	      reg = LEB ();
3434 	      roffs = LEB ();
3435 	      if (! do_debug_frames_interp)
3436 		printf ("  DW_CFA_offset_extended: r%ld at cfa%+ld\n",
3437 			reg, roffs * fc->data_factor);
3438 	      fc->col_type[reg] = DW_CFA_offset;
3439 	      fc->col_offset[reg] = roffs * fc->data_factor;
3440 	      break;
3441 
3442 	    case DW_CFA_val_offset:
3443 	      reg = LEB ();
3444 	      roffs = LEB ();
3445 	      if (! do_debug_frames_interp)
3446 		printf ("  DW_CFA_val_offset: r%ld at cfa%+ld\n",
3447 			reg, roffs * fc->data_factor);
3448 	      fc->col_type[reg] = DW_CFA_val_offset;
3449 	      fc->col_offset[reg] = roffs * fc->data_factor;
3450 	      break;
3451 
3452 	    case DW_CFA_restore_extended:
3453 	      reg = LEB ();
3454 	      if (! do_debug_frames_interp)
3455 		printf ("  DW_CFA_restore_extended: r%ld\n", reg);
3456 	      fc->col_type[reg] = cie->col_type[reg];
3457 	      fc->col_offset[reg] = cie->col_offset[reg];
3458 	      break;
3459 
3460 	    case DW_CFA_undefined:
3461 	      reg = LEB ();
3462 	      if (! do_debug_frames_interp)
3463 		printf ("  DW_CFA_undefined: r%ld\n", reg);
3464 	      fc->col_type[reg] = DW_CFA_undefined;
3465 	      fc->col_offset[reg] = 0;
3466 	      break;
3467 
3468 	    case DW_CFA_same_value:
3469 	      reg = LEB ();
3470 	      if (! do_debug_frames_interp)
3471 		printf ("  DW_CFA_same_value: r%ld\n", reg);
3472 	      fc->col_type[reg] = DW_CFA_same_value;
3473 	      fc->col_offset[reg] = 0;
3474 	      break;
3475 
3476 	    case DW_CFA_register:
3477 	      reg = LEB ();
3478 	      roffs = LEB ();
3479 	      if (! do_debug_frames_interp)
3480 		printf ("  DW_CFA_register: r%ld in r%ld\n", reg, roffs);
3481 	      fc->col_type[reg] = DW_CFA_register;
3482 	      fc->col_offset[reg] = roffs;
3483 	      break;
3484 
3485 	    case DW_CFA_remember_state:
3486 	      if (! do_debug_frames_interp)
3487 		printf ("  DW_CFA_remember_state\n");
3488 	      rs = xmalloc (sizeof (Frame_Chunk));
3489 	      rs->ncols = fc->ncols;
3490 	      rs->col_type = xcmalloc (rs->ncols, sizeof (short int));
3491 	      rs->col_offset = xcmalloc (rs->ncols, sizeof (int));
3492 	      memcpy (rs->col_type, fc->col_type, rs->ncols);
3493 	      memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
3494 	      rs->next = remembered_state;
3495 	      remembered_state = rs;
3496 	      break;
3497 
3498 	    case DW_CFA_restore_state:
3499 	      if (! do_debug_frames_interp)
3500 		printf ("  DW_CFA_restore_state\n");
3501 	      rs = remembered_state;
3502 	      if (rs)
3503 		{
3504 		  remembered_state = rs->next;
3505 		  frame_need_space (fc, rs->ncols-1);
3506 		  memcpy (fc->col_type, rs->col_type, rs->ncols);
3507 		  memcpy (fc->col_offset, rs->col_offset,
3508 			  rs->ncols * sizeof (int));
3509 		  free (rs->col_type);
3510 		  free (rs->col_offset);
3511 		  free (rs);
3512 		}
3513 	      else if (do_debug_frames_interp)
3514 		printf ("Mismatched DW_CFA_restore_state\n");
3515 	      break;
3516 
3517 	    case DW_CFA_def_cfa:
3518 	      fc->cfa_reg = LEB ();
3519 	      fc->cfa_offset = LEB ();
3520 	      fc->cfa_exp = 0;
3521 	      if (! do_debug_frames_interp)
3522 		printf ("  DW_CFA_def_cfa: r%d ofs %d\n",
3523 			fc->cfa_reg, fc->cfa_offset);
3524 	      break;
3525 
3526 	    case DW_CFA_def_cfa_register:
3527 	      fc->cfa_reg = LEB ();
3528 	      fc->cfa_exp = 0;
3529 	      if (! do_debug_frames_interp)
3530 		printf ("  DW_CFA_def_cfa_reg: r%d\n", fc->cfa_reg);
3531 	      break;
3532 
3533 	    case DW_CFA_def_cfa_offset:
3534 	      fc->cfa_offset = LEB ();
3535 	      if (! do_debug_frames_interp)
3536 		printf ("  DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
3537 	      break;
3538 
3539 	    case DW_CFA_nop:
3540 	      if (! do_debug_frames_interp)
3541 		printf ("  DW_CFA_nop\n");
3542 	      break;
3543 
3544 	    case DW_CFA_def_cfa_expression:
3545 	      ul = LEB ();
3546 	      if (! do_debug_frames_interp)
3547 		{
3548 		  printf ("  DW_CFA_def_cfa_expression (");
3549 		  decode_location_expression (start, eh_addr_size, ul, 0);
3550 		  printf (")\n");
3551 		}
3552 	      fc->cfa_exp = 1;
3553 	      start += ul;
3554 	      break;
3555 
3556 	    case DW_CFA_expression:
3557 	      reg = LEB ();
3558 	      ul = LEB ();
3559 	      if (! do_debug_frames_interp)
3560 		{
3561 		  printf ("  DW_CFA_expression: r%ld (", reg);
3562 		  decode_location_expression (start, eh_addr_size, ul, 0);
3563 		  printf (")\n");
3564 		}
3565 	      fc->col_type[reg] = DW_CFA_expression;
3566 	      start += ul;
3567 	      break;
3568 
3569 	    case DW_CFA_val_expression:
3570 	      reg = LEB ();
3571 	      ul = LEB ();
3572 	      if (! do_debug_frames_interp)
3573 		{
3574 		  printf ("  DW_CFA_val_expression: r%ld (", reg);
3575 		  decode_location_expression (start, eh_addr_size, ul, 0);
3576 		  printf (")\n");
3577 		}
3578 	      fc->col_type[reg] = DW_CFA_val_expression;
3579 	      start += ul;
3580 	      break;
3581 
3582 	    case DW_CFA_offset_extended_sf:
3583 	      reg = LEB ();
3584 	      l = SLEB ();
3585 	      frame_need_space (fc, reg);
3586 	      if (! do_debug_frames_interp)
3587 		printf ("  DW_CFA_offset_extended_sf: r%ld at cfa%+ld\n",
3588 			reg, l * fc->data_factor);
3589 	      fc->col_type[reg] = DW_CFA_offset;
3590 	      fc->col_offset[reg] = l * fc->data_factor;
3591 	      break;
3592 
3593 	    case DW_CFA_val_offset_sf:
3594 	      reg = LEB ();
3595 	      l = SLEB ();
3596 	      frame_need_space (fc, reg);
3597 	      if (! do_debug_frames_interp)
3598 		printf ("  DW_CFA_val_offset_sf: r%ld at cfa%+ld\n",
3599 			reg, l * fc->data_factor);
3600 	      fc->col_type[reg] = DW_CFA_val_offset;
3601 	      fc->col_offset[reg] = l * fc->data_factor;
3602 	      break;
3603 
3604 	    case DW_CFA_def_cfa_sf:
3605 	      fc->cfa_reg = LEB ();
3606 	      fc->cfa_offset = SLEB ();
3607 	      fc->cfa_offset = fc->cfa_offset * fc->data_factor;
3608 	      fc->cfa_exp = 0;
3609 	      if (! do_debug_frames_interp)
3610 		printf ("  DW_CFA_def_cfa_sf: r%d ofs %d\n",
3611 			fc->cfa_reg, fc->cfa_offset);
3612 	      break;
3613 
3614 	    case DW_CFA_def_cfa_offset_sf:
3615 	      fc->cfa_offset = SLEB ();
3616 	      fc->cfa_offset = fc->cfa_offset * fc->data_factor;
3617 	      if (! do_debug_frames_interp)
3618 		printf ("  DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
3619 	      break;
3620 
3621 	    case DW_CFA_MIPS_advance_loc8:
3622 	      ofs = byte_get (start, 8); start += 8;
3623 	      if (do_debug_frames_interp)
3624 		frame_display_row (fc, &need_col_headers, &max_regs);
3625 	      else
3626 		printf ("  DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
3627 			ofs * fc->code_factor,
3628 			fc->pc_begin + ofs * fc->code_factor);
3629 	      fc->pc_begin += ofs * fc->code_factor;
3630 	      break;
3631 
3632 	    case DW_CFA_GNU_window_save:
3633 	      if (! do_debug_frames_interp)
3634 		printf ("  DW_CFA_GNU_window_save\n");
3635 	      break;
3636 
3637 	    case DW_CFA_GNU_args_size:
3638 	      ul = LEB ();
3639 	      if (! do_debug_frames_interp)
3640 		printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
3641 	      break;
3642 
3643 	    case DW_CFA_GNU_negative_offset_extended:
3644 	      reg = LEB ();
3645 	      l = - LEB ();
3646 	      frame_need_space (fc, reg);
3647 	      if (! do_debug_frames_interp)
3648 		printf ("  DW_CFA_GNU_negative_offset_extended: r%ld at cfa%+ld\n",
3649 			reg, l * fc->data_factor);
3650 	      fc->col_type[reg] = DW_CFA_offset;
3651 	      fc->col_offset[reg] = l * fc->data_factor;
3652 	      break;
3653 
3654 	    default:
3655 	      if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
3656 		printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
3657 	      else
3658 		warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
3659 	      start = block_end;
3660 	    }
3661 	}
3662 
3663       if (do_debug_frames_interp)
3664 	frame_display_row (fc, &need_col_headers, &max_regs);
3665 
3666       start = block_end;
3667     }
3668 
3669   printf ("\n");
3670 
3671   return 1;
3672 }
3673 
3674 #undef GET
3675 #undef LEB
3676 #undef SLEB
3677 
3678 static int
display_debug_not_supported(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)3679 display_debug_not_supported (struct dwarf_section *section,
3680 			     void *file ATTRIBUTE_UNUSED)
3681 {
3682   printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
3683 	    section->name);
3684 
3685   return 1;
3686 }
3687 
3688 void *
cmalloc(size_t nmemb,size_t size)3689 cmalloc (size_t nmemb, size_t size)
3690 {
3691   /* Check for overflow.  */
3692   if (nmemb >= ~(size_t) 0 / size)
3693     return NULL;
3694   else
3695     return malloc (nmemb * size);
3696 }
3697 
3698 void *
xcmalloc(size_t nmemb,size_t size)3699 xcmalloc (size_t nmemb, size_t size)
3700 {
3701   /* Check for overflow.  */
3702   if (nmemb >= ~(size_t) 0 / size)
3703     return NULL;
3704   else
3705     return xmalloc (nmemb * size);
3706 }
3707 
3708 void *
xcrealloc(void * ptr,size_t nmemb,size_t size)3709 xcrealloc (void *ptr, size_t nmemb, size_t size)
3710 {
3711   /* Check for overflow.  */
3712   if (nmemb >= ~(size_t) 0 / size)
3713     return NULL;
3714   else
3715     return xrealloc (ptr, nmemb * size);
3716 }
3717 
3718 void
error(const char * message,...)3719 error (const char *message, ...)
3720 {
3721   va_list args;
3722 
3723   va_start (args, message);
3724   fprintf (stderr, _("%s: Error: "), program_name);
3725   vfprintf (stderr, message, args);
3726   va_end (args);
3727 }
3728 
3729 void
warn(const char * message,...)3730 warn (const char *message, ...)
3731 {
3732   va_list args;
3733 
3734   va_start (args, message);
3735   fprintf (stderr, _("%s: Warning: "), program_name);
3736   vfprintf (stderr, message, args);
3737   va_end (args);
3738 }
3739 
3740 void
free_debug_memory(void)3741 free_debug_memory (void)
3742 {
3743   enum dwarf_section_display_enum i;
3744 
3745   free_abbrevs ();
3746 
3747   for (i = 0; i < max; i++)
3748     free_debug_section (i);
3749 
3750   if (debug_information)
3751     {
3752       for (i = 0; i < num_debug_info_entries; i++)
3753 	{
3754 	  if (!debug_information [i].max_loc_offsets)
3755 	    {
3756 	      free (debug_information [i].loc_offsets);
3757 	      free (debug_information [i].have_frame_base);
3758 	    }
3759 	  if (!debug_information [i].max_range_lists)
3760 	    free (debug_information [i].range_lists);
3761 	}
3762       free (debug_information);
3763       debug_information = NULL;
3764       num_debug_info_entries = 0;
3765     }
3766 
3767 }
3768 
3769 struct dwarf_section_display debug_displays[] =
3770 {
3771   { { ".debug_abbrev",		NULL,	0,	0 },
3772     display_debug_abbrev,		0,	0 },
3773   { { ".debug_aranges",		NULL,	0,	0 },
3774     display_debug_aranges,		0,	0 },
3775   { { ".debug_frame",		NULL,	0,	0 },
3776     display_debug_frames,		1,	0 },
3777   { { ".debug_info",		NULL,	0,	0 },
3778     display_debug_info,			1,	0 },
3779   { { ".debug_line",		NULL,	0,	0 },
3780     display_debug_lines,		0,	0 },
3781   { { ".debug_pubnames",	NULL,	0,	0 },
3782     display_debug_pubnames,		0,	0 },
3783   { { ".eh_frame",		NULL,	0,	0 },
3784     display_debug_frames,		1,	1 },
3785   { { ".debug_macinfo",		NULL,	0,	0 },
3786     display_debug_macinfo,		0,	0 },
3787   { { ".debug_str",		NULL,	0,	0 },
3788     display_debug_str,			0,	0 },
3789   { { ".debug_loc",		NULL,	0,	0 },
3790     display_debug_loc,			0,	0 },
3791   { { ".debug_pubtypes",	NULL,	0,	0 },
3792     display_debug_pubnames,		0,	0 },
3793   { { ".debug_ranges",		NULL,	0,	0 },
3794     display_debug_ranges,		0,	0 },
3795   { { ".debug_static_func",	NULL,	0,	0 },
3796     display_debug_not_supported,	0,	0 },
3797   { { ".debug_static_vars",	NULL,	0,	0 },
3798     display_debug_not_supported,	0,	0 },
3799   { { ".debug_types",		NULL,	0,	0 },
3800     display_debug_not_supported,	0,	0 },
3801   { { ".debug_weaknames",	NULL,	0,	0 },
3802     display_debug_not_supported,	0,	0 }
3803 };
3804