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