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