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