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