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