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