]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/dwarf.c
PR27836, readelf -w pointer comparison UB
[thirdparty/binutils-gdb.git] / binutils / dwarf.c
CommitLineData
19e6b90e 1/* dwarf.c -- display DWARF contents of a BFD binary file
250d07de 2 Copyright (C) 2005-2021 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"
3dfb1b6d 24#include <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"
dda8d76d 31#include "filenames.h"
48467cb9 32#include "safe-ctype.h"
61364358
JK
33#include <assert.h>
34
301a9420
AM
35#ifdef HAVE_LIBDEBUGINFOD
36#include <elfutils/debuginfod.h>
37#endif
38
b9b204b3 39#include <limits.h>
b9b204b3
AM
40#ifndef CHAR_BIT
41#define CHAR_BIT 8
42#endif
43
61364358
JK
44#undef MAX
45#undef MIN
46#define MAX(a, b) ((a) > (b) ? (a) : (b))
47#define MIN(a, b) ((a) < (b) ? (a) : (b))
19e6b90e 48
18464d4d 49static const char *regname (unsigned int regno, int row);
1296bc99 50static const char *regname_internal_by_table_only (unsigned int regno);
18464d4d 51
19e6b90e
L
52static int have_frame_base;
53static int need_base_address;
54
19e6b90e 55static unsigned int num_debug_info_entries = 0;
82b1b41b 56static unsigned int alloc_num_debug_info_entries = 0;
19e6b90e 57static debug_info *debug_information = NULL;
cc86f28f
NC
58/* Special value for num_debug_info_entries to indicate
59 that the .debug_info section could not be loaded/parsed. */
60#define DEBUG_INFO_UNAVAILABLE (unsigned int) -1
19e6b90e 61
24841daa
NC
62/* A .debug_info section can contain multiple links to separate
63 DWO object files. We use these structures to record these links. */
64typedef enum dwo_type
65{
66 DWO_NAME,
67 DWO_DIR,
68 DWO_ID
69} dwo_type;
70
71typedef struct dwo_info
72{
73 dwo_type type;
74 const char * value;
50ea0877 75 dwarf_vma cu_offset;
24841daa
NC
76 struct dwo_info * next;
77} dwo_info;
78
015dc7e1
AM
79static dwo_info *first_dwo_info = NULL;
80static bool need_dwo_info;
24841daa
NC
81
82separate_info * first_separate_info = NULL;
d85bf2ba 83
77ef8654 84unsigned int eh_addr_size;
19e6b90e
L
85
86int do_debug_info;
87int do_debug_abbrevs;
88int do_debug_lines;
89int do_debug_pubnames;
f9f0e732 90int do_debug_pubtypes;
19e6b90e
L
91int do_debug_aranges;
92int do_debug_ranges;
93int do_debug_frames;
94int do_debug_frames_interp;
95int do_debug_macinfo;
96int do_debug_str;
e4b7104b 97int do_debug_str_offsets;
19e6b90e 98int do_debug_loc;
5bbdf3d5 99int do_gdb_index;
6f875884
TG
100int do_trace_info;
101int do_trace_abbrevs;
102int do_trace_aranges;
657d0d47
CC
103int do_debug_addr;
104int do_debug_cu_index;
a262ae96 105int do_wide;
dda8d76d 106int do_debug_links;
c46b7066 107int do_follow_links = DEFAULT_FOR_FOLLOW_LINKS;
015dc7e1 108bool do_checks;
19e6b90e 109
fd2f0033
TT
110int dwarf_cutoff_level = -1;
111unsigned long dwarf_start_die;
112
4723351a
CC
113int dwarf_check = 0;
114
9f272209
AO
115/* Convenient constant, to avoid having to cast -1 to dwarf_vma when
116 testing whether e.g. a locview list is present. */
117static const dwarf_vma vm1 = -1;
118
341f9135
CC
119/* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
120 sections. For version 1 package files, each set is stored in SHNDX_POOL
121 as a zero-terminated list of section indexes comprising one set of debug
122 sections from a .dwo file. */
123
341f9135
CC
124static unsigned int *shndx_pool = NULL;
125static unsigned int shndx_pool_size = 0;
126static unsigned int shndx_pool_used = 0;
127
128/* For version 2 package files, each set contains an array of section offsets
129 and an array of section sizes, giving the offset and size of the
130 contribution from a CU or TU within one of the debug sections.
131 When displaying debug info from a package file, we need to use these
132 tables to locate the corresponding contributions to each section. */
133
134struct cu_tu_set
135{
ec1b0fbb
NC
136 uint64_t signature;
137 dwarf_vma section_offsets[DW_SECT_MAX];
138 size_t section_sizes[DW_SECT_MAX];
341f9135
CC
139};
140
141static int cu_count = 0;
142static int tu_count = 0;
143static struct cu_tu_set *cu_sets = NULL;
144static struct cu_tu_set *tu_sets = NULL;
145
015dc7e1 146static bool load_cu_tu_indexes (void *);
341f9135 147
ec1b0fbb
NC
148/* An array that indicates for a given level of CU nesting whether
149 the latest DW_AT_type seen for that level was a signed type or
150 an unsigned type. */
151#define MAX_CU_NESTING (1 << 8)
015dc7e1 152static bool level_type_signed[MAX_CU_NESTING];
ec1b0fbb 153
4cb93e3b
TG
154/* Values for do_debug_lines. */
155#define FLAG_DEBUG_LINES_RAW 1
156#define FLAG_DEBUG_LINES_DECODED 2
157
77ef8654 158static unsigned int
f1c4cc75
RH
159size_of_encoded_value (int encoding)
160{
161 switch (encoding & 0x7)
162 {
163 default: /* ??? */
164 case 0: return eh_addr_size;
165 case 2: return 2;
166 case 3: return 4;
167 case 4: return 8;
168 }
169}
170
171static dwarf_vma
041830e0 172get_encoded_value (unsigned char **pdata,
bad62cf5 173 int encoding,
041830e0
NC
174 struct dwarf_section *section,
175 unsigned char * end)
f1c4cc75 176{
041830e0 177 unsigned char * data = * pdata;
6937bb54 178 unsigned int size = size_of_encoded_value (encoding);
bad62cf5 179 dwarf_vma val;
f1c4cc75 180
041830e0
NC
181 if (data + size >= end)
182 {
183 warn (_("Encoded value extends past end of section\n"));
184 * pdata = end;
185 return 0;
186 }
187
6937bb54
NC
188 /* PR 17512: file: 002-829853-0.004. */
189 if (size > 8)
190 {
191 warn (_("Encoded size of %d is too large to read\n"), size);
192 * pdata = end;
193 return 0;
194 }
195
0a9d414a
NC
196 /* PR 17512: file: 1085-5603-0.004. */
197 if (size == 0)
198 {
199 warn (_("Encoded size of 0 is too small to read\n"));
200 * pdata = end;
201 return 0;
202 }
203
f1c4cc75 204 if (encoding & DW_EH_PE_signed)
bad62cf5 205 val = byte_get_signed (data, size);
f1c4cc75 206 else
bad62cf5
AM
207 val = byte_get (data, size);
208
209 if ((encoding & 0x70) == DW_EH_PE_pcrel)
210 val += section->address + (data - section->start);
041830e0
NC
211
212 * pdata = data + size;
bad62cf5 213 return val;
f1c4cc75
RH
214}
215
87b9f255 216#if SIZEOF_LONG_LONG > SIZEOF_LONG
4c219c2e
AM
217# ifndef __MINGW32__
218# define DWARF_VMA_FMT "ll"
219# define DWARF_VMA_FMT_LONG "%16.16llx"
220# else
221# define DWARF_VMA_FMT "I64"
222# define DWARF_VMA_FMT_LONG "%016I64x"
223# endif
2e14fae2 224#else
4c219c2e
AM
225# define DWARF_VMA_FMT "l"
226# define DWARF_VMA_FMT_LONG "%16.16lx"
2d9472a2
NC
227#endif
228
bf5117e3
NC
229/* Convert a dwarf vma value into a string. Returns a pointer to a static
230 buffer containing the converted VALUE. The value is converted according
231 to the printf formating character FMTCH. If NUM_BYTES is non-zero then
232 it specifies the maximum number of bytes to be displayed in the converted
233 value and FMTCH is ignored - hex is always used. */
467c65bc 234
47704ddf 235static const char *
bf5117e3 236dwarf_vmatoa_1 (const char *fmtch, dwarf_vma value, unsigned num_bytes)
47704ddf
KT
237{
238 /* As dwarf_vmatoa is used more then once in a printf call
239 for output, we are cycling through an fixed array of pointers
240 for return address. */
241 static int buf_pos = 0;
467c65bc
NC
242 static struct dwarf_vmatoa_buf
243 {
47704ddf
KT
244 char place[64];
245 } buf[16];
47704ddf
KT
246 char *ret;
247
47704ddf 248 ret = buf[buf_pos++].place;
467c65bc 249 buf_pos %= ARRAY_SIZE (buf);
47704ddf 250
bf5117e3
NC
251 if (num_bytes)
252 {
222c2bf0 253 /* Printf does not have a way of specifying a maximum field width for an
bf5117e3
NC
254 integer value, so we print the full value into a buffer and then select
255 the precision we need. */
256 snprintf (ret, sizeof (buf[0].place), DWARF_VMA_FMT_LONG, value);
257 if (num_bytes > 8)
258 num_bytes = 8;
259 return ret + (16 - 2 * num_bytes);
260 }
261 else
262 {
263 char fmt[32];
264
0bae9e9e
NC
265 if (fmtch)
266 sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
267 else
268 sprintf (fmt, "%%%s", DWARF_VMA_FMT);
bf5117e3
NC
269 snprintf (ret, sizeof (buf[0].place), fmt, value);
270 return ret;
271 }
272}
47704ddf 273
bf5117e3
NC
274static inline const char *
275dwarf_vmatoa (const char * fmtch, dwarf_vma value)
276{
277 return dwarf_vmatoa_1 (fmtch, value, 0);
278}
279
280/* Print a dwarf_vma value (typically an address, offset or length) in
281 hexadecimal format, followed by a space. The length of the VALUE (and
282 hence the precision displayed) is determined by the NUM_BYTES parameter. */
283
284static void
285print_dwarf_vma (dwarf_vma value, unsigned num_bytes)
286{
287 printf ("%s ", dwarf_vmatoa_1 (NULL, value, num_bytes));
47704ddf
KT
288}
289
9f272209
AO
290/* Print a view number in hexadecimal value, with the same width
291 print_dwarf_vma would have printed it with the same num_bytes.
292 Print blanks for zero view, unless force is nonzero. */
293
294static void
295print_dwarf_view (dwarf_vma value, unsigned num_bytes, int force)
296{
297 int len;
298 if (!num_bytes)
299 len = 4;
300 else
301 len = num_bytes * 2;
302
303 assert (value == (unsigned long) value);
304 if (value || force)
305 printf ("v%0*lx ", len - 1, (unsigned long) value);
306 else
307 printf ("%*s", len + 1, "");
308}
309
74bc6052
CC
310/* Format a 64-bit value, given as two 32-bit values, in hex.
311 For reentrancy, this uses a buffer provided by the caller. */
312
313static const char *
314dwarf_vmatoa64 (dwarf_vma hvalue, dwarf_vma lvalue, char *buf,
315 unsigned int buf_len)
316{
317 int len = 0;
318
319 if (hvalue == 0)
320 snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", lvalue);
321 else
322 {
323 len = snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", hvalue);
324 snprintf (buf + len, buf_len - len,
325 "%08" DWARF_VMA_FMT "x", lvalue);
326 }
327
328 return buf;
329}
330
f6f0e17b
NC
331/* Read in a LEB128 encoded value starting at address DATA.
332 If SIGN is true, return a signed LEB128 value.
333 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
b9b204b3 334 If STATUS_RETURN is not NULL, return with bit 0 (LSB) set if the
cd30bcef
AM
335 terminating byte was not found and with bit 1 set if the value
336 overflows a dwarf_vma.
f6f0e17b
NC
337 No bytes will be read at address END or beyond. */
338
467c65bc 339dwarf_vma
f6f0e17b 340read_leb128 (unsigned char *data,
cd30bcef 341 const unsigned char *const end,
015dc7e1 342 bool sign,
cd30bcef
AM
343 unsigned int *length_return,
344 int *status_return)
19e6b90e 345{
467c65bc 346 dwarf_vma result = 0;
19e6b90e
L
347 unsigned int num_read = 0;
348 unsigned int shift = 0;
cd30bcef 349 int status = 1;
19e6b90e 350
f6f0e17b 351 while (data < end)
19e6b90e 352 {
cd30bcef 353 unsigned char byte = *data++;
b9b204b3 354 unsigned char lost, mask;
08d7da7d 355
19e6b90e
L
356 num_read++;
357
b9b204b3 358 if (shift < CHAR_BIT * sizeof (result))
cd30bcef 359 {
b9b204b3
AM
360 result |= ((dwarf_vma) (byte & 0x7f)) << shift;
361 /* These bits overflowed. */
362 lost = byte ^ (result >> shift);
363 /* And this is the mask of possible overflow bits. */
364 mask = 0x7f ^ ((dwarf_vma) 0x7f << shift >> shift);
cd30bcef
AM
365 shift += 7;
366 }
b9b204b3 367 else
08d7da7d 368 {
b9b204b3
AM
369 lost = byte;
370 mask = 0x7f;
08d7da7d 371 }
b9b204b3
AM
372 if ((lost & mask) != (sign && (dwarf_signed_vma) result < 0 ? mask : 0))
373 status |= 2;
19e6b90e 374
b9b204b3 375 if ((byte & 0x80) == 0)
cd30bcef
AM
376 {
377 status &= ~1;
b9b204b3 378 if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
cd30bcef
AM
379 result |= -((dwarf_vma) 1 << shift);
380 break;
381 }
19e6b90e 382 }
19e6b90e
L
383
384 if (length_return != NULL)
385 *length_return = num_read;
cd30bcef
AM
386 if (status_return != NULL)
387 *status_return = status;
19e6b90e
L
388
389 return result;
390}
391
d11ae95e
NC
392/* Read AMOUNT bytes from PTR and store them in VAL as an unsigned value.
393 Checks to make sure that the read will not reach or pass END
394 and that VAL is big enough to hold AMOUNT bytes. */
0c588247
NC
395#define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END) \
396 do \
397 { \
398 unsigned int amount = (AMOUNT); \
34b9f729
NC
399 if (sizeof (VAL) < amount) \
400 { \
d3a49aa8
AM
401 error (ngettext ("internal error: attempt to read %d byte " \
402 "of data in to %d sized variable", \
403 "internal error: attempt to read %d bytes " \
404 "of data in to %d sized variable", \
405 amount), \
34b9f729
NC
406 amount, (int) sizeof (VAL)); \
407 amount = sizeof (VAL); \
408 } \
2d4b4986 409 if ((PTR) >= (END) - amount) \
0c588247
NC
410 { \
411 if ((PTR) < (END)) \
412 amount = (END) - (PTR); \
413 else \
414 amount = 0; \
415 } \
6937bb54 416 if (amount == 0 || amount > 8) \
0c588247 417 VAL = 0; \
6937bb54
NC
418 else \
419 VAL = byte_get ((PTR), amount); \
0c588247
NC
420 } \
421 while (0)
422
d11ae95e 423/* Like SAFE_BYTE_GET, but also increments PTR by AMOUNT. */
0c588247
NC
424#define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
425 do \
426 { \
427 SAFE_BYTE_GET (VAL, PTR, AMOUNT, END); \
428 PTR += AMOUNT; \
429 } \
430 while (0)
431
d11ae95e 432/* Like SAFE_BYTE_GET, but reads a signed value. */
0c588247
NC
433#define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END) \
434 do \
435 { \
436 unsigned int amount = (AMOUNT); \
2d4b4986 437 if ((PTR) >= (END) - amount) \
0c588247
NC
438 { \
439 if ((PTR) < (END)) \
440 amount = (END) - (PTR); \
441 else \
442 amount = 0; \
443 } \
444 if (amount) \
445 VAL = byte_get_signed ((PTR), amount); \
446 else \
447 VAL = 0; \
448 } \
449 while (0)
450
d11ae95e 451/* Like SAFE_SIGNED_BYTE_GET, but also increments PTR by AMOUNT. */
0c588247
NC
452#define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
453 do \
454 { \
455 SAFE_SIGNED_BYTE_GET (VAL, PTR, AMOUNT, END); \
456 PTR += AMOUNT; \
457 } \
458 while (0)
459
460#define SAFE_BYTE_GET64(PTR, HIGH, LOW, END) \
461 do \
462 { \
2d4b4986 463 if ((PTR) <= (END) - 8) \
0c588247
NC
464 { \
465 byte_get_64 ((PTR), (HIGH), (LOW)); \
466 } \
467 else \
468 { \
0c588247
NC
469 * (LOW) = * (HIGH) = 0; \
470 } \
471 } \
472 while (0)
473
19e6b90e
L
474typedef struct State_Machine_Registers
475{
467c65bc 476 dwarf_vma address;
ba8826a8 477 unsigned int view;
19e6b90e
L
478 unsigned int file;
479 unsigned int line;
480 unsigned int column;
481 int is_stmt;
482 int basic_block;
a233b20c
JJ
483 unsigned char op_index;
484 unsigned char end_sequence;
ba8826a8
AO
485 /* This variable hold the number of the last entry seen
486 in the File Table. */
19e6b90e
L
487 unsigned int last_file_entry;
488} SMR;
489
490static SMR state_machine_regs;
491
492static void
493reset_state_machine (int is_stmt)
494{
495 state_machine_regs.address = 0;
ba8826a8 496 state_machine_regs.view = 0;
a233b20c 497 state_machine_regs.op_index = 0;
19e6b90e
L
498 state_machine_regs.file = 1;
499 state_machine_regs.line = 1;
500 state_machine_regs.column = 0;
501 state_machine_regs.is_stmt = is_stmt;
502 state_machine_regs.basic_block = 0;
503 state_machine_regs.end_sequence = 0;
504 state_machine_regs.last_file_entry = 0;
505}
506
507/* Handled an extend line op.
508 Returns the number of bytes read. */
509
cd30bcef 510static size_t
f6f0e17b
NC
511process_extended_line_op (unsigned char * data,
512 int is_stmt,
513 unsigned char * end)
19e6b90e
L
514{
515 unsigned char op_code;
cd30bcef 516 size_t len, header_len;
19e6b90e 517 unsigned char *name;
143a3db0 518 unsigned char *orig_data = data;
cd30bcef 519 dwarf_vma adr, val;
19e6b90e 520
cd30bcef
AM
521 READ_ULEB (len, data, end);
522 header_len = data - orig_data;
19e6b90e 523
55b26492 524 if (len == 0 || data >= end || len > (size_t) (end - data))
19e6b90e 525 {
f41e4712 526 warn (_("Badly formed extended line op encountered!\n"));
cd30bcef 527 return header_len;
19e6b90e
L
528 }
529
19e6b90e
L
530 op_code = *data++;
531
532 printf (_(" Extended opcode %d: "), op_code);
533
534 switch (op_code)
535 {
536 case DW_LNE_end_sequence:
537 printf (_("End of Sequence\n\n"));
538 reset_state_machine (is_stmt);
539 break;
540
541 case DW_LNE_set_address:
6937bb54 542 /* PR 17512: file: 002-100480-0.004. */
cd30bcef 543 if (len - 1 > 8)
77ef8654 544 {
cd30bcef
AM
545 warn (_("Length (%lu) of DW_LNE_set_address op is too long\n"),
546 (unsigned long) len - 1);
77ef8654
NC
547 adr = 0;
548 }
549 else
cd30bcef 550 SAFE_BYTE_GET (adr, data, len - 1, end);
47704ddf 551 printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
19e6b90e 552 state_machine_regs.address = adr;
ba8826a8 553 state_machine_regs.view = 0;
a233b20c 554 state_machine_regs.op_index = 0;
19e6b90e
L
555 break;
556
557 case DW_LNE_define_file:
143a3db0 558 printf (_("define new File Table entry\n"));
19e6b90e 559 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
cc5914eb 560 printf (" %d\t", ++state_machine_regs.last_file_entry);
f6f0e17b 561
d949ff56
NC
562 {
563 size_t l;
564
565 name = data;
566 l = strnlen ((char *) data, end - data);
cd30bcef
AM
567 data += l + 1;
568 READ_ULEB (val, data, end);
569 printf ("%s\t", dwarf_vmatoa ("u", val));
570 READ_ULEB (val, data, end);
571 printf ("%s\t", dwarf_vmatoa ("u", val));
572 READ_ULEB (val, data, end);
573 printf ("%s\t", dwarf_vmatoa ("u", val));
d949ff56
NC
574 printf ("%.*s\n\n", (int) l, name);
575 }
f6f0e17b 576
55b26492 577 if (((size_t) (data - orig_data) != len + header_len) || data >= end)
b4eb7656 578 warn (_("DW_LNE_define_file: Bad opcode length\n"));
19e6b90e
L
579 break;
580
ed4a4bdf 581 case DW_LNE_set_discriminator:
cd30bcef
AM
582 READ_ULEB (val, data, end);
583 printf (_("set Discriminator to %s\n"), dwarf_vmatoa ("u", val));
ed4a4bdf
CC
584 break;
585
e2a0d921
NC
586 /* HP extensions. */
587 case DW_LNE_HP_negate_is_UV_update:
ed4a4bdf 588 printf ("DW_LNE_HP_negate_is_UV_update\n");
e2a0d921
NC
589 break;
590 case DW_LNE_HP_push_context:
ed4a4bdf 591 printf ("DW_LNE_HP_push_context\n");
e2a0d921
NC
592 break;
593 case DW_LNE_HP_pop_context:
ed4a4bdf 594 printf ("DW_LNE_HP_pop_context\n");
e2a0d921
NC
595 break;
596 case DW_LNE_HP_set_file_line_column:
ed4a4bdf 597 printf ("DW_LNE_HP_set_file_line_column\n");
e2a0d921
NC
598 break;
599 case DW_LNE_HP_set_routine_name:
ed4a4bdf 600 printf ("DW_LNE_HP_set_routine_name\n");
e2a0d921
NC
601 break;
602 case DW_LNE_HP_set_sequence:
ed4a4bdf 603 printf ("DW_LNE_HP_set_sequence\n");
e2a0d921
NC
604 break;
605 case DW_LNE_HP_negate_post_semantics:
ed4a4bdf 606 printf ("DW_LNE_HP_negate_post_semantics\n");
e2a0d921
NC
607 break;
608 case DW_LNE_HP_negate_function_exit:
ed4a4bdf 609 printf ("DW_LNE_HP_negate_function_exit\n");
e2a0d921
NC
610 break;
611 case DW_LNE_HP_negate_front_end_logical:
ed4a4bdf 612 printf ("DW_LNE_HP_negate_front_end_logical\n");
e2a0d921
NC
613 break;
614 case DW_LNE_HP_define_proc:
ed4a4bdf 615 printf ("DW_LNE_HP_define_proc\n");
e2a0d921 616 break;
43294ab7
TG
617 case DW_LNE_HP_source_file_correlation:
618 {
cd30bcef 619 unsigned char *edata = data + len - 1;
b4eb7656
AM
620
621 printf ("DW_LNE_HP_source_file_correlation\n");
622
623 while (data < edata)
624 {
625 unsigned int opc;
626
cd30bcef 627 READ_ULEB (opc, data, edata);
b4eb7656
AM
628
629 switch (opc)
630 {
631 case DW_LNE_HP_SFC_formfeed:
632 printf (" DW_LNE_HP_SFC_formfeed\n");
633 break;
634 case DW_LNE_HP_SFC_set_listing_line:
cd30bcef 635 READ_ULEB (val, data, edata);
b4eb7656 636 printf (" DW_LNE_HP_SFC_set_listing_line (%s)\n",
cd30bcef 637 dwarf_vmatoa ("u", val));
b4eb7656
AM
638 break;
639 case DW_LNE_HP_SFC_associate:
640 printf (" DW_LNE_HP_SFC_associate ");
cd30bcef
AM
641 READ_ULEB (val, data, edata);
642 printf ("(%s", dwarf_vmatoa ("u", val));
643 READ_ULEB (val, data, edata);
644 printf (",%s", dwarf_vmatoa ("u", val));
645 READ_ULEB (val, data, edata);
646 printf (",%s)\n", dwarf_vmatoa ("u", val));
b4eb7656
AM
647 break;
648 default:
649 printf (_(" UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
650 data = edata;
651 break;
652 }
653 }
43294ab7
TG
654 }
655 break;
cecf136e 656
19e6b90e 657 default:
7e665af3 658 {
cd30bcef 659 unsigned int rlen = len - 1;
b4eb7656
AM
660
661 if (op_code >= DW_LNE_lo_user
662 /* The test against DW_LNW_hi_user is redundant due to
663 the limited range of the unsigned char data type used
664 for op_code. */
665 /*&& op_code <= DW_LNE_hi_user*/)
666 printf (_("user defined: "));
667 else
668 printf (_("UNKNOWN: "));
669 printf (_("length %d ["), rlen);
670 for (; rlen; rlen--)
671 printf (" %02x", *data++);
672 printf ("]\n");
7e665af3 673 }
19e6b90e
L
674 break;
675 }
676
cd30bcef 677 return len + header_len;
19e6b90e
L
678}
679
0c588247 680static const unsigned char *
467c65bc 681fetch_indirect_string (dwarf_vma offset)
19e6b90e
L
682{
683 struct dwarf_section *section = &debug_displays [str].section;
d949ff56 684 const unsigned char * ret;
19e6b90e
L
685
686 if (section->start == NULL)
0c588247 687 return (const unsigned char *) _("<no .debug_str section>");
19e6b90e 688
d949ff56 689 if (offset >= section->size)
19e6b90e 690 {
d296b736 691 warn (_("DW_FORM_strp offset too big: 0x%s\n"),
467c65bc 692 dwarf_vmatoa ("x", offset));
0c588247 693 return (const unsigned char *) _("<offset is too big>");
19e6b90e
L
694 }
695
d949ff56
NC
696 ret = section->start + offset;
697 /* Unfortunately we cannot rely upon the .debug_str section ending with a
698 NUL byte. Since our caller is expecting to receive a well formed C
699 string we test for the lack of a terminating byte here. */
700 if (strnlen ((const char *) ret, section->size - offset)
701 == section->size - offset)
702 ret = (const unsigned char *)
703 _("<no NUL byte at end of .debug_str section>");
704
521d4b19 705 return ret;
19e6b90e
L
706}
707
77145576
JK
708static const unsigned char *
709fetch_indirect_line_string (dwarf_vma offset)
710{
711 struct dwarf_section *section = &debug_displays [line_str].section;
d949ff56 712 const unsigned char * ret;
77145576
JK
713
714 if (section->start == NULL)
715 return (const unsigned char *) _("<no .debug_line_str section>");
716
d949ff56 717 if (offset >= section->size)
77145576 718 {
d296b736 719 warn (_("DW_FORM_line_strp offset too big: 0x%s\n"),
77145576
JK
720 dwarf_vmatoa ("x", offset));
721 return (const unsigned char *) _("<offset is too big>");
722 }
723
d949ff56
NC
724 ret = section->start + offset;
725 /* Unfortunately we cannot rely upon the .debug_line_str section ending
726 with a NUL byte. Since our caller is expecting to receive a well formed
727 C string we test for the lack of a terminating byte here. */
728 if (strnlen ((const char *) ret, section->size - offset)
729 == section->size - offset)
730 ret = (const unsigned char *)
731 _("<no NUL byte at end of .debug_line_str section>");
732
733 return ret;
77145576
JK
734}
735
4723351a 736static const char *
341f9135 737fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
015dc7e1 738 dwarf_vma offset_size, bool dwo)
4723351a
CC
739{
740 enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
741 enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
742 struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
743 struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
e4b7104b 744 dwarf_vma index_offset;
4723351a 745 dwarf_vma str_offset;
d949ff56 746 const char * ret;
e4b7104b 747 unsigned char *curr = index_section->start;
5ab39075 748 unsigned char *end = curr + index_section->size;
e4b7104b 749 dwarf_vma length;
4723351a
CC
750
751 if (index_section->start == NULL)
752 return (dwo ? _("<no .debug_str_offsets.dwo section>")
753 : _("<no .debug_str_offsets section>"));
754
e4b7104b
NC
755 if (str_section->start == NULL)
756 return (dwo ? _("<no .debug_str.dwo section>")
757 : _("<no .debug_str section>"));
758
759 /* FIXME: We should cache the length... */
760 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
761 if (length == 0xffffffff)
762 {
763 if (offset_size != 8)
39f381cb 764 warn (_("Expected offset size of 8 but given %s"), dwarf_vmatoa ("x", offset_size));
e4b7104b
NC
765 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
766 }
767 else if (offset_size != 4)
768 {
39f381cb 769 warn (_("Expected offset size of 4 but given %s"), dwarf_vmatoa ("x", offset_size));
e4b7104b
NC
770 }
771
39f381cb 772 if (length == 0)
e4b7104b 773 {
39f381cb
NC
774 /* This is probably an old style .debug_str_offset section which
775 just contains offsets and no header (and the first offset is 0). */
776 curr = index_section->start;
777 length = index_section->size;
778 }
779 else
780 {
781 /* Skip the version and padding bytes.
782 We assume that they are correct. */
5ab39075
AM
783 if (end - curr >= 4)
784 curr += 4;
785 else
786 curr = end;
787 if (length >= 4)
788 length -= 4;
789 else
790 length = 0;
791
792 if (this_set != NULL
793 && this_set->section_sizes[DW_SECT_STR_OFFSETS] < length)
794 length = this_set->section_sizes[DW_SECT_STR_OFFSETS];
39f381cb 795
5ab39075 796 if (length > (dwarf_vma) (end - curr))
39f381cb 797 {
5ab39075 798 warn (_("index table size too large for section %s vs %s\n"),
39f381cb
NC
799 dwarf_vmatoa ("x", length),
800 dwarf_vmatoa ("x", index_section->size));
5ab39075
AM
801 length = end - curr;
802 }
803
804 if (length < offset_size)
805 {
806 warn (_("index table size %s is too small\n"),
807 dwarf_vmatoa ("x", length));
39f381cb
NC
808 return _("<table too small>");
809 }
e4b7104b
NC
810 }
811
812 index_offset = idx * offset_size;
521d4b19 813
341f9135
CC
814 if (this_set != NULL)
815 index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
e4b7104b 816
5ab39075
AM
817 if (index_offset >= length
818 || length - index_offset < offset_size)
4723351a 819 {
d296b736 820 warn (_("DW_FORM_GNU_str_index offset too big: 0x%s vs 0x%s\n"),
e4b7104b
NC
821 dwarf_vmatoa ("x", index_offset),
822 dwarf_vmatoa ("x", length));
4723351a
CC
823 return _("<index offset is too big>");
824 }
825
e4b7104b 826 str_offset = byte_get (curr + index_offset, offset_size);
4723351a 827 str_offset -= str_section->address;
d949ff56 828 if (str_offset >= str_section->size)
4723351a 829 {
d296b736 830 warn (_("DW_FORM_GNU_str_index indirect offset too big: 0x%s\n"),
4723351a
CC
831 dwarf_vmatoa ("x", str_offset));
832 return _("<indirect index offset is too big>");
833 }
834
d949ff56
NC
835 ret = (const char *) str_section->start + str_offset;
836 /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
837 Since our caller is expecting to receive a well formed C string we test
838 for the lack of a terminating byte here. */
839 if (strnlen (ret, str_section->size - str_offset)
840 == str_section->size - str_offset)
841 ret = (const char *) _("<no NUL byte at end of section>");
842
843 return ret;
4723351a
CC
844}
845
846static const char *
847fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
848{
849 struct dwarf_section *section = &debug_displays [debug_addr].section;
850
851 if (section->start == NULL)
852 return (_("<no .debug_addr section>"));
853
854 if (offset + bytes > section->size)
855 {
d296b736 856 warn (_("Offset into section %s too big: 0x%s\n"),
b4eb7656 857 section->name, dwarf_vmatoa ("x", offset));
4723351a
CC
858 return "<offset too big>";
859 }
860
861 return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
862}
863
864
19e6b90e
L
865/* FIXME: There are better and more efficient ways to handle
866 these structures. For now though, I just want something that
867 is simple to implement. */
bcd213b2 868/* Records a single attribute in an abbrev. */
19e6b90e
L
869typedef struct abbrev_attr
870{
bcd213b2
NC
871 unsigned long attribute;
872 unsigned long form;
873 bfd_signed_vma implicit_const;
874 struct abbrev_attr * next;
19e6b90e
L
875}
876abbrev_attr;
877
bcd213b2 878/* Records a single abbrev. */
19e6b90e
L
879typedef struct abbrev_entry
880{
bcd213b2
NC
881 unsigned long number;
882 unsigned long tag;
883 int children;
884 struct abbrev_attr * first_attr;
885 struct abbrev_attr * last_attr;
886 struct abbrev_entry * next;
19e6b90e
L
887}
888abbrev_entry;
889
bcd213b2
NC
890/* Records a set of abbreviations. */
891typedef struct abbrev_list
892{
893 abbrev_entry * first_abbrev;
894 abbrev_entry * last_abbrev;
76868f36 895 dwarf_vma abbrev_base;
bcd213b2
NC
896 dwarf_vma abbrev_offset;
897 struct abbrev_list * next;
898 unsigned char * start_of_next_abbrevs;
899}
900abbrev_list;
901
902/* Records all the abbrevs found so far. */
903static struct abbrev_list * abbrev_lists = NULL;
904
905typedef struct abbrev_map
906{
907 dwarf_vma start;
908 dwarf_vma end;
909 abbrev_list * list;
910} abbrev_map;
911
912/* Maps between CU offsets and abbrev sets. */
913static abbrev_map * cu_abbrev_map = NULL;
914static unsigned long num_abbrev_map_entries = 0;
915static unsigned long next_free_abbrev_map_entry = 0;
916
917#define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
918#define ABBREV_MAP_ENTRIES_INCREMENT 8
919
920static void
921record_abbrev_list_for_cu (dwarf_vma start, dwarf_vma end, abbrev_list * list)
922{
923 if (cu_abbrev_map == NULL)
924 {
925 num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
926 cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
927 }
928 else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
929 {
930 num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
931 cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
932 }
933
934 cu_abbrev_map[next_free_abbrev_map_entry].start = start;
935 cu_abbrev_map[next_free_abbrev_map_entry].end = end;
936 cu_abbrev_map[next_free_abbrev_map_entry].list = list;
937 next_free_abbrev_map_entry ++;
938}
19e6b90e
L
939
940static void
bcd213b2 941free_all_abbrevs (void)
19e6b90e 942{
bcd213b2 943 abbrev_list * list;
19e6b90e 944
bcd213b2 945 for (list = abbrev_lists; list != NULL;)
19e6b90e 946 {
bcd213b2
NC
947 abbrev_list * next = list->next;
948 abbrev_entry * abbrv;
19e6b90e 949
bcd213b2 950 for (abbrv = list->first_abbrev; abbrv != NULL;)
19e6b90e 951 {
bcd213b2
NC
952 abbrev_entry * next_abbrev = abbrv->next;
953 abbrev_attr * attr;
954
955 for (attr = abbrv->first_attr; attr;)
956 {
957 abbrev_attr *next_attr = attr->next;
19e6b90e 958
bcd213b2
NC
959 free (attr);
960 attr = next_attr;
961 }
962
963 free (abbrv);
964 abbrv = next_abbrev;
19e6b90e
L
965 }
966
bcd213b2
NC
967 free (list);
968 list = next;
19e6b90e
L
969 }
970
bcd213b2
NC
971 abbrev_lists = NULL;
972}
973
974static abbrev_list *
76868f36 975new_abbrev_list (dwarf_vma abbrev_base, dwarf_vma abbrev_offset)
bcd213b2
NC
976{
977 abbrev_list * list = (abbrev_list *) xcalloc (sizeof * list, 1);
978
76868f36 979 list->abbrev_base = abbrev_base;
bcd213b2
NC
980 list->abbrev_offset = abbrev_offset;
981
982 list->next = abbrev_lists;
983 abbrev_lists = list;
984
985 return list;
986}
987
988static abbrev_list *
76868f36
L
989find_abbrev_list_by_abbrev_offset (dwarf_vma abbrev_base,
990 dwarf_vma abbrev_offset)
bcd213b2
NC
991{
992 abbrev_list * list;
993
994 for (list = abbrev_lists; list != NULL; list = list->next)
76868f36
L
995 if (list->abbrev_base == abbrev_base
996 && list->abbrev_offset == abbrev_offset)
bcd213b2
NC
997 return list;
998
999 return NULL;
1000}
1001
1002/* Find the abbreviation map for the CU that includes OFFSET.
1003 OFFSET is an absolute offset from the start of the .debug_info section. */
1004/* FIXME: This function is going to slow down readelf & objdump.
1005 Consider using a better algorithm to mitigate this effect. */
1006
1007static abbrev_map *
1008find_abbrev_map_by_offset (dwarf_vma offset)
1009{
1010 unsigned long i;
1011
1012 for (i = 0; i < next_free_abbrev_map_entry; i++)
1013 if (cu_abbrev_map[i].start <= offset
1014 && cu_abbrev_map[i].end > offset)
1015 return cu_abbrev_map + i;
1016
1017 return NULL;
19e6b90e
L
1018}
1019
1020static void
bcd213b2
NC
1021add_abbrev (unsigned long number,
1022 unsigned long tag,
1023 int children,
1024 abbrev_list * list)
19e6b90e 1025{
bcd213b2 1026 abbrev_entry * entry;
19e6b90e 1027
bcd213b2 1028 entry = (abbrev_entry *) xmalloc (sizeof (*entry));
19e6b90e 1029
bcd213b2 1030 entry->number = number;
19e6b90e
L
1031 entry->tag = tag;
1032 entry->children = children;
1033 entry->first_attr = NULL;
1034 entry->last_attr = NULL;
1035 entry->next = NULL;
1036
bcd213b2
NC
1037 assert (list != NULL);
1038
1039 if (list->first_abbrev == NULL)
1040 list->first_abbrev = entry;
19e6b90e 1041 else
bcd213b2 1042 list->last_abbrev->next = entry;
19e6b90e 1043
bcd213b2 1044 list->last_abbrev = entry;
19e6b90e
L
1045}
1046
1047static void
bcd213b2
NC
1048add_abbrev_attr (unsigned long attribute,
1049 unsigned long form,
1050 bfd_signed_vma implicit_const,
1051 abbrev_list * list)
19e6b90e
L
1052{
1053 abbrev_attr *attr;
1054
bcd213b2 1055 attr = (abbrev_attr *) xmalloc (sizeof (*attr));
19e6b90e
L
1056
1057 attr->attribute = attribute;
1058 attr->form = form;
77145576 1059 attr->implicit_const = implicit_const;
19e6b90e
L
1060 attr->next = NULL;
1061
bcd213b2
NC
1062 assert (list != NULL && list->last_abbrev != NULL);
1063
1064 if (list->last_abbrev->first_attr == NULL)
1065 list->last_abbrev->first_attr = attr;
19e6b90e 1066 else
bcd213b2 1067 list->last_abbrev->last_attr->next = attr;
19e6b90e 1068
bcd213b2 1069 list->last_abbrev->last_attr = attr;
19e6b90e
L
1070}
1071
1072/* Processes the (partial) contents of a .debug_abbrev section.
1073 Returns NULL if the end of the section was encountered.
1074 Returns the address after the last byte read if the end of
1075 an abbreviation set was found. */
1076
1077static unsigned char *
f2f9554b
AM
1078process_abbrev_set (struct dwarf_section *section,
1079 dwarf_vma abbrev_base,
1080 dwarf_vma abbrev_size,
1081 dwarf_vma abbrev_offset,
1082 abbrev_list *list)
19e6b90e 1083{
f2f9554b
AM
1084 if (abbrev_base >= section->size
1085 || abbrev_size > section->size - abbrev_base)
1086 {
1087 /* PR 17531: file:4bcd9ce9. */
1088 warn (_("Debug info is corrupted, abbrev size (%lx) is larger than "
1089 "abbrev section size (%lx)\n"),
d30182b5 1090 (unsigned long) (abbrev_base + abbrev_size),
f2f9554b
AM
1091 (unsigned long) section->size);
1092 return NULL;
1093 }
1094 if (abbrev_offset >= abbrev_size)
1095 {
1096 warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than "
1097 "abbrev section size (%lx)\n"),
1098 (unsigned long) abbrev_offset,
1099 (unsigned long) abbrev_size);
1100 return NULL;
1101 }
1102
1103 unsigned char *start = section->start + abbrev_base;
1104 unsigned char *end = start + abbrev_size;
1105 start += abbrev_offset;
19e6b90e
L
1106 while (start < end)
1107 {
19e6b90e
L
1108 unsigned long entry;
1109 unsigned long tag;
1110 unsigned long attribute;
1111 int children;
1112
cd30bcef 1113 READ_ULEB (entry, start, end);
19e6b90e 1114
bcd213b2 1115 /* A single zero is supposed to end the set according
19e6b90e
L
1116 to the standard. If there's more, then signal that to
1117 the caller. */
f6f0e17b
NC
1118 if (start == end)
1119 return NULL;
19e6b90e 1120 if (entry == 0)
f6f0e17b 1121 return start;
19e6b90e 1122
cd30bcef 1123 READ_ULEB (tag, start, end);
f6f0e17b
NC
1124 if (start == end)
1125 return NULL;
19e6b90e
L
1126
1127 children = *start++;
1128
bcd213b2 1129 add_abbrev (entry, tag, children, list);
19e6b90e
L
1130
1131 do
1132 {
1133 unsigned long form;
77145576
JK
1134 /* Initialize it due to a false compiler warning. */
1135 bfd_signed_vma implicit_const = -1;
19e6b90e 1136
cd30bcef 1137 READ_ULEB (attribute, start, end);
f6f0e17b
NC
1138 if (start == end)
1139 break;
19e6b90e 1140
cd30bcef 1141 READ_ULEB (form, start, end);
f6f0e17b
NC
1142 if (start == end)
1143 break;
19e6b90e 1144
77145576
JK
1145 if (form == DW_FORM_implicit_const)
1146 {
cd30bcef 1147 READ_SLEB (implicit_const, start, end);
77145576
JK
1148 if (start == end)
1149 break;
1150 }
1151
bcd213b2 1152 add_abbrev_attr (attribute, form, implicit_const, list);
19e6b90e
L
1153 }
1154 while (attribute != 0);
1155 }
1156
399c99f7
L
1157 /* Report the missing single zero which ends the section. */
1158 error (_(".debug_abbrev section not zero terminated\n"));
1159
19e6b90e
L
1160 return NULL;
1161}
1162
a19c41a7 1163static const char *
19e6b90e
L
1164get_TAG_name (unsigned long tag)
1165{
04914e37 1166 const char *name = get_DW_TAG_name ((unsigned int) tag);
a19c41a7
TT
1167
1168 if (name == NULL)
19e6b90e 1169 {
a19c41a7 1170 static char buffer[100];
19e6b90e 1171
04914e37
NC
1172 if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1173 snprintf (buffer, sizeof (buffer), _("User TAG value: %#lx"), tag);
1174 else
1175 snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %#lx"), tag);
a19c41a7 1176 return buffer;
19e6b90e 1177 }
a19c41a7
TT
1178
1179 return name;
19e6b90e
L
1180}
1181
a19c41a7 1182static const char *
19e6b90e
L
1183get_FORM_name (unsigned long form)
1184{
399c99f7 1185 const char *name;
bf5117e3 1186
399c99f7
L
1187 if (form == 0)
1188 return "DW_FORM value: 0";
a19c41a7 1189
399c99f7 1190 name = get_DW_FORM_name (form);
a19c41a7 1191 if (name == NULL)
19e6b90e 1192 {
a19c41a7 1193 static char buffer[100];
19e6b90e 1194
a19c41a7
TT
1195 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1196 return buffer;
19e6b90e 1197 }
a19c41a7
TT
1198
1199 return name;
19e6b90e
L
1200}
1201
61364358
JK
1202static const char *
1203get_IDX_name (unsigned long idx)
1204{
1205 const char *name = get_DW_IDX_name ((unsigned int) idx);
1206
1207 if (name == NULL)
1208 {
1209 static char buffer[100];
1210
1211 snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1212 return buffer;
1213 }
1214
1215 return name;
1216}
1217
19e6b90e 1218static unsigned char *
0c588247
NC
1219display_block (unsigned char *data,
1220 dwarf_vma length,
ef0b5f1c 1221 const unsigned char * const end, char delimiter)
19e6b90e 1222{
0c588247
NC
1223 dwarf_vma maxlen;
1224
ef0b5f1c 1225 printf (_("%c%s byte block: "), delimiter, dwarf_vmatoa ("u", length));
a1165289
NC
1226 if (data > end)
1227 return (unsigned char *) end;
19e6b90e 1228
0c588247
NC
1229 maxlen = (dwarf_vma) (end - data);
1230 length = length > maxlen ? maxlen : length;
1231
19e6b90e
L
1232 while (length --)
1233 printf ("%lx ", (unsigned long) byte_get (data++, 1));
1234
1235 return data;
1236}
1237
1238static int
1239decode_location_expression (unsigned char * data,
1240 unsigned int pointer_size,
b7807392
JJ
1241 unsigned int offset_size,
1242 int dwarf_version,
467c65bc
NC
1243 dwarf_vma length,
1244 dwarf_vma cu_offset,
f1c4cc75 1245 struct dwarf_section * section)
19e6b90e
L
1246{
1247 unsigned op;
467c65bc 1248 dwarf_vma uvalue;
0c588247 1249 dwarf_signed_vma svalue;
19e6b90e
L
1250 unsigned char *end = data + length;
1251 int need_frame_base = 0;
1252
1253 while (data < end)
1254 {
1255 op = *data++;
1256
1257 switch (op)
1258 {
1259 case DW_OP_addr:
0c588247
NC
1260 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1261 printf ("DW_OP_addr: %s", dwarf_vmatoa ("x", uvalue));
19e6b90e
L
1262 break;
1263 case DW_OP_deref:
1264 printf ("DW_OP_deref");
1265 break;
1266 case DW_OP_const1u:
0c588247
NC
1267 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1268 printf ("DW_OP_const1u: %lu", (unsigned long) uvalue);
19e6b90e
L
1269 break;
1270 case DW_OP_const1s:
0c588247
NC
1271 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1272 printf ("DW_OP_const1s: %ld", (long) svalue);
19e6b90e
L
1273 break;
1274 case DW_OP_const2u:
87bc83b3 1275 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
0c588247 1276 printf ("DW_OP_const2u: %lu", (unsigned long) uvalue);
19e6b90e
L
1277 break;
1278 case DW_OP_const2s:
0c588247
NC
1279 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1280 printf ("DW_OP_const2s: %ld", (long) svalue);
19e6b90e
L
1281 break;
1282 case DW_OP_const4u:
0c588247
NC
1283 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1284 printf ("DW_OP_const4u: %lu", (unsigned long) uvalue);
19e6b90e
L
1285 break;
1286 case DW_OP_const4s:
0c588247
NC
1287 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1288 printf ("DW_OP_const4s: %ld", (long) svalue);
19e6b90e
L
1289 break;
1290 case DW_OP_const8u:
0c588247
NC
1291 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1292 printf ("DW_OP_const8u: %lu ", (unsigned long) uvalue);
1293 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1294 printf ("%lu", (unsigned long) uvalue);
19e6b90e
L
1295 break;
1296 case DW_OP_const8s:
0c588247
NC
1297 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1298 printf ("DW_OP_const8s: %ld ", (long) svalue);
1299 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1300 printf ("%ld", (long) svalue);
19e6b90e
L
1301 break;
1302 case DW_OP_constu:
cd30bcef
AM
1303 READ_ULEB (uvalue, data, end);
1304 printf ("DW_OP_constu: %s", dwarf_vmatoa ("u", uvalue));
19e6b90e
L
1305 break;
1306 case DW_OP_consts:
cd30bcef
AM
1307 READ_SLEB (svalue, data, end);
1308 printf ("DW_OP_consts: %s", dwarf_vmatoa ("d", svalue));
19e6b90e
L
1309 break;
1310 case DW_OP_dup:
1311 printf ("DW_OP_dup");
1312 break;
1313 case DW_OP_drop:
1314 printf ("DW_OP_drop");
1315 break;
1316 case DW_OP_over:
1317 printf ("DW_OP_over");
1318 break;
1319 case DW_OP_pick:
0c588247
NC
1320 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1321 printf ("DW_OP_pick: %ld", (unsigned long) uvalue);
19e6b90e
L
1322 break;
1323 case DW_OP_swap:
1324 printf ("DW_OP_swap");
1325 break;
1326 case DW_OP_rot:
1327 printf ("DW_OP_rot");
1328 break;
1329 case DW_OP_xderef:
1330 printf ("DW_OP_xderef");
1331 break;
1332 case DW_OP_abs:
1333 printf ("DW_OP_abs");
1334 break;
1335 case DW_OP_and:
1336 printf ("DW_OP_and");
1337 break;
1338 case DW_OP_div:
1339 printf ("DW_OP_div");
1340 break;
1341 case DW_OP_minus:
1342 printf ("DW_OP_minus");
1343 break;
1344 case DW_OP_mod:
1345 printf ("DW_OP_mod");
1346 break;
1347 case DW_OP_mul:
1348 printf ("DW_OP_mul");
1349 break;
1350 case DW_OP_neg:
1351 printf ("DW_OP_neg");
1352 break;
1353 case DW_OP_not:
1354 printf ("DW_OP_not");
1355 break;
1356 case DW_OP_or:
1357 printf ("DW_OP_or");
1358 break;
1359 case DW_OP_plus:
1360 printf ("DW_OP_plus");
1361 break;
1362 case DW_OP_plus_uconst:
cd30bcef
AM
1363 READ_ULEB (uvalue, data, end);
1364 printf ("DW_OP_plus_uconst: %s", dwarf_vmatoa ("u", uvalue));
19e6b90e
L
1365 break;
1366 case DW_OP_shl:
1367 printf ("DW_OP_shl");
1368 break;
1369 case DW_OP_shr:
1370 printf ("DW_OP_shr");
1371 break;
1372 case DW_OP_shra:
1373 printf ("DW_OP_shra");
1374 break;
1375 case DW_OP_xor:
1376 printf ("DW_OP_xor");
1377 break;
1378 case DW_OP_bra:
0c588247
NC
1379 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1380 printf ("DW_OP_bra: %ld", (long) svalue);
19e6b90e
L
1381 break;
1382 case DW_OP_eq:
1383 printf ("DW_OP_eq");
1384 break;
1385 case DW_OP_ge:
1386 printf ("DW_OP_ge");
1387 break;
1388 case DW_OP_gt:
1389 printf ("DW_OP_gt");
1390 break;
1391 case DW_OP_le:
1392 printf ("DW_OP_le");
1393 break;
1394 case DW_OP_lt:
1395 printf ("DW_OP_lt");
1396 break;
1397 case DW_OP_ne:
1398 printf ("DW_OP_ne");
1399 break;
1400 case DW_OP_skip:
0c588247
NC
1401 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1402 printf ("DW_OP_skip: %ld", (long) svalue);
19e6b90e
L
1403 break;
1404
1405 case DW_OP_lit0:
1406 case DW_OP_lit1:
1407 case DW_OP_lit2:
1408 case DW_OP_lit3:
1409 case DW_OP_lit4:
1410 case DW_OP_lit5:
1411 case DW_OP_lit6:
1412 case DW_OP_lit7:
1413 case DW_OP_lit8:
1414 case DW_OP_lit9:
1415 case DW_OP_lit10:
1416 case DW_OP_lit11:
1417 case DW_OP_lit12:
1418 case DW_OP_lit13:
1419 case DW_OP_lit14:
1420 case DW_OP_lit15:
1421 case DW_OP_lit16:
1422 case DW_OP_lit17:
1423 case DW_OP_lit18:
1424 case DW_OP_lit19:
1425 case DW_OP_lit20:
1426 case DW_OP_lit21:
1427 case DW_OP_lit22:
1428 case DW_OP_lit23:
1429 case DW_OP_lit24:
1430 case DW_OP_lit25:
1431 case DW_OP_lit26:
1432 case DW_OP_lit27:
1433 case DW_OP_lit28:
1434 case DW_OP_lit29:
1435 case DW_OP_lit30:
1436 case DW_OP_lit31:
1437 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1438 break;
1439
1440 case DW_OP_reg0:
1441 case DW_OP_reg1:
1442 case DW_OP_reg2:
1443 case DW_OP_reg3:
1444 case DW_OP_reg4:
1445 case DW_OP_reg5:
1446 case DW_OP_reg6:
1447 case DW_OP_reg7:
1448 case DW_OP_reg8:
1449 case DW_OP_reg9:
1450 case DW_OP_reg10:
1451 case DW_OP_reg11:
1452 case DW_OP_reg12:
1453 case DW_OP_reg13:
1454 case DW_OP_reg14:
1455 case DW_OP_reg15:
1456 case DW_OP_reg16:
1457 case DW_OP_reg17:
1458 case DW_OP_reg18:
1459 case DW_OP_reg19:
1460 case DW_OP_reg20:
1461 case DW_OP_reg21:
1462 case DW_OP_reg22:
1463 case DW_OP_reg23:
1464 case DW_OP_reg24:
1465 case DW_OP_reg25:
1466 case DW_OP_reg26:
1467 case DW_OP_reg27:
1468 case DW_OP_reg28:
1469 case DW_OP_reg29:
1470 case DW_OP_reg30:
1471 case DW_OP_reg31:
18464d4d
JK
1472 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1473 regname (op - DW_OP_reg0, 1));
19e6b90e
L
1474 break;
1475
1476 case DW_OP_breg0:
1477 case DW_OP_breg1:
1478 case DW_OP_breg2:
1479 case DW_OP_breg3:
1480 case DW_OP_breg4:
1481 case DW_OP_breg5:
1482 case DW_OP_breg6:
1483 case DW_OP_breg7:
1484 case DW_OP_breg8:
1485 case DW_OP_breg9:
1486 case DW_OP_breg10:
1487 case DW_OP_breg11:
1488 case DW_OP_breg12:
1489 case DW_OP_breg13:
1490 case DW_OP_breg14:
1491 case DW_OP_breg15:
1492 case DW_OP_breg16:
1493 case DW_OP_breg17:
1494 case DW_OP_breg18:
1495 case DW_OP_breg19:
1496 case DW_OP_breg20:
1497 case DW_OP_breg21:
1498 case DW_OP_breg22:
1499 case DW_OP_breg23:
1500 case DW_OP_breg24:
1501 case DW_OP_breg25:
1502 case DW_OP_breg26:
1503 case DW_OP_breg27:
1504 case DW_OP_breg28:
1505 case DW_OP_breg29:
1506 case DW_OP_breg30:
1507 case DW_OP_breg31:
cd30bcef
AM
1508 READ_SLEB (svalue, data, end);
1509 printf ("DW_OP_breg%d (%s): %s", op - DW_OP_breg0,
1510 regname (op - DW_OP_breg0, 1), dwarf_vmatoa ("d", svalue));
19e6b90e
L
1511 break;
1512
1513 case DW_OP_regx:
cd30bcef 1514 READ_ULEB (uvalue, data, end);
467c65bc
NC
1515 printf ("DW_OP_regx: %s (%s)",
1516 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
19e6b90e
L
1517 break;
1518 case DW_OP_fbreg:
1519 need_frame_base = 1;
cd30bcef
AM
1520 READ_SLEB (svalue, data, end);
1521 printf ("DW_OP_fbreg: %s", dwarf_vmatoa ("d", svalue));
19e6b90e
L
1522 break;
1523 case DW_OP_bregx:
cd30bcef
AM
1524 READ_ULEB (uvalue, data, end);
1525 READ_SLEB (svalue, data, end);
467c65bc
NC
1526 printf ("DW_OP_bregx: %s (%s) %s",
1527 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
cd30bcef 1528 dwarf_vmatoa ("d", svalue));
19e6b90e
L
1529 break;
1530 case DW_OP_piece:
cd30bcef
AM
1531 READ_ULEB (uvalue, data, end);
1532 printf ("DW_OP_piece: %s", dwarf_vmatoa ("u", uvalue));
19e6b90e
L
1533 break;
1534 case DW_OP_deref_size:
0c588247
NC
1535 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1536 printf ("DW_OP_deref_size: %ld", (long) uvalue);
19e6b90e
L
1537 break;
1538 case DW_OP_xderef_size:
0c588247
NC
1539 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1540 printf ("DW_OP_xderef_size: %ld", (long) uvalue);
19e6b90e
L
1541 break;
1542 case DW_OP_nop:
1543 printf ("DW_OP_nop");
1544 break;
1545
1546 /* DWARF 3 extensions. */
1547 case DW_OP_push_object_address:
1548 printf ("DW_OP_push_object_address");
1549 break;
1550 case DW_OP_call2:
d85bf2ba 1551 /* FIXME: Strictly speaking for 64-bit DWARF3 files
19e6b90e 1552 this ought to be an 8-byte wide computation. */
0c588247 1553 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
467c65bc 1554 printf ("DW_OP_call2: <0x%s>",
0c588247 1555 dwarf_vmatoa ("x", svalue + cu_offset));
19e6b90e
L
1556 break;
1557 case DW_OP_call4:
d85bf2ba 1558 /* FIXME: Strictly speaking for 64-bit DWARF3 files
19e6b90e 1559 this ought to be an 8-byte wide computation. */
0c588247 1560 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
467c65bc 1561 printf ("DW_OP_call4: <0x%s>",
0c588247 1562 dwarf_vmatoa ("x", svalue + cu_offset));
19e6b90e
L
1563 break;
1564 case DW_OP_call_ref:
d85bf2ba 1565 /* FIXME: Strictly speaking for 64-bit DWARF3 files
e2a0d921 1566 this ought to be an 8-byte wide computation. */
b7807392
JJ
1567 if (dwarf_version == -1)
1568 {
1569 printf (_("(DW_OP_call_ref in frame info)"));
1570 /* No way to tell where the next op is, so just bail. */
1571 return need_frame_base;
1572 }
1573 if (dwarf_version == 2)
1574 {
0c588247 1575 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
b7807392
JJ
1576 }
1577 else
1578 {
0c588247 1579 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
b7807392 1580 }
0c588247 1581 printf ("DW_OP_call_ref: <0x%s>", dwarf_vmatoa ("x", uvalue));
19e6b90e 1582 break;
a87b0a59
NS
1583 case DW_OP_form_tls_address:
1584 printf ("DW_OP_form_tls_address");
1585 break;
e2a0d921
NC
1586 case DW_OP_call_frame_cfa:
1587 printf ("DW_OP_call_frame_cfa");
1588 break;
1589 case DW_OP_bit_piece:
1590 printf ("DW_OP_bit_piece: ");
cd30bcef
AM
1591 READ_ULEB (uvalue, data, end);
1592 printf (_("size: %s "), dwarf_vmatoa ("u", uvalue));
1593 READ_ULEB (uvalue, data, end);
1594 printf (_("offset: %s "), dwarf_vmatoa ("u", uvalue));
e2a0d921 1595 break;
19e6b90e 1596
3244e8f5
JJ
1597 /* DWARF 4 extensions. */
1598 case DW_OP_stack_value:
1599 printf ("DW_OP_stack_value");
1600 break;
1601
1602 case DW_OP_implicit_value:
1603 printf ("DW_OP_implicit_value");
cd30bcef 1604 READ_ULEB (uvalue, data, end);
ef0b5f1c 1605 data = display_block (data, uvalue, end, ' ');
3244e8f5
JJ
1606 break;
1607
19e6b90e
L
1608 /* GNU extensions. */
1609 case DW_OP_GNU_push_tls_address:
9cf03b7e 1610 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
e2a0d921
NC
1611 break;
1612 case DW_OP_GNU_uninit:
1613 printf ("DW_OP_GNU_uninit");
1614 /* FIXME: Is there data associated with this OP ? */
1615 break;
f1c4cc75
RH
1616 case DW_OP_GNU_encoded_addr:
1617 {
6937bb54 1618 int encoding = 0;
f1c4cc75 1619 dwarf_vma addr;
467c65bc 1620
6937bb54
NC
1621 if (data < end)
1622 encoding = *data++;
041830e0 1623 addr = get_encoded_value (&data, encoding, section, end);
f1c4cc75
RH
1624
1625 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1626 print_dwarf_vma (addr, pointer_size);
1627 }
1628 break;
bc0a77d2 1629 case DW_OP_implicit_pointer:
b7807392 1630 case DW_OP_GNU_implicit_pointer:
d85bf2ba 1631 /* FIXME: Strictly speaking for 64-bit DWARF3 files
b7807392
JJ
1632 this ought to be an 8-byte wide computation. */
1633 if (dwarf_version == -1)
1634 {
bc0a77d2
JK
1635 printf (_("(%s in frame info)"),
1636 (op == DW_OP_implicit_pointer
1637 ? "DW_OP_implicit_pointer"
1638 : "DW_OP_GNU_implicit_pointer"));
b7807392
JJ
1639 /* No way to tell where the next op is, so just bail. */
1640 return need_frame_base;
1641 }
1642 if (dwarf_version == 2)
1643 {
0c588247 1644 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
b7807392
JJ
1645 }
1646 else
1647 {
0c588247 1648 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
b7807392 1649 }
cd30bcef 1650 READ_SLEB (svalue, data, end);
bc0a77d2
JK
1651 printf ("%s: <0x%s> %s",
1652 (op == DW_OP_implicit_pointer
1653 ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
0c588247 1654 dwarf_vmatoa ("x", uvalue),
cd30bcef 1655 dwarf_vmatoa ("d", svalue));
b7807392 1656 break;
77145576 1657 case DW_OP_entry_value:
0892011d 1658 case DW_OP_GNU_entry_value:
cd30bcef 1659 READ_ULEB (uvalue, data, end);
058037d3
NC
1660 /* PR 17531: file: 0cc9cd00. */
1661 if (uvalue > (dwarf_vma) (end - data))
1662 uvalue = end - data;
77145576
JK
1663 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1664 : "DW_OP_GNU_entry_value"));
0892011d
JJ
1665 if (decode_location_expression (data, pointer_size, offset_size,
1666 dwarf_version, uvalue,
1667 cu_offset, section))
1668 need_frame_base = 1;
1669 putchar (')');
1670 data += uvalue;
6937bb54
NC
1671 if (data > end)
1672 data = end;
0892011d 1673 break;
bc0a77d2 1674 case DW_OP_const_type:
0892011d 1675 case DW_OP_GNU_const_type:
cd30bcef 1676 READ_ULEB (uvalue, data, end);
bc0a77d2
JK
1677 printf ("%s: <0x%s> ",
1678 (op == DW_OP_const_type ? "DW_OP_const_type"
1679 : "DW_OP_GNU_const_type"),
0892011d 1680 dwarf_vmatoa ("x", cu_offset + uvalue));
0c588247 1681 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
ef0b5f1c 1682 data = display_block (data, uvalue, end, ' ');
0892011d 1683 break;
bc0a77d2 1684 case DW_OP_regval_type:
0892011d 1685 case DW_OP_GNU_regval_type:
cd30bcef 1686 READ_ULEB (uvalue, data, end);
bc0a77d2
JK
1687 printf ("%s: %s (%s)",
1688 (op == DW_OP_regval_type ? "DW_OP_regval_type"
1689 : "DW_OP_GNU_regval_type"),
0892011d 1690 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
cd30bcef 1691 READ_ULEB (uvalue, data, end);
0892011d
JJ
1692 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1693 break;
bc0a77d2 1694 case DW_OP_deref_type:
0892011d 1695 case DW_OP_GNU_deref_type:
0c588247 1696 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
bc0a77d2
JK
1697 printf ("%s: %ld",
1698 (op == DW_OP_deref_type ? "DW_OP_deref_type"
1699 : "DW_OP_GNU_deref_type"),
1700 (long) uvalue);
cd30bcef 1701 READ_ULEB (uvalue, data, end);
0892011d
JJ
1702 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1703 break;
bc0a77d2 1704 case DW_OP_convert:
0892011d 1705 case DW_OP_GNU_convert:
cd30bcef 1706 READ_ULEB (uvalue, data, end);
bc0a77d2
JK
1707 printf ("%s <0x%s>",
1708 (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
f8b999f9 1709 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
0892011d 1710 break;
bc0a77d2 1711 case DW_OP_reinterpret:
0892011d 1712 case DW_OP_GNU_reinterpret:
cd30bcef 1713 READ_ULEB (uvalue, data, end);
bc0a77d2
JK
1714 printf ("%s <0x%s>",
1715 (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1716 : "DW_OP_GNU_reinterpret"),
f8b999f9
JJ
1717 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1718 break;
1719 case DW_OP_GNU_parameter_ref:
0c588247 1720 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
f8b999f9 1721 printf ("DW_OP_GNU_parameter_ref: <0x%s>",
0c588247 1722 dwarf_vmatoa ("x", cu_offset + uvalue));
0892011d 1723 break;
b4eb7656 1724 case DW_OP_GNU_addr_index:
cd30bcef 1725 READ_ULEB (uvalue, data, end);
b4eb7656
AM
1726 printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1727 break;
1728 case DW_OP_GNU_const_index:
cd30bcef 1729 READ_ULEB (uvalue, data, end);
b4eb7656
AM
1730 printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1731 break;
f384a1f0
KB
1732 case DW_OP_GNU_variable_value:
1733 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1734 this ought to be an 8-byte wide computation. */
1735 if (dwarf_version == -1)
1736 {
1737 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1738 /* No way to tell where the next op is, so just bail. */
1739 return need_frame_base;
1740 }
1741 if (dwarf_version == 2)
1742 {
1743 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1744 }
1745 else
1746 {
1747 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1748 }
1749 printf ("DW_OP_GNU_variable_value: <0x%s>", dwarf_vmatoa ("x", uvalue));
1750 break;
e2a0d921
NC
1751
1752 /* HP extensions. */
1753 case DW_OP_HP_is_value:
1754 printf ("DW_OP_HP_is_value");
1755 /* FIXME: Is there data associated with this OP ? */
1756 break;
1757 case DW_OP_HP_fltconst4:
1758 printf ("DW_OP_HP_fltconst4");
1759 /* FIXME: Is there data associated with this OP ? */
1760 break;
1761 case DW_OP_HP_fltconst8:
1762 printf ("DW_OP_HP_fltconst8");
1763 /* FIXME: Is there data associated with this OP ? */
1764 break;
1765 case DW_OP_HP_mod_range:
1766 printf ("DW_OP_HP_mod_range");
1767 /* FIXME: Is there data associated with this OP ? */
1768 break;
1769 case DW_OP_HP_unmod_range:
1770 printf ("DW_OP_HP_unmod_range");
1771 /* FIXME: Is there data associated with this OP ? */
1772 break;
1773 case DW_OP_HP_tls:
1774 printf ("DW_OP_HP_tls");
1775 /* FIXME: Is there data associated with this OP ? */
19e6b90e
L
1776 break;
1777
35d60fe4
NC
1778 /* PGI (STMicroelectronics) extensions. */
1779 case DW_OP_PGI_omp_thread_num:
1780 /* Pushes the thread number for the current thread as it would be
1781 returned by the standard OpenMP library function:
1782 omp_get_thread_num(). The "current thread" is the thread for
1783 which the expression is being evaluated. */
1784 printf ("DW_OP_PGI_omp_thread_num");
1785 break;
1786
19e6b90e
L
1787 default:
1788 if (op >= DW_OP_lo_user
1789 && op <= DW_OP_hi_user)
0502a2b4 1790 printf (_("(User defined location op 0x%x)"), op);
19e6b90e 1791 else
0502a2b4 1792 printf (_("(Unknown location op 0x%x)"), op);
19e6b90e
L
1793 /* No way to tell where the next op is, so just bail. */
1794 return need_frame_base;
1795 }
1796
1797 /* Separate the ops. */
1798 if (data < end)
1799 printf ("; ");
1800 }
1801
1802 return need_frame_base;
1803}
1804
341f9135
CC
1805/* Find the CU or TU set corresponding to the given CU_OFFSET.
1806 This is used for DWARF package files. */
1807
1808static struct cu_tu_set *
1809find_cu_tu_set_v2 (dwarf_vma cu_offset, int do_types)
1810{
1811 struct cu_tu_set *p;
1812 unsigned int nsets;
1813 unsigned int dw_sect;
1814
1815 if (do_types)
1816 {
1817 p = tu_sets;
1818 nsets = tu_count;
1819 dw_sect = DW_SECT_TYPES;
1820 }
1821 else
1822 {
1823 p = cu_sets;
1824 nsets = cu_count;
1825 dw_sect = DW_SECT_INFO;
1826 }
1827 while (nsets > 0)
1828 {
1829 if (p->section_offsets [dw_sect] == cu_offset)
1830 return p;
1831 p++;
1832 nsets--;
1833 }
1834 return NULL;
1835}
1836
a69c4772
NC
1837/* Add INC to HIGH_BITS:LOW_BITS. */
1838static void
1839add64 (dwarf_vma * high_bits, dwarf_vma * low_bits, dwarf_vma inc)
1840{
1841 dwarf_vma tmp = * low_bits;
1842
1843 tmp += inc;
1844
1845 /* FIXME: There is probably a better way of handling this:
1846
1847 We need to cope with dwarf_vma being a 32-bit or 64-bit
1848 type. Plus regardless of its size LOW_BITS is meant to
1849 only hold 32-bits, so if there is overflow or wrap around
1850 we must propagate into HIGH_BITS. */
1851 if (tmp < * low_bits)
1852 {
1853 ++ * high_bits;
1854 }
1855 else if (sizeof (tmp) > 8
1856 && (tmp >> 31) > 1)
1857 {
1858 ++ * high_bits;
1859 tmp &= 0xFFFFFFFF;
1860 }
1861
1862 * low_bits = tmp;
1863}
1864
dda8d76d
NC
1865static const char *
1866fetch_alt_indirect_string (dwarf_vma offset)
1867{
24841daa 1868 separate_info * i;
dda8d76d
NC
1869
1870 if (! do_follow_links)
1871 return "";
1872
24841daa
NC
1873 if (first_separate_info == NULL)
1874 return _("<no links available>");
dda8d76d 1875
24841daa
NC
1876 for (i = first_separate_info; i != NULL; i = i->next)
1877 {
1878 struct dwarf_section * section;
1879 const char * ret;
dda8d76d 1880
24841daa
NC
1881 if (! load_debug_section (separate_debug_str, i->handle))
1882 continue;
dda8d76d 1883
24841daa 1884 section = &debug_displays [separate_debug_str].section;
dda8d76d 1885
24841daa
NC
1886 if (section->start == NULL)
1887 continue;
dda8d76d 1888
24841daa
NC
1889 if (offset >= section->size)
1890 continue;
1891
1892 ret = (const char *) (section->start + offset);
1893 /* Unfortunately we cannot rely upon the .debug_str section ending with a
1894 NUL byte. Since our caller is expecting to receive a well formed C
1895 string we test for the lack of a terminating byte here. */
1896 if (strnlen ((const char *) ret, section->size - offset)
1897 == section->size - offset)
1898 return _("<no NUL byte at end of alt .debug_str section>");
1899
1900 return ret;
1901 }
521d4b19 1902
24841daa
NC
1903 warn (_("DW_FORM_GNU_strp_alt offset (%s) too big or no string sections available\n"),
1904 dwarf_vmatoa ("x", offset));
1905 return _("<offset is too big>");
dda8d76d
NC
1906}
1907
d85bf2ba
NC
1908static const char *
1909get_AT_name (unsigned long attribute)
1910{
1911 const char *name;
1912
1913 if (attribute == 0)
1914 return "DW_AT value: 0";
1915
1916 /* One value is shared by the MIPS and HP extensions: */
1917 if (attribute == DW_AT_MIPS_fde)
1918 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1919
1920 name = get_DW_AT_name (attribute);
1921
1922 if (name == NULL)
1923 {
1924 static char buffer[100];
1925
1926 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1927 attribute);
1928 return buffer;
1929 }
1930
1931 return name;
1932}
1933
24841daa 1934static void
50ea0877 1935add_dwo_info (const char * value, dwarf_vma cu_offset, dwo_type type)
24841daa
NC
1936{
1937 dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1938
50ea0877
NC
1939 dwinfo->type = type;
1940 dwinfo->value = value;
1941 dwinfo->cu_offset = cu_offset;
1942 dwinfo->next = first_dwo_info;
24841daa
NC
1943 first_dwo_info = dwinfo;
1944}
1945
1946static void
50ea0877 1947add_dwo_name (const char * name, dwarf_vma cu_offset)
24841daa 1948{
50ea0877 1949 add_dwo_info (name, cu_offset, DWO_NAME);
24841daa
NC
1950}
1951
1952static void
50ea0877 1953add_dwo_dir (const char * dir, dwarf_vma cu_offset)
24841daa 1954{
50ea0877 1955 add_dwo_info (dir, cu_offset, DWO_DIR);
24841daa
NC
1956}
1957
1958static void
50ea0877 1959add_dwo_id (const char * id, dwarf_vma cu_offset)
24841daa 1960{
50ea0877 1961 add_dwo_info (id, cu_offset, DWO_ID);
24841daa
NC
1962}
1963
1964static void
1965free_dwo_info (void)
1966{
1967 dwo_info * dwinfo;
1968 dwo_info * next;
1969
1970 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1971 {
1972 next = dwinfo->next;
1973 free (dwinfo);
1974 }
1975 first_dwo_info = NULL;
1976}
1977
afc72f15
NC
1978/* Ensure that START + UVALUE is less than END.
1979 Return an adjusted UVALUE if necessary to ensure this relationship. */
1980
1981static inline dwarf_vma
1982check_uvalue (const unsigned char * start,
1983 dwarf_vma uvalue,
1984 const unsigned char * end)
1985{
1986 dwarf_vma max_uvalue = end - start;
1987
afc72f15
NC
1988 /* See PR 17512: file: 008-103549-0.001:0.1.
1989 and PR 24829 for examples of where these tests are triggered. */
a85eba51 1990 if (uvalue > max_uvalue)
afc72f15
NC
1991 {
1992 warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1993 uvalue = max_uvalue;
1994 }
1995
1996 return uvalue;
1997}
1998
ec1b0fbb
NC
1999static unsigned char *
2000skip_attr_bytes (unsigned long form,
2001 unsigned char * data,
2002 unsigned const char * end,
2003 dwarf_vma pointer_size,
2004 dwarf_vma offset_size,
2005 int dwarf_version,
2006 dwarf_vma * value_return)
2007{
cd30bcef
AM
2008 dwarf_signed_vma svalue;
2009 dwarf_vma uvalue = 0;
ec1b0fbb
NC
2010
2011 * value_return = 0;
2012
2013 switch (form)
2014 {
2015 case DW_FORM_ref_addr:
2016 if (dwarf_version == 2)
2017 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
59624513 2018 else if (dwarf_version > 2)
ec1b0fbb
NC
2019 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2020 else
2021 return NULL;
2022 break;
2023
2024 case DW_FORM_addr:
2025 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2026 break;
2027
2028 case DW_FORM_strp:
2029 case DW_FORM_line_strp:
2030 case DW_FORM_sec_offset:
2031 case DW_FORM_GNU_ref_alt:
2032 case DW_FORM_GNU_strp_alt:
2033 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2034 break;
2035
2036 case DW_FORM_flag_present:
2037 uvalue = 1;
2038 break;
2039
2040 case DW_FORM_ref1:
2041 case DW_FORM_flag:
2042 case DW_FORM_data1:
32e4f96c
TV
2043 case DW_FORM_strx1:
2044 case DW_FORM_addrx1:
ec1b0fbb
NC
2045 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2046 break;
2047
32e4f96c
TV
2048 case DW_FORM_strx3:
2049 case DW_FORM_addrx3:
2050 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2051 break;
2052
ec1b0fbb
NC
2053 case DW_FORM_ref2:
2054 case DW_FORM_data2:
32e4f96c
TV
2055 case DW_FORM_strx2:
2056 case DW_FORM_addrx2:
ec1b0fbb
NC
2057 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2058 break;
2059
2060 case DW_FORM_ref4:
2061 case DW_FORM_data4:
32e4f96c
TV
2062 case DW_FORM_strx4:
2063 case DW_FORM_addrx4:
ec1b0fbb
NC
2064 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2065 break;
2066
2067 case DW_FORM_sdata:
cd30bcef
AM
2068 READ_SLEB (svalue, data, end);
2069 uvalue = svalue;
ec1b0fbb
NC
2070 break;
2071
2072 case DW_FORM_ref_udata:
2073 case DW_FORM_udata:
2074 case DW_FORM_GNU_str_index:
32e4f96c 2075 case DW_FORM_strx:
ec1b0fbb 2076 case DW_FORM_GNU_addr_index:
32e4f96c 2077 case DW_FORM_addrx:
cd30bcef 2078 READ_ULEB (uvalue, data, end);
ec1b0fbb
NC
2079 break;
2080
2081 case DW_FORM_ref8:
1f573141
NC
2082 {
2083 dwarf_vma high_bits;
2084
2085 SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
2086 data += 8;
2087 if (sizeof (uvalue) > 4)
2088 uvalue += high_bits << 32;
2089 else if (high_bits != 0)
2090 {
2091 /* FIXME: What to do ? */
2092 return NULL;
2093 }
2094 break;
2095 }
2096
ec1b0fbb 2097 case DW_FORM_data8:
59624513 2098 case DW_FORM_ref_sig8:
ec1b0fbb
NC
2099 data += 8;
2100 break;
2101
2102 case DW_FORM_data16:
2103 data += 16;
2104 break;
2105
2106 case DW_FORM_string:
2107 data += strnlen ((char *) data, end - data) + 1;
2108 break;
2109
2110 case DW_FORM_block:
2111 case DW_FORM_exprloc:
cd30bcef 2112 READ_ULEB (uvalue, data, end);
59624513 2113 data += uvalue;
ec1b0fbb
NC
2114 break;
2115
2116 case DW_FORM_block1:
2117 SAFE_BYTE_GET (uvalue, data, 1, end);
2118 data += 1 + uvalue;
2119 break;
2120
2121 case DW_FORM_block2:
2122 SAFE_BYTE_GET (uvalue, data, 2, end);
2123 data += 2 + uvalue;
2124 break;
2125
2126 case DW_FORM_block4:
2127 SAFE_BYTE_GET (uvalue, data, 4, end);
2128 data += 4 + uvalue;
2129 break;
2130
ec1b0fbb 2131 case DW_FORM_indirect:
59624513
NC
2132 READ_ULEB (form, data, end);
2133 if (form == DW_FORM_implicit_const)
2134 SKIP_ULEB (data, end);
2135 return skip_attr_bytes (form, data, end, pointer_size, offset_size, dwarf_version, value_return);
2136
ec1b0fbb
NC
2137 default:
2138 return NULL;
2139 }
2140
2141 * value_return = uvalue;
2142 if (data > end)
2143 data = (unsigned char *) end;
2144 return data;
2145}
2146
bcd213b2
NC
2147/* Given form FORM with value UVALUE, locate and return the abbreviation
2148 associated with it. */
2149
2150static abbrev_entry *
2151get_type_abbrev_from_form (unsigned long form,
2152 unsigned long uvalue,
2153 dwarf_vma cu_offset,
2154 const struct dwarf_section * section,
2155 unsigned long * abbrev_num_return,
2156 unsigned char ** data_return,
2157 unsigned long * cu_offset_return)
2158{
2159 unsigned long abbrev_number;
2160 abbrev_map * map;
2161 abbrev_entry * entry;
2162 unsigned char * data;
2163
2164 if (abbrev_num_return != NULL)
2165 * abbrev_num_return = 0;
2166 if (data_return != NULL)
2167 * data_return = NULL;
2168
2169 switch (form)
2170 {
2171 case DW_FORM_GNU_ref_alt:
5f128a25 2172 case DW_FORM_ref_sig8:
bcd213b2
NC
2173 /* FIXME: We are unable to handle this form at the moment. */
2174 return NULL;
2175
2176 case DW_FORM_ref_addr:
2177 if (uvalue >= section->size)
2178 {
2179 warn (_("Unable to resolve ref_addr form: uvalue %lx > section size %lx (%s)\n"),
2180 uvalue, (long) section->size, section->name);
2181 return NULL;
2182 }
2183 break;
2184
e38332c2
NC
2185 case DW_FORM_ref_sup4:
2186 case DW_FORM_ref_sup8:
2187 break;
2188
bcd213b2
NC
2189 case DW_FORM_ref1:
2190 case DW_FORM_ref2:
2191 case DW_FORM_ref4:
1f573141 2192 case DW_FORM_ref8:
bcd213b2
NC
2193 case DW_FORM_ref_udata:
2194 if (uvalue + cu_offset > section->size)
2195 {
2196 warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %lx > section size %lx\n"),
2197 uvalue, (long) cu_offset, (long) section->size);
2198 return NULL;
2199 }
2200 uvalue += cu_offset;
2201 break;
2202
2203 /* FIXME: Are there other DW_FORMs that can be used by types ? */
2204
2205 default:
2206 warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2207 return NULL;
2208 }
2209
2210 data = (unsigned char *) section->start + uvalue;
2211 map = find_abbrev_map_by_offset (uvalue);
521d4b19 2212
bcd213b2
NC
2213 if (map == NULL)
2214 {
2215 warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2216 return NULL;
2217 }
2218 if (map->list == NULL)
2219 {
2220 warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2221 return NULL;
2222 }
2223
2224 if (cu_offset_return != NULL)
2225 {
2226 if (form == DW_FORM_ref_addr)
2227 * cu_offset_return = map->start;
2228 else
2229 * cu_offset_return = cu_offset;
2230 }
2231
2232 READ_ULEB (abbrev_number, data, section->start + section->size);
2233
2234 for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2235 if (entry->number == abbrev_number)
2236 break;
521d4b19 2237
bcd213b2
NC
2238 if (abbrev_num_return != NULL)
2239 * abbrev_num_return = abbrev_number;
2240
2241 if (data_return != NULL)
2242 * data_return = data;
2243
2244 if (entry == NULL)
2245 warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2246
2247 return entry;
2248}
2249
2250/* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2251 can be determined to be a signed type. The data for ENTRY can be
2252 found starting at DATA. */
ec1b0fbb
NC
2253
2254static void
015dc7e1
AM
2255get_type_signedness (abbrev_entry *entry,
2256 const struct dwarf_section *section,
2257 unsigned char *data,
2258 unsigned const char *end,
2259 dwarf_vma cu_offset,
2260 dwarf_vma pointer_size,
2261 dwarf_vma offset_size,
2262 int dwarf_version,
2263 bool *is_signed,
2264 unsigned int nesting)
ec1b0fbb 2265{
ec1b0fbb
NC
2266 abbrev_attr * attr;
2267
015dc7e1 2268 * is_signed = false;
ec1b0fbb 2269
59624513
NC
2270#define MAX_NESTING 20
2271 if (nesting > MAX_NESTING)
2272 {
2273 /* FIXME: Warn - or is this expected ?
2274 NB/ We need to avoid infinite recursion. */
2275 return;
2276 }
2277
ec1b0fbb
NC
2278 for (attr = entry->first_attr;
2279 attr != NULL && attr->attribute;
2280 attr = attr->next)
2281 {
bcd213b2 2282 unsigned char * orig_data = data;
ec1b0fbb
NC
2283 dwarf_vma uvalue = 0;
2284
2285 data = skip_attr_bytes (attr->form, data, end, pointer_size,
2286 offset_size, dwarf_version, & uvalue);
2287 if (data == NULL)
2288 return;
2289
2290 switch (attr->attribute)
2291 {
bcd213b2 2292 case DW_AT_linkage_name:
ec1b0fbb 2293 case DW_AT_name:
bcd213b2
NC
2294 if (do_wide)
2295 {
2296 if (attr->form == DW_FORM_strp)
2297 printf (", %s", fetch_indirect_string (uvalue));
2298 else if (attr->form == DW_FORM_string)
2299 printf (", %s", orig_data);
2300 }
ec1b0fbb 2301 break;
bcd213b2 2302
ec1b0fbb
NC
2303 case DW_AT_type:
2304 /* Recurse. */
bcd213b2
NC
2305 {
2306 abbrev_entry * type_abbrev;
2307 unsigned char * type_data;
2308 unsigned long type_cu_offset;
2309
2310 type_abbrev = get_type_abbrev_from_form (attr->form,
2311 uvalue,
2312 cu_offset,
2313 section,
2314 NULL /* abbrev num return */,
2315 & type_data,
2316 & type_cu_offset);
2317 if (type_abbrev == NULL)
2318 break;
2319
2320 get_type_signedness (type_abbrev, section, type_data, end, type_cu_offset,
2321 pointer_size, offset_size, dwarf_version,
2322 is_signed, nesting + 1);
2323 }
ec1b0fbb
NC
2324 break;
2325
2326 case DW_AT_encoding:
2327 /* Determine signness. */
2328 switch (uvalue)
2329 {
2330 case DW_ATE_address:
2331 /* FIXME - some architectures have signed addresses. */
2332 case DW_ATE_boolean:
2333 case DW_ATE_unsigned:
2334 case DW_ATE_unsigned_char:
2335 case DW_ATE_unsigned_fixed:
015dc7e1 2336 * is_signed = false;
ec1b0fbb
NC
2337 break;
2338
2339 default:
2340 case DW_ATE_complex_float:
2341 case DW_ATE_float:
2342 case DW_ATE_signed:
2343 case DW_ATE_signed_char:
2344 case DW_ATE_imaginary_float:
2345 case DW_ATE_decimal_float:
2346 case DW_ATE_signed_fixed:
015dc7e1 2347 * is_signed = true;
ec1b0fbb
NC
2348 break;
2349 }
2350 break;
2351 }
2352 }
2353}
2354
2355static void
015dc7e1
AM
2356read_and_print_leb128 (unsigned char *data,
2357 unsigned int *bytes_read,
2358 unsigned const char *end,
2359 bool is_signed)
ec1b0fbb 2360{
cd30bcef
AM
2361 int status;
2362 dwarf_vma val = read_leb128 (data, end, is_signed, bytes_read, &status);
2363 if (status != 0)
1b513401 2364 report_leb_status (status, __FILE__, __LINE__);
ec1b0fbb 2365 else
cd30bcef 2366 printf ("%s", dwarf_vmatoa (is_signed ? "d" : "u", val));
ec1b0fbb
NC
2367}
2368
2369static void
2370display_discr_list (unsigned long form,
2371 dwarf_vma uvalue,
2372 unsigned char * data,
2373 unsigned const char * end,
2374 int level)
2375{
2376 if (uvalue == 0)
2377 {
2378 printf ("[default]");
2379 return;
2380 }
521d4b19 2381
ec1b0fbb
NC
2382 switch (form)
2383 {
2384 case DW_FORM_block:
2385 case DW_FORM_block1:
2386 case DW_FORM_block2:
2387 case DW_FORM_block4:
2388 /* Move data pointer back to the start of the byte array. */
2389 data -= uvalue;
2390 break;
2391 default:
2392 printf ("<corrupt>\n");
2393 warn (_("corrupt discr_list - not using a block form\n"));
2394 return;
2395 }
2396
2397 if (uvalue < 2)
2398 {
2399 printf ("<corrupt>\n");
2400 warn (_("corrupt discr_list - block not long enough\n"));
2401 return;
2402 }
2403
015dc7e1 2404 bool is_signed =
ec1b0fbb 2405 (level > 0 && level <= MAX_CU_NESTING)
015dc7e1 2406 ? level_type_signed [level - 1] : false;
521d4b19 2407
ec1b0fbb
NC
2408 printf ("(");
2409 while (uvalue)
2410 {
2411 unsigned char discriminant;
2412 unsigned int bytes_read;
2413
2414 SAFE_BYTE_GET (discriminant, data, 1, end);
2415 -- uvalue;
2416 data ++;
2417
2418 assert (uvalue > 0);
2419 switch (discriminant)
2420 {
2421 case DW_DSC_label:
2422 printf ("label ");
2423 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2424 assert (bytes_read <= uvalue && bytes_read > 0);
2425 uvalue -= bytes_read;
2426 data += bytes_read;
2427 break;
2428
2429 case DW_DSC_range:
2430 printf ("range ");
2431 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2432 assert (bytes_read <= uvalue && bytes_read > 0);
2433 uvalue -= bytes_read;
2434 data += bytes_read;
2435
2436 printf ("..");
2437 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2438 assert (bytes_read <= uvalue && bytes_read > 0);
2439 uvalue -= bytes_read;
2440 data += bytes_read;
2441 break;
2442
2443 default:
2444 printf ("<corrupt>\n");
e38332c2 2445 warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
ec1b0fbb
NC
2446 discriminant);
2447 return;
2448 }
2449
2450 if (uvalue)
2451 printf (", ");
2452 }
2453
2454 if (is_signed)
2455 printf (")(signed)");
2456 else
2457 printf (")(unsigned)");
2458}
2459
19e6b90e 2460static unsigned char *
dda8d76d
NC
2461read_and_display_attr_value (unsigned long attribute,
2462 unsigned long form,
2463 dwarf_signed_vma implicit_const,
ec1b0fbb 2464 unsigned char * start,
dda8d76d
NC
2465 unsigned char * data,
2466 unsigned char * end,
2467 dwarf_vma cu_offset,
2468 dwarf_vma pointer_size,
2469 dwarf_vma offset_size,
2470 int dwarf_version,
2471 debug_info * debug_info_p,
2472 int do_loc,
2473 struct dwarf_section * section,
2474 struct cu_tu_set * this_set,
ec1b0fbb
NC
2475 char delimiter,
2476 int level)
19e6b90e 2477{
cd30bcef 2478 dwarf_signed_vma svalue;
ec1b0fbb
NC
2479 dwarf_vma uvalue = 0;
2480 unsigned char * block_start = NULL;
2481 unsigned char * orig_data = data;
19e6b90e 2482
f41e4712 2483 if (data > end || (data == end && form != DW_FORM_flag_present))
0c588247 2484 {
f41e4712 2485 warn (_("Corrupt attribute\n"));
0c588247
NC
2486 return data;
2487 }
2488
521d4b19
NC
2489 if (do_wide && ! do_loc)
2490 {
2491 /* PR 26847: Display the name of the form. */
2492 const char * name = get_FORM_name (form);
2493
2494 /* For convenience we skip the DW_FORM_ prefix to the name. */
2495 if (name[0] == 'D')
2496 name += 8; /* strlen ("DW_FORM_") */
2497 printf ("%c(%s)", delimiter, name);
2498 }
2499
19e6b90e
L
2500 switch (form)
2501 {
2502 default:
2503 break;
2504
2505 case DW_FORM_ref_addr:
2506 if (dwarf_version == 2)
0c588247 2507 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
ec47b32a 2508 else if (dwarf_version > 2)
0c588247 2509 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
19e6b90e 2510 else
ec47b32a 2511 error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
19e6b90e
L
2512 break;
2513
2514 case DW_FORM_addr:
0c588247 2515 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
19e6b90e
L
2516 break;
2517
e38332c2 2518 case DW_FORM_strp_sup:
19e6b90e 2519 case DW_FORM_strp:
77145576 2520 case DW_FORM_line_strp:
932fd279 2521 case DW_FORM_sec_offset:
a081f3cd
JJ
2522 case DW_FORM_GNU_ref_alt:
2523 case DW_FORM_GNU_strp_alt:
0c588247 2524 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
19e6b90e
L
2525 break;
2526
932fd279
JJ
2527 case DW_FORM_flag_present:
2528 uvalue = 1;
2529 break;
2530
19e6b90e
L
2531 case DW_FORM_ref1:
2532 case DW_FORM_flag:
2533 case DW_FORM_data1:
32e4f96c
TV
2534 case DW_FORM_strx1:
2535 case DW_FORM_addrx1:
0c588247 2536 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
19e6b90e
L
2537 break;
2538
2539 case DW_FORM_ref2:
2540 case DW_FORM_data2:
32e4f96c
TV
2541 case DW_FORM_strx2:
2542 case DW_FORM_addrx2:
0c588247 2543 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
19e6b90e
L
2544 break;
2545
32e4f96c
TV
2546 case DW_FORM_strx3:
2547 case DW_FORM_addrx3:
2548 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2549 break;
2550
e38332c2 2551 case DW_FORM_ref_sup4:
19e6b90e
L
2552 case DW_FORM_ref4:
2553 case DW_FORM_data4:
32e4f96c
TV
2554 case DW_FORM_strx4:
2555 case DW_FORM_addrx4:
0c588247 2556 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
19e6b90e
L
2557 break;
2558
2559 case DW_FORM_sdata:
cd30bcef
AM
2560 READ_SLEB (svalue, data, end);
2561 uvalue = svalue;
19e6b90e
L
2562 break;
2563
4723351a 2564 case DW_FORM_GNU_str_index:
32e4f96c 2565 case DW_FORM_strx:
19e6b90e
L
2566 case DW_FORM_ref_udata:
2567 case DW_FORM_udata:
cd30bcef 2568 case DW_FORM_GNU_addr_index:
32e4f96c 2569 case DW_FORM_addrx:
cd30bcef 2570 READ_ULEB (uvalue, data, end);
19e6b90e
L
2571 break;
2572
2573 case DW_FORM_indirect:
cd30bcef 2574 READ_ULEB (form, data, end);
19e6b90e 2575 if (!do_loc)
ef0b5f1c 2576 printf ("%c%s", delimiter, get_FORM_name (form));
77145576 2577 if (form == DW_FORM_implicit_const)
cd30bcef 2578 READ_SLEB (implicit_const, data, end);
ec1b0fbb
NC
2579 return read_and_display_attr_value (attribute, form, implicit_const,
2580 start, data, end,
2581 cu_offset, pointer_size,
19e6b90e 2582 offset_size, dwarf_version,
ec4d4525 2583 debug_info_p, do_loc,
ec1b0fbb 2584 section, this_set, delimiter, level);
19e6b90e
L
2585 }
2586
2587 switch (form)
2588 {
2589 case DW_FORM_ref_addr:
2590 if (!do_loc)
bcd213b2 2591 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
19e6b90e
L
2592 break;
2593
a081f3cd
JJ
2594 case DW_FORM_GNU_ref_alt:
2595 if (!do_loc)
521d4b19
NC
2596 {
2597 if (do_wide)
2598 /* We have already printed the form name. */
2599 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2600 else
2601 printf ("%c<alt 0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2602 }
dda8d76d 2603 /* FIXME: Follow the reference... */
a081f3cd
JJ
2604 break;
2605
19e6b90e
L
2606 case DW_FORM_ref1:
2607 case DW_FORM_ref2:
2608 case DW_FORM_ref4:
e38332c2 2609 case DW_FORM_ref_sup4:
19e6b90e
L
2610 case DW_FORM_ref_udata:
2611 if (!do_loc)
ef0b5f1c 2612 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
19e6b90e
L
2613 break;
2614
2615 case DW_FORM_data4:
2616 case DW_FORM_addr:
932fd279 2617 case DW_FORM_sec_offset:
19e6b90e 2618 if (!do_loc)
ef0b5f1c 2619 printf ("%c0x%s", delimiter, dwarf_vmatoa ("x", uvalue));
19e6b90e
L
2620 break;
2621
932fd279 2622 case DW_FORM_flag_present:
19e6b90e
L
2623 case DW_FORM_flag:
2624 case DW_FORM_data1:
2625 case DW_FORM_data2:
2626 case DW_FORM_sdata:
2627 case DW_FORM_udata:
2628 if (!do_loc)
ef0b5f1c 2629 printf ("%c%s", delimiter, dwarf_vmatoa ("d", uvalue));
19e6b90e
L
2630 break;
2631
77145576
JK
2632 case DW_FORM_implicit_const:
2633 if (!do_loc)
2634 printf ("%c%s", delimiter, dwarf_vmatoa ("d", implicit_const));
2635 break;
2636
e38332c2 2637 case DW_FORM_ref_sup8:
19e6b90e
L
2638 case DW_FORM_ref8:
2639 case DW_FORM_data8:
2bc8690c 2640 if (!do_loc)
19e6b90e 2641 {
74bc6052 2642 dwarf_vma high_bits;
a69c4772 2643 dwarf_vma utmp;
74bc6052
CC
2644 char buf[64];
2645
0c588247 2646 SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
a69c4772
NC
2647 utmp = uvalue;
2648 if (form == DW_FORM_ref8)
2649 add64 (& high_bits, & utmp, cu_offset);
ef0b5f1c 2650 printf ("%c0x%s", delimiter,
a69c4772 2651 dwarf_vmatoa64 (high_bits, utmp, buf, sizeof (buf)));
19e6b90e 2652 }
0c588247 2653
19e6b90e
L
2654 if ((do_loc || do_debug_loc || do_debug_ranges)
2655 && num_debug_info_entries == 0)
2656 {
2657 if (sizeof (uvalue) == 8)
0c588247 2658 SAFE_BYTE_GET (uvalue, data, 8, end);
19e6b90e 2659 else
467c65bc 2660 error (_("DW_FORM_data8 is unsupported when sizeof (dwarf_vma) != 8\n"));
19e6b90e 2661 }
0c588247 2662
19e6b90e
L
2663 data += 8;
2664 break;
2665
2f6cd591
JK
2666 case DW_FORM_data16:
2667 if (!do_loc)
2668 {
2669 dwarf_vma left_high_bits, left_low_bits;
2670 dwarf_vma right_high_bits, right_low_bits;
2671
2672 SAFE_BYTE_GET64 (data, &left_high_bits, &left_low_bits, end);
2673 SAFE_BYTE_GET64 (data + 8, &right_high_bits, &right_low_bits, end);
2674 if (byte_get == byte_get_little_endian)
2675 {
2676 /* Swap them. */
2677 left_high_bits ^= right_high_bits;
2678 right_high_bits ^= left_high_bits;
2679 left_high_bits ^= right_high_bits;
2680 left_low_bits ^= right_low_bits;
2681 right_low_bits ^= left_low_bits;
2682 left_low_bits ^= right_low_bits;
2683 }
2684 printf (" 0x%08" DWARF_VMA_FMT "x%08" DWARF_VMA_FMT "x"
2685 "%08" DWARF_VMA_FMT "x%08" DWARF_VMA_FMT "x",
2686 left_high_bits, left_low_bits, right_high_bits,
2687 right_low_bits);
2688 }
2689 data += 16;
2690 break;
2691
19e6b90e
L
2692 case DW_FORM_string:
2693 if (!do_loc)
ef0b5f1c 2694 printf ("%c%.*s", delimiter, (int) (end - data), data);
0c588247 2695 data += strnlen ((char *) data, end - data) + 1;
19e6b90e
L
2696 break;
2697
2698 case DW_FORM_block:
932fd279 2699 case DW_FORM_exprloc:
cd30bcef
AM
2700 READ_ULEB (uvalue, data, end);
2701 do_block:
2702 block_start = data;
a1165289
NC
2703 if (block_start >= end)
2704 {
2705 warn (_("Block ends prematurely\n"));
2706 uvalue = 0;
2707 block_start = end;
2708 }
afc72f15
NC
2709
2710 uvalue = check_uvalue (block_start, uvalue, end);
2711
19e6b90e
L
2712 if (do_loc)
2713 data = block_start + uvalue;
2714 else
ef0b5f1c 2715 data = display_block (block_start, uvalue, end, delimiter);
19e6b90e
L
2716 break;
2717
2718 case DW_FORM_block1:
cd30bcef
AM
2719 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2720 goto do_block;
19e6b90e
L
2721
2722 case DW_FORM_block2:
cd30bcef
AM
2723 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2724 goto do_block;
19e6b90e
L
2725
2726 case DW_FORM_block4:
cd30bcef
AM
2727 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2728 goto do_block;
19e6b90e
L
2729
2730 case DW_FORM_strp:
2731 if (!do_loc)
521d4b19
NC
2732 {
2733 if (do_wide)
2734 /* We have already displayed the form name. */
2735 printf (_("%c(offset: 0x%s): %s"), delimiter,
2736 dwarf_vmatoa ("x", uvalue),
2737 fetch_indirect_string (uvalue));
2738 else
2739 printf (_("%c(indirect string, offset: 0x%s): %s"), delimiter,
2740 dwarf_vmatoa ("x", uvalue),
2741 fetch_indirect_string (uvalue));
2742 }
19e6b90e
L
2743 break;
2744
77145576
JK
2745 case DW_FORM_line_strp:
2746 if (!do_loc)
521d4b19
NC
2747 {
2748 if (do_wide)
2749 /* We have already displayed the form name. */
2750 printf (_("%c(offset: 0x%s): %s"), delimiter,
2751 dwarf_vmatoa ("x", uvalue),
2752 fetch_indirect_line_string (uvalue));
2753 else
2754 printf (_("%c(indirect line string, offset: 0x%s): %s"), delimiter,
2755 dwarf_vmatoa ("x", uvalue),
2756 fetch_indirect_line_string (uvalue));
2757 }
77145576
JK
2758 break;
2759
4723351a 2760 case DW_FORM_GNU_str_index:
32e4f96c
TV
2761 case DW_FORM_strx:
2762 case DW_FORM_strx1:
2763 case DW_FORM_strx2:
2764 case DW_FORM_strx3:
2765 case DW_FORM_strx4:
4723351a 2766 if (!do_loc)
b4eb7656 2767 {
63b4cc53 2768 const char *suffix = strrchr (section->name, '.');
015dc7e1 2769 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
b4eb7656 2770
521d4b19
NC
2771 if (do_wide)
2772 /* We have already displayed the form name. */
2773 printf (_("%c(offset: 0x%s): %s"), delimiter,
2774 dwarf_vmatoa ("x", uvalue),
2775 fetch_indexed_string (uvalue, this_set, offset_size, dwo));
2776 else
2777 printf (_("%c(indexed string: 0x%s): %s"), delimiter,
2778 dwarf_vmatoa ("x", uvalue),
2779 fetch_indexed_string (uvalue, this_set, offset_size, dwo));
b4eb7656 2780 }
4723351a
CC
2781 break;
2782
a081f3cd
JJ
2783 case DW_FORM_GNU_strp_alt:
2784 if (!do_loc)
dda8d76d 2785 {
521d4b19
NC
2786 if (do_wide)
2787 /* We have already displayed the form name. */
2788 printf (_("%c(offset: 0x%s) %s"), delimiter,
2789 dwarf_vmatoa ("x", uvalue),
2790 fetch_alt_indirect_string (uvalue));
2791 else
2792 printf (_("%c(alt indirect string, offset: 0x%s) %s"), delimiter,
2793 dwarf_vmatoa ("x", uvalue),
2794 fetch_alt_indirect_string (uvalue));
dda8d76d 2795 }
a081f3cd
JJ
2796 break;
2797
19e6b90e
L
2798 case DW_FORM_indirect:
2799 /* Handled above. */
2800 break;
2801
2b6f5997
CC
2802 case DW_FORM_ref_sig8:
2803 if (!do_loc)
2804 {
74bc6052
CC
2805 dwarf_vma high_bits;
2806 char buf[64];
2807
0c588247 2808 SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
521d4b19
NC
2809 if (do_wide)
2810 /* We have already displayed the form name. */
2811 printf ("%c: 0x%s", delimiter,
2812 dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
2813 else
2814 printf ("%csignature: 0x%s", delimiter,
2815 dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
2b6f5997 2816 }
74bc6052 2817 data += 8;
2b6f5997
CC
2818 break;
2819
4723351a 2820 case DW_FORM_GNU_addr_index:
32e4f96c
TV
2821 case DW_FORM_addrx:
2822 case DW_FORM_addrx1:
2823 case DW_FORM_addrx2:
2824 case DW_FORM_addrx3:
2825 case DW_FORM_addrx4:
4723351a 2826 if (!do_loc)
521d4b19 2827 {
32e4f96c
TV
2828 dwarf_vma base;
2829 dwarf_vma offset;
2830
2831 if (debug_info_p == NULL)
2832 base = 0;
2833 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2834 base = 0;
2835 else
2836 base = debug_info_p->addr_base;
2837
2838 offset = base + uvalue * pointer_size;
2839
521d4b19
NC
2840 if (do_wide)
2841 /* We have already displayed the form name. */
2842 printf (_("%c(index: 0x%s): %s"), delimiter,
2843 dwarf_vmatoa ("x", uvalue),
32e4f96c 2844 fetch_indexed_value (offset, pointer_size));
521d4b19
NC
2845 else
2846 printf (_("%c(addr_index: 0x%s): %s"), delimiter,
2847 dwarf_vmatoa ("x", uvalue),
32e4f96c 2848 fetch_indexed_value (offset, pointer_size));
521d4b19 2849 }
4723351a
CC
2850 break;
2851
e38332c2
NC
2852 case DW_FORM_strp_sup:
2853 if (!do_loc)
2854 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
2855 break;
2856
19e6b90e 2857 default:
e38332c2 2858 warn (_("Unrecognized form: 0x%lx\n"), form);
19e6b90e
L
2859 break;
2860 }
2861
19e6b90e 2862 if ((do_loc || do_debug_loc || do_debug_ranges)
fd2f0033
TT
2863 && num_debug_info_entries == 0
2864 && debug_info_p != NULL)
19e6b90e
L
2865 {
2866 switch (attribute)
2867 {
2868 case DW_AT_frame_base:
2869 have_frame_base = 1;
1a0670f3 2870 /* Fall through. */
19e6b90e 2871 case DW_AT_location:
9f272209 2872 case DW_AT_GNU_locviews:
e2a0d921
NC
2873 case DW_AT_string_length:
2874 case DW_AT_return_addr:
19e6b90e
L
2875 case DW_AT_data_member_location:
2876 case DW_AT_vtable_elem_location:
e2a0d921
NC
2877 case DW_AT_segment:
2878 case DW_AT_static_link:
2879 case DW_AT_use_location:
bc0a77d2 2880 case DW_AT_call_value:
629e7ca8 2881 case DW_AT_GNU_call_site_value:
bc0a77d2 2882 case DW_AT_call_data_value:
629e7ca8 2883 case DW_AT_GNU_call_site_data_value:
bc0a77d2 2884 case DW_AT_call_target:
629e7ca8 2885 case DW_AT_GNU_call_site_target:
bc0a77d2 2886 case DW_AT_call_target_clobbered:
629e7ca8 2887 case DW_AT_GNU_call_site_target_clobbered:
b4eb7656 2888 if ((dwarf_version < 4
212b6063 2889 && (form == DW_FORM_data4 || form == DW_FORM_data8))
932fd279 2890 || form == DW_FORM_sec_offset)
19e6b90e
L
2891 {
2892 /* Process location list. */
91d6fa6a 2893 unsigned int lmax = debug_info_p->max_loc_offsets;
19e6b90e
L
2894 unsigned int num = debug_info_p->num_loc_offsets;
2895
91d6fa6a 2896 if (lmax == 0 || num >= lmax)
19e6b90e 2897 {
91d6fa6a 2898 lmax += 1024;
467c65bc 2899 debug_info_p->loc_offsets = (dwarf_vma *)
b4eb7656
AM
2900 xcrealloc (debug_info_p->loc_offsets,
2901 lmax, sizeof (*debug_info_p->loc_offsets));
9f272209
AO
2902 debug_info_p->loc_views = (dwarf_vma *)
2903 xcrealloc (debug_info_p->loc_views,
2904 lmax, sizeof (*debug_info_p->loc_views));
3f5e193b 2905 debug_info_p->have_frame_base = (int *)
b4eb7656
AM
2906 xcrealloc (debug_info_p->have_frame_base,
2907 lmax, sizeof (*debug_info_p->have_frame_base));
91d6fa6a 2908 debug_info_p->max_loc_offsets = lmax;
19e6b90e 2909 }
341f9135 2910 if (this_set != NULL)
b4eb7656 2911 uvalue += this_set->section_offsets [DW_SECT_LOC];
19e6b90e 2912 debug_info_p->have_frame_base [num] = have_frame_base;
9f272209
AO
2913 if (attribute != DW_AT_GNU_locviews)
2914 {
a7504f87
NC
2915 /* Corrupt DWARF info can produce more offsets than views.
2916 See PR 23062 for an example. */
2917 if (debug_info_p->num_loc_offsets
2918 > debug_info_p->num_loc_views)
2919 warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2920 else
2921 {
2922 debug_info_p->loc_offsets [num] = uvalue;
2923 debug_info_p->num_loc_offsets++;
2924 }
9f272209
AO
2925 }
2926 else
2927 {
2928 assert (debug_info_p->num_loc_views <= num);
2929 num = debug_info_p->num_loc_views;
a7504f87
NC
2930 if (num > debug_info_p->num_loc_offsets)
2931 warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
2932 else
2933 {
2934 debug_info_p->loc_views [num] = uvalue;
2935 debug_info_p->num_loc_views++;
2936 }
9f272209 2937 }
19e6b90e
L
2938 }
2939 break;
e2a0d921 2940
19e6b90e
L
2941 case DW_AT_low_pc:
2942 if (need_base_address)
2943 debug_info_p->base_address = uvalue;
2944 break;
2945
4723351a 2946 case DW_AT_GNU_addr_base:
32e4f96c 2947 case DW_AT_addr_base:
b4eb7656 2948 debug_info_p->addr_base = uvalue;
4723351a
CC
2949 break;
2950
2951 case DW_AT_GNU_ranges_base:
b4eb7656 2952 debug_info_p->ranges_base = uvalue;
4723351a
CC
2953 break;
2954
19e6b90e 2955 case DW_AT_ranges:
b4eb7656 2956 if ((dwarf_version < 4
212b6063 2957 && (form == DW_FORM_data4 || form == DW_FORM_data8))
932fd279 2958 || form == DW_FORM_sec_offset)
19e6b90e
L
2959 {
2960 /* Process range list. */
91d6fa6a 2961 unsigned int lmax = debug_info_p->max_range_lists;
19e6b90e
L
2962 unsigned int num = debug_info_p->num_range_lists;
2963
91d6fa6a 2964 if (lmax == 0 || num >= lmax)
19e6b90e 2965 {
91d6fa6a 2966 lmax += 1024;
467c65bc 2967 debug_info_p->range_lists = (dwarf_vma *)
b4eb7656
AM
2968 xcrealloc (debug_info_p->range_lists,
2969 lmax, sizeof (*debug_info_p->range_lists));
91d6fa6a 2970 debug_info_p->max_range_lists = lmax;
19e6b90e
L
2971 }
2972 debug_info_p->range_lists [num] = uvalue;
2973 debug_info_p->num_range_lists++;
2974 }
2975 break;
2976
d85bf2ba
NC
2977 case DW_AT_GNU_dwo_name:
2978 case DW_AT_dwo_name:
2979 if (need_dwo_info)
2980 switch (form)
2981 {
2982 case DW_FORM_strp:
50ea0877 2983 add_dwo_name ((const char *) fetch_indirect_string (uvalue), cu_offset);
d85bf2ba 2984 break;
5568cc9e 2985 case DW_FORM_GNU_strp_alt:
50ea0877 2986 add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue), cu_offset);
5568cc9e 2987 break;
d85bf2ba 2988 case DW_FORM_GNU_str_index:
32e4f96c
TV
2989 case DW_FORM_strx:
2990 case DW_FORM_strx1:
2991 case DW_FORM_strx2:
2992 case DW_FORM_strx3:
2993 case DW_FORM_strx4:
015dc7e1 2994 add_dwo_name (fetch_indexed_string (uvalue, this_set, offset_size, false), cu_offset);
d85bf2ba
NC
2995 break;
2996 case DW_FORM_string:
50ea0877 2997 add_dwo_name ((const char *) orig_data, cu_offset);
d85bf2ba
NC
2998 break;
2999 default:
3000 warn (_("Unsupported form (%s) for attribute %s\n"),
3001 get_FORM_name (form), get_AT_name (attribute));
d85bf2ba
NC
3002 break;
3003 }
3004 break;
521d4b19 3005
d85bf2ba
NC
3006 case DW_AT_comp_dir:
3007 /* FIXME: Also extract a build-id in a CU/TU. */
3008 if (need_dwo_info)
3009 switch (form)
3010 {
3011 case DW_FORM_strp:
50ea0877 3012 add_dwo_dir ((const char *) fetch_indirect_string (uvalue), cu_offset);
d85bf2ba 3013 break;
5568cc9e 3014 case DW_FORM_GNU_strp_alt:
50ea0877 3015 add_dwo_dir (fetch_alt_indirect_string (uvalue), cu_offset);
5568cc9e 3016 break;
d85bf2ba 3017 case DW_FORM_line_strp:
50ea0877 3018 add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue), cu_offset);
d85bf2ba
NC
3019 break;
3020 case DW_FORM_GNU_str_index:
32e4f96c
TV
3021 case DW_FORM_strx:
3022 case DW_FORM_strx1:
3023 case DW_FORM_strx2:
3024 case DW_FORM_strx3:
3025 case DW_FORM_strx4:
015dc7e1 3026 add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, false), cu_offset);
d85bf2ba
NC
3027 break;
3028 case DW_FORM_string:
50ea0877 3029 add_dwo_dir ((const char *) orig_data, cu_offset);
d85bf2ba
NC
3030 break;
3031 default:
3032 warn (_("Unsupported form (%s) for attribute %s\n"),
3033 get_FORM_name (form), get_AT_name (attribute));
d85bf2ba
NC
3034 break;
3035 }
3036 break;
521d4b19 3037
d85bf2ba
NC
3038 case DW_AT_GNU_dwo_id:
3039 if (need_dwo_info)
3040 switch (form)
3041 {
3042 case DW_FORM_data8:
24841daa 3043 /* FIXME: Record the length of the ID as well ? */
50ea0877 3044 add_dwo_id ((const char *) (data - 8), cu_offset);
d85bf2ba
NC
3045 break;
3046 default:
3047 warn (_("Unsupported form (%s) for attribute %s\n"),
3048 get_FORM_name (form), get_AT_name (attribute));
d85bf2ba
NC
3049 break;
3050 }
3051 break;
521d4b19 3052
19e6b90e
L
3053 default:
3054 break;
3055 }
3056 }
3057
4ccf1e31 3058 if (do_loc || attribute == 0)
19e6b90e
L
3059 return data;
3060
ec4d4525 3061 /* For some attributes we can display further information. */
19e6b90e
L
3062 switch (attribute)
3063 {
ec1b0fbb 3064 case DW_AT_type:
b3fe587e
AM
3065 if (level >= 0 && level < MAX_CU_NESTING
3066 && uvalue < (size_t) (end - start))
ec1b0fbb 3067 {
015dc7e1
AM
3068 bool is_signed = false;
3069 abbrev_entry *type_abbrev;
3070 unsigned char *type_data;
3071 unsigned long type_cu_offset;
521d4b19 3072
bcd213b2
NC
3073 type_abbrev = get_type_abbrev_from_form (form, uvalue, cu_offset,
3074 section, NULL, & type_data, & type_cu_offset);
3075 if (type_abbrev != NULL)
3076 {
3077 get_type_signedness (type_abbrev, section, type_data, end, type_cu_offset,
3078 pointer_size, offset_size, dwarf_version,
3079 & is_signed, 0);
3080 }
ec1b0fbb
NC
3081 level_type_signed[level] = is_signed;
3082 }
3083 break;
521d4b19 3084
19e6b90e 3085 case DW_AT_inline:
2e9e81a8 3086 printf ("\t");
19e6b90e
L
3087 switch (uvalue)
3088 {
3089 case DW_INL_not_inlined:
3090 printf (_("(not inlined)"));
3091 break;
3092 case DW_INL_inlined:
3093 printf (_("(inlined)"));
3094 break;
3095 case DW_INL_declared_not_inlined:
3096 printf (_("(declared as inline but ignored)"));
3097 break;
3098 case DW_INL_declared_inlined:
3099 printf (_("(declared as inline and inlined)"));
3100 break;
3101 default:
47704ddf
KT
3102 printf (_(" (Unknown inline attribute value: %s)"),
3103 dwarf_vmatoa ("x", uvalue));
19e6b90e
L
3104 break;
3105 }
3106 break;
3107
3108 case DW_AT_language:
2e9e81a8 3109 printf ("\t");
19e6b90e
L
3110 switch (uvalue)
3111 {
4b78141a 3112 /* Ordered by the numeric value of these constants. */
19e6b90e 3113 case DW_LANG_C89: printf ("(ANSI C)"); break;
4b78141a
NC
3114 case DW_LANG_C: printf ("(non-ANSI C)"); break;
3115 case DW_LANG_Ada83: printf ("(Ada)"); break;
19e6b90e 3116 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
4b78141a
NC
3117 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
3118 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
19e6b90e
L
3119 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
3120 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
19e6b90e 3121 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
4b78141a 3122 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
19e6b90e 3123 /* DWARF 2.1 values. */
4b78141a 3124 case DW_LANG_Java: printf ("(Java)"); break;
19e6b90e
L
3125 case DW_LANG_C99: printf ("(ANSI C99)"); break;
3126 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
3127 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
4b78141a
NC
3128 /* DWARF 3 values. */
3129 case DW_LANG_PLI: printf ("(PLI)"); break;
3130 case DW_LANG_ObjC: printf ("(Objective C)"); break;
3131 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
3132 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
3133 case DW_LANG_D: printf ("(D)"); break;
2b6f5997
CC
3134 /* DWARF 4 values. */
3135 case DW_LANG_Python: printf ("(Python)"); break;
3362e0d9 3136 /* DWARF 5 values. */
2008a0db 3137 case DW_LANG_OpenCL: printf ("(OpenCL)"); break;
3362e0d9 3138 case DW_LANG_Go: printf ("(Go)"); break;
2008a0db
TT
3139 case DW_LANG_Modula3: printf ("(Modula 3)"); break;
3140 case DW_LANG_Haskell: printf ("(Haskell)"); break;
3141 case DW_LANG_C_plus_plus_03: printf ("(C++03)"); break;
8bc10620 3142 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
2008a0db
TT
3143 case DW_LANG_OCaml: printf ("(OCaml)"); break;
3144 case DW_LANG_Rust: printf ("(Rust)"); break;
6ddfe5b4 3145 case DW_LANG_C11: printf ("(C11)"); break;
2008a0db
TT
3146 case DW_LANG_Swift: printf ("(Swift)"); break;
3147 case DW_LANG_Julia: printf ("(Julia)"); break;
3148 case DW_LANG_Dylan: printf ("(Dylan)"); break;
8bc10620 3149 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
5a195044
MW
3150 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
3151 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
2008a0db 3152 case DW_LANG_RenderScript: printf ("(RenderScript)"); break;
19e6b90e
L
3153 /* MIPS extension. */
3154 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
3155 /* UPC extension. */
3156 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
3157 default:
4b78141a 3158 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
9cf03b7e 3159 printf (_("(implementation defined: %s)"),
467c65bc 3160 dwarf_vmatoa ("x", uvalue));
4b78141a 3161 else
9cf03b7e 3162 printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
19e6b90e
L
3163 break;
3164 }
3165 break;
3166
3167 case DW_AT_encoding:
2e9e81a8 3168 printf ("\t");
19e6b90e
L
3169 switch (uvalue)
3170 {
3171 case DW_ATE_void: printf ("(void)"); break;
3172 case DW_ATE_address: printf ("(machine address)"); break;
3173 case DW_ATE_boolean: printf ("(boolean)"); break;
3174 case DW_ATE_complex_float: printf ("(complex float)"); break;
3175 case DW_ATE_float: printf ("(float)"); break;
3176 case DW_ATE_signed: printf ("(signed)"); break;
3177 case DW_ATE_signed_char: printf ("(signed char)"); break;
3178 case DW_ATE_unsigned: printf ("(unsigned)"); break;
3179 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
e2a0d921 3180 /* DWARF 2.1 values: */
19e6b90e
L
3181 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
3182 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
e2a0d921
NC
3183 /* DWARF 3 values: */
3184 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
3185 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
3186 case DW_ATE_edited: printf ("(edited)"); break;
3187 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
3188 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
04914e37
NC
3189 /* DWARF 4 values: */
3190 case DW_ATE_UTF: printf ("(unicode string)"); break;
3191 /* DWARF 5 values: */
3192 case DW_ATE_UCS: printf ("(UCS)"); break;
3193 case DW_ATE_ASCII: printf ("(ASCII)"); break;
3194
e2a0d921
NC
3195 /* HP extensions: */
3196 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
3197 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
3198 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
3199 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
3200 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
3201 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
3202 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
3203
19e6b90e
L
3204 default:
3205 if (uvalue >= DW_ATE_lo_user
3206 && uvalue <= DW_ATE_hi_user)
9cf03b7e 3207 printf (_("(user defined type)"));
19e6b90e 3208 else
9cf03b7e 3209 printf (_("(unknown type)"));
19e6b90e
L
3210 break;
3211 }
3212 break;
3213
3214 case DW_AT_accessibility:
2e9e81a8 3215 printf ("\t");
19e6b90e
L
3216 switch (uvalue)
3217 {
3218 case DW_ACCESS_public: printf ("(public)"); break;
3219 case DW_ACCESS_protected: printf ("(protected)"); break;
3220 case DW_ACCESS_private: printf ("(private)"); break;
3221 default:
9cf03b7e 3222 printf (_("(unknown accessibility)"));
19e6b90e
L
3223 break;
3224 }
3225 break;
3226
3227 case DW_AT_visibility:
2e9e81a8 3228 printf ("\t");
19e6b90e
L
3229 switch (uvalue)
3230 {
3231 case DW_VIS_local: printf ("(local)"); break;
3232 case DW_VIS_exported: printf ("(exported)"); break;
3233 case DW_VIS_qualified: printf ("(qualified)"); break;
9cf03b7e 3234 default: printf (_("(unknown visibility)")); break;
19e6b90e
L
3235 }
3236 break;
3237
04914e37
NC
3238 case DW_AT_endianity:
3239 printf ("\t");
3240 switch (uvalue)
3241 {
3242 case DW_END_default: printf ("(default)"); break;
3243 case DW_END_big: printf ("(big)"); break;
3244 case DW_END_little: printf ("(little)"); break;
3245 default:
3246 if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
3247 printf (_("(user specified)"));
3248 else
3249 printf (_("(unknown endianity)"));
3250 break;
3251 }
3252 break;
3253
19e6b90e 3254 case DW_AT_virtuality:
2e9e81a8 3255 printf ("\t");
19e6b90e
L
3256 switch (uvalue)
3257 {
3258 case DW_VIRTUALITY_none: printf ("(none)"); break;
3259 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
3260 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
9cf03b7e 3261 default: printf (_("(unknown virtuality)")); break;
19e6b90e
L
3262 }
3263 break;
3264
3265 case DW_AT_identifier_case:
2e9e81a8 3266 printf ("\t");
19e6b90e
L
3267 switch (uvalue)
3268 {
3269 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
3270 case DW_ID_up_case: printf ("(up_case)"); break;
3271 case DW_ID_down_case: printf ("(down_case)"); break;
3272 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
9cf03b7e 3273 default: printf (_("(unknown case)")); break;
19e6b90e
L
3274 }
3275 break;
3276
3277 case DW_AT_calling_convention:
2e9e81a8 3278 printf ("\t");
19e6b90e
L
3279 switch (uvalue)
3280 {
3281 case DW_CC_normal: printf ("(normal)"); break;
3282 case DW_CC_program: printf ("(program)"); break;
3283 case DW_CC_nocall: printf ("(nocall)"); break;
04914e37
NC
3284 case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
3285 case DW_CC_pass_by_value: printf ("(pass by value)"); break;
3286 case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
3287 case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
19e6b90e
L
3288 default:
3289 if (uvalue >= DW_CC_lo_user
3290 && uvalue <= DW_CC_hi_user)
9cf03b7e 3291 printf (_("(user defined)"));
19e6b90e 3292 else
9cf03b7e 3293 printf (_("(unknown convention)"));
19e6b90e
L
3294 }
3295 break;
3296
3297 case DW_AT_ordering:
2e9e81a8 3298 printf ("\t");
19e6b90e
L
3299 switch (uvalue)
3300 {
04914e37 3301 case 255:
9cf03b7e 3302 case -1: printf (_("(undefined)")); break;
19e6b90e
L
3303 case 0: printf ("(row major)"); break;
3304 case 1: printf ("(column major)"); break;
3305 }
3306 break;
3307
04914e37
NC
3308 case DW_AT_decimal_sign:
3309 printf ("\t");
3310 switch (uvalue)
3311 {
3312 case DW_DS_unsigned: printf (_("(unsigned)")); break;
3313 case DW_DS_leading_overpunch: printf (_("(leading overpunch)")); break;
3314 case DW_DS_trailing_overpunch: printf (_("(trailing overpunch)")); break;
3315 case DW_DS_leading_separate: printf (_("(leading separate)")); break;
3316 case DW_DS_trailing_separate: printf (_("(trailing separate)")); break;
3317 default: printf (_("(unrecognised)")); break;
3318 }
3319 break;
3320
3321 case DW_AT_defaulted:
3322 printf ("\t");
3323 switch (uvalue)
3324 {
3325 case DW_DEFAULTED_no: printf (_("(no)")); break;
3326 case DW_DEFAULTED_in_class: printf (_("(in class)")); break;
3327 case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
3328 default: printf (_("(unrecognised)")); break;
3329 }
3330 break;
3331
3332 case DW_AT_discr_list:
3333 printf ("\t");
ec1b0fbb 3334 display_discr_list (form, uvalue, data, end, level);
04914e37 3335 break;
521d4b19 3336
19e6b90e
L
3337 case DW_AT_frame_base:
3338 have_frame_base = 1;
1a0670f3 3339 /* Fall through. */
19e6b90e 3340 case DW_AT_location:
e2a0d921
NC
3341 case DW_AT_string_length:
3342 case DW_AT_return_addr:
19e6b90e
L
3343 case DW_AT_data_member_location:
3344 case DW_AT_vtable_elem_location:
e2a0d921
NC
3345 case DW_AT_segment:
3346 case DW_AT_static_link:
3347 case DW_AT_use_location:
bc0a77d2 3348 case DW_AT_call_value:
629e7ca8 3349 case DW_AT_GNU_call_site_value:
bc0a77d2 3350 case DW_AT_call_data_value:
629e7ca8 3351 case DW_AT_GNU_call_site_data_value:
bc0a77d2 3352 case DW_AT_call_target:
629e7ca8 3353 case DW_AT_GNU_call_site_target:
bc0a77d2 3354 case DW_AT_call_target_clobbered:
629e7ca8 3355 case DW_AT_GNU_call_site_target_clobbered:
212b6063 3356 if ((dwarf_version < 4
b4eb7656 3357 && (form == DW_FORM_data4 || form == DW_FORM_data8))
932fd279 3358 || form == DW_FORM_sec_offset)
2e9e81a8 3359 printf (_(" (location list)"));
e2a0d921 3360 /* Fall through. */
19e6b90e
L
3361 case DW_AT_allocated:
3362 case DW_AT_associated:
3363 case DW_AT_data_location:
3364 case DW_AT_stride:
3365 case DW_AT_upper_bound:
cecf136e 3366 case DW_AT_lower_bound:
19e6b90e
L
3367 if (block_start)
3368 {
3369 int need_frame_base;
3370
2e9e81a8 3371 printf ("\t(");
19e6b90e
L
3372 need_frame_base = decode_location_expression (block_start,
3373 pointer_size,
b7807392
JJ
3374 offset_size,
3375 dwarf_version,
19e6b90e 3376 uvalue,
f1c4cc75 3377 cu_offset, section);
19e6b90e
L
3378 printf (")");
3379 if (need_frame_base && !have_frame_base)
3380 printf (_(" [without DW_AT_frame_base]"));
3381 }
19e6b90e
L
3382 break;
3383
c54207d3
NC
3384 case DW_AT_data_bit_offset:
3385 case DW_AT_byte_size:
3386 case DW_AT_bit_size:
3387 case DW_AT_string_length_byte_size:
3388 case DW_AT_string_length_bit_size:
3389 case DW_AT_bit_stride:
3390 if (form == DW_FORM_exprloc)
3391 {
3392 printf ("\t(");
3393 (void) decode_location_expression (block_start, pointer_size,
3394 offset_size, dwarf_version,
3395 uvalue, cu_offset, section);
3396 printf (")");
3397 }
3398 break;
3399
ec4d4525
NC
3400 case DW_AT_import:
3401 {
bcd213b2
NC
3402 unsigned long abbrev_number;
3403 abbrev_entry *entry;
2b6f5997 3404
bcd213b2
NC
3405 entry = get_type_abbrev_from_form (form, uvalue, cu_offset,
3406 section, & abbrev_number, NULL, NULL);
3407 if (entry == NULL)
3408 {
3409 if (form != DW_FORM_GNU_ref_alt)
3410 warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset 0x%lx is too big.\n"),
3411 dwarf_vmatoa ("x", uvalue),
3412 (unsigned long) (orig_data - section->start));
3413 }
6e3d6dc1
NC
3414 else
3415 {
2e9e81a8 3416 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
bcd213b2 3417 printf (" (%s)", get_TAG_name (entry->tag));
6e3d6dc1
NC
3418 printf ("]");
3419 }
ec4d4525
NC
3420 }
3421 break;
3422
19e6b90e
L
3423 default:
3424 break;
3425 }
3426
3427 return data;
3428}
3429
19e6b90e 3430static unsigned char *
dda8d76d
NC
3431read_and_display_attr (unsigned long attribute,
3432 unsigned long form,
3433 dwarf_signed_vma implicit_const,
ec1b0fbb 3434 unsigned char * start,
dda8d76d
NC
3435 unsigned char * data,
3436 unsigned char * end,
3437 dwarf_vma cu_offset,
3438 dwarf_vma pointer_size,
3439 dwarf_vma offset_size,
3440 int dwarf_version,
3441 debug_info * debug_info_p,
3442 int do_loc,
3443 struct dwarf_section * section,
ec1b0fbb
NC
3444 struct cu_tu_set * this_set,
3445 int level)
19e6b90e
L
3446{
3447 if (!do_loc)
750f03b7 3448 printf (" %-18s:", get_AT_name (attribute));
ec1b0fbb
NC
3449 data = read_and_display_attr_value (attribute, form, implicit_const,
3450 start, data, end,
f6f0e17b 3451 cu_offset, pointer_size, offset_size,
19e6b90e 3452 dwarf_version, debug_info_p,
ec1b0fbb 3453 do_loc, section, this_set, ' ', level);
19e6b90e
L
3454 if (!do_loc)
3455 printf ("\n");
3456 return data;
3457}
3458
dda8d76d 3459/* Like load_debug_section, but if the ordinary call fails, and we are
24841daa
NC
3460 following debug links, then attempt to load the requested section
3461 from one of the separate debug info files. */
dda8d76d 3462
015dc7e1 3463static bool
dda8d76d 3464load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
24841daa 3465 void * handle)
dda8d76d 3466{
24841daa 3467 if (load_debug_section (sec_enum, handle))
dda8d76d 3468 {
24841daa
NC
3469 if (debug_displays[sec_enum].section.filename == NULL)
3470 {
3471 /* See if we can associate a filename with this section. */
3472 separate_info * i;
3473
3474 for (i = first_separate_info; i != NULL; i = i->next)
3475 if (i->handle == handle)
3476 {
3477 debug_displays[sec_enum].section.filename = i->filename;
3478 break;
3479 }
3480 }
3481
015dc7e1 3482 return true;
dda8d76d
NC
3483 }
3484
24841daa
NC
3485 if (do_follow_links)
3486 {
3487 separate_info * i;
3488
3489 for (i = first_separate_info; i != NULL; i = i->next)
3490 {
3491 if (load_debug_section (sec_enum, i->handle))
3492 {
3493 debug_displays[sec_enum].section.filename = i->filename;
3494
3495 /* FIXME: We should check to see if any of the remaining debug info
3496 files also contain this section, and, umm, do something about it. */
015dc7e1 3497 return true;
24841daa
NC
3498 }
3499 }
3500 }
dda8d76d 3501
015dc7e1 3502 return false;
dda8d76d
NC
3503}
3504
3505static void
015dc7e1 3506introduce (struct dwarf_section * section, bool raw)
dda8d76d
NC
3507{
3508 if (raw)
3509 {
3510 if (do_follow_links && section->filename)
3511 printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3512 section->name, section->filename);
3513 else
3514 printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3515 }
3516 else
3517 {
3518 if (do_follow_links && section->filename)
3519 printf (_("Contents of the %s section (loaded from %s):\n\n"),
3520 section->name, section->filename);
3521 else
3522 printf (_("Contents of the %s section:\n\n"), section->name);
3523 }
3524}
521d4b19 3525
d85bf2ba
NC
3526/* Process the contents of a .debug_info section.
3527 If do_loc is TRUE then we are scanning for location lists and dwo tags
3528 and we do not want to display anything to the user.
3529 If do_types is TRUE, we are processing a .debug_types section instead of
3530 a .debug_info section.
3531 The information displayed is restricted by the values in DWARF_START_DIE
3532 and DWARF_CUTOFF_LEVEL.
3533 Returns TRUE upon success. Otherwise an error or warning message is
3534 printed and FALSE is returned. */
19e6b90e 3535
015dc7e1
AM
3536static bool
3537process_debug_info (struct dwarf_section * section,
3538 void *file,
3539 enum dwarf_section_display_enum abbrev_sec,
3540 bool do_loc,
3541 bool do_types)
19e6b90e
L
3542{
3543 unsigned char *start = section->start;
3544 unsigned char *end = start + section->size;
3545 unsigned char *section_begin;
3546 unsigned int unit;
3547 unsigned int num_units = 0;
3548
3549 if ((do_loc || do_debug_loc || do_debug_ranges)
2b6f5997
CC
3550 && num_debug_info_entries == 0
3551 && ! do_types)
19e6b90e 3552 {
767221a9 3553 dwarf_vma length;
19e6b90e
L
3554
3555 /* First scan the section to get the number of comp units. */
3556 for (section_begin = start, num_units = 0; section_begin < end;
3557 num_units ++)
3558 {
3559 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3560 will be the length. For a 64-bit DWARF section, it'll be
3561 the escape code 0xffffffff followed by an 8 byte length. */
0c588247 3562 SAFE_BYTE_GET (length, section_begin, 4, end);
19e6b90e
L
3563
3564 if (length == 0xffffffff)
3565 {
0c588247 3566 SAFE_BYTE_GET (length, section_begin + 4, 8, end);
19e6b90e
L
3567 section_begin += length + 12;
3568 }
ec4d4525
NC
3569 else if (length >= 0xfffffff0 && length < 0xffffffff)
3570 {
767221a9
NC
3571 warn (_("Reserved length value (0x%s) found in section %s\n"),
3572 dwarf_vmatoa ("x", length), section->name);
015dc7e1 3573 return false;
ec4d4525 3574 }
19e6b90e
L
3575 else
3576 section_begin += length + 4;
aca88567
NC
3577
3578 /* Negative values are illegal, they may even cause infinite
3579 looping. This can happen if we can't accurately apply
f3853b34
NC
3580 relocations to an object file, or if the file is corrupt. */
3581 if ((signed long) length <= 0 || section_begin < start)
aca88567 3582 {
767221a9
NC
3583 warn (_("Corrupt unit length (0x%s) found in section %s\n"),
3584 dwarf_vmatoa ("x", length), section->name);
015dc7e1 3585 return false;
aca88567 3586 }
19e6b90e
L
3587 }
3588
3589 if (num_units == 0)
3590 {
f41e4712 3591 error (_("No comp units in %s section ?\n"), section->name);
015dc7e1 3592 return false;
19e6b90e
L
3593 }
3594
3595 /* Then allocate an array to hold the information. */
3f5e193b 3596 debug_information = (debug_info *) cmalloc (num_units,
1306a742 3597 sizeof (* debug_information));
19e6b90e
L
3598 if (debug_information == NULL)
3599 {
f41e4712 3600 error (_("Not enough memory for a debug info array of %u entries\n"),
19e6b90e 3601 num_units);
82b1b41b 3602 alloc_num_debug_info_entries = num_debug_info_entries = 0;
015dc7e1 3603 return false;
19e6b90e 3604 }
d85bf2ba 3605
03a91817
NC
3606 /* PR 17531: file: 92ca3797.
3607 We cannot rely upon the debug_information array being initialised
3608 before it is used. A corrupt file could easily contain references
3609 to a unit for which information has not been made available. So
3610 we ensure that the array is zeroed here. */
b4eb7656
AM
3611 memset (debug_information, 0, num_units * sizeof (*debug_information));
3612
82b1b41b 3613 alloc_num_debug_info_entries = num_units;
19e6b90e
L
3614 }
3615
3616 if (!do_loc)
3617 {
dda8d76d
NC
3618 load_debug_section_with_follow (str, file);
3619 load_debug_section_with_follow (line_str, file);
3620 load_debug_section_with_follow (str_dwo, file);
3621 load_debug_section_with_follow (str_index, file);
3622 load_debug_section_with_follow (str_index_dwo, file);
3623 load_debug_section_with_follow (debug_addr, file);
19e6b90e 3624 }
521d4b19 3625
dda8d76d 3626 load_debug_section_with_follow (abbrev_sec, file);
6f875884 3627 if (debug_displays [abbrev_sec].section.start == NULL)
19e6b90e
L
3628 {
3629 warn (_("Unable to locate %s section!\n"),
dda8d76d 3630 debug_displays [abbrev_sec].section.uncompressed_name);
015dc7e1 3631 return false;
19e6b90e
L
3632 }
3633
dda8d76d 3634 if (!do_loc && dwarf_start_die == 0)
015dc7e1 3635 introduce (section, false);
bcd213b2
NC
3636
3637 free_all_abbrevs ();
3638 free (cu_abbrev_map);
3639 cu_abbrev_map = NULL;
3640 next_free_abbrev_map_entry = 0;
521d4b19 3641
bcd213b2
NC
3642 /* In order to be able to resolve DW_FORM_ref_attr forms we need
3643 to load *all* of the abbrevs for all CUs in this .debug_info
3644 section. This does effectively mean that we (partially) read
3645 every CU header twice. */
3646 for (section_begin = start; start < end;)
3647 {
3648 DWARF2_Internal_CompUnit compunit;
3649 unsigned char * hdrptr;
76868f36
L
3650 dwarf_vma abbrev_base;
3651 size_t abbrev_size;
bcd213b2
NC
3652 dwarf_vma cu_offset;
3653 unsigned int offset_size;
3654 unsigned int initial_length_size;
3655 struct cu_tu_set * this_set;
3656 abbrev_list * list;
3657
3658 hdrptr = start;
3659
3660 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3661
3662 if (compunit.cu_length == 0xffffffff)
3663 {
3664 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3665 offset_size = 8;
3666 initial_length_size = 12;
3667 }
3668 else
3669 {
3670 offset_size = 4;
3671 initial_length_size = 4;
3672 }
3673
3674 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end);
3675
3676 cu_offset = start - section_begin;
3677
3678 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3679
3680 if (compunit.cu_version < 5)
3681 {
3682 compunit.cu_unit_type = DW_UT_compile;
3683 /* Initialize it due to a false compiler warning. */
3684 compunit.cu_pointer_size = -1;
3685 }
3686 else
3687 {
3688 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end);
3689 do_types = (compunit.cu_unit_type == DW_UT_type);
3690
3691 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
3692 }
3693
3694 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end);
3695
9b87f84a
TV
3696 if (compunit.cu_unit_type == DW_UT_split_compile
3697 || compunit.cu_unit_type == DW_UT_skeleton)
3698 {
3699 uint64_t dwo_id;
3700 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end);
3701 }
3702
76868f36
L
3703 if (this_set == NULL)
3704 {
3705 abbrev_base = 0;
3706 abbrev_size = debug_displays [abbrev_sec].section.size;
3707 }
3708 else
3709 {
3710 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3711 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3712 }
3713
3714 list = find_abbrev_list_by_abbrev_offset (abbrev_base,
3715 compunit.cu_abbrev_offset);
bcd213b2
NC
3716 if (list == NULL)
3717 {
bcd213b2
NC
3718 unsigned char * next;
3719
76868f36
L
3720 list = new_abbrev_list (abbrev_base,
3721 compunit.cu_abbrev_offset);
f2f9554b
AM
3722 next = process_abbrev_set (&debug_displays[abbrev_sec].section,
3723 abbrev_base, abbrev_size,
3724 compunit.cu_abbrev_offset, list);
bcd213b2
NC
3725 list->start_of_next_abbrevs = next;
3726 }
521d4b19 3727
bcd213b2
NC
3728 start = section_begin + cu_offset + compunit.cu_length
3729 + initial_length_size;
3730 record_abbrev_list_for_cu (cu_offset, start - section_begin, list);
3731 }
3732
3733 for (start = section_begin, unit = 0; start < end; unit++)
19e6b90e
L
3734 {
3735 DWARF2_Internal_CompUnit compunit;
3736 unsigned char *hdrptr;
19e6b90e 3737 unsigned char *tags;
fd2f0033 3738 int level, last_level, saved_level;
467c65bc 3739 dwarf_vma cu_offset;
e98fdf1a 3740 unsigned long sec_off;
bf5117e3 3741 unsigned int offset_size;
19485196 3742 unsigned int initial_length_size;
74bc6052
CC
3743 dwarf_vma signature_high = 0;
3744 dwarf_vma signature_low = 0;
767221a9 3745 dwarf_vma type_offset = 0;
341f9135
CC
3746 struct cu_tu_set *this_set;
3747 dwarf_vma abbrev_base;
3748 size_t abbrev_size;
bcd213b2 3749 abbrev_list * list = NULL;
19e6b90e
L
3750
3751 hdrptr = start;
3752
0c588247 3753 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
19e6b90e
L
3754
3755 if (compunit.cu_length == 0xffffffff)
3756 {
0c588247 3757 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
19e6b90e
L
3758 offset_size = 8;
3759 initial_length_size = 12;
3760 }
3761 else
3762 {
3763 offset_size = 4;
3764 initial_length_size = 4;
3765 }
3766
0c588247 3767 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end);
19e6b90e
L
3768
3769 cu_offset = start - section_begin;
19e6b90e 3770
341f9135
CC
3771 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3772
77145576
JK
3773 if (compunit.cu_version < 5)
3774 {
3775 compunit.cu_unit_type = DW_UT_compile;
3776 /* Initialize it due to a false compiler warning. */
3777 compunit.cu_pointer_size = -1;
3778 }
3779 else
3780 {
3781 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end);
3782 do_types = (compunit.cu_unit_type == DW_UT_type);
3783
3784 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
3785 }
3786
0c588247 3787 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end);
19e6b90e 3788
341f9135
CC
3789 if (this_set == NULL)
3790 {
3791 abbrev_base = 0;
3792 abbrev_size = debug_displays [abbrev_sec].section.size;
3793 }
3794 else
3795 {
3796 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3797 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3798 }
3799
77145576
JK
3800 if (compunit.cu_version < 5)
3801 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
3802
015dc7e1 3803 bool do_dwo_id = false;
e6ca1878 3804 uint64_t dwo_id = 0;
9b87f84a
TV
3805 if (compunit.cu_unit_type == DW_UT_split_compile
3806 || compunit.cu_unit_type == DW_UT_skeleton)
3807 {
9b87f84a 3808 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end);
015dc7e1 3809 do_dwo_id = true;
9b87f84a
TV
3810 }
3811
f41e4712
NC
3812 /* PR 17512: file: 001-108546-0.001:0.1. */
3813 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3814 {
3815 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3816 compunit.cu_pointer_size, offset_size);
3817 compunit.cu_pointer_size = offset_size;
3818 }
2b6f5997
CC
3819
3820 if (do_types)
b4eb7656 3821 {
0c588247 3822 SAFE_BYTE_GET64 (hdrptr, &signature_high, &signature_low, end);
f048b142 3823 hdrptr += 8;
0c588247 3824 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end);
b4eb7656 3825 }
2b6f5997 3826
ae7e7825
NC
3827 if (dwarf_start_die > (cu_offset + compunit.cu_length
3828 + initial_length_size))
3829 {
3830 start = section_begin + cu_offset + compunit.cu_length
3831 + initial_length_size;
3832 continue;
3833 }
3834
19e6b90e 3835 if ((do_loc || do_debug_loc || do_debug_ranges)
2b6f5997 3836 && num_debug_info_entries == 0
c4b2f181 3837 && alloc_num_debug_info_entries > unit
2b6f5997 3838 && ! do_types)
19e6b90e
L
3839 {
3840 debug_information [unit].cu_offset = cu_offset;
3841 debug_information [unit].pointer_size
3842 = compunit.cu_pointer_size;
b7807392
JJ
3843 debug_information [unit].offset_size = offset_size;
3844 debug_information [unit].dwarf_version = compunit.cu_version;
19e6b90e 3845 debug_information [unit].base_address = 0;
4723351a
CC
3846 debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
3847 debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
19e6b90e
L
3848 debug_information [unit].loc_offsets = NULL;
3849 debug_information [unit].have_frame_base = NULL;
3850 debug_information [unit].max_loc_offsets = 0;
3851 debug_information [unit].num_loc_offsets = 0;
3852 debug_information [unit].range_lists = NULL;
3853 debug_information [unit].max_range_lists= 0;
3854 debug_information [unit].num_range_lists = 0;
3855 }
3856
fd2f0033 3857 if (!do_loc && dwarf_start_die == 0)
19e6b90e 3858 {
47704ddf
KT
3859 printf (_(" Compilation Unit @ offset 0x%s:\n"),
3860 dwarf_vmatoa ("x", cu_offset));
3861 printf (_(" Length: 0x%s (%s)\n"),
3862 dwarf_vmatoa ("x", compunit.cu_length),
49e7b350 3863 offset_size == 8 ? "64-bit" : "32-bit");
19e6b90e 3864 printf (_(" Version: %d\n"), compunit.cu_version);
debd1a62 3865 if (compunit.cu_version >= 5)
4896932e
MF
3866 {
3867 const char *name = get_DW_UT_name (compunit.cu_unit_type);
3868
3869 printf (_(" Unit Type: %s (%x)\n"),
3870 name ? name : "???",
3871 compunit.cu_unit_type);
3872 }
7282333f
AM
3873 printf (_(" Abbrev Offset: 0x%s\n"),
3874 dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
19e6b90e 3875 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
2b6f5997
CC
3876 if (do_types)
3877 {
74bc6052
CC
3878 char buf[64];
3879
3880 printf (_(" Signature: 0x%s\n"),
3881 dwarf_vmatoa64 (signature_high, signature_low,
3882 buf, sizeof (buf)));
3883 printf (_(" Type Offset: 0x%s\n"),
3884 dwarf_vmatoa ("x", type_offset));
2b6f5997 3885 }
d60f7998
TV
3886 if (do_dwo_id)
3887 printf (_(" DWO ID: 0x%s\n"), dwarf_vmatoa ("x", dwo_id));
341f9135
CC
3888 if (this_set != NULL)
3889 {
3890 dwarf_vma *offsets = this_set->section_offsets;
3891 size_t *sizes = this_set->section_sizes;
3892
3893 printf (_(" Section contributions:\n"));
3894 printf (_(" .debug_abbrev.dwo: 0x%s 0x%s\n"),
3895 dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
3896 dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
3897 printf (_(" .debug_line.dwo: 0x%s 0x%s\n"),
3898 dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
3899 dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
3900 printf (_(" .debug_loc.dwo: 0x%s 0x%s\n"),
3901 dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
3902 dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
3903 printf (_(" .debug_str_offsets.dwo: 0x%s 0x%s\n"),
3904 dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
3905 dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
3906 }
19e6b90e
L
3907 }
3908
e98fdf1a
AM
3909 sec_off = cu_offset + initial_length_size;
3910 if (sec_off + compunit.cu_length < sec_off
3911 || sec_off + compunit.cu_length > section->size)
460c89ff 3912 {
e98fdf1a
AM
3913 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
3914 section->name,
3915 (unsigned long) cu_offset,
19485196
NC
3916 dwarf_vmatoa ("x", compunit.cu_length));
3917 num_units = unit;
3918 break;
3919 }
3920
460c89ff
NS
3921 tags = hdrptr;
3922 start += compunit.cu_length + initial_length_size;
3923
77145576 3924 if (compunit.cu_version < 2 || compunit.cu_version > 5)
19e6b90e 3925 {
47704ddf
KT
3926 warn (_("CU at offset %s contains corrupt or "
3927 "unsupported version number: %d.\n"),
3928 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
19e6b90e
L
3929 continue;
3930 }
3931
77145576 3932 if (compunit.cu_unit_type != DW_UT_compile
ec47b32a 3933 && compunit.cu_unit_type != DW_UT_partial
9b87f84a
TV
3934 && compunit.cu_unit_type != DW_UT_type
3935 && compunit.cu_unit_type != DW_UT_split_compile
3936 && compunit.cu_unit_type != DW_UT_skeleton)
77145576
JK
3937 {
3938 warn (_("CU at offset %s contains corrupt or "
3939 "unsupported unit type: %d.\n"),
3940 dwarf_vmatoa ("x", cu_offset), compunit.cu_unit_type);
3941 continue;
3942 }
3943
d493b283 3944 /* Process the abbrevs used by this compilation unit. */
f2f9554b
AM
3945 list = find_abbrev_list_by_abbrev_offset (abbrev_base,
3946 compunit.cu_abbrev_offset);
3947 if (list == NULL)
bcd213b2 3948 {
f2f9554b
AM
3949 unsigned char *next;
3950
3951 list = new_abbrev_list (abbrev_base,
3952 compunit.cu_abbrev_offset);
3953 next = process_abbrev_set (&debug_displays[abbrev_sec].section,
3954 abbrev_base, abbrev_size,
3955 compunit.cu_abbrev_offset, list);
3956 list->start_of_next_abbrevs = next;
bcd213b2 3957 }
19e6b90e
L
3958
3959 level = 0;
fd2f0033
TT
3960 last_level = level;
3961 saved_level = -1;
19e6b90e
L
3962 while (tags < start)
3963 {
19e6b90e 3964 unsigned long abbrev_number;
ec4d4525 3965 unsigned long die_offset;
19e6b90e
L
3966 abbrev_entry *entry;
3967 abbrev_attr *attr;
fd2f0033 3968 int do_printing = 1;
19e6b90e 3969
ec4d4525
NC
3970 die_offset = tags - section_begin;
3971
cd30bcef 3972 READ_ULEB (abbrev_number, tags, start);
19e6b90e 3973
eb7cc021
JK
3974 /* A null DIE marks the end of a list of siblings or it may also be
3975 a section padding. */
19e6b90e
L
3976 if (abbrev_number == 0)
3977 {
eb7cc021
JK
3978 /* Check if it can be a section padding for the last CU. */
3979 if (level == 0 && start == end)
3980 {
3981 unsigned char *chk;
3982
3983 for (chk = tags; chk < start; chk++)
3984 if (*chk != 0)
3985 break;
3986 if (chk == start)
3987 break;
3988 }
3989
4337774f
TT
3990 if (!do_loc && die_offset >= dwarf_start_die
3991 && (dwarf_cutoff_level == -1
3992 || level < dwarf_cutoff_level))
399c99f7
L
3993 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
3994 level, die_offset);
3995
19e6b90e 3996 --level;
ec4d4525
NC
3997 if (level < 0)
3998 {
3999 static unsigned num_bogus_warns = 0;
4000
4001 if (num_bogus_warns < 3)
4002 {
4723351a
CC
4003 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
4004 die_offset, section->name);
ec4d4525
NC
4005 num_bogus_warns ++;
4006 if (num_bogus_warns == 3)
4007 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
4008 }
4009 }
fd2f0033 4010 if (dwarf_start_die != 0 && level < saved_level)
015dc7e1 4011 return true;
19e6b90e
L
4012 continue;
4013 }
4014
4b78141a 4015 if (!do_loc)
fd2f0033
TT
4016 {
4017 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
4018 do_printing = 0;
4019 else
4020 {
4021 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
4022 saved_level = level;
4023 do_printing = (dwarf_cutoff_level == -1
4024 || level < dwarf_cutoff_level);
4025 if (do_printing)
4026 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
4027 level, die_offset, abbrev_number);
4028 else if (dwarf_cutoff_level == -1
4029 || last_level < dwarf_cutoff_level)
4030 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
4031 last_level = level;
4032 }
4033 }
cecf136e 4034
19e6b90e
L
4035 /* Scan through the abbreviation list until we reach the
4036 correct entry. */
bcd213b2 4037 if (list == NULL)
19e6b90e
L
4038 continue;
4039
bcd213b2
NC
4040 for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
4041 if (entry->number == abbrev_number)
4042 break;
4043
19e6b90e
L
4044 if (entry == NULL)
4045 {
fd2f0033 4046 if (!do_loc && do_printing)
4b78141a
NC
4047 {
4048 printf ("\n");
4049 fflush (stdout);
4050 }
f3853b34 4051 warn (_("DIE at offset 0x%lx refers to abbreviation number %lu which does not exist\n"),
cc86f28f 4052 die_offset, abbrev_number);
015dc7e1 4053 return false;
19e6b90e
L
4054 }
4055
fd2f0033 4056 if (!do_loc && do_printing)
cc5914eb 4057 printf (" (%s)\n", get_TAG_name (entry->tag));
cecf136e 4058
19e6b90e
L
4059 switch (entry->tag)
4060 {
4061 default:
4062 need_base_address = 0;
4063 break;
4064 case DW_TAG_compile_unit:
d85bf2ba
NC
4065 need_base_address = 1;
4066 need_dwo_info = do_loc;
19e6b90e
L
4067 break;
4068 case DW_TAG_entry_point:
19e6b90e
L
4069 case DW_TAG_subprogram:
4070 need_base_address = 0;
4071 /* Assuming that there is no DW_AT_frame_base. */
4072 have_frame_base = 0;
4073 break;
4074 }
4075
9f272209
AO
4076 debug_info *debug_info_p =
4077 (debug_information && unit < alloc_num_debug_info_entries)
4078 ? debug_information + unit : NULL;
4079
4080 assert (!debug_info_p
4081 || (debug_info_p->num_loc_offsets
4082 == debug_info_p->num_loc_views));
4083
399c99f7
L
4084 for (attr = entry->first_attr;
4085 attr && attr->attribute;
4086 attr = attr->next)
4b78141a 4087 {
fd2f0033 4088 if (! do_loc && do_printing)
4b78141a 4089 /* Show the offset from where the tag was extracted. */
fd2f0033 4090 printf (" <%lx>", (unsigned long)(tags - section_begin));
4b78141a
NC
4091 tags = read_and_display_attr (attr->attribute,
4092 attr->form,
77145576 4093 attr->implicit_const,
ec1b0fbb 4094 section_begin,
341f9135 4095 tags,
f6f0e17b 4096 end,
341f9135 4097 cu_offset,
4b78141a
NC
4098 compunit.cu_pointer_size,
4099 offset_size,
4100 compunit.cu_version,
9f272209 4101 debug_info_p,
341f9135
CC
4102 do_loc || ! do_printing,
4103 section,
ec1b0fbb
NC
4104 this_set,
4105 level);
4b78141a 4106 }
cecf136e 4107
9f272209
AO
4108 /* If a locview attribute appears before a location one,
4109 make sure we don't associate it with an earlier
4110 loclist. */
4111 if (debug_info_p)
4112 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
4113 {
4114 case 1:
4115 debug_info_p->loc_views [debug_info_p->num_loc_views] = vm1;
4116 debug_info_p->num_loc_views++;
4117 assert (debug_info_p->num_loc_views
4118 == debug_info_p->num_loc_offsets);
4119 break;
4120
4121 case 0:
4122 break;
4123
4124 case -1:
4125 warn(_("DIE has locviews without loclist\n"));
4126 debug_info_p->num_loc_views--;
4127 break;
4128
4129 default:
4130 assert (0);
4131 }
4132
b4eb7656
AM
4133 if (entry->children)
4134 ++level;
4135 }
19e6b90e 4136 }
cecf136e 4137
19e6b90e
L
4138 /* Set num_debug_info_entries here so that it can be used to check if
4139 we need to process .debug_loc and .debug_ranges sections. */
4140 if ((do_loc || do_debug_loc || do_debug_ranges)
2b6f5997
CC
4141 && num_debug_info_entries == 0
4142 && ! do_types)
82b1b41b 4143 {
b4eb7656 4144 if (num_units > alloc_num_debug_info_entries)
82b1b41b
NC
4145 num_debug_info_entries = alloc_num_debug_info_entries;
4146 else
4147 num_debug_info_entries = num_units;
4148 }
cecf136e 4149
19e6b90e 4150 if (!do_loc)
467c65bc 4151 printf ("\n");
cecf136e 4152
015dc7e1 4153 return true;
19e6b90e
L
4154}
4155
4156/* Locate and scan the .debug_info section in the file and record the pointer
4157 sizes and offsets for the compilation units in it. Usually an executable
4158 will have just one pointer size, but this is not guaranteed, and so we try
4159 not to make any assumptions. Returns zero upon failure, or the number of
4160 compilation units upon success. */
4161
4162static unsigned int
4163load_debug_info (void * file)
4164{
1febe64d 4165 /* If we have already tried and failed to load the .debug_info
657d0d47 4166 section then do not bother to repeat the task. */
cc86f28f 4167 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
1febe64d
NC
4168 return 0;
4169
19e6b90e
L
4170 /* If we already have the information there is nothing else to do. */
4171 if (num_debug_info_entries > 0)
4172 return num_debug_info_entries;
4173
341f9135 4174 /* If this is a DWARF package file, load the CU and TU indexes. */
43a444f9 4175 (void) load_cu_tu_indexes (file);
341f9135 4176
dda8d76d 4177 if (load_debug_section_with_follow (info, file)
015dc7e1 4178 && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
19e6b90e 4179 return num_debug_info_entries;
82b1b41b 4180
dda8d76d 4181 if (load_debug_section_with_follow (info_dwo, file)
82b1b41b 4182 && process_debug_info (&debug_displays [info_dwo].section, file,
015dc7e1 4183 abbrev_dwo, true, false))
4723351a 4184 return num_debug_info_entries;
1febe64d 4185
cc86f28f 4186 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
1febe64d 4187 return 0;
19e6b90e
L
4188}
4189
b40bf0a2
NC
4190/* Read a DWARF .debug_line section header starting at DATA.
4191 Upon success returns an updated DATA pointer and the LINFO
4192 structure and the END_OF_SEQUENCE pointer will be filled in.
4193 Otherwise returns NULL. */
19e6b90e 4194
b40bf0a2
NC
4195static unsigned char *
4196read_debug_line_header (struct dwarf_section * section,
4197 unsigned char * data,
4198 unsigned char * end,
4199 DWARF2_Internal_LineInfo * linfo,
4200 unsigned char ** end_of_sequence)
4201{
4202 unsigned char *hdrptr;
b40bf0a2 4203 unsigned int initial_length_size;
19e6b90e 4204
b40bf0a2
NC
4205 /* Extract information from the Line Number Program Header.
4206 (section 6.2.4 in the Dwarf3 doc). */
6937bb54 4207 hdrptr = data;
19e6b90e 4208
b40bf0a2
NC
4209 /* Get and check the length of the block. */
4210 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
19e6b90e 4211
b40bf0a2 4212 if (linfo->li_length == 0xffffffff)
f41e4712
NC
4213 {
4214 /* This section is 64-bit DWARF 3. */
b40bf0a2 4215 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
77145576 4216 linfo->li_offset_size = 8;
f41e4712
NC
4217 initial_length_size = 12;
4218 }
4219 else
4220 {
77145576 4221 linfo->li_offset_size = 4;
f41e4712
NC
4222 initial_length_size = 4;
4223 }
19e6b90e 4224
b40bf0a2 4225 if (linfo->li_length + initial_length_size > section->size)
f41e4712 4226 {
8fcc61b4
NC
4227 /* If the length field has a relocation against it, then we should
4228 not complain if it is inaccurate (and probably negative). This
4229 happens in object files when the .debug_line section is actually
4230 comprised of several different .debug_line.* sections, (some of
4231 which may be removed by linker garbage collection), and a relocation
4232 is used to compute the correct length once that is done. */
77145576 4233 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
b40bf0a2 4234 {
8fcc61b4 4235 linfo->li_length = (end - data) - initial_length_size;
b40bf0a2
NC
4236 }
4237 else
4238 {
8fcc61b4
NC
4239 warn (_("The length field (0x%lx) in the debug_line header is wrong - the section is too small\n"),
4240 (long) linfo->li_length);
b40bf0a2
NC
4241 return NULL;
4242 }
f41e4712 4243 }
19e6b90e 4244
b40bf0a2
NC
4245 /* Get and check the version number. */
4246 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4247
4248 if (linfo->li_version != 2
4249 && linfo->li_version != 3
77145576
JK
4250 && linfo->li_version != 4
4251 && linfo->li_version != 5)
f41e4712 4252 {
77145576
JK
4253 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4254 "is currently supported.\n"));
b40bf0a2 4255 return NULL;
f41e4712 4256 }
19e6b90e 4257
77145576
JK
4258 if (linfo->li_version >= 5)
4259 {
5496f3c6 4260 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
77145576 4261
5496f3c6
NC
4262 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4263 if (linfo->li_segment_size != 0)
77145576
JK
4264 {
4265 warn (_("The %s section contains "
4266 "unsupported segment selector size: %d.\n"),
5496f3c6
NC
4267 section->name, linfo->li_segment_size);
4268 return NULL;
77145576
JK
4269 }
4270 }
4271
4272 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4273 linfo->li_offset_size, end);
b40bf0a2 4274 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
0c588247 4275
b40bf0a2 4276 if (linfo->li_version >= 4)
f41e4712 4277 {
b40bf0a2 4278 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
0c588247 4279
b40bf0a2 4280 if (linfo->li_max_ops_per_insn == 0)
f41e4712
NC
4281 {
4282 warn (_("Invalid maximum operations per insn.\n"));
b40bf0a2 4283 return NULL;
a233b20c 4284 }
f41e4712
NC
4285 }
4286 else
b40bf0a2 4287 linfo->li_max_ops_per_insn = 1;
0c588247 4288
b40bf0a2 4289 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
65879393 4290 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
b40bf0a2
NC
4291 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4292 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
19e6b90e 4293
b40bf0a2 4294 * end_of_sequence = data + linfo->li_length + initial_length_size;
b4eb7656 4295 /* PR 17512: file:002-117414-0.004. */
6937bb54
NC
4296 if (* end_of_sequence > end)
4297 {
4c219c2e
AM
4298 warn (_("Line length %s extends beyond end of section\n"),
4299 dwarf_vmatoa ("u", linfo->li_length));
6937bb54
NC
4300 * end_of_sequence = end;
4301 return NULL;
4302 }
4303
b40bf0a2
NC
4304 return hdrptr;
4305}
19e6b90e 4306
77145576 4307static unsigned char *
015dc7e1
AM
4308display_formatted_table (unsigned char *data,
4309 unsigned char *start,
4310 unsigned char *end,
4311 const DWARF2_Internal_LineInfo *linfo,
4312 struct dwarf_section *section,
4313 bool is_dir)
77145576
JK
4314{
4315 unsigned char *format_start, format_count, *format, formati;
4316 dwarf_vma data_count, datai;
cd30bcef 4317 unsigned int namepass, last_entry = 0;
5496f3c6 4318 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
521d4b19 4319
77145576 4320 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
546cb2d8
NC
4321 if (do_checks && format_count > 5)
4322 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4323 table_name, format_count);
4324
77145576
JK
4325 format_start = data;
4326 for (formati = 0; formati < format_count; formati++)
4327 {
cd30bcef
AM
4328 SKIP_ULEB (data, end);
4329 SKIP_ULEB (data, end);
55b26492 4330 if (data >= end)
77145576 4331 {
5496f3c6 4332 warn (_("%s: Corrupt format description entry\n"), table_name);
77145576
JK
4333 return data;
4334 }
4335 }
4336
cd30bcef 4337 READ_ULEB (data_count, data, end);
546cb2d8 4338 if (data_count == 0)
77145576 4339 {
546cb2d8 4340 printf (_("\n The %s is empty.\n"), table_name);
77145576
JK
4341 return data;
4342 }
55b26492 4343 else if (data >= end)
77145576 4344 {
546cb2d8
NC
4345 warn (_("%s: Corrupt entry count - expected %s but none found\n"),
4346 table_name, dwarf_vmatoa ("x", data_count));
77145576
JK
4347 return data;
4348 }
546cb2d8 4349
5496f3c6
NC
4350 else if (format_count == 0)
4351 {
4352 warn (_("%s: format count is zero, but the table is not empty\n"),
4353 table_name);
4354 return end;
4355 }
77145576 4356
5496f3c6
NC
4357 printf (_("\n The %s (offset 0x%lx, lines %s, columns %u):\n"),
4358 table_name, (long) (data - start), dwarf_vmatoa ("u", data_count),
4359 format_count);
77145576
JK
4360
4361 printf (_(" Entry"));
521d4b19 4362 /* Delay displaying name as the last entry for better screen layout. */
77145576
JK
4363 for (namepass = 0; namepass < 2; namepass++)
4364 {
4365 format = format_start;
4366 for (formati = 0; formati < format_count; formati++)
4367 {
4368 dwarf_vma content_type;
4369
cd30bcef 4370 READ_ULEB (content_type, format, end);
77145576
JK
4371 if ((content_type == DW_LNCT_path) == (namepass == 1))
4372 switch (content_type)
4373 {
4374 case DW_LNCT_path:
4375 printf (_("\tName"));
4376 break;
4377 case DW_LNCT_directory_index:
4378 printf (_("\tDir"));
4379 break;
4380 case DW_LNCT_timestamp:
4381 printf (_("\tTime"));
4382 break;
4383 case DW_LNCT_size:
4384 printf (_("\tSize"));
4385 break;
4386 case DW_LNCT_MD5:
5496f3c6 4387 printf (_("\tMD5\t\t\t"));
77145576
JK
4388 break;
4389 default:
4390 printf (_("\t(Unknown format content type %s)"),
4391 dwarf_vmatoa ("u", content_type));
4392 }
cd30bcef 4393 SKIP_ULEB (format, end);
77145576
JK
4394 }
4395 }
4396 putchar ('\n');
4397
4398 for (datai = 0; datai < data_count; datai++)
4399 {
4400 unsigned char *datapass = data;
4401
4402 printf (" %d", last_entry++);
521d4b19 4403 /* Delay displaying name as the last entry for better screen layout. */
77145576
JK
4404 for (namepass = 0; namepass < 2; namepass++)
4405 {
4406 format = format_start;
4407 data = datapass;
4408 for (formati = 0; formati < format_count; formati++)
4409 {
4410 dwarf_vma content_type, form;
4411
cd30bcef
AM
4412 READ_ULEB (content_type, format, end);
4413 READ_ULEB (form, format, end);
4414 data = read_and_display_attr_value (0, form, 0, start, data, end,
4415 0, 0, linfo->li_offset_size,
77145576
JK
4416 linfo->li_version, NULL,
4417 ((content_type == DW_LNCT_path) != (namepass == 1)),
ec1b0fbb 4418 section, NULL, '\t', -1);
77145576
JK
4419 }
4420 }
5496f3c6 4421
55b26492 4422 if (data >= end && (datai < data_count - 1))
77145576 4423 {
5496f3c6 4424 warn (_("\n%s: Corrupt entries list\n"), table_name);
77145576
JK
4425 return data;
4426 }
4427 putchar ('\n');
4428 }
4429 return data;
4430}
4431
e38332c2
NC
4432static int
4433display_debug_sup (struct dwarf_section * section,
4434 void * file ATTRIBUTE_UNUSED)
4435{
4436 unsigned char * start = section->start;
4437 unsigned char * end = section->start + section->size;
4438 unsigned int version;
4439 char is_supplementary;
4440 const unsigned char * sup_filename;
4441 size_t sup_filename_len;
4442 unsigned int num_read;
4443 int status;
4444 dwarf_vma checksum_len;
4445
4446
015dc7e1 4447 introduce (section, true);
e38332c2
NC
4448 if (section->size < 4)
4449 {
4450 error (_("corrupt .debug_sup section: size is too small\n"));
4451 return 0;
4452 }
4453
4454 /* Read the data. */
4455 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4456 if (version < 5)
4457 warn (_("corrupt .debug_sup section: version < 5"));
4458
4459 SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4460 if (is_supplementary != 0 && is_supplementary != 1)
4461 warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4462
4463 sup_filename = start;
4464 if (is_supplementary && sup_filename[0] != 0)
4465 warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4466
4467 sup_filename_len = strnlen ((const char *) start, end - start);
4468 if (sup_filename_len == (size_t) (end - start))
4469 {
4470 error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4471 return 0;
4472 }
4473 start += sup_filename_len + 1;
4474
015dc7e1 4475 checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
e38332c2
NC
4476 if (status)
4477 {
4478 error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4479 checksum_len = 0;
4480 }
4481 start += num_read;
4482 if (checksum_len > (dwarf_vma) (end - start))
4483 {
4484 error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4485 checksum_len = end - start;
4486 }
4487 else if (checksum_len < (dwarf_vma) (end - start))
4488 {
4489 warn (_("corrupt .debug_sup section: there are 0x%lx extra, unused bytes at the end of the section\n"),
4490 (long) ((end - start) - checksum_len));
4491 }
4492
4493 printf (_(" Version: %u\n"), version);
4494 printf (_(" Is Supp: %u\n"), is_supplementary);
4495 printf (_(" Filename: %s\n"), sup_filename);
4496 printf (_(" Checksum Len: %lu\n"), (long) checksum_len);
4497 if (checksum_len > 0)
4498 {
4499 printf (_(" Checksum: "));
4500 while (checksum_len--)
4501 printf ("0x%x ", * start++ );
4502 printf ("\n");
4503 }
4504 return 1;
4505}
4506
b40bf0a2 4507static int
dda8d76d
NC
4508display_debug_lines_raw (struct dwarf_section * section,
4509 unsigned char * data,
4510 unsigned char * end,
4511 void * file)
b40bf0a2
NC
4512{
4513 unsigned char *start = section->start;
ba8826a8 4514 int verbose_view = 0;
19e6b90e 4515
015dc7e1 4516 introduce (section, true);
19e6b90e 4517
b40bf0a2
NC
4518 while (data < end)
4519 {
4520 static DWARF2_Internal_LineInfo saved_linfo;
4521 DWARF2_Internal_LineInfo linfo;
4522 unsigned char *standard_opcodes;
4523 unsigned char *end_of_sequence;
fe59e83d 4524 int i;
19e6b90e 4525
24d127aa 4526 if (startswith (section->name, ".debug_line.")
4925cdd7
NC
4527 /* Note: the following does not apply to .debug_line.dwo sections.
4528 These are full debug_line sections. */
4529 && strcmp (section->name, ".debug_line.dwo") != 0)
19e6b90e 4530 {
b40bf0a2
NC
4531 /* Sections named .debug_line.<foo> are fragments of a .debug_line
4532 section containing just the Line Number Statements. They are
4533 created by the assembler and intended to be used alongside gcc's
4534 -ffunction-sections command line option. When the linker's
4535 garbage collection decides to discard a .text.<foo> section it
4536 can then also discard the line number information in .debug_line.<foo>.
4537
4925cdd7 4538 Since the section is a fragment it does not have the details
b40bf0a2 4539 needed to fill out a LineInfo structure, so instead we use the
4925cdd7 4540 details from the last full debug_line section that we processed. */
b40bf0a2
NC
4541 end_of_sequence = end;
4542 standard_opcodes = NULL;
4543 linfo = saved_linfo;
058037d3
NC
4544 /* PR 17531: file: 0522b371. */
4545 if (linfo.li_line_range == 0)
4546 {
1306a742 4547 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
058037d3
NC
4548 return 0;
4549 }
b40bf0a2 4550 reset_state_machine (linfo.li_default_is_stmt);
19e6b90e 4551 }
19e6b90e
L
4552 else
4553 {
b40bf0a2 4554 unsigned char * hdrptr;
19e6b90e 4555
b40bf0a2
NC
4556 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4557 & end_of_sequence)) == NULL)
4558 return 0;
19e6b90e 4559
b40bf0a2
NC
4560 printf (_(" Offset: 0x%lx\n"), (long)(data - start));
4561 printf (_(" Length: %ld\n"), (long) linfo.li_length);
4562 printf (_(" DWARF Version: %d\n"), linfo.li_version);
5496f3c6
NC
4563 if (linfo.li_version >= 5)
4564 {
4565 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
4566 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
4567 }
77ef8654 4568 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
b40bf0a2
NC
4569 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
4570 if (linfo.li_version >= 4)
4571 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4572 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
4573 printf (_(" Line Base: %d\n"), linfo.li_line_base);
4574 printf (_(" Line Range: %d\n"), linfo.li_line_range);
4575 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
19e6b90e 4576
0a9d414a
NC
4577 /* PR 17512: file: 1665-6428-0.004. */
4578 if (linfo.li_line_range == 0)
4579 {
4580 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4581 linfo.li_line_range = 1;
4582 }
77ef8654 4583
b40bf0a2 4584 reset_state_machine (linfo.li_default_is_stmt);
19e6b90e 4585
b40bf0a2
NC
4586 /* Display the contents of the Opcodes table. */
4587 standard_opcodes = hdrptr;
19e6b90e 4588
6937bb54
NC
4589 /* PR 17512: file: 002-417945-0.004. */
4590 if (standard_opcodes + linfo.li_opcode_base >= end)
4591 {
4592 warn (_("Line Base extends beyond end of section\n"));
4593 return 0;
4594 }
4595
b40bf0a2 4596 printf (_("\n Opcodes:\n"));
19e6b90e 4597
b40bf0a2 4598 for (i = 1; i < linfo.li_opcode_base; i++)
d3a49aa8
AM
4599 printf (ngettext (" Opcode %d has %d arg\n",
4600 " Opcode %d has %d args\n",
4601 standard_opcodes[i - 1]),
4602 i, standard_opcodes[i - 1]);
19e6b90e 4603
b40bf0a2
NC
4604 /* Display the contents of the Directory table. */
4605 data = standard_opcodes + linfo.li_opcode_base - 1;
19e6b90e 4606
77145576 4607 if (linfo.li_version >= 5)
b40bf0a2 4608 {
dda8d76d 4609 load_debug_section_with_follow (line_str, file);
19e6b90e 4610
77145576 4611 data = display_formatted_table (data, start, end, &linfo, section,
015dc7e1 4612 true);
77145576 4613 data = display_formatted_table (data, start, end, &linfo, section,
015dc7e1 4614 false);
77145576
JK
4615 }
4616 else
4617 {
4618 if (*data == 0)
4619 printf (_("\n The Directory Table is empty.\n"));
4620 else
a233b20c 4621 {
77145576 4622 unsigned int last_dir_entry = 0;
6937bb54 4623
77145576
JK
4624 printf (_("\n The Directory Table (offset 0x%lx):\n"),
4625 (long)(data - start));
19e6b90e 4626
77145576
JK
4627 while (data < end && *data != 0)
4628 {
4629 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
19e6b90e 4630
77145576
JK
4631 data += strnlen ((char *) data, end - data) + 1;
4632 }
19e6b90e 4633
77145576
JK
4634 /* PR 17512: file: 002-132094-0.004. */
4635 if (data >= end - 1)
4636 break;
4637 }
19e6b90e 4638
77145576
JK
4639 /* Skip the NUL at the end of the table. */
4640 data++;
19e6b90e 4641
77145576
JK
4642 /* Display the contents of the File Name table. */
4643 if (*data == 0)
4644 printf (_("\n The File Name Table is empty.\n"));
4645 else
4646 {
4647 printf (_("\n The File Name Table (offset 0x%lx):\n"),
4648 (long)(data - start));
4649 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
19e6b90e 4650
77145576 4651 while (data < end && *data != 0)
b40bf0a2 4652 {
77145576 4653 unsigned char *name;
cd30bcef 4654 dwarf_vma val;
77145576
JK
4655
4656 printf (" %d\t", ++state_machine_regs.last_file_entry);
4657 name = data;
4658 data += strnlen ((char *) data, end - data) + 1;
4659
cd30bcef
AM
4660 READ_ULEB (val, data, end);
4661 printf ("%s\t", dwarf_vmatoa ("u", val));
4662 READ_ULEB (val, data, end);
4663 printf ("%s\t", dwarf_vmatoa ("u", val));
4664 READ_ULEB (val, data, end);
4665 printf ("%s\t", dwarf_vmatoa ("u", val));
77145576
JK
4666 printf ("%.*s\n", (int)(end - name), name);
4667
55b26492 4668 if (data >= end)
77145576
JK
4669 {
4670 warn (_("Corrupt file name table entry\n"));
4671 break;
4672 }
b40bf0a2 4673 }
a233b20c 4674 }
77145576
JK
4675
4676 /* Skip the NUL at the end of the table. */
4677 data++;
b40bf0a2 4678 }
19e6b90e 4679
b40bf0a2
NC
4680 putchar ('\n');
4681 saved_linfo = linfo;
4682 }
19e6b90e 4683
b40bf0a2
NC
4684 /* Now display the statements. */
4685 if (data >= end_of_sequence)
4686 printf (_(" No Line Number Statements.\n"));
4687 else
4688 {
4689 printf (_(" Line Number Statements:\n"));
19e6b90e 4690
b40bf0a2
NC
4691 while (data < end_of_sequence)
4692 {
4693 unsigned char op_code;
4694 dwarf_signed_vma adv;
4695 dwarf_vma uladv;
19e6b90e 4696
fe59e83d
CC
4697 printf (" [0x%08lx]", (long)(data - start));
4698
b40bf0a2 4699 op_code = *data++;
19e6b90e 4700
b40bf0a2 4701 if (op_code >= linfo.li_opcode_base)
19e6b90e 4702 {
b40bf0a2
NC
4703 op_code -= linfo.li_opcode_base;
4704 uladv = (op_code / linfo.li_line_range);
4705 if (linfo.li_max_ops_per_insn == 1)
4706 {
4707 uladv *= linfo.li_min_insn_length;
4708 state_machine_regs.address += uladv;
ba8826a8
AO
4709 if (uladv)
4710 state_machine_regs.view = 0;
b40bf0a2 4711 printf (_(" Special opcode %d: "
ba8826a8 4712 "advance Address by %s to 0x%s%s"),
b40bf0a2 4713 op_code, dwarf_vmatoa ("u", uladv),
ba8826a8
AO
4714 dwarf_vmatoa ("x", state_machine_regs.address),
4715 verbose_view && uladv
4716 ? _(" (reset view)") : "");
b40bf0a2
NC
4717 }
4718 else
4719 {
ba8826a8
AO
4720 unsigned addrdelta
4721 = ((state_machine_regs.op_index + uladv)
b40bf0a2
NC
4722 / linfo.li_max_ops_per_insn)
4723 * linfo.li_min_insn_length;
ba8826a8
AO
4724
4725 state_machine_regs.address += addrdelta;
b40bf0a2
NC
4726 state_machine_regs.op_index
4727 = (state_machine_regs.op_index + uladv)
4728 % linfo.li_max_ops_per_insn;
ba8826a8
AO
4729 if (addrdelta)
4730 state_machine_regs.view = 0;
b40bf0a2 4731 printf (_(" Special opcode %d: "
ba8826a8 4732 "advance Address by %s to 0x%s[%d]%s"),
b40bf0a2
NC
4733 op_code, dwarf_vmatoa ("u", uladv),
4734 dwarf_vmatoa ("x", state_machine_regs.address),
ba8826a8
AO
4735 state_machine_regs.op_index,
4736 verbose_view && addrdelta
4737 ? _(" (reset view)") : "");
b40bf0a2
NC
4738 }
4739 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4740 state_machine_regs.line += adv;
ba8826a8 4741 printf (_(" and Line by %s to %d"),
b40bf0a2 4742 dwarf_vmatoa ("d", adv), state_machine_regs.line);
ba8826a8
AO
4743 if (verbose_view || state_machine_regs.view)
4744 printf (_(" (view %u)\n"), state_machine_regs.view);
4745 else
4746 putchar ('\n');
4747 state_machine_regs.view++;
19e6b90e 4748 }
cd30bcef
AM
4749 else
4750 switch (op_code)
4751 {
4752 case DW_LNS_extended_op:
4753 data += process_extended_line_op (data,
4754 linfo.li_default_is_stmt,
4755 end);
4756 break;
4757
4758 case DW_LNS_copy:
4759 printf (_(" Copy"));
4760 if (verbose_view || state_machine_regs.view)
4761 printf (_(" (view %u)\n"), state_machine_regs.view);
4762 else
4763 putchar ('\n');
4764 state_machine_regs.view++;
4765 break;
4766
4767 case DW_LNS_advance_pc:
4768 READ_ULEB (uladv, data, end);
4769 if (linfo.li_max_ops_per_insn == 1)
4770 {
4771 uladv *= linfo.li_min_insn_length;
4772 state_machine_regs.address += uladv;
4773 if (uladv)
4774 state_machine_regs.view = 0;
4775 printf (_(" Advance PC by %s to 0x%s%s\n"),
4776 dwarf_vmatoa ("u", uladv),
4777 dwarf_vmatoa ("x", state_machine_regs.address),
4778 verbose_view && uladv
4779 ? _(" (reset view)") : "");
4780 }
4781 else
4782 {
4783 unsigned addrdelta
4784 = ((state_machine_regs.op_index + uladv)
4785 / linfo.li_max_ops_per_insn)
4786 * linfo.li_min_insn_length;
4787 state_machine_regs.address
4788 += addrdelta;
4789 state_machine_regs.op_index
4790 = (state_machine_regs.op_index + uladv)
4791 % linfo.li_max_ops_per_insn;
4792 if (addrdelta)
4793 state_machine_regs.view = 0;
4794 printf (_(" Advance PC by %s to 0x%s[%d]%s\n"),
4795 dwarf_vmatoa ("u", uladv),
4796 dwarf_vmatoa ("x", state_machine_regs.address),
4797 state_machine_regs.op_index,
4798 verbose_view && addrdelta
4799 ? _(" (reset view)") : "");
4800 }
4801 break;
4802
4803 case DW_LNS_advance_line:
4804 READ_SLEB (adv, data, end);
4805 state_machine_regs.line += adv;
4806 printf (_(" Advance Line by %s to %d\n"),
4807 dwarf_vmatoa ("d", adv),
4808 state_machine_regs.line);
4809 break;
4810
4811 case DW_LNS_set_file:
4812 READ_ULEB (uladv, data, end);
4813 printf (_(" Set File Name to entry %s in the File Name Table\n"),
4814 dwarf_vmatoa ("u", uladv));
4815 state_machine_regs.file = uladv;
4816 break;
4817
4818 case DW_LNS_set_column:
4819 READ_ULEB (uladv, data, end);
4820 printf (_(" Set column to %s\n"),
4821 dwarf_vmatoa ("u", uladv));
4822 state_machine_regs.column = uladv;
4823 break;
4824
4825 case DW_LNS_negate_stmt:
4826 adv = state_machine_regs.is_stmt;
4827 adv = ! adv;
4828 printf (_(" Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
4829 state_machine_regs.is_stmt = adv;
4830 break;
4831
4832 case DW_LNS_set_basic_block:
4833 printf (_(" Set basic block\n"));
4834 state_machine_regs.basic_block = 1;
4835 break;
4836
4837 case DW_LNS_const_add_pc:
4838 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4839 if (linfo.li_max_ops_per_insn)
4840 {
4841 uladv *= linfo.li_min_insn_length;
4842 state_machine_regs.address += uladv;
4843 if (uladv)
4844 state_machine_regs.view = 0;
4845 printf (_(" Advance PC by constant %s to 0x%s%s\n"),
4846 dwarf_vmatoa ("u", uladv),
4847 dwarf_vmatoa ("x", state_machine_regs.address),
4848 verbose_view && uladv
4849 ? _(" (reset view)") : "");
4850 }
4851 else
4852 {
4853 unsigned addrdelta
4854 = ((state_machine_regs.op_index + uladv)
4855 / linfo.li_max_ops_per_insn)
4856 * linfo.li_min_insn_length;
4857 state_machine_regs.address
4858 += addrdelta;
4859 state_machine_regs.op_index
4860 = (state_machine_regs.op_index + uladv)
4861 % linfo.li_max_ops_per_insn;
4862 if (addrdelta)
4863 state_machine_regs.view = 0;
4864 printf (_(" Advance PC by constant %s to 0x%s[%d]%s\n"),
4865 dwarf_vmatoa ("u", uladv),
4866 dwarf_vmatoa ("x", state_machine_regs.address),
4867 state_machine_regs.op_index,
4868 verbose_view && addrdelta
4869 ? _(" (reset view)") : "");
4870 }
4871 break;
4872
4873 case DW_LNS_fixed_advance_pc:
4874 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4875 state_machine_regs.address += uladv;
4876 state_machine_regs.op_index = 0;
4877 printf (_(" Advance PC by fixed size amount %s to 0x%s\n"),
4878 dwarf_vmatoa ("u", uladv),
4879 dwarf_vmatoa ("x", state_machine_regs.address));
4880 /* Do NOT reset view. */
4881 break;
4882
4883 case DW_LNS_set_prologue_end:
4884 printf (_(" Set prologue_end to true\n"));
4885 break;
4886
4887 case DW_LNS_set_epilogue_begin:
4888 printf (_(" Set epilogue_begin to true\n"));
4889 break;
4890
4891 case DW_LNS_set_isa:
4892 READ_ULEB (uladv, data, end);
4893 printf (_(" Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
4894 break;
4895
4896 default:
4897 printf (_(" Unknown opcode %d with operands: "), op_code);
4898
4899 if (standard_opcodes != NULL)
4900 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4901 {
4902 READ_ULEB (uladv, data, end);
4903 printf ("0x%s%s", dwarf_vmatoa ("x", uladv),
4904 i == 1 ? "" : ", ");
4905 }
4906 putchar ('\n');
4907 break;
4908 }
19e6b90e 4909 }
b40bf0a2 4910 putchar ('\n');
19e6b90e 4911 }
19e6b90e
L
4912 }
4913
4914 return 1;
4915}
4916
a262ae96
NC
4917typedef struct
4918{
467c65bc
NC
4919 unsigned char *name;
4920 unsigned int directory_index;
4921 unsigned int modification_date;
4922 unsigned int length;
a262ae96
NC
4923} File_Entry;
4924
4925/* Output a decoded representation of the .debug_line section. */
4926
4927static int
dda8d76d 4928display_debug_lines_decoded (struct dwarf_section * section,
ec1b0fbb 4929 unsigned char * start,
dda8d76d
NC
4930 unsigned char * data,
4931 unsigned char * end,
4932 void * fileptr)
a262ae96 4933{
b40bf0a2
NC
4934 static DWARF2_Internal_LineInfo saved_linfo;
4935
015dc7e1 4936 introduce (section, false);
a262ae96
NC
4937
4938 while (data < end)
4939 {
4940 /* This loop amounts to one iteration per compilation unit. */
91d6fa6a 4941 DWARF2_Internal_LineInfo linfo;
a262ae96
NC
4942 unsigned char *standard_opcodes;
4943 unsigned char *end_of_sequence;
a262ae96
NC
4944 int i;
4945 File_Entry *file_table = NULL;
143a3db0 4946 unsigned int n_files = 0;
a262ae96 4947 unsigned char **directory_table = NULL;
77145576 4948 dwarf_vma n_directories = 0;
a262ae96 4949
24d127aa 4950 if (startswith (section->name, ".debug_line.")
4925cdd7
NC
4951 /* Note: the following does not apply to .debug_line.dwo sections.
4952 These are full debug_line sections. */
4953 && strcmp (section->name, ".debug_line.dwo") != 0)
b4eb7656 4954 {
4925cdd7 4955 /* See comment in display_debug_lines_raw(). */
b40bf0a2
NC
4956 end_of_sequence = end;
4957 standard_opcodes = NULL;
4958 linfo = saved_linfo;
058037d3
NC
4959 /* PR 17531: file: 0522b371. */
4960 if (linfo.li_line_range == 0)
4961 {
1306a742 4962 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
058037d3
NC
4963 return 0;
4964 }
b40bf0a2 4965 reset_state_machine (linfo.li_default_is_stmt);
b4eb7656 4966 }
a262ae96 4967 else
b4eb7656 4968 {
b40bf0a2 4969 unsigned char *hdrptr;
a262ae96 4970
b40bf0a2
NC
4971 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4972 & end_of_sequence)) == NULL)
a233b20c 4973 return 0;
0c588247 4974
058037d3
NC
4975 /* PR 17531: file: 0522b371. */
4976 if (linfo.li_line_range == 0)
4977 {
4978 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4979 linfo.li_line_range = 1;
4980 }
b40bf0a2 4981 reset_state_machine (linfo.li_default_is_stmt);
a262ae96 4982
b40bf0a2
NC
4983 /* Save a pointer to the contents of the Opcodes table. */
4984 standard_opcodes = hdrptr;
a262ae96 4985
b40bf0a2
NC
4986 /* Traverse the Directory table just to count entries. */
4987 data = standard_opcodes + linfo.li_opcode_base - 1;
d8024a91
NC
4988 /* PR 20440 */
4989 if (data >= end)
4990 {
4991 warn (_("opcode base of %d extends beyond end of section\n"),
4992 linfo.li_opcode_base);
4993 return 0;
4994 }
4995
77145576 4996 if (linfo.li_version >= 5)
b40bf0a2 4997 {
77145576
JK
4998 unsigned char *format_start, format_count, *format;
4999 dwarf_vma formati, entryi;
a262ae96 5000
dda8d76d 5001 load_debug_section_with_follow (line_str, fileptr);
77145576
JK
5002
5003 /* Skip directories format. */
5004 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
546cb2d8
NC
5005 if (do_checks && format_count > 1)
5006 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
5007 format_count);
77145576
JK
5008 format_start = data;
5009 for (formati = 0; formati < format_count; formati++)
b40bf0a2 5010 {
cd30bcef
AM
5011 SKIP_ULEB (data, end);
5012 SKIP_ULEB (data, end);
b40bf0a2 5013 }
a262ae96 5014
cd30bcef 5015 READ_ULEB (n_directories, data, end);
55b26492 5016 if (data >= end)
d8024a91 5017 {
77145576 5018 warn (_("Corrupt directories list\n"));
d8024a91
NC
5019 break;
5020 }
5021
070b775f
NC
5022 if (n_directories == 0)
5023 directory_table = NULL;
5024 else
5025 directory_table = (unsigned char **)
5026 xmalloc (n_directories * sizeof (unsigned char *));
a262ae96 5027
77145576 5028 for (entryi = 0; entryi < n_directories; entryi++)
b40bf0a2 5029 {
77145576 5030 unsigned char **pathp = &directory_table[entryi];
a262ae96 5031
77145576
JK
5032 format = format_start;
5033 for (formati = 0; formati < format_count; formati++)
5034 {
5035 dwarf_vma content_type, form;
5036 dwarf_vma uvalue;
5037
cd30bcef
AM
5038 READ_ULEB (content_type, format, end);
5039 READ_ULEB (form, format, end);
55b26492 5040 if (data >= end)
77145576
JK
5041 {
5042 warn (_("Corrupt directories list\n"));
5043 break;
5044 }
5045 switch (content_type)
5046 {
5047 case DW_LNCT_path:
5048 switch (form)
5049 {
5050 case DW_FORM_string:
5051 *pathp = data;
5052 break;
5053 case DW_FORM_line_strp:
5054 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5055 end);
5056 /* Remove const by the cast. */
5057 *pathp = (unsigned char *)
5058 fetch_indirect_line_string (uvalue);
5059 break;
5060 }
5061 break;
5062 }
cd30bcef
AM
5063 data = read_and_display_attr_value (0, form, 0, start,
5064 data, end, 0, 0,
77145576
JK
5065 linfo.li_offset_size,
5066 linfo.li_version,
5067 NULL, 1, section,
ec1b0fbb 5068 NULL, '\t', -1);
77145576 5069 }
55b26492 5070 if (data >= end)
77145576
JK
5071 {
5072 warn (_("Corrupt directories list\n"));
5073 break;
5074 }
5075 }
a262ae96 5076
77145576
JK
5077 /* Skip files format. */
5078 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
546cb2d8
NC
5079 if (do_checks && format_count > 5)
5080 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5081 format_count);
77145576
JK
5082 format_start = data;
5083 for (formati = 0; formati < format_count; formati++)
b40bf0a2 5084 {
cd30bcef
AM
5085 SKIP_ULEB (data, end);
5086 SKIP_ULEB (data, end);
b40bf0a2 5087 }
a262ae96 5088
cd30bcef 5089 READ_ULEB (n_files, data, end);
55b26492 5090 if (data >= end && n_files > 0)
d8024a91 5091 {
77145576 5092 warn (_("Corrupt file name list\n"));
d8024a91
NC
5093 break;
5094 }
5095
070b775f
NC
5096 if (n_files == 0)
5097 file_table = NULL;
5098 else
5099 file_table = (File_Entry *) xcalloc (1, n_files
5100 * sizeof (File_Entry));
a262ae96 5101
77145576 5102 for (entryi = 0; entryi < n_files; entryi++)
b40bf0a2 5103 {
77145576 5104 File_Entry *file = &file_table[entryi];
a262ae96 5105
77145576
JK
5106 format = format_start;
5107 for (formati = 0; formati < format_count; formati++)
5108 {
5109 dwarf_vma content_type, form;
5110 dwarf_vma uvalue;
cd30bcef 5111 unsigned char *tmp;
77145576 5112
cd30bcef
AM
5113 READ_ULEB (content_type, format, end);
5114 READ_ULEB (form, format, end);
55b26492 5115 if (data >= end)
77145576
JK
5116 {
5117 warn (_("Corrupt file name list\n"));
5118 break;
5119 }
5120 switch (content_type)
5121 {
5122 case DW_LNCT_path:
5123 switch (form)
5124 {
5125 case DW_FORM_string:
5126 file->name = data;
5127 break;
5128 case DW_FORM_line_strp:
5129 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5130 end);
5131 /* Remove const by the cast. */
5132 file->name = (unsigned char *)
5133 fetch_indirect_line_string (uvalue);
5134 break;
5135 }
5136 break;
5137 case DW_LNCT_directory_index:
5138 switch (form)
5139 {
5140 case DW_FORM_data1:
5141 SAFE_BYTE_GET (file->directory_index, data, 1,
5142 end);
5143 break;
5144 case DW_FORM_data2:
5145 SAFE_BYTE_GET (file->directory_index, data, 2,
5146 end);
5147 break;
5148 case DW_FORM_udata:
cd30bcef
AM
5149 tmp = data;
5150 READ_ULEB (file->directory_index, tmp, end);
77145576
JK
5151 break;
5152 }
5153 break;
5154 }
cd30bcef
AM
5155 data = read_and_display_attr_value (0, form, 0, start,
5156 data, end, 0, 0,
77145576
JK
5157 linfo.li_offset_size,
5158 linfo.li_version,
5159 NULL, 1, section,
ec1b0fbb 5160 NULL, '\t', -1);
77145576 5161 }
55b26492 5162 if (data >= end)
77145576
JK
5163 {
5164 warn (_("Corrupt file name list\n"));
5165 break;
5166 }
5167 }
5168 }
5169 else
5170 {
5171 if (*data != 0)
b40bf0a2 5172 {
77145576
JK
5173 unsigned char *ptr_directory_table = data;
5174
5175 while (data < end && *data != 0)
5176 {
5177 data += strnlen ((char *) data, end - data) + 1;
5178 n_directories++;
5179 }
5180
5181 /* PR 20440 */
5182 if (data >= end)
5183 {
5184 warn (_("directory table ends unexpectedly\n"));
5185 n_directories = 0;
5186 break;
5187 }
5188
5189 /* Go through the directory table again to save the directories. */
5190 directory_table = (unsigned char **)
5191 xmalloc (n_directories * sizeof (unsigned char *));
5192
5193 i = 0;
5194 while (*ptr_directory_table != 0)
5195 {
5196 directory_table[i] = ptr_directory_table;
5197 ptr_directory_table += strnlen ((char *) ptr_directory_table,
5198 ptr_directory_table - end) + 1;
5199 i++;
5200 }
b40bf0a2 5201 }
77145576
JK
5202 /* Skip the NUL at the end of the table. */
5203 data++;
5204
5205 /* Traverse the File Name table just to count the entries. */
5206 if (data < end && *data != 0)
b40bf0a2 5207 {
77145576
JK
5208 unsigned char *ptr_file_name_table = data;
5209
5210 while (data < end && *data != 0)
db9537d2 5211 {
cd30bcef
AM
5212 /* Skip Name, directory index, last modification
5213 time and length of file. */
77145576 5214 data += strnlen ((char *) data, end - data) + 1;
cd30bcef
AM
5215 SKIP_ULEB (data, end);
5216 SKIP_ULEB (data, end);
5217 SKIP_ULEB (data, end);
77145576 5218 n_files++;
db9537d2 5219 }
a262ae96 5220
77145576
JK
5221 if (data >= end)
5222 {
5223 warn (_("file table ends unexpectedly\n"));
5224 n_files = 0;
5225 break;
5226 }
5227
5228 /* Go through the file table again to save the strings. */
5229 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
0c588247 5230
77145576
JK
5231 i = 0;
5232 while (*ptr_file_name_table != 0)
5233 {
77145576
JK
5234 file_table[i].name = ptr_file_name_table;
5235 ptr_file_name_table += strnlen ((char *) ptr_file_name_table,
5236 end - ptr_file_name_table) + 1;
5237
5238 /* We are not interested in directory, time or size. */
cd30bcef
AM
5239 READ_ULEB (file_table[i].directory_index,
5240 ptr_file_name_table, end);
5241 READ_ULEB (file_table[i].modification_date,
5242 ptr_file_name_table, end);
5243 READ_ULEB (file_table[i].length,
5244 ptr_file_name_table, end);
77145576
JK
5245 i++;
5246 }
5247 i = 0;
b40bf0a2 5248 }
77145576
JK
5249
5250 /* Skip the NUL at the end of the table. */
5251 data++;
b40bf0a2 5252 }
cc5914eb 5253
77145576 5254 /* Print the Compilation Unit's name and a header. */
f082820d 5255 if (file_table == NULL)
070b775f 5256 printf (_("CU: No directory table\n"));
f082820d
AM
5257 else if (directory_table == NULL)
5258 printf (_("CU: %s:\n"), file_table[0].name);
77145576
JK
5259 else
5260 {
5261 unsigned int ix = file_table[0].directory_index;
5262 const char *directory;
5263
5264 if (ix == 0)
5265 directory = ".";
5266 /* PR 20439 */
5267 else if (n_directories == 0)
5268 directory = _("<unknown>");
5269 else if (ix > n_directories)
5270 {
5271 warn (_("directory index %u > number of directories %s\n"),
5272 ix, dwarf_vmatoa ("u", n_directories));
5273 directory = _("<corrupt>");
5274 }
5275 else
5276 directory = (char *) directory_table[ix - 1];
5277
5278 if (do_wide || strlen (directory) < 76)
5279 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
5280 else
5281 printf ("%s:\n", file_table[0].name);
77145576 5282 }
a262ae96 5283
070b775f
NC
5284 if (n_files > 0)
5285 printf (_("File name Line number Starting address View Stmt\n"));
5286 else
5287 printf (_("CU: Empty file name table\n"));
b40bf0a2
NC
5288 saved_linfo = linfo;
5289 }
a262ae96
NC
5290
5291 /* This loop iterates through the Dwarf Line Number Program. */
5292 while (data < end_of_sequence)
b4eb7656 5293 {
a262ae96 5294 unsigned char op_code;
ba8826a8 5295 int xop;
b4eb7656
AM
5296 int adv;
5297 unsigned long int uladv;
b4eb7656 5298 int is_special_opcode = 0;
a262ae96 5299
b4eb7656 5300 op_code = *data++;
ba8826a8 5301 xop = op_code;
a262ae96 5302
b4eb7656 5303 if (op_code >= linfo.li_opcode_base)
a262ae96 5304 {
91d6fa6a 5305 op_code -= linfo.li_opcode_base;
a233b20c
JJ
5306 uladv = (op_code / linfo.li_line_range);
5307 if (linfo.li_max_ops_per_insn == 1)
5308 {
5309 uladv *= linfo.li_min_insn_length;
5310 state_machine_regs.address += uladv;
ba8826a8
AO
5311 if (uladv)
5312 state_machine_regs.view = 0;
a233b20c
JJ
5313 }
5314 else
5315 {
ba8826a8
AO
5316 unsigned addrdelta
5317 = ((state_machine_regs.op_index + uladv)
5318 / linfo.li_max_ops_per_insn)
b40bf0a2 5319 * linfo.li_min_insn_length;
ba8826a8
AO
5320 state_machine_regs.address
5321 += addrdelta;
a233b20c
JJ
5322 state_machine_regs.op_index
5323 = (state_machine_regs.op_index + uladv)
b40bf0a2 5324 % linfo.li_max_ops_per_insn;
ba8826a8
AO
5325 if (addrdelta)
5326 state_machine_regs.view = 0;
a233b20c 5327 }
a262ae96 5328
b4eb7656
AM
5329 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5330 state_machine_regs.line += adv;
5331 is_special_opcode = 1;
ba8826a8 5332 /* Increment view after printing this row. */
b4eb7656 5333 }
cd30bcef
AM
5334 else
5335 switch (op_code)
5336 {
5337 case DW_LNS_extended_op:
5338 {
5339 unsigned int ext_op_code_len;
5340 unsigned char ext_op_code;
5341 unsigned char *op_code_end;
5342 unsigned char *op_code_data = data;
5343
5344 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5345 op_code_end = op_code_data + ext_op_code_len;
5346 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5347 {
5348 warn (_("Badly formed extended line op encountered!\n"));
5349 break;
5350 }
5351 ext_op_code = *op_code_data++;
5352 xop = ext_op_code;
5353 xop = -xop;
5354
5355 switch (ext_op_code)
5356 {
5357 case DW_LNE_end_sequence:
5358 /* Reset stuff after printing this row. */
5359 break;
5360 case DW_LNE_set_address:
5361 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5362 op_code_data,
5363 op_code_end - op_code_data,
5364 op_code_end);
5365 state_machine_regs.op_index = 0;
5366 state_machine_regs.view = 0;
5367 break;
5368 case DW_LNE_define_file:
5369 file_table = (File_Entry *) xrealloc
5370 (file_table, (n_files + 1) * sizeof (File_Entry));
5371
5372 ++state_machine_regs.last_file_entry;
5373 /* Source file name. */
5374 file_table[n_files].name = op_code_data;
5375 op_code_data += strlen ((char *) op_code_data) + 1;
5376 /* Directory index. */
5377 READ_ULEB (file_table[n_files].directory_index,
5378 op_code_data, op_code_end);
5379 /* Last modification time. */
5380 READ_ULEB (file_table[n_files].modification_date,
5381 op_code_data, op_code_end);
5382 /* File length. */
5383 READ_ULEB (file_table[n_files].length,
5384 op_code_data, op_code_end);
5385 n_files++;
5386 break;
5387
5388 case DW_LNE_set_discriminator:
5389 case DW_LNE_HP_set_sequence:
5390 /* Simply ignored. */
5391 break;
5392
5393 default:
5394 printf (_("UNKNOWN (%u): length %ld\n"),
27653fba 5395 ext_op_code, (long int) (op_code_data - data));
cd30bcef
AM
5396 break;
5397 }
5398 data = op_code_end;
5399 break;
5400 }
5401 case DW_LNS_copy:
5402 /* Increment view after printing this row. */
5403 break;
5404
5405 case DW_LNS_advance_pc:
5406 READ_ULEB (uladv, data, end);
5407 if (linfo.li_max_ops_per_insn == 1)
5408 {
5409 uladv *= linfo.li_min_insn_length;
5410 state_machine_regs.address += uladv;
5411 if (uladv)
5412 state_machine_regs.view = 0;
5413 }
5414 else
5415 {
5416 unsigned addrdelta
5417 = ((state_machine_regs.op_index + uladv)
5418 / linfo.li_max_ops_per_insn)
5419 * linfo.li_min_insn_length;
5420 state_machine_regs.address
5421 += addrdelta;
5422 state_machine_regs.op_index
5423 = (state_machine_regs.op_index + uladv)
5424 % linfo.li_max_ops_per_insn;
5425 if (addrdelta)
5426 state_machine_regs.view = 0;
5427 }
5428 break;
5429
5430 case DW_LNS_advance_line:
5431 READ_SLEB (adv, data, end);
5432 state_machine_regs.line += adv;
5433 break;
5434
5435 case DW_LNS_set_file:
5436 READ_ULEB (uladv, data, end);
5437 state_machine_regs.file = uladv;
5438
5439 {
5440 unsigned file = state_machine_regs.file - 1;
5441 unsigned dir;
5442
5443 if (file_table == NULL || n_files == 0)
5444 printf (_("\n [Use file table entry %d]\n"), file);
5445 /* PR 20439 */
5446 else if (file >= n_files)
5447 {
5448 warn (_("file index %u > number of files %u\n"), file + 1, n_files);
5449 printf (_("\n <over large file table index %u>"), file);
5450 }
5451 else if ((dir = file_table[file].directory_index) == 0)
5452 /* If directory index is 0, that means current directory. */
5453 printf ("\n./%s:[++]\n", file_table[file].name);
5454 else if (directory_table == NULL || n_directories == 0)
5455 printf (_("\n [Use file %s in directory table entry %d]\n"),
5456 file_table[file].name, dir);
5457 /* PR 20439 */
5458 else if (dir > n_directories)
5459 {
5460 warn (_("directory index %u > number of directories %s\n"),
5461 dir, dwarf_vmatoa ("u", n_directories));
5462 printf (_("\n <over large directory table entry %u>\n"), dir);
5463 }
5464 else
5465 printf ("\n%s/%s:\n",
5466 /* The directory index starts counting at 1. */
5467 directory_table[dir - 1], file_table[file].name);
5468 }
5469 break;
5470
5471 case DW_LNS_set_column:
5472 READ_ULEB (uladv, data, end);
5473 state_machine_regs.column = uladv;
5474 break;
5475
5476 case DW_LNS_negate_stmt:
5477 adv = state_machine_regs.is_stmt;
5478 adv = ! adv;
5479 state_machine_regs.is_stmt = adv;
5480 break;
5481
5482 case DW_LNS_set_basic_block:
5483 state_machine_regs.basic_block = 1;
5484 break;
5485
5486 case DW_LNS_const_add_pc:
5487 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5488 if (linfo.li_max_ops_per_insn == 1)
5489 {
5490 uladv *= linfo.li_min_insn_length;
5491 state_machine_regs.address += uladv;
5492 if (uladv)
5493 state_machine_regs.view = 0;
5494 }
5495 else
5496 {
5497 unsigned addrdelta
5498 = ((state_machine_regs.op_index + uladv)
5499 / linfo.li_max_ops_per_insn)
5500 * linfo.li_min_insn_length;
5501 state_machine_regs.address
5502 += addrdelta;
5503 state_machine_regs.op_index
5504 = (state_machine_regs.op_index + uladv)
5505 % linfo.li_max_ops_per_insn;
5506 if (addrdelta)
5507 state_machine_regs.view = 0;
5508 }
5509 break;
5510
5511 case DW_LNS_fixed_advance_pc:
5512 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5513 state_machine_regs.address += uladv;
5514 state_machine_regs.op_index = 0;
5515 /* Do NOT reset view. */
5516 break;
5517
5518 case DW_LNS_set_prologue_end:
5519 break;
5520
5521 case DW_LNS_set_epilogue_begin:
5522 break;
5523
5524 case DW_LNS_set_isa:
5525 READ_ULEB (uladv, data, end);
5526 printf (_(" Set ISA to %lu\n"), uladv);
5527 break;
5528
5529 default:
5530 printf (_(" Unknown opcode %d with operands: "), op_code);
5531
5532 if (standard_opcodes != NULL)
5533 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5534 {
5535 dwarf_vma val;
5536
5537 READ_ULEB (val, data, end);
5538 printf ("0x%s%s", dwarf_vmatoa ("x", val),
5539 i == 1 ? "" : ", ");
5540 }
5541 putchar ('\n');
5542 break;
5543 }
a262ae96 5544
b4eb7656
AM
5545 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5546 to the DWARF address/line matrix. */
ba8826a8
AO
5547 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5548 || (xop == DW_LNS_copy))
b4eb7656
AM
5549 {
5550 const unsigned int MAX_FILENAME_LENGTH = 35;
5551 char *fileName;
5552 char *newFileName = NULL;
5553 size_t fileNameLength;
b40bf0a2
NC
5554
5555 if (file_table)
db9537d2
NC
5556 {
5557 unsigned indx = state_machine_regs.file - 1;
5558 /* PR 20439 */
5559 if (indx >= n_files)
5560 {
5561 warn (_("corrupt file index %u encountered\n"), indx);
5562 fileName = _("<corrupt>");
5563 }
5564 else
5565 fileName = (char *) file_table[indx].name;
5566 }
b40bf0a2 5567 else
db9537d2 5568 fileName = _("<unknown>");
b40bf0a2
NC
5569
5570 fileNameLength = strlen (fileName);
a262ae96 5571
b4eb7656
AM
5572 if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
5573 {
5574 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5575 /* Truncate file name */
5576 strncpy (newFileName,
5577 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5578 MAX_FILENAME_LENGTH + 1);
e1104d08
NC
5579 /* FIXME: This is to pacify gcc-10 which can warn that the
5580 strncpy above might leave a non-NUL terminated string
5581 in newFileName. It won't, but gcc's analysis doesn't
5582 quite go far enough to discover this. */
5583 newFileName[MAX_FILENAME_LENGTH] = 0;
b4eb7656
AM
5584 }
5585 else
5586 {
5587 newFileName = (char *) xmalloc (fileNameLength + 1);
5588 strncpy (newFileName, fileName, fileNameLength + 1);
5589 }
5590
af2b3186
TV
5591 /* A row with end_seq set to true has a meaningful address, but
5592 the other information in the same row is not significant.
5593 In such a row, print line as "-", and don't print
5594 view/is_stmt. */
b4eb7656
AM
5595 if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
5596 {
a233b20c 5597 if (linfo.li_max_ops_per_insn == 1)
af2b3186
TV
5598 {
5599 if (xop == -DW_LNE_end_sequence)
5600 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x",
5601 newFileName, "-",
5602 state_machine_regs.address);
5603 else
5604 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x",
5605 newFileName, state_machine_regs.line,
5606 state_machine_regs.address);
5607 }
a233b20c 5608 else
af2b3186
TV
5609 {
5610 if (xop == -DW_LNE_end_sequence)
5611 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x[%d]",
5612 newFileName, "-",
5613 state_machine_regs.address,
5614 state_machine_regs.op_index);
5615 else
5616 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x[%d]",
5617 newFileName, state_machine_regs.line,
5618 state_machine_regs.address,
5619 state_machine_regs.op_index);
5620 }
b4eb7656
AM
5621 }
5622 else
5623 {
a233b20c 5624 if (linfo.li_max_ops_per_insn == 1)
af2b3186
TV
5625 {
5626 if (xop == -DW_LNE_end_sequence)
5627 printf ("%s %11s %#18" DWARF_VMA_FMT "x",
5628 newFileName, "-",
5629 state_machine_regs.address);
5630 else
5631 printf ("%s %11d %#18" DWARF_VMA_FMT "x",
5632 newFileName, state_machine_regs.line,
5633 state_machine_regs.address);
5634 }
a233b20c 5635 else
af2b3186
TV
5636 {
5637 if (xop == -DW_LNE_end_sequence)
5638 printf ("%s %11s %#18" DWARF_VMA_FMT "x[%d]",
5639 newFileName, "-",
5640 state_machine_regs.address,
5641 state_machine_regs.op_index);
5642 else
5643 printf ("%s %11d %#18" DWARF_VMA_FMT "x[%d]",
5644 newFileName, state_machine_regs.line,
5645 state_machine_regs.address,
5646 state_machine_regs.op_index);
5647 }
b4eb7656 5648 }
a262ae96 5649
af2b3186
TV
5650 if (xop != -DW_LNE_end_sequence)
5651 {
5652 if (state_machine_regs.view)
5653 printf (" %6u", state_machine_regs.view);
5654 else
5655 printf (" ");
17f6ade2 5656
af2b3186
TV
5657 if (state_machine_regs.is_stmt)
5658 printf (" x");
5659 }
17f6ade2
JD
5660
5661 putchar ('\n');
ba8826a8
AO
5662 state_machine_regs.view++;
5663
5664 if (xop == -DW_LNE_end_sequence)
5665 {
5666 reset_state_machine (linfo.li_default_is_stmt);
5667 putchar ('\n');
5668 }
a262ae96 5669
b4eb7656
AM
5670 free (newFileName);
5671 }
5672 }
b40bf0a2
NC
5673
5674 if (file_table)
5675 {
5676 free (file_table);
5677 file_table = NULL;
5678 n_files = 0;
5679 }
5680
5681 if (directory_table)
5682 {
5683 free (directory_table);
5684 directory_table = NULL;
5685 n_directories = 0;
5686 }
5687
a262ae96
NC
5688 putchar ('\n');
5689 }
5690
5691 return 1;
5692}
5693
5694static int
77145576 5695display_debug_lines (struct dwarf_section *section, void *file)
a262ae96
NC
5696{
5697 unsigned char *data = section->start;
5698 unsigned char *end = data + section->size;
4cb93e3b
TG
5699 int retValRaw = 1;
5700 int retValDecoded = 1;
a262ae96 5701
008f4c78
NC
5702 if (do_debug_lines == 0)
5703 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5704
4cb93e3b 5705 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
77145576 5706 retValRaw = display_debug_lines_raw (section, data, end, file);
a262ae96 5707
4cb93e3b 5708 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
ec1b0fbb 5709 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
a262ae96 5710
4cb93e3b 5711 if (!retValRaw || !retValDecoded)
a262ae96
NC
5712 return 0;
5713
5714 return 1;
5715}
5716
6e3d6dc1
NC
5717static debug_info *
5718find_debug_info_for_offset (unsigned long offset)
5719{
5720 unsigned int i;
5721
5722 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5723 return NULL;
5724
5725 for (i = 0; i < num_debug_info_entries; i++)
5726 if (debug_information[i].cu_offset == offset)
5727 return debug_information + i;
5728
5729 return NULL;
5730}
5731
459d52c8
DE
5732static const char *
5733get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5734{
5735 /* See gdb/gdb-index.h. */
5736 static const char * const kinds[] =
5737 {
5738 N_ ("no info"),
5739 N_ ("type"),
5740 N_ ("variable"),
5741 N_ ("function"),
5742 N_ ("other"),
5743 N_ ("unused5"),
5744 N_ ("unused6"),
5745 N_ ("unused7")
5746 };
5747
5748 return _ (kinds[kind]);
5749}
5750
19e6b90e 5751static int
459d52c8
DE
5752display_debug_pubnames_worker (struct dwarf_section *section,
5753 void *file ATTRIBUTE_UNUSED,
5754 int is_gnu)
19e6b90e 5755{
91d6fa6a 5756 DWARF2_Internal_PubNames names;
19e6b90e
L
5757 unsigned char *start = section->start;
5758 unsigned char *end = start + section->size;
5759
6e3d6dc1
NC
5760 /* It does not matter if this load fails,
5761 we test for that later on. */
5762 load_debug_info (file);
5763
015dc7e1 5764 introduce (section, false);
19e6b90e
L
5765
5766 while (start < end)
5767 {
5768 unsigned char *data;
e98fdf1a 5769 unsigned long sec_off;
bf5117e3 5770 unsigned int offset_size, initial_length_size;
19e6b90e 5771
e98fdf1a 5772 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
91d6fa6a 5773 if (names.pn_length == 0xffffffff)
19e6b90e 5774 {
e98fdf1a 5775 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
19e6b90e
L
5776 offset_size = 8;
5777 initial_length_size = 12;
5778 }
5779 else
5780 {
5781 offset_size = 4;
5782 initial_length_size = 4;
5783 }
5784
e98fdf1a
AM
5785 sec_off = start - section->start;
5786 if (sec_off + names.pn_length < sec_off
5787 || sec_off + names.pn_length > section->size)
5788 {
5789 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
5790 section->name,
5791 sec_off - initial_length_size,
5792 dwarf_vmatoa ("x", names.pn_length));
5793 break;
5794 }
5795
5796 data = start;
5797 start += names.pn_length;
5798
0c588247
NC
5799 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, end);
5800 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, end);
6e3d6dc1
NC
5801
5802 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5803 && num_debug_info_entries > 0
91d6fa6a 5804 && find_debug_info_for_offset (names.pn_offset) == NULL)
6e3d6dc1 5805 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
47704ddf 5806 (unsigned long) names.pn_offset, section->name);
cecf136e 5807
0c588247 5808 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, end);
19e6b90e 5809
058037d3
NC
5810 printf (_(" Length: %ld\n"),
5811 (long) names.pn_length);
5812 printf (_(" Version: %d\n"),
5813 names.pn_version);
5814 printf (_(" Offset into .debug_info section: 0x%lx\n"),
5815 (unsigned long) names.pn_offset);
5816 printf (_(" Size of area in .debug_info section: %ld\n"),
5817 (long) names.pn_size);
19e6b90e 5818
91d6fa6a 5819 if (names.pn_version != 2 && names.pn_version != 3)
19e6b90e
L
5820 {
5821 static int warned = 0;
5822
5823 if (! warned)
5824 {
5825 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5826 warned = 1;
5827 }
5828
5829 continue;
5830 }
5831
459d52c8
DE
5832 if (is_gnu)
5833 printf (_("\n Offset Kind Name\n"));
5834 else
5835 printf (_("\n Offset\tName\n"));
19e6b90e 5836
e98fdf1a 5837 while (1)
19e6b90e 5838 {
f41e4712 5839 bfd_size_type maxprint;
e98fdf1a 5840 dwarf_vma offset;
f41e4712 5841
0c588247 5842 SAFE_BYTE_GET (offset, data, offset_size, end);
19e6b90e 5843
e98fdf1a
AM
5844 if (offset == 0)
5845 break;
b4eb7656 5846
e98fdf1a
AM
5847 data += offset_size;
5848 if (data >= end)
5849 break;
5850 maxprint = (end - data) - 1;
f41e4712 5851
e98fdf1a
AM
5852 if (is_gnu)
5853 {
5854 unsigned int kind_data;
5855 gdb_index_symbol_kind kind;
5856 const char *kind_name;
5857 int is_static;
5858
5859 SAFE_BYTE_GET (kind_data, data, 1, end);
5860 data++;
5861 maxprint --;
5862 /* GCC computes the kind as the upper byte in the CU index
5863 word, and then right shifts it by the CU index size.
5864 Left shift KIND to where the gdb-index.h accessor macros
5865 can use it. */
5866 kind_data <<= GDB_INDEX_CU_BITSIZE;
5867 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5868 kind_name = get_gdb_index_symbol_kind_name (kind);
5869 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5870 printf (" %-6lx %s,%-10s %.*s\n",
5871 (unsigned long) offset, is_static ? _("s") : _("g"),
5872 kind_name, (int) maxprint, data);
19e6b90e 5873 }
e98fdf1a
AM
5874 else
5875 printf (" %-6lx\t%.*s\n",
5876 (unsigned long) offset, (int) maxprint, data);
5877
5878 data += strnlen ((char *) data, maxprint) + 1;
5879 if (data >= end)
5880 break;
19e6b90e 5881 }
19e6b90e
L
5882 }
5883
5884 printf ("\n");
5885 return 1;
5886}
5887
459d52c8
DE
5888static int
5889display_debug_pubnames (struct dwarf_section *section, void *file)
5890{
5891 return display_debug_pubnames_worker (section, file, 0);
5892}
5893
5894static int
5895display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5896{
5897 return display_debug_pubnames_worker (section, file, 1);
5898}
5899
19e6b90e
L
5900static int
5901display_debug_macinfo (struct dwarf_section *section,
5902 void *file ATTRIBUTE_UNUSED)
5903{
5904 unsigned char *start = section->start;
5905 unsigned char *end = start + section->size;
5906 unsigned char *curr = start;
19e6b90e
L
5907 enum dwarf_macinfo_record_type op;
5908
015dc7e1 5909 introduce (section, false);
19e6b90e
L
5910
5911 while (curr < end)
5912 {
5913 unsigned int lineno;
0c588247 5914 const unsigned char *string;
19e6b90e 5915
3f5e193b 5916 op = (enum dwarf_macinfo_record_type) *curr;
19e6b90e
L
5917 curr++;
5918
5919 switch (op)
5920 {
5921 case DW_MACINFO_start_file:
5922 {
5923 unsigned int filenum;
5924
cd30bcef
AM
5925 READ_ULEB (lineno, curr, end);
5926 READ_ULEB (filenum, curr, end);
19e6b90e
L
5927 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5928 lineno, filenum);
5929 }
5930 break;
5931
5932 case DW_MACINFO_end_file:
5933 printf (_(" DW_MACINFO_end_file\n"));
5934 break;
5935
5936 case DW_MACINFO_define:
cd30bcef 5937 READ_ULEB (lineno, curr, end);
0c588247
NC
5938 string = curr;
5939 curr += strnlen ((char *) string, end - string) + 1;
19e6b90e
L
5940 printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
5941 lineno, string);
5942 break;
5943
5944 case DW_MACINFO_undef:
cd30bcef 5945 READ_ULEB (lineno, curr, end);
0c588247
NC
5946 string = curr;
5947 curr += strnlen ((char *) string, end - string) + 1;
19e6b90e
L
5948 printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
5949 lineno, string);
5950 break;
5951
5952 case DW_MACINFO_vendor_ext:
5953 {
5954 unsigned int constant;
5955
cd30bcef 5956 READ_ULEB (constant, curr, end);
0c588247
NC
5957 string = curr;
5958 curr += strnlen ((char *) string, end - string) + 1;
19e6b90e
L
5959 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
5960 constant, string);
5961 }
5962 break;
5963 }
5964 }
5965
5966 return 1;
5967}
5968
4ccf1e31
JJ
5969/* Given LINE_OFFSET into the .debug_line section, attempt to return
5970 filename and dirname corresponding to file name table entry with index
5971 FILEIDX. Return NULL on failure. */
5972
5973static unsigned char *
f6f0e17b
NC
5974get_line_filename_and_dirname (dwarf_vma line_offset,
5975 dwarf_vma fileidx,
4ccf1e31
JJ
5976 unsigned char **dir_name)
5977{
5978 struct dwarf_section *section = &debug_displays [line].section;
5979 unsigned char *hdrptr, *dirtable, *file_name;
5980 unsigned int offset_size, initial_length_size;
cd30bcef 5981 unsigned int version, opcode_base;
4ccf1e31 5982 dwarf_vma length, diridx;
f6f0e17b 5983 const unsigned char * end;
4ccf1e31
JJ
5984
5985 *dir_name = NULL;
5986 if (section->start == NULL
5987 || line_offset >= section->size
5988 || fileidx == 0)
5989 return NULL;
5990
5991 hdrptr = section->start + line_offset;
f6f0e17b 5992 end = section->start + section->size;
0c588247
NC
5993
5994 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
4ccf1e31
JJ
5995 if (length == 0xffffffff)
5996 {
5997 /* This section is 64-bit DWARF 3. */
0c588247 5998 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
4ccf1e31
JJ
5999 offset_size = 8;
6000 initial_length_size = 12;
6001 }
6002 else
6003 {
6004 offset_size = 4;
6005 initial_length_size = 4;
6006 }
e98fdf1a
AM
6007 if (length + initial_length_size < length
6008 || length + initial_length_size > section->size)
4ccf1e31 6009 return NULL;
0c588247
NC
6010
6011 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
4ccf1e31
JJ
6012 if (version != 2 && version != 3 && version != 4)
6013 return NULL;
6014 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
6015 if (version >= 4)
6016 hdrptr++; /* Skip max_ops_per_insn. */
6017 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
0c588247
NC
6018
6019 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
4ccf1e31
JJ
6020 if (opcode_base == 0)
6021 return NULL;
0c588247 6022
4ccf1e31 6023 hdrptr += opcode_base - 1;
5c1c468d
NC
6024 if (hdrptr >= end)
6025 return NULL;
6026
4ccf1e31
JJ
6027 dirtable = hdrptr;
6028 /* Skip over dirname table. */
6029 while (*hdrptr != '\0')
5c1c468d
NC
6030 {
6031 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
6032 if (hdrptr >= end)
6033 return NULL;
6034 }
4ccf1e31 6035 hdrptr++; /* Skip the NUL at the end of the table. */
5c1c468d 6036
4ccf1e31 6037 /* Now skip over preceding filename table entries. */
5c1c468d 6038 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
4ccf1e31 6039 {
0c588247 6040 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
cd30bcef
AM
6041 SKIP_ULEB (hdrptr, end);
6042 SKIP_ULEB (hdrptr, end);
6043 SKIP_ULEB (hdrptr, end);
4ccf1e31 6044 }
5c1c468d 6045 if (hdrptr >= end || *hdrptr == '\0')
4ccf1e31 6046 return NULL;
5c1c468d 6047
4ccf1e31 6048 file_name = hdrptr;
0c588247 6049 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
5c1c468d
NC
6050 if (hdrptr >= end)
6051 return NULL;
cd30bcef 6052 READ_ULEB (diridx, hdrptr, end);
4ccf1e31
JJ
6053 if (diridx == 0)
6054 return file_name;
5c1c468d 6055 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
0c588247 6056 dirtable += strnlen ((char *) dirtable, end - dirtable) + 1;
5c1c468d 6057 if (dirtable >= end || *dirtable == '\0')
4ccf1e31
JJ
6058 return NULL;
6059 *dir_name = dirtable;
6060 return file_name;
6061}
6062
6063static int
6064display_debug_macro (struct dwarf_section *section,
6065 void *file)
6066{
6067 unsigned char *start = section->start;
6068 unsigned char *end = start + section->size;
6069 unsigned char *curr = start;
6070 unsigned char *extended_op_buf[256];
015dc7e1 6071 bool is_dwo = false;
d296b736
NC
6072 const char *suffix = strrchr (section->name, '.');
6073
6074 if (suffix && strcmp (suffix, ".dwo") == 0)
015dc7e1 6075 is_dwo = true;
4ccf1e31 6076
dda8d76d
NC
6077 load_debug_section_with_follow (str, file);
6078 load_debug_section_with_follow (line, file);
e4b7104b 6079 load_debug_section_with_follow (str_index, file);
d296b736 6080
015dc7e1 6081 introduce (section, false);
4ccf1e31
JJ
6082
6083 while (curr < end)
6084 {
6085 unsigned int lineno, version, flags;
d296b736 6086 unsigned int offset_size;
0c588247 6087 const unsigned char *string;
4ccf1e31
JJ
6088 dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
6089 unsigned char **extended_ops = NULL;
6090
0c588247 6091 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
7a7e1061 6092 if (version != 4 && version != 5)
4ccf1e31 6093 {
0e12f6c8
TV
6094 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6095 section->name, version);
4ccf1e31
JJ
6096 return 0;
6097 }
6098
0c588247 6099 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
d296b736 6100 offset_size = (flags & 1) ? 8 : 4;
4ccf1e31
JJ
6101 printf (_(" Offset: 0x%lx\n"),
6102 (unsigned long) sec_offset);
6103 printf (_(" Version: %d\n"), version);
6104 printf (_(" Offset size: %d\n"), offset_size);
6105 if (flags & 2)
6106 {
0c588247 6107 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
4ccf1e31
JJ
6108 printf (_(" Offset into .debug_line: 0x%lx\n"),
6109 (unsigned long) line_offset);
6110 }
6111 if (flags & 4)
6112 {
0c588247 6113 unsigned int i, count, op;
4ccf1e31 6114 dwarf_vma nargs, n;
0c588247
NC
6115
6116 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
bf5117e3 6117
4ccf1e31
JJ
6118 memset (extended_op_buf, 0, sizeof (extended_op_buf));
6119 extended_ops = extended_op_buf;
6120 if (count)
6121 {
6122 printf (_(" Extension opcode arguments:\n"));
6123 for (i = 0; i < count; i++)
6124 {
0c588247 6125 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4ccf1e31 6126 extended_ops[op] = curr;
cd30bcef 6127 READ_ULEB (nargs, curr, end);
4ccf1e31 6128 if (nargs == 0)
7a7e1061 6129 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
4ccf1e31
JJ
6130 else
6131 {
7a7e1061 6132 printf (_(" DW_MACRO_%02x arguments: "), op);
4ccf1e31
JJ
6133 for (n = 0; n < nargs; n++)
6134 {
0c588247
NC
6135 unsigned int form;
6136
6137 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
4ccf1e31
JJ
6138 printf ("%s%s", get_FORM_name (form),
6139 n == nargs - 1 ? "\n" : ", ");
6140 switch (form)
6141 {
6142 case DW_FORM_data1:
6143 case DW_FORM_data2:
6144 case DW_FORM_data4:
6145 case DW_FORM_data8:
6146 case DW_FORM_sdata:
6147 case DW_FORM_udata:
6148 case DW_FORM_block:
6149 case DW_FORM_block1:
6150 case DW_FORM_block2:
6151 case DW_FORM_block4:
6152 case DW_FORM_flag:
6153 case DW_FORM_string:
6154 case DW_FORM_strp:
6155 case DW_FORM_sec_offset:
6156 break;
6157 default:
6158 error (_("Invalid extension opcode form %s\n"),
6159 get_FORM_name (form));
6160 return 0;
6161 }
6162 }
6163 }
6164 }
6165 }
6166 }
6167 printf ("\n");
6168
6169 while (1)
6170 {
6171 unsigned int op;
6172
6173 if (curr >= end)
6174 {
6175 error (_(".debug_macro section not zero terminated\n"));
6176 return 0;
6177 }
6178
0c588247 6179 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4ccf1e31
JJ
6180 if (op == 0)
6181 break;
6182
6183 switch (op)
6184 {
e4b7104b
NC
6185 case DW_MACRO_define:
6186 READ_ULEB (lineno, curr, end);
6187 string = curr;
6188 curr += strnlen ((char *) string, end - string) + 1;
6189 printf (_(" DW_MACRO_define - lineno : %d macro : %s\n"),
6190 lineno, string);
6191 break;
6192
6193 case DW_MACRO_undef:
6194 READ_ULEB (lineno, curr, end);
6195 string = curr;
6196 curr += strnlen ((char *) string, end - string) + 1;
6197 printf (_(" DW_MACRO_undef - lineno : %d macro : %s\n"),
6198 lineno, string);
6199 break;
6200
7a7e1061 6201 case DW_MACRO_start_file:
4ccf1e31
JJ
6202 {
6203 unsigned int filenum;
6204 unsigned char *file_name = NULL, *dir_name = NULL;
6205
cd30bcef
AM
6206 READ_ULEB (lineno, curr, end);
6207 READ_ULEB (filenum, curr, end);
4ccf1e31
JJ
6208
6209 if ((flags & 2) == 0)
7a7e1061 6210 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
4ccf1e31
JJ
6211 else
6212 file_name
6213 = get_line_filename_and_dirname (line_offset, filenum,
6214 &dir_name);
6215 if (file_name == NULL)
7a7e1061 6216 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
4ccf1e31
JJ
6217 lineno, filenum);
6218 else
7a7e1061 6219 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
4ccf1e31
JJ
6220 lineno, filenum,
6221 dir_name != NULL ? (const char *) dir_name : "",
6222 dir_name != NULL ? "/" : "", file_name);
6223 }
6224 break;
6225
7a7e1061
JK
6226 case DW_MACRO_end_file:
6227 printf (_(" DW_MACRO_end_file\n"));
4ccf1e31
JJ
6228 break;
6229
7a7e1061 6230 case DW_MACRO_define_strp:
cd30bcef 6231 READ_ULEB (lineno, curr, end);
d296b736
NC
6232 if (version == 4 && is_dwo)
6233 READ_ULEB (offset, curr, end);
6234 else
6235 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4ccf1e31 6236 string = fetch_indirect_string (offset);
7a7e1061 6237 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
4ccf1e31
JJ
6238 lineno, string);
6239 break;
6240
7a7e1061 6241 case DW_MACRO_undef_strp:
cd30bcef 6242 READ_ULEB (lineno, curr, end);
d296b736
NC
6243 if (version == 4 && is_dwo)
6244 READ_ULEB (offset, curr, end);
6245 else
6246 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4ccf1e31 6247 string = fetch_indirect_string (offset);
7a7e1061 6248 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
4ccf1e31
JJ
6249 lineno, string);
6250 break;
6251
7a7e1061 6252 case DW_MACRO_import:
0c588247 6253 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 6254 printf (_(" DW_MACRO_import - offset : 0x%lx\n"),
4ccf1e31
JJ
6255 (unsigned long) offset);
6256 break;
6257
7a7e1061 6258 case DW_MACRO_define_sup:
cd30bcef 6259 READ_ULEB (lineno, curr, end);
0c588247 6260 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 6261 printf (_(" DW_MACRO_define_sup - lineno : %d macro offset : 0x%lx\n"),
a081f3cd
JJ
6262 lineno, (unsigned long) offset);
6263 break;
6264
7a7e1061 6265 case DW_MACRO_undef_sup:
cd30bcef 6266 READ_ULEB (lineno, curr, end);
0c588247 6267 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 6268 printf (_(" DW_MACRO_undef_sup - lineno : %d macro offset : 0x%lx\n"),
a081f3cd
JJ
6269 lineno, (unsigned long) offset);
6270 break;
6271
7a7e1061 6272 case DW_MACRO_import_sup:
0c588247 6273 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 6274 printf (_(" DW_MACRO_import_sup - offset : 0x%lx\n"),
a081f3cd
JJ
6275 (unsigned long) offset);
6276 break;
6277
e4b7104b
NC
6278 case DW_MACRO_define_strx:
6279 case DW_MACRO_undef_strx:
6280 READ_ULEB (lineno, curr, end);
6281 READ_ULEB (offset, curr, end);
6282 string = (const unsigned char *)
015dc7e1 6283 fetch_indexed_string (offset, NULL, offset_size, false);
e4b7104b
NC
6284 if (op == DW_MACRO_define_strx)
6285 printf (" DW_MACRO_define_strx ");
6286 else
6287 printf (" DW_MACRO_undef_strx ");
6288 if (do_wide)
39f381cb 6289 printf (_("(with offset %s) "), dwarf_vmatoa ("x", offset));
e4b7104b
NC
6290 printf (_("lineno : %d macro : %s\n"),
6291 lineno, string);
6292 break;
6293
4ccf1e31 6294 default:
e4b7104b
NC
6295 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6296 {
6297 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6298 break;
6299 }
6300
4ccf1e31
JJ
6301 if (extended_ops == NULL || extended_ops[op] == NULL)
6302 {
6303 error (_(" Unknown macro opcode %02x seen\n"), op);
6304 return 0;
6305 }
6306 else
6307 {
6308 /* Skip over unhandled opcodes. */
6309 dwarf_vma nargs, n;
6310 unsigned char *desc = extended_ops[op];
cd30bcef 6311 READ_ULEB (nargs, desc, end);
4ccf1e31
JJ
6312 if (nargs == 0)
6313 {
7a7e1061 6314 printf (_(" DW_MACRO_%02x\n"), op);
4ccf1e31
JJ
6315 break;
6316 }
7a7e1061 6317 printf (_(" DW_MACRO_%02x -"), op);
4ccf1e31
JJ
6318 for (n = 0; n < nargs; n++)
6319 {
0c588247
NC
6320 int val;
6321
77145576 6322 /* DW_FORM_implicit_const is not expected here. */
0c588247 6323 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
4ccf1e31 6324 curr
77145576 6325 = read_and_display_attr_value (0, val, 0,
ec1b0fbb 6326 start, curr, end, 0, 0, offset_size,
341f9135 6327 version, NULL, 0, NULL,
ec1b0fbb 6328 NULL, ' ', -1);
4ccf1e31
JJ
6329 if (n != nargs - 1)
6330 printf (",");
6331 }
6332 printf ("\n");
6333 }
6334 break;
6335 }
6336 }
6337
6338 printf ("\n");
b4eb7656 6339 }
4ccf1e31
JJ
6340
6341 return 1;
6342}
6343
19e6b90e
L
6344static int
6345display_debug_abbrev (struct dwarf_section *section,
6346 void *file ATTRIBUTE_UNUSED)
6347{
6348 abbrev_entry *entry;
6349 unsigned char *start = section->start;
19e6b90e 6350
015dc7e1 6351 introduce (section, false);
19e6b90e
L
6352
6353 do
6354 {
bcd213b2
NC
6355 abbrev_list * list;
6356 dwarf_vma offset;
7282333f 6357
bcd213b2 6358 offset = start - section->start;
76868f36 6359 list = find_abbrev_list_by_abbrev_offset (0, offset);
bcd213b2
NC
6360 if (list == NULL)
6361 {
76868f36 6362 list = new_abbrev_list (0, offset);
f2f9554b 6363 start = process_abbrev_set (section, 0, section->size, offset, list);
bcd213b2
NC
6364 list->start_of_next_abbrevs = start;
6365 }
6366 else
6367 start = list->start_of_next_abbrevs;
19e6b90e 6368
bcd213b2 6369 if (list->first_abbrev == NULL)
19e6b90e
L
6370 continue;
6371
bcd213b2 6372 printf (_(" Number TAG (0x%lx)\n"), (long) offset);
19e6b90e 6373
bcd213b2 6374 for (entry = list->first_abbrev; entry; entry = entry->next)
19e6b90e
L
6375 {
6376 abbrev_attr *attr;
6377
cc5914eb 6378 printf (" %ld %s [%s]\n",
bcd213b2 6379 entry->number,
19e6b90e
L
6380 get_TAG_name (entry->tag),
6381 entry->children ? _("has children") : _("no children"));
6382
6383 for (attr = entry->first_attr; attr; attr = attr->next)
77145576
JK
6384 {
6385 printf (" %-18s %s",
6386 get_AT_name (attr->attribute),
6387 get_FORM_name (attr->form));
6388 if (attr->form == DW_FORM_implicit_const)
6389 printf (": %" BFD_VMA_FMT "d", attr->implicit_const);
6390 putchar ('\n');
6391 }
19e6b90e
L
6392 }
6393 }
6394 while (start);
6395
6396 printf ("\n");
6397
6398 return 1;
6399}
6400
42bcef4a
AB
6401/* Return true when ADDR is the maximum address, when addresses are
6402 POINTER_SIZE bytes long. */
6403
015dc7e1 6404static bool
42bcef4a
AB
6405is_max_address (dwarf_vma addr, unsigned int pointer_size)
6406{
6407 dwarf_vma mask = ~(~(dwarf_vma) 1 << (pointer_size * 8 - 1));
6408 return ((addr & mask) == mask);
6409}
6410
9f272209
AO
6411/* Display a view pair list starting at *VSTART_PTR and ending at
6412 VLISTEND within SECTION. */
6413
6414static void
6415display_view_pair_list (struct dwarf_section *section,
6416 unsigned char **vstart_ptr,
6417 unsigned int debug_info_entry,
6418 unsigned char *vlistend)
6419{
6420 unsigned char *vstart = *vstart_ptr;
6421 unsigned char *section_end = section->start + section->size;
6422 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6423
6424 if (vlistend < section_end)
6425 section_end = vlistend;
6426
6427 putchar ('\n');
6428
6429 while (vstart < section_end)
6430 {
6431 dwarf_vma off = vstart - section->start;
6432 dwarf_vma vbegin, vend;
6433
cd30bcef 6434 READ_ULEB (vbegin, vstart, section_end);
9f272209 6435 if (vstart == section_end)
cd30bcef 6436 break;
9f272209 6437
cd30bcef 6438 READ_ULEB (vend, vstart, section_end);
9f272209
AO
6439 printf (" %8.8lx ", (unsigned long) off);
6440
6441 print_dwarf_view (vbegin, pointer_size, 1);
6442 print_dwarf_view (vend, pointer_size, 1);
6443 printf (_("location view pair\n"));
6444 }
6445
6446 putchar ('\n');
6447 *vstart_ptr = vstart;
6448}
6449
4723351a
CC
6450/* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6451
6452static void
6453display_loc_list (struct dwarf_section *section,
b4eb7656
AM
6454 unsigned char **start_ptr,
6455 unsigned int debug_info_entry,
359ca075
JK
6456 dwarf_vma offset,
6457 dwarf_vma base_address,
9f272209 6458 unsigned char **vstart_ptr,
b4eb7656 6459 int has_frame_base)
4723351a 6460{
9f272209 6461 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
4723351a 6462 unsigned char *section_end = section->start + section->size;
82b1b41b
NC
6463 unsigned long cu_offset;
6464 unsigned int pointer_size;
6465 unsigned int offset_size;
6466 int dwarf_version;
4723351a
CC
6467
6468 dwarf_vma begin;
6469 dwarf_vma end;
6470 unsigned short length;
6471 int need_frame_base;
6472
82b1b41b
NC
6473 if (debug_info_entry >= num_debug_info_entries)
6474 {
6475 warn (_("No debug information available for loc lists of entry: %u\n"),
6476 debug_info_entry);
6477 return;
6478 }
b4eb7656 6479
82b1b41b
NC
6480 cu_offset = debug_information [debug_info_entry].cu_offset;
6481 pointer_size = debug_information [debug_info_entry].pointer_size;
6482 offset_size = debug_information [debug_info_entry].offset_size;
6483 dwarf_version = debug_information [debug_info_entry].dwarf_version;
b4eb7656 6484
f41e4712
NC
6485 if (pointer_size < 2 || pointer_size > 8)
6486 {
6487 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6488 pointer_size, debug_info_entry);
6489 return;
6490 }
6491
4723351a
CC
6492 while (1)
6493 {
359ca075 6494 dwarf_vma off = offset + (start - *start_ptr);
9f272209 6495 dwarf_vma vbegin = vm1, vend = vm1;
d1c4b12b 6496
4723351a 6497 if (start + 2 * pointer_size > section_end)
b4eb7656
AM
6498 {
6499 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6500 (unsigned long) offset);
b4eb7656
AM
6501 break;
6502 }
4723351a 6503
359ca075 6504 printf (" %8.8lx ", (unsigned long) off);
fab128ef 6505
0c588247
NC
6506 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6507 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
4723351a 6508
4723351a 6509 if (begin == 0 && end == 0)
b4eb7656 6510 {
d1c4b12b
NC
6511 /* PR 18374: In a object file we can have a location list that
6512 starts with a begin and end of 0 because there are relocations
6513 that need to be applied to the addresses. Actually applying
6514 the relocations now does not help as they will probably resolve
6515 to 0, since the object file has not been fully linked. Real
6516 end of list markers will not have any relocations against them. */
6517 if (! reloc_at (section, off)
6518 && ! reloc_at (section, off + pointer_size))
6519 {
6520 printf (_("<End of list>\n"));
6521 break;
6522 }
b4eb7656 6523 }
4723351a
CC
6524
6525 /* Check base address specifiers. */
42bcef4a
AB
6526 if (is_max_address (begin, pointer_size)
6527 && !is_max_address (end, pointer_size))
b4eb7656
AM
6528 {
6529 base_address = end;
6530 print_dwarf_vma (begin, pointer_size);
6531 print_dwarf_vma (end, pointer_size);
6532 printf (_("(base address)\n"));
6533 continue;
6534 }
4723351a 6535
9f272209
AO
6536 if (vstart)
6537 {
9f272209
AO
6538 off = offset + (vstart - *start_ptr);
6539
cd30bcef 6540 READ_ULEB (vbegin, vstart, section_end);
9f272209
AO
6541 print_dwarf_view (vbegin, pointer_size, 1);
6542
cd30bcef 6543 READ_ULEB (vend, vstart, section_end);
9f272209
AO
6544 print_dwarf_view (vend, pointer_size, 1);
6545
6546 printf (_("views at %8.8lx for:\n %*s "),
6547 (unsigned long) off, 8, "");
6548 }
6549
4723351a 6550 if (start + 2 > section_end)
b4eb7656
AM
6551 {
6552 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6553 (unsigned long) offset);
b4eb7656
AM
6554 break;
6555 }
4723351a 6556
0c588247 6557 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4723351a
CC
6558
6559 if (start + length > section_end)
b4eb7656
AM
6560 {
6561 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6562 (unsigned long) offset);
b4eb7656
AM
6563 break;
6564 }
4723351a
CC
6565
6566 print_dwarf_vma (begin + base_address, pointer_size);
6567 print_dwarf_vma (end + base_address, pointer_size);
6568
6569 putchar ('(');
6570 need_frame_base = decode_location_expression (start,
b4eb7656
AM
6571 pointer_size,
6572 offset_size,
6573 dwarf_version,
6574 length,
6575 cu_offset, section);
4723351a
CC
6576 putchar (')');
6577
6578 if (need_frame_base && !has_frame_base)
b4eb7656 6579 printf (_(" [without DW_AT_frame_base]"));
4723351a 6580
9f272209 6581 if (begin == end && vbegin == vend)
b4eb7656 6582 fputs (_(" (start == end)"), stdout);
9f272209 6583 else if (begin > end || (begin == end && vbegin > vend))
b4eb7656 6584 fputs (_(" (start > end)"), stdout);
4723351a
CC
6585
6586 putchar ('\n');
6587
6588 start += length;
6589 }
6590
6591 *start_ptr = start;
9f272209 6592 *vstart_ptr = vstart;
4723351a
CC
6593}
6594
77145576
JK
6595/* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6596
6597static void
6598display_loclists_list (struct dwarf_section *section,
6599 unsigned char **start_ptr,
6600 unsigned int debug_info_entry,
6601 dwarf_vma offset,
6602 dwarf_vma base_address,
9f272209 6603 unsigned char **vstart_ptr,
77145576
JK
6604 int has_frame_base)
6605{
9f272209 6606 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
77145576
JK
6607 unsigned char *section_end = section->start + section->size;
6608 unsigned long cu_offset;
6609 unsigned int pointer_size;
6610 unsigned int offset_size;
6611 int dwarf_version;
77145576 6612
9dfd0db9 6613 /* Initialize it due to a false compiler warning. */
9f272209
AO
6614 dwarf_vma begin = -1, vbegin = -1;
6615 dwarf_vma end = -1, vend = -1;
77145576
JK
6616 dwarf_vma length;
6617 int need_frame_base;
6618
6619 if (debug_info_entry >= num_debug_info_entries)
6620 {
6621 warn (_("No debug information available for "
6622 "loclists lists of entry: %u\n"),
6623 debug_info_entry);
6624 return;
6625 }
6626
6627 cu_offset = debug_information [debug_info_entry].cu_offset;
6628 pointer_size = debug_information [debug_info_entry].pointer_size;
6629 offset_size = debug_information [debug_info_entry].offset_size;
6630 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6631
6632 if (pointer_size < 2 || pointer_size > 8)
6633 {
6634 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6635 pointer_size, debug_info_entry);
6636 return;
6637 }
6638
6639 while (1)
6640 {
6641 dwarf_vma off = offset + (start - *start_ptr);
6642 enum dwarf_location_list_entry_type llet;
6643
6644 if (start + 1 > section_end)
6645 {
6646 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6647 (unsigned long) offset);
6648 break;
6649 }
6650
6651 printf (" %8.8lx ", (unsigned long) off);
6652
6653 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6654
1c9f770d
MW
6655 if (vstart && (llet == DW_LLE_offset_pair
6656 || llet == DW_LLE_start_end
6657 || llet == DW_LLE_start_length))
9f272209
AO
6658 {
6659 off = offset + (vstart - *start_ptr);
6660
cd30bcef 6661 READ_ULEB (vbegin, vstart, section_end);
9f272209
AO
6662 print_dwarf_view (vbegin, pointer_size, 1);
6663
cd30bcef 6664 READ_ULEB (vend, vstart, section_end);
9f272209
AO
6665 print_dwarf_view (vend, pointer_size, 1);
6666
6667 printf (_("views at %8.8lx for:\n %*s "),
6668 (unsigned long) off, 8, "");
6669 }
6670
77145576
JK
6671 switch (llet)
6672 {
6673 case DW_LLE_end_of_list:
6674 printf (_("<End of list>\n"));
6675 break;
6676 case DW_LLE_offset_pair:
cd30bcef 6677 READ_ULEB (begin, start, section_end);
1c9f770d 6678 begin += base_address;
cd30bcef 6679 READ_ULEB (end, start, section_end);
1c9f770d
MW
6680 end += base_address;
6681 break;
6682 case DW_LLE_start_end:
6683 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6684 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6685 break;
6686 case DW_LLE_start_length:
6687 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6688 READ_ULEB (end, start, section_end);
6689 end += begin;
77145576
JK
6690 break;
6691 case DW_LLE_base_address:
6692 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6693 section_end);
6694 print_dwarf_vma (base_address, pointer_size);
6695 printf (_("(base address)\n"));
6696 break;
9f272209
AO
6697#ifdef DW_LLE_view_pair
6698 case DW_LLE_view_pair:
6699 if (vstart)
6700 printf (_("View pair entry in loclist with locviews attribute\n"));
cd30bcef 6701 READ_ULEB (vbegin, start, section_end);
9f272209
AO
6702 print_dwarf_view (vbegin, pointer_size, 1);
6703
cd30bcef 6704 READ_ULEB (vend, start, section_end);
9f272209
AO
6705 print_dwarf_view (vend, pointer_size, 1);
6706
6707 printf (_("views for:\n"));
6708 continue;
6709#endif
77145576
JK
6710 default:
6711 error (_("Invalid location list entry type %d\n"), llet);
6712 return;
6713 }
6714 if (llet == DW_LLE_end_of_list)
6715 break;
1c9f770d
MW
6716 if (llet != DW_LLE_offset_pair
6717 && llet != DW_LLE_start_end
6718 && llet != DW_LLE_start_length)
77145576
JK
6719 continue;
6720
6721 if (start + 2 > section_end)
6722 {
6723 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6724 (unsigned long) offset);
6725 break;
6726 }
6727
cd30bcef 6728 READ_ULEB (length, start, section_end);
77145576 6729
1c9f770d
MW
6730 print_dwarf_vma (begin, pointer_size);
6731 print_dwarf_vma (end, pointer_size);
77145576
JK
6732
6733 putchar ('(');
6734 need_frame_base = decode_location_expression (start,
6735 pointer_size,
6736 offset_size,
6737 dwarf_version,
6738 length,
6739 cu_offset, section);
6740 putchar (')');
6741
6742 if (need_frame_base && !has_frame_base)
6743 printf (_(" [without DW_AT_frame_base]"));
6744
9f272209 6745 if (begin == end && vbegin == vend)
77145576 6746 fputs (_(" (start == end)"), stdout);
9f272209 6747 else if (begin > end || (begin == end && vbegin > vend))
77145576
JK
6748 fputs (_(" (start > end)"), stdout);
6749
6750 putchar ('\n');
6751
6752 start += length;
9f272209 6753 vbegin = vend = -1;
77145576
JK
6754 }
6755
9f272209
AO
6756 if (vbegin != vm1 || vend != vm1)
6757 printf (_("Trailing view pair not used in a range"));
6758
77145576 6759 *start_ptr = start;
9f272209 6760 *vstart_ptr = vstart;
77145576
JK
6761}
6762
fab128ef
CC
6763/* Print a .debug_addr table index in decimal, surrounded by square brackets,
6764 right-adjusted in a field of length LEN, and followed by a space. */
6765
6766static void
6767print_addr_index (unsigned int idx, unsigned int len)
6768{
6769 static char buf[15];
6770 snprintf (buf, sizeof (buf), "[%d]", idx);
341f9135 6771 printf ("%*s ", len, buf);
fab128ef
CC
6772}
6773
4723351a
CC
6774/* Display a location list from a .dwo section. It uses address indexes rather
6775 than embedded addresses. This code closely follows display_loc_list, but the
6776 two are sufficiently different that combining things is very ugly. */
6777
6778static void
6779display_loc_list_dwo (struct dwarf_section *section,
b4eb7656
AM
6780 unsigned char **start_ptr,
6781 unsigned int debug_info_entry,
359ca075 6782 dwarf_vma offset,
9f272209 6783 unsigned char **vstart_ptr,
b4eb7656 6784 int has_frame_base)
4723351a 6785{
9f272209 6786 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
4723351a 6787 unsigned char *section_end = section->start + section->size;
82b1b41b
NC
6788 unsigned long cu_offset;
6789 unsigned int pointer_size;
6790 unsigned int offset_size;
6791 int dwarf_version;
4723351a
CC
6792 int entry_type;
6793 unsigned short length;
6794 int need_frame_base;
fab128ef 6795 unsigned int idx;
4723351a 6796
82b1b41b
NC
6797 if (debug_info_entry >= num_debug_info_entries)
6798 {
6799 warn (_("No debug information for loc lists of entry: %u\n"),
6800 debug_info_entry);
6801 return;
6802 }
6803
6804 cu_offset = debug_information [debug_info_entry].cu_offset;
6805 pointer_size = debug_information [debug_info_entry].pointer_size;
6806 offset_size = debug_information [debug_info_entry].offset_size;
6807 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6808
f41e4712
NC
6809 if (pointer_size < 2 || pointer_size > 8)
6810 {
6811 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6812 pointer_size, debug_info_entry);
6813 return;
6814 }
6815
4723351a
CC
6816 while (1)
6817 {
359ca075 6818 printf (" %8.8lx ", (unsigned long) (offset + (start - *start_ptr)));
4723351a 6819
fab128ef 6820 if (start >= section_end)
b4eb7656
AM
6821 {
6822 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6823 (unsigned long) offset);
b4eb7656
AM
6824 break;
6825 }
4723351a 6826
0c588247 6827 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
9f272209
AO
6828
6829 if (vstart)
6830 switch (entry_type)
6831 {
6832 default:
6833 break;
6834
6835 case 2:
6836 case 3:
6837 case 4:
6838 {
6839 dwarf_vma view;
6840 dwarf_vma off = offset + (vstart - *start_ptr);
6841
cd30bcef 6842 READ_ULEB (view, vstart, section_end);
9f272209
AO
6843 print_dwarf_view (view, 8, 1);
6844
cd30bcef 6845 READ_ULEB (view, vstart, section_end);
9f272209
AO
6846 print_dwarf_view (view, 8, 1);
6847
6848 printf (_("views at %8.8lx for:\n %*s "),
6849 (unsigned long) off, 8, "");
6850
6851 }
6852 break;
6853 }
6854
4723351a 6855 switch (entry_type)
b4eb7656
AM
6856 {
6857 case 0: /* A terminating entry. */
6858 *start_ptr = start;
9f272209 6859 *vstart_ptr = vstart;
b4eb7656
AM
6860 printf (_("<End of list>\n"));
6861 return;
6862 case 1: /* A base-address entry. */
cd30bcef 6863 READ_ULEB (idx, start, section_end);
b4eb7656 6864 print_addr_index (idx, 8);
9f272209 6865 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
b4eb7656
AM
6866 printf (_("(base address selection entry)\n"));
6867 continue;
6868 case 2: /* A start/end entry. */
cd30bcef 6869 READ_ULEB (idx, start, section_end);
b4eb7656 6870 print_addr_index (idx, 8);
cd30bcef 6871 READ_ULEB (idx, start, section_end);
b4eb7656
AM
6872 print_addr_index (idx, 8);
6873 break;
6874 case 3: /* A start/length entry. */
cd30bcef 6875 READ_ULEB (idx, start, section_end);
b4eb7656
AM
6876 print_addr_index (idx, 8);
6877 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6878 printf ("%08x ", idx);
6879 break;
6880 case 4: /* An offset pair entry. */
6881 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6882 printf ("%08x ", idx);
6883 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6884 printf ("%08x ", idx);
6885 break;
6886 default:
6887 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6888 *start_ptr = start;
9f272209 6889 *vstart_ptr = vstart;
b4eb7656
AM
6890 return;
6891 }
4723351a
CC
6892
6893 if (start + 2 > section_end)
b4eb7656
AM
6894 {
6895 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6896 (unsigned long) offset);
b4eb7656
AM
6897 break;
6898 }
4723351a 6899
0c588247 6900 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4723351a 6901 if (start + length > section_end)
b4eb7656
AM
6902 {
6903 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6904 (unsigned long) offset);
b4eb7656
AM
6905 break;
6906 }
4723351a
CC
6907
6908 putchar ('(');
6909 need_frame_base = decode_location_expression (start,
b4eb7656
AM
6910 pointer_size,
6911 offset_size,
6912 dwarf_version,
6913 length,
6914 cu_offset, section);
4723351a
CC
6915 putchar (')');
6916
6917 if (need_frame_base && !has_frame_base)
b4eb7656 6918 printf (_(" [without DW_AT_frame_base]"));
4723351a
CC
6919
6920 putchar ('\n');
6921
6922 start += length;
6923 }
6924
6925 *start_ptr = start;
9f272209 6926 *vstart_ptr = vstart;
4723351a
CC
6927}
6928
9f272209
AO
6929/* Sort array of indexes in ascending order of loc_offsets[idx] and
6930 loc_views. */
51d0d03f 6931
9f272209 6932static dwarf_vma *loc_offsets, *loc_views;
51d0d03f
JJ
6933
6934static int
6935loc_offsets_compar (const void *ap, const void *bp)
6936{
6937 dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
6938 dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
6939
9f272209
AO
6940 int ret = (a > b) - (b > a);
6941 if (ret)
6942 return ret;
6943
6944 a = loc_views[*(const unsigned int *) ap];
6945 b = loc_views[*(const unsigned int *) bp];
6946
6947 ret = (a > b) - (b > a);
6948
6949 return ret;
51d0d03f
JJ
6950}
6951
19e6b90e
L
6952static int
6953display_debug_loc (struct dwarf_section *section, void *file)
6954{
9f272209 6955 unsigned char *start = section->start, *vstart = NULL;
19e6b90e
L
6956 unsigned long bytes;
6957 unsigned char *section_begin = start;
6958 unsigned int num_loc_list = 0;
6959 unsigned long last_offset = 0;
9f272209 6960 unsigned long last_view = 0;
19e6b90e
L
6961 unsigned int first = 0;
6962 unsigned int i;
6963 unsigned int j;
6964 int seen_first_offset = 0;
51d0d03f 6965 int locs_sorted = 1;
9f272209 6966 unsigned char *next = start, *vnext = vstart;
51d0d03f 6967 unsigned int *array = NULL;
4723351a 6968 const char *suffix = strrchr (section->name, '.');
015dc7e1 6969 bool is_dwo = false;
77145576
JK
6970 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
6971 dwarf_vma expected_start = 0;
4723351a
CC
6972
6973 if (suffix && strcmp (suffix, ".dwo") == 0)
015dc7e1 6974 is_dwo = true;
19e6b90e
L
6975
6976 bytes = section->size;
19e6b90e
L
6977
6978 if (bytes == 0)
6979 {
6980 printf (_("\nThe %s section is empty.\n"), section->name);
6981 return 0;
6982 }
6983
77145576
JK
6984 if (is_loclists)
6985 {
6986 unsigned char *hdrptr = section_begin;
6987 dwarf_vma ll_length;
6988 unsigned short ll_version;
6989 unsigned char *end = section_begin + section->size;
6990 unsigned char address_size, segment_selector_size;
6991 uint32_t offset_entry_count;
6992
6993 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
6994 if (ll_length == 0xffffffff)
6995 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
6996
6997 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
6998 if (ll_version != 5)
6999 {
7000 warn (_("The %s section contains corrupt or "
7001 "unsupported version number: %d.\n"),
7002 section->name, ll_version);
7003 return 0;
7004 }
7005
7006 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7007
7008 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7009 if (segment_selector_size != 0)
7010 {
7011 warn (_("The %s section contains "
7012 "unsupported segment selector size: %d.\n"),
7013 section->name, segment_selector_size);
7014 return 0;
7015 }
7016
7017 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7018 if (offset_entry_count != 0)
7019 {
7020 warn (_("The %s section contains "
7021 "unsupported offset entry count: %d.\n"),
7022 section->name, offset_entry_count);
7023 return 0;
7024 }
7025
7026 expected_start = hdrptr - section_begin;
7027 }
7028
1febe64d
NC
7029 if (load_debug_info (file) == 0)
7030 {
7031 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7032 section->name);
7033 return 0;
7034 }
19e6b90e
L
7035
7036 /* Check the order of location list in .debug_info section. If
7037 offsets of location lists are in the ascending order, we can
7038 use `debug_information' directly. */
7039 for (i = 0; i < num_debug_info_entries; i++)
7040 {
7041 unsigned int num;
7042
7043 num = debug_information [i].num_loc_offsets;
51d0d03f
JJ
7044 if (num > num_loc_list)
7045 num_loc_list = num;
19e6b90e
L
7046
7047 /* Check if we can use `debug_information' directly. */
51d0d03f 7048 if (locs_sorted && num != 0)
19e6b90e
L
7049 {
7050 if (!seen_first_offset)
7051 {
7052 /* This is the first location list. */
7053 last_offset = debug_information [i].loc_offsets [0];
9f272209 7054 last_view = debug_information [i].loc_views [0];
19e6b90e
L
7055 first = i;
7056 seen_first_offset = 1;
7057 j = 1;
7058 }
7059 else
7060 j = 0;
7061
7062 for (; j < num; j++)
7063 {
7064 if (last_offset >
9f272209
AO
7065 debug_information [i].loc_offsets [j]
7066 || (last_offset == debug_information [i].loc_offsets [j]
7067 && last_view > debug_information [i].loc_views [j]))
19e6b90e 7068 {
51d0d03f 7069 locs_sorted = 0;
19e6b90e
L
7070 break;
7071 }
7072 last_offset = debug_information [i].loc_offsets [j];
9f272209 7073 last_view = debug_information [i].loc_views [j];
19e6b90e
L
7074 }
7075 }
7076 }
7077
19e6b90e
L
7078 if (!seen_first_offset)
7079 error (_("No location lists in .debug_info section!\n"));
7080
d4bfc77b 7081 if (debug_information [first].num_loc_offsets > 0
9f272209
AO
7082 && debug_information [first].loc_offsets [0] != expected_start
7083 && debug_information [first].loc_views [0] != expected_start)
47704ddf
KT
7084 warn (_("Location lists in %s section start at 0x%s\n"),
7085 section->name,
7086 dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
19e6b90e 7087
51d0d03f
JJ
7088 if (!locs_sorted)
7089 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
dda8d76d 7090
015dc7e1 7091 introduce (section, false);
dda8d76d 7092
d1c4b12b
NC
7093 if (reloc_at (section, 0))
7094 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
dda8d76d 7095
d1c4b12b 7096 printf (_(" Offset Begin End Expression\n"));
19e6b90e
L
7097
7098 seen_first_offset = 0;
7099 for (i = first; i < num_debug_info_entries; i++)
7100 {
9f272209 7101 dwarf_vma offset, voffset;
359ca075 7102 dwarf_vma base_address;
d1c4b12b 7103 unsigned int k;
19e6b90e
L
7104 int has_frame_base;
7105
51d0d03f
JJ
7106 if (!locs_sorted)
7107 {
7108 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7109 array[k] = k;
7110 loc_offsets = debug_information [i].loc_offsets;
9f272209 7111 loc_views = debug_information [i].loc_views;
51d0d03f
JJ
7112 qsort (array, debug_information [i].num_loc_offsets,
7113 sizeof (*array), loc_offsets_compar);
7114 }
19e6b90e 7115
9f272209 7116 int adjacent_view_loclists = 1;
51d0d03f 7117 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
19e6b90e 7118 {
51d0d03f
JJ
7119 j = locs_sorted ? k : array[k];
7120 if (k
9f272209 7121 && (debug_information [i].loc_offsets [locs_sorted
51d0d03f 7122 ? k - 1 : array [k - 1]]
9f272209
AO
7123 == debug_information [i].loc_offsets [j])
7124 && (debug_information [i].loc_views [locs_sorted
7125 ? k - 1 : array [k - 1]]
7126 == debug_information [i].loc_views [j]))
51d0d03f 7127 continue;
19e6b90e 7128 has_frame_base = debug_information [i].have_frame_base [j];
d493b283 7129 offset = debug_information [i].loc_offsets [j];
19e6b90e 7130 next = section_begin + offset;
9f272209
AO
7131 voffset = debug_information [i].loc_views [j];
7132 if (voffset != vm1)
7133 vnext = section_begin + voffset;
7134 else
7135 vnext = NULL;
19e6b90e
L
7136 base_address = debug_information [i].base_address;
7137
9f272209
AO
7138 if (vnext && vnext < next)
7139 {
7140 vstart = vnext;
7141 display_view_pair_list (section, &vstart, i, next);
7142 if (start == vnext)
7143 start = vstart;
7144 }
7145
7146 if (!seen_first_offset || !adjacent_view_loclists)
19e6b90e
L
7147 seen_first_offset = 1;
7148 else
7149 {
7150 if (start < next)
7151 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
0af1713e 7152 (unsigned long) (start - section_begin),
c8071705 7153 (unsigned long) offset);
19e6b90e
L
7154 else if (start > next)
7155 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
0af1713e 7156 (unsigned long) (start - section_begin),
c8071705 7157 (unsigned long) offset);
19e6b90e
L
7158 }
7159 start = next;
9f272209 7160 vstart = vnext;
19e6b90e
L
7161
7162 if (offset >= bytes)
7163 {
7164 warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
359ca075 7165 (unsigned long) offset);
19e6b90e
L
7166 continue;
7167 }
7168
9f272209
AO
7169 if (vnext && voffset >= bytes)
7170 {
7171 warn (_("View Offset 0x%lx is bigger than .debug_loc section size.\n"),
7172 (unsigned long) voffset);
7173 continue;
7174 }
7175
77145576
JK
7176 if (!is_loclists)
7177 {
7178 if (is_dwo)
7179 display_loc_list_dwo (section, &start, i, offset,
9f272209 7180 &vstart, has_frame_base);
77145576
JK
7181 else
7182 display_loc_list (section, &start, i, offset, base_address,
9f272209 7183 &vstart, has_frame_base);
77145576 7184 }
b4eb7656 7185 else
77145576
JK
7186 {
7187 if (is_dwo)
7188 warn (_("DWO is not yet supported.\n"));
7189 else
7190 display_loclists_list (section, &start, i, offset, base_address,
9f272209
AO
7191 &vstart, has_frame_base);
7192 }
7193
7194 /* FIXME: this arrangement is quite simplistic. Nothing
7195 requires locview lists to be adjacent to corresponding
7196 loclists, and a single loclist could be augmented by
7197 different locview lists, and vice-versa, unlikely as it
7198 is that it would make sense to do so. Hopefully we'll
7199 have view pair support built into loclists before we ever
7200 need to address all these possibilities. */
7201 if (adjacent_view_loclists && vnext
7202 && vnext != start && vstart != next)
7203 {
7204 adjacent_view_loclists = 0;
7205 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
77145576 7206 }
9f272209
AO
7207
7208 if (vnext && vnext == start)
7209 display_view_pair_list (section, &start, i, vstart);
19e6b90e
L
7210 }
7211 }
031cd65f 7212
4723351a 7213 if (start < section->start + section->size)
d3a49aa8
AM
7214 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7215 "There are %ld unused bytes at the end of section %s\n",
7216 (long) (section->start + section->size - start)),
4723351a 7217 (long) (section->start + section->size - start), section->name);
98fb390a 7218 putchar ('\n');
51d0d03f 7219 free (array);
19e6b90e
L
7220 return 1;
7221}
7222
7223static int
7224display_debug_str (struct dwarf_section *section,
7225 void *file ATTRIBUTE_UNUSED)
7226{
7227 unsigned char *start = section->start;
7228 unsigned long bytes = section->size;
7229 dwarf_vma addr = section->address;
7230
7231 if (bytes == 0)
7232 {
7233 printf (_("\nThe %s section is empty.\n"), section->name);
7234 return 0;
7235 }
7236
015dc7e1 7237 introduce (section, false);
19e6b90e
L
7238
7239 while (bytes)
7240 {
7241 int j;
7242 int k;
7243 int lbytes;
7244
7245 lbytes = (bytes > 16 ? 16 : bytes);
7246
7247 printf (" 0x%8.8lx ", (unsigned long) addr);
7248
7249 for (j = 0; j < 16; j++)
7250 {
7251 if (j < lbytes)
7252 printf ("%2.2x", start[j]);
7253 else
7254 printf (" ");
7255
7256 if ((j & 3) == 3)
7257 printf (" ");
7258 }
7259
7260 for (j = 0; j < lbytes; j++)
7261 {
7262 k = start[j];
7263 if (k >= ' ' && k < 0x80)
7264 printf ("%c", k);
7265 else
7266 printf (".");
7267 }
7268
7269 putchar ('\n');
7270
7271 start += lbytes;
7272 addr += lbytes;
7273 bytes -= lbytes;
7274 }
7275
7276 putchar ('\n');
7277
7278 return 1;
7279}
7280
19e6b90e
L
7281static int
7282display_debug_info (struct dwarf_section *section, void *file)
7283{
015dc7e1 7284 return process_debug_info (section, file, section->abbrev_sec, false, false);
19e6b90e
L
7285}
7286
2b6f5997
CC
7287static int
7288display_debug_types (struct dwarf_section *section, void *file)
7289{
015dc7e1 7290 return process_debug_info (section, file, section->abbrev_sec, false, true);
6f875884
TG
7291}
7292
7293static int
7294display_trace_info (struct dwarf_section *section, void *file)
7295{
015dc7e1 7296 return process_debug_info (section, file, section->abbrev_sec, false, true);
2b6f5997 7297}
19e6b90e
L
7298
7299static int
7300display_debug_aranges (struct dwarf_section *section,
7301 void *file ATTRIBUTE_UNUSED)
7302{
7303 unsigned char *start = section->start;
7304 unsigned char *end = start + section->size;
7305
015dc7e1 7306 introduce (section, false);
19e6b90e 7307
6e3d6dc1
NC
7308 /* It does not matter if this load fails,
7309 we test for that later on. */
7310 load_debug_info (file);
7311
19e6b90e
L
7312 while (start < end)
7313 {
7314 unsigned char *hdrptr;
7315 DWARF2_Internal_ARange arange;
91d6fa6a 7316 unsigned char *addr_ranges;
2d9472a2
NC
7317 dwarf_vma length;
7318 dwarf_vma address;
e98fdf1a 7319 unsigned long sec_off;
53b8873b 7320 unsigned char address_size;
19e6b90e 7321 int excess;
bf5117e3
NC
7322 unsigned int offset_size;
7323 unsigned int initial_length_size;
19e6b90e
L
7324
7325 hdrptr = start;
7326
0c588247 7327 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
19e6b90e
L
7328 if (arange.ar_length == 0xffffffff)
7329 {
0c588247 7330 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
19e6b90e
L
7331 offset_size = 8;
7332 initial_length_size = 12;
7333 }
7334 else
7335 {
7336 offset_size = 4;
7337 initial_length_size = 4;
7338 }
7339
e98fdf1a
AM
7340 sec_off = hdrptr - section->start;
7341 if (sec_off + arange.ar_length < sec_off
7342 || sec_off + arange.ar_length > section->size)
7343 {
7344 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
7345 section->name,
7346 sec_off - initial_length_size,
7347 dwarf_vmatoa ("x", arange.ar_length));
7348 break;
7349 }
7350
0c588247
NC
7351 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end);
7352 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size, end);
19e6b90e 7353
6e3d6dc1
NC
7354 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7355 && num_debug_info_entries > 0
7356 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7357 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
47704ddf 7358 (unsigned long) arange.ar_info_offset, section->name);
6e3d6dc1 7359
0c588247
NC
7360 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end);
7361 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end);
19e6b90e
L
7362
7363 if (arange.ar_version != 2 && arange.ar_version != 3)
7364 {
67f101ee
NC
7365 /* PR 19872: A version number of 0 probably means that there is
7366 padding at the end of the .debug_aranges section. Gold puts
7367 it there when performing an incremental link, for example.
7368 So do not generate a warning in this case. */
7369 if (arange.ar_version)
7370 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
19e6b90e
L
7371 break;
7372 }
7373
47704ddf
KT
7374 printf (_(" Length: %ld\n"),
7375 (long) arange.ar_length);
19e6b90e 7376 printf (_(" Version: %d\n"), arange.ar_version);
47704ddf
KT
7377 printf (_(" Offset into .debug_info: 0x%lx\n"),
7378 (unsigned long) arange.ar_info_offset);
19e6b90e
L
7379 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7380 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7381
53b8873b
NC
7382 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7383
f41e4712
NC
7384 /* PR 17512: file: 001-108546-0.001:0.1. */
7385 if (address_size == 0 || address_size > 8)
b3681d67
L
7386 {
7387 error (_("Invalid address size in %s section!\n"),
7388 section->name);
7389 break;
7390 }
7391
53b8873b
NC
7392 /* The DWARF spec does not require that the address size be a power
7393 of two, but we do. This will have to change if we ever encounter
7394 an uneven architecture. */
7395 if ((address_size & (address_size - 1)) != 0)
7396 {
7397 warn (_("Pointer size + Segment size is not a power of two.\n"));
7398 break;
7399 }
cecf136e 7400
209c9a13
NC
7401 if (address_size > 4)
7402 printf (_("\n Address Length\n"));
7403 else
7404 printf (_("\n Address Length\n"));
19e6b90e 7405
91d6fa6a 7406 addr_ranges = hdrptr;
19e6b90e 7407
53b8873b
NC
7408 /* Must pad to an alignment boundary that is twice the address size. */
7409 excess = (hdrptr - start) % (2 * address_size);
19e6b90e 7410 if (excess)
91d6fa6a 7411 addr_ranges += (2 * address_size) - excess;
19e6b90e 7412
e98fdf1a 7413 start += arange.ar_length + initial_length_size;
1617e571 7414
91d6fa6a 7415 while (addr_ranges + 2 * address_size <= start)
19e6b90e 7416 {
0c588247
NC
7417 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size, end);
7418 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size, end);
19e6b90e 7419
80c35038 7420 printf (" ");
2d9472a2
NC
7421 print_dwarf_vma (address, address_size);
7422 print_dwarf_vma (length, address_size);
7423 putchar ('\n');
19e6b90e 7424 }
19e6b90e
L
7425 }
7426
7427 printf ("\n");
7428
7429 return 1;
7430}
7431
4723351a
CC
7432/* Comparison function for qsort. */
7433static int
7434comp_addr_base (const void * v0, const void * v1)
7435{
d367307b
AM
7436 debug_info *info0 = *(debug_info **) v0;
7437 debug_info *info1 = *(debug_info **) v1;
4723351a
CC
7438 return info0->addr_base - info1->addr_base;
7439}
7440
7441/* Display the debug_addr section. */
7442static int
7443display_debug_addr (struct dwarf_section *section,
b4eb7656 7444 void *file)
4723351a
CC
7445{
7446 debug_info **debug_addr_info;
7447 unsigned char *entry;
7448 unsigned char *end;
7449 unsigned int i;
7450 unsigned int count;
0e12f6c8 7451 unsigned char * header;
4723351a
CC
7452
7453 if (section->size == 0)
7454 {
7455 printf (_("\nThe %s section is empty.\n"), section->name);
7456 return 0;
7457 }
7458
7459 if (load_debug_info (file) == 0)
7460 {
7461 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7462 section->name);
7463 return 0;
7464 }
7465
015dc7e1 7466 introduce (section, false);
4723351a 7467
1306a742
NC
7468 /* PR 17531: file: cf38d01b.
7469 We use xcalloc because a corrupt file may not have initialised all of the
7470 fields in the debug_info structure, which means that the sort below might
7471 try to move uninitialised data. */
7472 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
b4eb7656 7473 sizeof (debug_info *));
4723351a
CC
7474
7475 count = 0;
7476 for (i = 0; i < num_debug_info_entries; i++)
82b1b41b 7477 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
1306a742
NC
7478 {
7479 /* PR 17531: file: cf38d01b. */
7480 if (debug_information[i].addr_base >= section->size)
7481 warn (_("Corrupt address base (%lx) found in debug section %u\n"),
7482 (unsigned long) debug_information[i].addr_base, i);
7483 else
7484 debug_addr_info [count++] = debug_information + i;
7485 }
4723351a
CC
7486
7487 /* Add a sentinel to make iteration convenient. */
7488 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7489 debug_addr_info [count]->addr_base = section->size;
4723351a 7490 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
1306a742 7491
0e12f6c8 7492 header = section->start;
4723351a
CC
7493 for (i = 0; i < count; i++)
7494 {
7495 unsigned int idx;
fab128ef 7496 unsigned int address_size = debug_addr_info [i]->pointer_size;
4723351a
CC
7497
7498 printf (_(" For compilation unit at offset 0x%s:\n"),
b4eb7656 7499 dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
4723351a 7500
fab128ef 7501 printf (_("\tIndex\tAddress\n"));
4723351a 7502 entry = section->start + debug_addr_info [i]->addr_base;
0e12f6c8
TV
7503 if (debug_addr_info [i]->dwarf_version >= 5)
7504 {
7505 size_t header_size = entry - header;
7506 unsigned char * curr_header = header;
7507 dwarf_vma length;
7508 int version;
7509 int segment_selector_size;
7510
7511 if (header_size != 8 && header_size != 16)
7512 {
7513 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %ld instead\n"),
7514 section->name, (long) header_size);
7515 return 0;
7516 }
7517
7518 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7519 if (length == 0xffffffff)
7520 SAFE_BYTE_GET (length, curr_header, 8, entry);
7521 end = curr_header + length;
7522
7523 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7524 if (version != 5)
7525 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7526 section->name, version);
7527
7528 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7529 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7530 address_size += segment_selector_size;
7531 }
7532 else
7533 end = section->start + debug_addr_info [i + 1]->addr_base;
7534 header = end;
4723351a
CC
7535 idx = 0;
7536 while (entry < end)
b4eb7656
AM
7537 {
7538 dwarf_vma base = byte_get (entry, address_size);
7539 printf (_("\t%d:\t"), idx);
7540 print_dwarf_vma (base, address_size);
7541 printf ("\n");
7542 entry += address_size;
7543 idx++;
7544 }
4723351a
CC
7545 }
7546 printf ("\n");
7547
7548 free (debug_addr_info);
7549 return 1;
7550}
7551
7552/* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
24841daa 7553
4723351a
CC
7554static int
7555display_debug_str_offsets (struct dwarf_section *section,
b4eb7656 7556 void *file ATTRIBUTE_UNUSED)
4723351a 7557{
9f27c364
HPN
7558 unsigned long idx;
7559
4723351a
CC
7560 if (section->size == 0)
7561 {
7562 printf (_("\nThe %s section is empty.\n"), section->name);
7563 return 0;
7564 }
e4b7104b
NC
7565
7566 unsigned char *start = section->start;
7567 unsigned char *end = start + section->size;
7568 unsigned char *curr = start;
7569
63b4cc53 7570 const char *suffix = strrchr (section->name, '.');
015dc7e1 7571 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
39f381cb
NC
7572
7573 if (dwo)
7574 load_debug_section_with_follow (str_dwo, file);
7575 else
7576 load_debug_section_with_follow (str, file);
e4b7104b 7577
015dc7e1 7578 introduce (section, false);
e4b7104b
NC
7579
7580 while (curr < end)
7581 {
7582 dwarf_vma length;
7583 dwarf_vma entry_length;
7584
7585 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7586 /* FIXME: We assume that this means 64-bit DWARF is being used. */
7587 if (length == 0xffffffff)
7588 {
7589 SAFE_BYTE_GET (length, curr, 8, end);
7590 entry_length = 8;
7591 }
7592 else
7593 entry_length = 4;
7594
95abb394 7595 unsigned char *entries_end;
39f381cb
NC
7596 if (length == 0)
7597 {
7598 /* This is probably an old style .debug_str_offset section which
7599 just contains offsets and no header (and the first offset is 0). */
7600 length = section->size;
7601 curr = section->start;
95abb394 7602 entries_end = end;
e4b7104b 7603
39f381cb
NC
7604 printf (_(" Length: %#lx\n"), (unsigned long) length);
7605 printf (_(" Index Offset [String]\n"));
7606 }
7607 else
7608 {
95abb394
TV
7609 entries_end = curr + length;
7610
39f381cb
NC
7611 int version;
7612 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
7613 if (version != 5)
7614 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7615
7616 int padding;
7617 SAFE_BYTE_GET_AND_INC (padding, curr, 2, end);
7618 if (padding != 0)
7619 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7620
7621 printf (_(" Length: %#lx\n"), (unsigned long) length);
7622 printf (_(" Version: %#lx\n"), (unsigned long) version);
7623 printf (_(" Index Offset [String]\n"));
7624 }
e4b7104b 7625
95abb394 7626 for (idx = 0; curr < entries_end; idx++)
e4b7104b
NC
7627 {
7628 dwarf_vma offset;
7629 const unsigned char * string;
7630
95abb394
TV
7631 if (curr + entry_length > entries_end)
7632 /* Not enough space to read one entry_length, give up. */
7633 return 0;
7634
e4b7104b 7635 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, end);
39f381cb
NC
7636 if (dwo)
7637 string = (const unsigned char *)
9f27c364 7638 fetch_indexed_string (idx, NULL, entry_length, dwo);
39f381cb
NC
7639 else
7640 string = fetch_indirect_string (offset);
7641
9f27c364 7642 printf (" %8lu %8s %s\n", idx, dwarf_vmatoa ("x", offset),
e4b7104b
NC
7643 string);
7644 }
7645 }
39f381cb 7646
4723351a
CC
7647 return 1;
7648}
7649
01a8f077
JK
7650/* Each debug_information[x].range_lists[y] gets this representation for
7651 sorting purposes. */
7652
7653struct range_entry
467c65bc
NC
7654{
7655 /* The debug_information[x].range_lists[y] value. */
359ca075 7656 dwarf_vma ranges_offset;
01a8f077 7657
467c65bc
NC
7658 /* Original debug_information to find parameters of the data. */
7659 debug_info *debug_info_p;
7660};
01a8f077
JK
7661
7662/* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
7663
7664static int
7665range_entry_compar (const void *ap, const void *bp)
7666{
3f5e193b
NC
7667 const struct range_entry *a_re = (const struct range_entry *) ap;
7668 const struct range_entry *b_re = (const struct range_entry *) bp;
359ca075
JK
7669 const dwarf_vma a = a_re->ranges_offset;
7670 const dwarf_vma b = b_re->ranges_offset;
01a8f077
JK
7671
7672 return (a > b) - (b > a);
7673}
7674
77145576
JK
7675static void
7676display_debug_ranges_list (unsigned char *start, unsigned char *finish,
7677 unsigned int pointer_size, unsigned long offset,
7678 unsigned long base_address)
7679{
7680 while (start < finish)
7681 {
7682 dwarf_vma begin;
7683 dwarf_vma end;
7684
7685 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7686 if (start >= finish)
7687 break;
7688 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7689
7690 printf (" %8.8lx ", offset);
7691
7692 if (begin == 0 && end == 0)
7693 {
7694 printf (_("<End of list>\n"));
7695 break;
7696 }
7697
7698 /* Check base address specifiers. */
7699 if (is_max_address (begin, pointer_size)
7700 && !is_max_address (end, pointer_size))
7701 {
7702 base_address = end;
7703 print_dwarf_vma (begin, pointer_size);
7704 print_dwarf_vma (end, pointer_size);
7705 printf ("(base address)\n");
7706 continue;
7707 }
7708
7709 print_dwarf_vma (begin + base_address, pointer_size);
7710 print_dwarf_vma (end + base_address, pointer_size);
7711
7712 if (begin == end)
7713 fputs (_("(start == end)"), stdout);
7714 else if (begin > end)
7715 fputs (_("(start > end)"), stdout);
7716
7717 putchar ('\n');
7718 }
7719}
7720
7721static void
7722display_debug_rnglists_list (unsigned char *start, unsigned char *finish,
7723 unsigned int pointer_size, unsigned long offset,
7724 unsigned long base_address)
7725{
7726 unsigned char *next = start;
7727
7728 while (1)
7729 {
7730 unsigned long off = offset + (start - next);
7731 enum dwarf_range_list_entry rlet;
9dfd0db9
JK
7732 /* Initialize it due to a false compiler warning. */
7733 dwarf_vma begin = -1, length, end = -1;
77145576
JK
7734
7735 if (start + 1 > finish)
7736 {
7737 warn (_("Range list starting at offset 0x%lx is not terminated.\n"),
7738 offset);
7739 break;
7740 }
7741
7742 printf (" %8.8lx ", off);
7743
7744 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
7745
7746 switch (rlet)
7747 {
7748 case DW_RLE_end_of_list:
7749 printf (_("<End of list>\n"));
7750 break;
7751 case DW_RLE_base_address:
7752 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
7753 print_dwarf_vma (base_address, pointer_size);
7754 printf (_("(base address)\n"));
7755 break;
7756 case DW_RLE_start_length:
7757 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
cd30bcef 7758 READ_ULEB (length, start, finish);
77145576
JK
7759 end = begin + length;
7760 break;
7761 case DW_RLE_offset_pair:
cd30bcef
AM
7762 READ_ULEB (begin, start, finish);
7763 READ_ULEB (end, start, finish);
77145576
JK
7764 break;
7765 case DW_RLE_start_end:
7766 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7767 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7768 break;
7769 default:
7770 error (_("Invalid range list entry type %d\n"), rlet);
7771 rlet = DW_RLE_end_of_list;
7772 break;
7773 }
7774 if (rlet == DW_RLE_end_of_list)
7775 break;
7776 if (rlet == DW_RLE_base_address)
7777 continue;
7778
4d932715
BE
7779 /* Only a DW_RLE_offset_pair needs the base address added. */
7780 if (rlet == DW_RLE_offset_pair)
7781 {
7782 begin += base_address;
7783 end += base_address;
7784 }
7785
7786 print_dwarf_vma (begin, pointer_size);
7787 print_dwarf_vma (end, pointer_size);
77145576
JK
7788
7789 if (begin == end)
7790 fputs (_("(start == end)"), stdout);
7791 else if (begin > end)
7792 fputs (_("(start > end)"), stdout);
7793
7794 putchar ('\n');
7795 }
7796}
7797
19e6b90e
L
7798static int
7799display_debug_ranges (struct dwarf_section *section,
7800 void *file ATTRIBUTE_UNUSED)
7801{
7802 unsigned char *start = section->start;
a2ff7a4b 7803 unsigned char *last_start = start;
f6f0e17b 7804 unsigned long bytes = section->size;
19e6b90e 7805 unsigned char *section_begin = start;
f6f0e17b 7806 unsigned char *finish = start + bytes;
01a8f077
JK
7807 unsigned int num_range_list, i;
7808 struct range_entry *range_entries, *range_entry_fill;
77145576
JK
7809 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
7810 /* Initialize it due to a false compiler warning. */
7811 unsigned char address_size = 0;
cb4c35cf 7812 dwarf_vma last_offset = 0;
19e6b90e 7813
19e6b90e
L
7814 if (bytes == 0)
7815 {
7816 printf (_("\nThe %s section is empty.\n"), section->name);
7817 return 0;
7818 }
7819
77145576
JK
7820 if (is_rnglists)
7821 {
7822 dwarf_vma initial_length;
7823 unsigned int initial_length_size;
7824 unsigned char segment_selector_size;
7825 unsigned int offset_size, offset_entry_count;
7826 unsigned short version;
7827
7828 /* Get and check the length of the block. */
7829 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
7830
7831 if (initial_length == 0xffffffff)
7832 {
7833 /* This section is 64-bit DWARF 3. */
7834 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
7835 offset_size = 8;
7836 initial_length_size = 12;
7837 }
7838 else
7839 {
7840 offset_size = 4;
7841 initial_length_size = 4;
7842 }
7843
7844 if (initial_length + initial_length_size > section->size)
7845 {
7846 /* If the length field has a relocation against it, then we should
7847 not complain if it is inaccurate (and probably negative).
7848 It is copied from .debug_line handling code. */
7849 if (reloc_at (section, (start - section->start) - offset_size))
7850 {
7851 initial_length = (finish - start) - initial_length_size;
7852 }
7853 else
7854 {
7855 warn (_("The length field (0x%lx) in the debug_rnglists header is wrong - the section is too small\n"),
7856 (long) initial_length);
7857 return 0;
7858 }
7859 }
7860
7861 /* Get and check the version number. */
7862 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
7863
7864 if (version != 5)
7865 {
7866 warn (_("Only DWARF version 5 debug_rnglists info "
7867 "is currently supported.\n"));
7868 return 0;
7869 }
7870
7871 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
7872
7873 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
7874 if (segment_selector_size != 0)
7875 {
7876 warn (_("The %s section contains "
7877 "unsupported segment selector size: %d.\n"),
7878 section->name, segment_selector_size);
7879 return 0;
7880 }
7881
7882 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
7883 if (offset_entry_count != 0)
7884 {
7885 warn (_("The %s section contains "
7886 "unsupported offset entry count: %u.\n"),
7887 section->name, offset_entry_count);
7888 return 0;
7889 }
7890 }
7891
1febe64d
NC
7892 if (load_debug_info (file) == 0)
7893 {
7894 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7895 section->name);
7896 return 0;
7897 }
19e6b90e 7898
01a8f077 7899 num_range_list = 0;
19e6b90e 7900 for (i = 0; i < num_debug_info_entries; i++)
52ff20fe
TV
7901 {
7902 if (debug_information [i].dwarf_version < 5 && is_rnglists)
7903 /* Skip .debug_rnglists reference. */
7904 continue;
7905 if (debug_information [i].dwarf_version >= 5 && !is_rnglists)
7906 /* Skip .debug_range reference. */
7907 continue;
7908 num_range_list += debug_information [i].num_range_lists;
7909 }
19e6b90e 7910
01a8f077 7911 if (num_range_list == 0)
4723351a
CC
7912 {
7913 /* This can happen when the file was compiled with -gsplit-debug
b4eb7656 7914 which removes references to range lists from the primary .o file. */
4723351a
CC
7915 printf (_("No range lists in .debug_info section.\n"));
7916 return 1;
7917 }
19e6b90e 7918
3f5e193b
NC
7919 range_entries = (struct range_entry *)
7920 xmalloc (sizeof (*range_entries) * num_range_list);
01a8f077 7921 range_entry_fill = range_entries;
19e6b90e 7922
01a8f077
JK
7923 for (i = 0; i < num_debug_info_entries; i++)
7924 {
7925 debug_info *debug_info_p = &debug_information[i];
7926 unsigned int j;
7927
52ff20fe
TV
7928 if (debug_information [i].dwarf_version < 5 && is_rnglists)
7929 /* Skip .debug_rnglists reference. */
7930 continue;
7931 if (debug_information [i].dwarf_version >= 5 && !is_rnglists)
7932 /* Skip .debug_range reference. */
7933 continue;
7934
01a8f077
JK
7935 for (j = 0; j < debug_info_p->num_range_lists; j++)
7936 {
7937 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
7938 range_entry_fill->debug_info_p = debug_info_p;
7939 range_entry_fill++;
19e6b90e
L
7940 }
7941 }
7942
01a8f077
JK
7943 qsort (range_entries, num_range_list, sizeof (*range_entries),
7944 range_entry_compar);
19e6b90e 7945
d493b283 7946 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
19e6b90e 7947 warn (_("Range lists in %s section start at 0x%lx\n"),
359ca075 7948 section->name, (unsigned long) range_entries[0].ranges_offset);
19e6b90e 7949
015dc7e1 7950 introduce (section, false);
dda8d76d 7951
19e6b90e
L
7952 printf (_(" Offset Begin End\n"));
7953
01a8f077 7954 for (i = 0; i < num_range_list; i++)
19e6b90e 7955 {
01a8f077
JK
7956 struct range_entry *range_entry = &range_entries[i];
7957 debug_info *debug_info_p = range_entry->debug_info_p;
19e6b90e 7958 unsigned int pointer_size;
359ca075 7959 dwarf_vma offset;
01a8f077 7960 unsigned char *next;
359ca075 7961 dwarf_vma base_address;
19e6b90e 7962
77145576 7963 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
d493b283 7964 offset = range_entry->ranges_offset;
01a8f077
JK
7965 next = section_begin + offset;
7966 base_address = debug_info_p->base_address;
cecf136e 7967
f41e4712
NC
7968 /* PR 17512: file: 001-101485-0.001:0.1. */
7969 if (pointer_size < 2 || pointer_size > 8)
7970 {
7971 warn (_("Corrupt pointer size (%d) in debug entry at offset %8.8lx\n"),
359ca075 7972 pointer_size, (unsigned long) offset);
f41e4712
NC
7973 continue;
7974 }
b4eb7656 7975
d11ae95e
NC
7976 if (next < section_begin || next >= finish)
7977 {
7978 warn (_("Corrupt offset (%#8.8lx) in range entry %u\n"),
7979 (unsigned long) offset, i);
7980 continue;
7981 }
7982
cb4c35cf
AB
7983 /* If multiple DWARF entities reference the same range then we will
7984 have multiple entries in the `range_entries' list for the same
7985 offset. Thanks to the sort above these will all be consecutive in
7986 the `range_entries' list, so we can easily ignore duplicates
7987 here. */
7988 if (i > 0 && last_offset == offset)
7989 continue;
7990 last_offset = offset;
7991
4723351a 7992 if (dwarf_check != 0 && i > 0)
19e6b90e 7993 {
01a8f077
JK
7994 if (start < next)
7995 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
7996 (unsigned long) (start - section_begin),
7997 (unsigned long) (next - section_begin), section->name);
7998 else if (start > next)
a2ff7a4b
AM
7999 {
8000 if (next == last_start)
8001 continue;
8002 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
8003 (unsigned long) (start - section_begin),
8004 (unsigned long) (next - section_begin), section->name);
8005 }
01a8f077 8006 }
d11ae95e 8007
01a8f077 8008 start = next;
a2ff7a4b 8009 last_start = next;
19e6b90e 8010
77145576
JK
8011 (is_rnglists ? display_debug_rnglists_list : display_debug_ranges_list)
8012 (start, finish, pointer_size, offset, base_address);
19e6b90e
L
8013 }
8014 putchar ('\n');
01a8f077
JK
8015
8016 free (range_entries);
8017
19e6b90e
L
8018 return 1;
8019}
8020
8021typedef struct Frame_Chunk
8022{
8023 struct Frame_Chunk *next;
8024 unsigned char *chunk_start;
a1165289 8025 unsigned int ncols;
19e6b90e
L
8026 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8027 short int *col_type;
8028 int *col_offset;
8029 char *augmentation;
8030 unsigned int code_factor;
8031 int data_factor;
bf5117e3
NC
8032 dwarf_vma pc_begin;
8033 dwarf_vma pc_range;
32ef3000 8034 unsigned int cfa_reg;
c8071705 8035 dwarf_vma cfa_offset;
a1165289 8036 unsigned int ra;
19e6b90e
L
8037 unsigned char fde_encoding;
8038 unsigned char cfa_exp;
604282a7
JJ
8039 unsigned char ptr_size;
8040 unsigned char segment_size;
19e6b90e
L
8041}
8042Frame_Chunk;
8043
1296bc99
AB
8044typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8045static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
665ce1f6
L
8046static const char *const *dwarf_regnames;
8047static unsigned int dwarf_regnames_count;
8048
1296bc99 8049
19e6b90e
L
8050/* A marker for a col_type that means this column was never referenced
8051 in the frame info. */
8052#define DW_CFA_unreferenced (-1)
8053
a1165289 8054/* Return 0 if no more space is needed, 1 if more space is needed,
665ce1f6
L
8055 -1 for invalid reg. */
8056
8057static int
8058frame_need_space (Frame_Chunk *fc, unsigned int reg)
19e6b90e 8059{
a1165289 8060 unsigned int prev = fc->ncols;
19e6b90e 8061
665ce1f6
L
8062 if (reg < (unsigned int) fc->ncols)
8063 return 0;
8064
d9acf707 8065 if (dwarf_regnames_count > 0
665ce1f6
L
8066 && reg > dwarf_regnames_count)
8067 return -1;
19e6b90e
L
8068
8069 fc->ncols = reg + 1;
a1165289
NC
8070 /* PR 17512: file: 10450-2643-0.004.
8071 If reg == -1 then this can happen... */
8072 if (fc->ncols == 0)
8073 return -1;
8074
06614111 8075 /* PR 17512: file: 2844a11d. */
d9acf707 8076 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
06614111
NC
8077 {
8078 error (_("Unfeasibly large register number: %u\n"), reg);
8079 fc->ncols = 0;
8080 /* FIXME: 1024 is an arbitrary limit. Increase it if
8081 we ever encounter a valid binary that exceeds it. */
8082 return -1;
8083 }
8084
3f5e193b 8085 fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
b4eb7656 8086 sizeof (short int));
3f5e193b 8087 fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
b4eb7656 8088 /* PR 17512: file:002-10025-0.005. */
041830e0
NC
8089 if (fc->col_type == NULL || fc->col_offset == NULL)
8090 {
8091 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8092 fc->ncols);
8093 fc->ncols = 0;
8094 return -1;
8095 }
19e6b90e
L
8096
8097 while (prev < fc->ncols)
8098 {
8099 fc->col_type[prev] = DW_CFA_unreferenced;
8100 fc->col_offset[prev] = 0;
8101 prev++;
8102 }
665ce1f6 8103 return 1;
19e6b90e
L
8104}
8105
2dc4cec1
L
8106static const char *const dwarf_regnames_i386[] =
8107{
43234a1e
L
8108 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8109 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8110 "eip", "eflags", NULL, /* 8 - 10 */
8111 "st0", "st1", "st2", "st3", /* 11 - 14 */
8112 "st4", "st5", "st6", "st7", /* 15 - 18 */
8113 NULL, NULL, /* 19 - 20 */
8114 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8115 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8116 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8117 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8118 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8119 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8120 "tr", "ldtr", /* 48 - 49 */
8121 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8122 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8123 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8124 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8125 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8126 NULL, NULL, NULL, /* 90 - 92 */
8127 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
2dc4cec1
L
8128};
8129
3d875af5
L
8130static const char *const dwarf_regnames_iamcu[] =
8131{
8132 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8133 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8134 "eip", "eflags", NULL, /* 8 - 10 */
8135 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8136 NULL, NULL, /* 19 - 20 */
8137 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8138 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8139 NULL, NULL, NULL, /* 37 - 39 */
8140 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8141 "tr", "ldtr", /* 48 - 49 */
8142 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8143 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8144 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8145 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8146 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8147 NULL, NULL, NULL, /* 90 - 92 */
8148 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8149};
8150
99f6fdd9 8151static void
b129eb0e
RH
8152init_dwarf_regnames_i386 (void)
8153{
8154 dwarf_regnames = dwarf_regnames_i386;
8155 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
1296bc99 8156 dwarf_regnames_lookup_func = regname_internal_by_table_only;
b129eb0e
RH
8157}
8158
99f6fdd9 8159static void
3d875af5
L
8160init_dwarf_regnames_iamcu (void)
8161{
8162 dwarf_regnames = dwarf_regnames_iamcu;
8163 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
1296bc99 8164 dwarf_regnames_lookup_func = regname_internal_by_table_only;
3d875af5
L
8165}
8166
2dc4cec1
L
8167static const char *const dwarf_regnames_x86_64[] =
8168{
8169 "rax", "rdx", "rcx", "rbx",
8170 "rsi", "rdi", "rbp", "rsp",
8171 "r8", "r9", "r10", "r11",
8172 "r12", "r13", "r14", "r15",
8173 "rip",
8174 "xmm0", "xmm1", "xmm2", "xmm3",
8175 "xmm4", "xmm5", "xmm6", "xmm7",
8176 "xmm8", "xmm9", "xmm10", "xmm11",
8177 "xmm12", "xmm13", "xmm14", "xmm15",
8178 "st0", "st1", "st2", "st3",
8179 "st4", "st5", "st6", "st7",
8180 "mm0", "mm1", "mm2", "mm3",
8181 "mm4", "mm5", "mm6", "mm7",
8182 "rflags",
8183 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8184 "fs.base", "gs.base", NULL, NULL,
8185 "tr", "ldtr",
43234a1e
L
8186 "mxcsr", "fcw", "fsw",
8187 "xmm16", "xmm17", "xmm18", "xmm19",
8188 "xmm20", "xmm21", "xmm22", "xmm23",
8189 "xmm24", "xmm25", "xmm26", "xmm27",
8190 "xmm28", "xmm29", "xmm30", "xmm31",
8191 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8192 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8193 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8194 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8195 NULL, NULL, NULL, /* 115 - 117 */
8196 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
2dc4cec1
L
8197};
8198
99f6fdd9 8199static void
b129eb0e
RH
8200init_dwarf_regnames_x86_64 (void)
8201{
8202 dwarf_regnames = dwarf_regnames_x86_64;
8203 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
1296bc99 8204 dwarf_regnames_lookup_func = regname_internal_by_table_only;
b129eb0e
RH
8205}
8206
4ee22035
RH
8207static const char *const dwarf_regnames_aarch64[] =
8208{
b4eb7656
AM
8209 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8210 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
4ee22035
RH
8211 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8212 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8213 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
fab7c86e
TC
8214 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8215 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8216 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
b4eb7656
AM
8217 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8218 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
4ee22035
RH
8219 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8220 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
fab7c86e
TC
8221 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8222 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8223 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8224 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
4ee22035
RH
8225};
8226
99f6fdd9 8227static void
4ee22035
RH
8228init_dwarf_regnames_aarch64 (void)
8229{
8230 dwarf_regnames = dwarf_regnames_aarch64;
8231 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
1296bc99 8232 dwarf_regnames_lookup_func = regname_internal_by_table_only;
4ee22035
RH
8233}
8234
d6bb17b0
AA
8235static const char *const dwarf_regnames_s390[] =
8236{
8237 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8238 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8239 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8240 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8241 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8242 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8243 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8244 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8245 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8246 "pswm", "pswa",
8247 NULL, NULL,
8248 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8249 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8250};
8251
99f6fdd9 8252static void
d6bb17b0
AA
8253init_dwarf_regnames_s390 (void)
8254{
8255 dwarf_regnames = dwarf_regnames_s390;
8256 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
1296bc99 8257 dwarf_regnames_lookup_func = regname_internal_by_table_only;
d6bb17b0
AA
8258}
8259
5bb0830d
AB
8260static const char *const dwarf_regnames_riscv[] =
8261{
8262 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8263 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8264 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8265 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8266 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8267 "fs0", "fs1", /* 40 - 41 */
8268 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8269 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8270 "fs10", "fs11", /* 58 - 59 */
8271 "ft8", "ft9", "ft10", "ft11" /* 60 - 63 */
8272};
8273
4762fe62
AB
8274/* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8275 the large number of CSRs. */
8276
8277static const char *
8278regname_internal_riscv (unsigned int regno)
8279{
8280 const char *name = NULL;
8281
8282 /* Lookup in the table first, this covers GPR and FPR. */
8283 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8284 name = dwarf_regnames_riscv [regno];
8285 else if (regno >= 4096 && regno <= 8191)
8286 {
8287 /* This might be a CSR, these live in a sparse number space from 4096
8288 to 8191 These numbers are defined in the RISC-V ELF ABI
8289 document. */
8290 switch (regno)
8291 {
8f595e9b
NC
8292#define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8293 case VALUE + 4096: name = #NAME; break;
4762fe62
AB
8294#include "opcode/riscv-opc.h"
8295#undef DECLARE_CSR
8296
8297 default:
8298 {
8299 static char csr_name[10];
8300 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8301 name = csr_name;
8302 }
8303 break;
8304 }
8305 }
8306
8307 return name;
8308}
8309
99f6fdd9 8310static void
5bb0830d
AB
8311init_dwarf_regnames_riscv (void)
8312{
4762fe62
AB
8313 dwarf_regnames = NULL;
8314 dwarf_regnames_count = 8192;
8315 dwarf_regnames_lookup_func = regname_internal_riscv;
5bb0830d
AB
8316}
8317
2dc4cec1 8318void
955ff7fc 8319init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
2dc4cec1 8320{
1296bc99
AB
8321 dwarf_regnames_lookup_func = NULL;
8322
2dc4cec1
L
8323 switch (e_machine)
8324 {
8325 case EM_386:
b129eb0e 8326 init_dwarf_regnames_i386 ();
2dc4cec1
L
8327 break;
8328
3d875af5
L
8329 case EM_IAMCU:
8330 init_dwarf_regnames_iamcu ();
8331 break;
8332
2dc4cec1 8333 case EM_X86_64:
7f502d6c 8334 case EM_L1OM:
7a9068fe 8335 case EM_K1OM:
b129eb0e 8336 init_dwarf_regnames_x86_64 ();
2dc4cec1
L
8337 break;
8338
4ee22035
RH
8339 case EM_AARCH64:
8340 init_dwarf_regnames_aarch64 ();
8341 break;
8342
d6bb17b0
AA
8343 case EM_S390:
8344 init_dwarf_regnames_s390 ();
8345 break;
8346
5bb0830d
AB
8347 case EM_RISCV:
8348 init_dwarf_regnames_riscv ();
8349 break;
8350
2dc4cec1
L
8351 default:
8352 break;
8353 }
8354}
8355
229a22cf
AB
8356/* Initialize the DWARF register name lookup state based on the
8357 architecture and specific machine type of a BFD. */
8358
8359void
8360init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8361 unsigned long mach)
8362{
1296bc99
AB
8363 dwarf_regnames_lookup_func = NULL;
8364
229a22cf
AB
8365 switch (arch)
8366 {
8367 case bfd_arch_i386:
8368 switch (mach)
8369 {
8370 case bfd_mach_x86_64:
8371 case bfd_mach_x86_64_intel_syntax:
229a22cf
AB
8372 case bfd_mach_x64_32:
8373 case bfd_mach_x64_32_intel_syntax:
229a22cf
AB
8374 init_dwarf_regnames_x86_64 ();
8375 break;
8376
8377 default:
8378 init_dwarf_regnames_i386 ();
8379 break;
8380 }
8381 break;
8382
8383 case bfd_arch_iamcu:
8384 init_dwarf_regnames_iamcu ();
8385 break;
8386
8387 case bfd_arch_aarch64:
8388 init_dwarf_regnames_aarch64();
8389 break;
8390
8391 case bfd_arch_s390:
8392 init_dwarf_regnames_s390 ();
8393 break;
8394
8395 case bfd_arch_riscv:
8396 init_dwarf_regnames_riscv ();
8397 break;
8398
8399 default:
8400 break;
8401 }
8402}
8403
2dc4cec1 8404static const char *
1296bc99 8405regname_internal_by_table_only (unsigned int regno)
2dc4cec1 8406{
1296bc99 8407 if (dwarf_regnames != NULL
2dc4cec1
L
8408 && regno < dwarf_regnames_count
8409 && dwarf_regnames [regno] != NULL)
1296bc99
AB
8410 return dwarf_regnames [regno];
8411
8412 return NULL;
8413}
8414
8415static const char *
8416regname (unsigned int regno, int name_only_p)
8417{
8418 static char reg[64];
8419
8420 const char *name = NULL;
8421
8422 if (dwarf_regnames_lookup_func != NULL)
8423 name = dwarf_regnames_lookup_func (regno);
8424
8425 if (name != NULL)
2dc4cec1 8426 {
1296bc99
AB
8427 if (name_only_p)
8428 return name;
8429 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
2dc4cec1
L
8430 }
8431 else
8432 snprintf (reg, sizeof (reg), "r%d", regno);
8433 return reg;
8434}
8435
19e6b90e 8436static void
a1165289 8437frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
19e6b90e 8438{
a1165289 8439 unsigned int r;
19e6b90e
L
8440 char tmp[100];
8441
d8024a91 8442 if (*max_regs != fc->ncols)
19e6b90e
L
8443 *max_regs = fc->ncols;
8444
8445 if (*need_col_headers)
8446 {
8447 *need_col_headers = 0;
8448
288f0ba2 8449 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
19e6b90e
L
8450
8451 for (r = 0; r < *max_regs; r++)
8452 if (fc->col_type[r] != DW_CFA_unreferenced)
8453 {
8454 if (r == fc->ra)
50751e18 8455 printf ("ra ");
19e6b90e 8456 else
2dc4cec1 8457 printf ("%-5s ", regname (r, 1));
19e6b90e
L
8458 }
8459
8460 printf ("\n");
8461 }
8462
bf5117e3 8463 print_dwarf_vma (fc->pc_begin, eh_addr_size);
19e6b90e
L
8464 if (fc->cfa_exp)
8465 strcpy (tmp, "exp");
8466 else
c8071705 8467 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
19e6b90e
L
8468 printf ("%-8s ", tmp);
8469
8470 for (r = 0; r < fc->ncols; r++)
8471 {
8472 if (fc->col_type[r] != DW_CFA_unreferenced)
8473 {
8474 switch (fc->col_type[r])
8475 {
8476 case DW_CFA_undefined:
8477 strcpy (tmp, "u");
8478 break;
8479 case DW_CFA_same_value:
8480 strcpy (tmp, "s");
8481 break;
8482 case DW_CFA_offset:
8483 sprintf (tmp, "c%+d", fc->col_offset[r]);
8484 break;
12eae2d3
JJ
8485 case DW_CFA_val_offset:
8486 sprintf (tmp, "v%+d", fc->col_offset[r]);
8487 break;
19e6b90e 8488 case DW_CFA_register:
2dc4cec1 8489 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
19e6b90e
L
8490 break;
8491 case DW_CFA_expression:
8492 strcpy (tmp, "exp");
8493 break;
12eae2d3
JJ
8494 case DW_CFA_val_expression:
8495 strcpy (tmp, "vexp");
8496 break;
19e6b90e
L
8497 default:
8498 strcpy (tmp, "n/a");
8499 break;
8500 }
2dc4cec1 8501 printf ("%-5s ", tmp);
19e6b90e
L
8502 }
8503 }
8504 printf ("\n");
8505}
8506
49727e46 8507#define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
19e6b90e 8508
49727e46
AM
8509static unsigned char *
8510read_cie (unsigned char *start, unsigned char *end,
8511 Frame_Chunk **p_cie, int *p_version,
bf59c5d5 8512 bfd_size_type *p_aug_len, unsigned char **p_aug)
49727e46
AM
8513{
8514 int version;
8515 Frame_Chunk *fc;
49727e46 8516 unsigned char *augmentation_data = NULL;
bf59c5d5 8517 bfd_size_type augmentation_data_len = 0;
49727e46 8518
041830e0 8519 * p_cie = NULL;
f41e4712
NC
8520 /* PR 17512: file: 001-228113-0.004. */
8521 if (start >= end)
8522 return end;
8523
49727e46
AM
8524 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8525 memset (fc, 0, sizeof (Frame_Chunk));
8526
8527 fc->col_type = (short int *) xmalloc (sizeof (short int));
8528 fc->col_offset = (int *) xmalloc (sizeof (int));
8529
8530 version = *start++;
8531
8532 fc->augmentation = (char *) start;
f41e4712
NC
8533 /* PR 17512: file: 001-228113-0.004.
8534 Skip past augmentation name, but avoid running off the end of the data. */
8535 while (start < end)
8536 if (* start ++ == '\0')
8537 break;
8538 if (start == end)
8539 {
8540 warn (_("No terminator for augmentation name\n"));
442a6ce8 8541 goto fail;
f41e4712 8542 }
49727e46
AM
8543
8544 if (strcmp (fc->augmentation, "eh") == 0)
8545 start += eh_addr_size;
8546
8547 if (version >= 4)
8548 {
8549 GET (fc->ptr_size, 1);
77ef8654
NC
8550 if (fc->ptr_size < 1 || fc->ptr_size > 8)
8551 {
8552 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
442a6ce8 8553 goto fail;
77ef8654
NC
8554 }
8555
49727e46 8556 GET (fc->segment_size, 1);
77ef8654
NC
8557 /* PR 17512: file: e99d2804. */
8558 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
8559 {
8560 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
442a6ce8 8561 goto fail;
77ef8654
NC
8562 }
8563
49727e46
AM
8564 eh_addr_size = fc->ptr_size;
8565 }
8566 else
8567 {
8568 fc->ptr_size = eh_addr_size;
8569 fc->segment_size = 0;
8570 }
442a6ce8 8571
cd30bcef
AM
8572 READ_ULEB (fc->code_factor, start, end);
8573 READ_SLEB (fc->data_factor, start, end);
442a6ce8 8574
49727e46
AM
8575 if (version == 1)
8576 {
8577 GET (fc->ra, 1);
8578 }
8579 else
8580 {
cd30bcef 8581 READ_ULEB (fc->ra, start, end);
49727e46
AM
8582 }
8583
8584 if (fc->augmentation[0] == 'z')
8585 {
cd30bcef 8586 READ_ULEB (augmentation_data_len, start, end);
49727e46 8587 augmentation_data = start;
a1165289 8588 /* PR 17512: file: 11042-2589-0.004. */
bf59c5d5 8589 if (augmentation_data_len > (bfd_size_type) (end - start))
a1165289 8590 {
bf59c5d5
NC
8591 warn (_("Augmentation data too long: 0x%s, expected at most %#lx\n"),
8592 dwarf_vmatoa ("x", augmentation_data_len),
8593 (unsigned long) (end - start));
442a6ce8 8594 goto fail;
a1165289 8595 }
9c0f3d3f 8596 start += augmentation_data_len;
49727e46
AM
8597 }
8598
8599 if (augmentation_data_len)
8600 {
058037d3
NC
8601 unsigned char *p;
8602 unsigned char *q;
8603 unsigned char *qend;
b4eb7656 8604
49727e46
AM
8605 p = (unsigned char *) fc->augmentation + 1;
8606 q = augmentation_data;
058037d3
NC
8607 qend = q + augmentation_data_len;
8608
9c0f3d3f 8609 while (p < end && q < qend)
49727e46
AM
8610 {
8611 if (*p == 'L')
8612 q++;
8613 else if (*p == 'P')
8614 q += 1 + size_of_encoded_value (*q);
8615 else if (*p == 'R')
8616 fc->fde_encoding = *q++;
8617 else if (*p == 'S')
8618 ;
09038062
ST
8619 else if (*p == 'B')
8620 ;
49727e46
AM
8621 else
8622 break;
8623 p++;
8624 }
c361b9ac
NC
8625 /* Note - it is OK if this loop terminates with q < qend.
8626 Padding may have been inserted to align the end of the CIE. */
49727e46
AM
8627 }
8628
8629 *p_cie = fc;
8630 if (p_version)
8631 *p_version = version;
8632 if (p_aug_len)
8633 {
8634 *p_aug_len = augmentation_data_len;
8635 *p_aug = augmentation_data;
8636 }
8637 return start;
442a6ce8
NC
8638
8639 fail:
8640 free (fc->col_offset);
8641 free (fc->col_type);
8642 free (fc);
8643 return end;
49727e46
AM
8644}
8645
d85bf2ba
NC
8646/* Prints out the contents on the DATA array formatted as unsigned bytes.
8647 If do_wide is not enabled, then formats the output to fit into 80 columns.
8648 PRINTED contains the number of characters already written to the current
8649 output line. */
bf59c5d5
NC
8650
8651static void
d85bf2ba
NC
8652display_data (bfd_size_type printed,
8653 const unsigned char * data,
8654 const bfd_size_type len)
bf59c5d5 8655{
d85bf2ba
NC
8656 if (do_wide || len < ((80 - printed) / 3))
8657 for (printed = 0; printed < len; ++printed)
8658 printf (" %02x", data[printed]);
bf59c5d5
NC
8659 else
8660 {
d85bf2ba 8661 for (printed = 0; printed < len; ++printed)
bf59c5d5 8662 {
d85bf2ba 8663 if (printed % (80 / 3) == 0)
bf59c5d5 8664 putchar ('\n');
d85bf2ba 8665 printf (" %02x", data[printed]);
bf59c5d5
NC
8666 }
8667 }
d85bf2ba
NC
8668}
8669
8670/* Prints out the contents on the augmentation data array.
8671 If do_wide is not enabled, then formats the output to fit into 80 columns. */
8672
8673static void
8674display_augmentation_data (const unsigned char * data, const bfd_size_type len)
8675{
8676 bfd_size_type i;
8677
8678 i = printf (_(" Augmentation data: "));
8679 display_data (i, data, len);
bf59c5d5
NC
8680}
8681
19e6b90e
L
8682static int
8683display_debug_frames (struct dwarf_section *section,
8684 void *file ATTRIBUTE_UNUSED)
8685{
8686 unsigned char *start = section->start;
8687 unsigned char *end = start + section->size;
8688 unsigned char *section_start = start;
3391569f
NC
8689 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
8690 Frame_Chunk *remembered_state = NULL;
19e6b90e 8691 Frame_Chunk *rs;
015dc7e1 8692 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
a1165289 8693 unsigned int max_regs = 0;
665ce1f6 8694 const char *bad_reg = _("bad register: ");
77ef8654 8695 unsigned int saved_eh_addr_size = eh_addr_size;
19e6b90e 8696
015dc7e1 8697 introduce (section, false);
19e6b90e
L
8698
8699 while (start < end)
8700 {
8701 unsigned char *saved_start;
8702 unsigned char *block_end;
bf5117e3
NC
8703 dwarf_vma length;
8704 dwarf_vma cie_id;
19e6b90e
L
8705 Frame_Chunk *fc;
8706 Frame_Chunk *cie;
8707 int need_col_headers = 1;
8708 unsigned char *augmentation_data = NULL;
bf59c5d5 8709 bfd_size_type augmentation_data_len = 0;
bf5117e3
NC
8710 unsigned int encoded_ptr_size = saved_eh_addr_size;
8711 unsigned int offset_size;
8712 unsigned int initial_length_size;
015dc7e1 8713 bool all_nops;
a788aedd 8714 static Frame_Chunk fde_fc;
19e6b90e
L
8715
8716 saved_start = start;
19e6b90e 8717
0c588247 8718 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
041830e0 8719
19e6b90e
L
8720 if (length == 0)
8721 {
8722 printf ("\n%08lx ZERO terminator\n\n",
8723 (unsigned long)(saved_start - section_start));
6937bb54
NC
8724 /* Skip any zero terminators that directly follow.
8725 A corrupt section size could have loaded a whole
8726 slew of zero filled memory bytes. eg
8727 PR 17512: file: 070-19381-0.004. */
8728 while (start < end && * start == 0)
8729 ++ start;
b758e50f 8730 continue;
19e6b90e
L
8731 }
8732
8733 if (length == 0xffffffff)
8734 {
0c588247 8735 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
19e6b90e
L
8736 offset_size = 8;
8737 initial_length_size = 12;
8738 }
8739 else
8740 {
8741 offset_size = 4;
8742 initial_length_size = 4;
8743 }
8744
8745 block_end = saved_start + length + initial_length_size;
041830e0 8746 if (block_end > end || block_end < start)
53b8873b 8747 {
bf5117e3
NC
8748 warn ("Invalid length 0x%s in FDE at %#08lx\n",
8749 dwarf_vmatoa_1 (NULL, length, offset_size),
8750 (unsigned long) (saved_start - section_start));
53b8873b
NC
8751 block_end = end;
8752 }
0c588247
NC
8753
8754 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, end);
19e6b90e 8755
846a11e4
NC
8756 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
8757 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
19e6b90e
L
8758 {
8759 int version;
a1165289 8760 unsigned int mreg;
19e6b90e 8761
49727e46
AM
8762 start = read_cie (start, end, &cie, &version,
8763 &augmentation_data_len, &augmentation_data);
041830e0
NC
8764 /* PR 17512: file: 027-135133-0.005. */
8765 if (cie == NULL)
8766 break;
a1165289 8767
49727e46 8768 fc = cie;
19e6b90e
L
8769 fc->next = chunks;
8770 chunks = fc;
8771 fc->chunk_start = saved_start;
a1165289 8772 mreg = max_regs > 0 ? max_regs - 1 : 0;
49727e46
AM
8773 if (mreg < fc->ra)
8774 mreg = fc->ra;
06614111
NC
8775 if (frame_need_space (fc, mreg) < 0)
8776 break;
49727e46
AM
8777 if (fc->fde_encoding)
8778 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
19e6b90e 8779
bf5117e3
NC
8780 printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
8781 print_dwarf_vma (length, fc->ptr_size);
9c41109d 8782 print_dwarf_vma (cie_id, offset_size);
bf5117e3 8783
19e6b90e 8784 if (do_debug_frames_interp)
bf5117e3
NC
8785 {
8786 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
8787 fc->code_factor, fc->data_factor, fc->ra);
8788 }
19e6b90e
L
8789 else
8790 {
bf5117e3 8791 printf ("CIE\n");
19e6b90e
L
8792 printf (" Version: %d\n", version);
8793 printf (" Augmentation: \"%s\"\n", fc->augmentation);
604282a7
JJ
8794 if (version >= 4)
8795 {
8796 printf (" Pointer Size: %u\n", fc->ptr_size);
8797 printf (" Segment Size: %u\n", fc->segment_size);
8798 }
19e6b90e
L
8799 printf (" Code alignment factor: %u\n", fc->code_factor);
8800 printf (" Data alignment factor: %d\n", fc->data_factor);
8801 printf (" Return address column: %d\n", fc->ra);
8802
8803 if (augmentation_data_len)
bf59c5d5 8804 display_augmentation_data (augmentation_data, augmentation_data_len);
a1165289 8805
19e6b90e
L
8806 putchar ('\n');
8807 }
19e6b90e
L
8808 }
8809 else
8810 {
8811 unsigned char *look_for;
604282a7 8812 unsigned long segment_selector;
0d872fca 8813 dwarf_vma cie_off;
19e6b90e 8814
0d872fca 8815 cie_off = cie_id;
49727e46
AM
8816 if (is_eh)
8817 {
8818 dwarf_vma sign = (dwarf_vma) 1 << (offset_size * 8 - 1);
0d872fca
AM
8819 cie_off = (cie_off ^ sign) - sign;
8820 cie_off = start - 4 - section_start - cie_off;
49727e46 8821 }
19e6b90e 8822
0d872fca
AM
8823 look_for = section_start + cie_off;
8824 if (cie_off <= (dwarf_vma) (saved_start - section_start))
49727e46
AM
8825 {
8826 for (cie = chunks; cie ; cie = cie->next)
8827 if (cie->chunk_start == look_for)
8828 break;
8829 }
8830 else
8831 {
8832 for (cie = forward_refs; cie ; cie = cie->next)
8833 if (cie->chunk_start == look_for)
8834 break;
8835 if (!cie)
8836 {
8837 unsigned int off_size;
8838 unsigned char *cie_scan;
19e6b90e 8839
49727e46
AM
8840 cie_scan = look_for;
8841 off_size = 4;
8842 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
8843 if (length == 0xffffffff)
8844 {
8845 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
8846 off_size = 8;
8847 }
8848 if (length != 0)
8849 {
8850 dwarf_vma c_id;
8851
8852 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size, end);
8853 if (is_eh
8854 ? c_id == 0
8855 : ((off_size == 4 && c_id == DW_CIE_ID)
8856 || (off_size == 8 && c_id == DW64_CIE_ID)))
8857 {
8858 int version;
a1165289 8859 unsigned int mreg;
49727e46
AM
8860
8861 read_cie (cie_scan, end, &cie, &version,
8862 &augmentation_data_len, &augmentation_data);
a1165289
NC
8863 /* PR 17512: file: 3450-2098-0.004. */
8864 if (cie == NULL)
8865 {
8866 warn (_("Failed to read CIE information\n"));
8867 break;
8868 }
49727e46
AM
8869 cie->next = forward_refs;
8870 forward_refs = cie;
8871 cie->chunk_start = look_for;
a1165289 8872 mreg = max_regs > 0 ? max_regs - 1 : 0;
49727e46
AM
8873 if (mreg < cie->ra)
8874 mreg = cie->ra;
06614111
NC
8875 if (frame_need_space (cie, mreg) < 0)
8876 {
8877 warn (_("Invalid max register\n"));
8878 break;
8879 }
49727e46
AM
8880 if (cie->fde_encoding)
8881 encoded_ptr_size
8882 = size_of_encoded_value (cie->fde_encoding);
8883 }
8884 }
8885 }
8886 }
8887
8888 fc = &fde_fc;
8889 memset (fc, 0, sizeof (Frame_Chunk));
19e6b90e
L
8890
8891 if (!cie)
8892 {
bf5117e3
NC
8893 warn ("Invalid CIE pointer 0x%s in FDE at %#08lx\n",
8894 dwarf_vmatoa_1 (NULL, cie_id, offset_size),
8895 (unsigned long) (saved_start - section_start));
19e6b90e 8896 fc->ncols = 0;
3f5e193b
NC
8897 fc->col_type = (short int *) xmalloc (sizeof (short int));
8898 fc->col_offset = (int *) xmalloc (sizeof (int));
06614111
NC
8899 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
8900 {
8901 warn (_("Invalid max register\n"));
8902 break;
8903 }
19e6b90e
L
8904 cie = fc;
8905 fc->augmentation = "";
8906 fc->fde_encoding = 0;
604282a7
JJ
8907 fc->ptr_size = eh_addr_size;
8908 fc->segment_size = 0;
19e6b90e
L
8909 }
8910 else
8911 {
8912 fc->ncols = cie->ncols;
3f5e193b
NC
8913 fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
8914 fc->col_offset = (int *) xcmalloc (fc->ncols, sizeof (int));
19e6b90e
L
8915 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
8916 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
8917 fc->augmentation = cie->augmentation;
604282a7
JJ
8918 fc->ptr_size = cie->ptr_size;
8919 eh_addr_size = cie->ptr_size;
8920 fc->segment_size = cie->segment_size;
19e6b90e
L
8921 fc->code_factor = cie->code_factor;
8922 fc->data_factor = cie->data_factor;
8923 fc->cfa_reg = cie->cfa_reg;
8924 fc->cfa_offset = cie->cfa_offset;
8925 fc->ra = cie->ra;
06614111
NC
8926 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
8927 {
8928 warn (_("Invalid max register\n"));
8929 break;
8930 }
19e6b90e
L
8931 fc->fde_encoding = cie->fde_encoding;
8932 }
8933
8934 if (fc->fde_encoding)
8935 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8936
604282a7
JJ
8937 segment_selector = 0;
8938 if (fc->segment_size)
c8071705
NC
8939 {
8940 if (fc->segment_size > sizeof (segment_selector))
8941 {
8942 /* PR 17512: file: 9e196b3e. */
8943 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
8944 fc->segment_size = 4;
8945 }
8946 SAFE_BYTE_GET_AND_INC (segment_selector, start, fc->segment_size, end);
8947 }
041830e0
NC
8948
8949 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section, end);
19e6b90e 8950
0c588247
NC
8951 /* FIXME: It appears that sometimes the final pc_range value is
8952 encoded in less than encoded_ptr_size bytes. See the x86_64
8953 run of the "objcopy on compressed debug sections" test for an
8954 example of this. */
8955 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size, end);
bf5117e3 8956
19e6b90e
L
8957 if (cie->augmentation[0] == 'z')
8958 {
cd30bcef 8959 READ_ULEB (augmentation_data_len, start, end);
19e6b90e 8960 augmentation_data = start;
bf59c5d5 8961 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
d292364e 8962 if (augmentation_data_len > (bfd_size_type) (end - start))
0a9d414a 8963 {
d292364e
AM
8964 warn (_("Augmentation data too long: 0x%s, "
8965 "expected at most %#lx\n"),
8966 dwarf_vmatoa ("x", augmentation_data_len),
8967 (unsigned long) (end - start));
0a9d414a
NC
8968 start = end;
8969 augmentation_data = NULL;
8970 augmentation_data_len = 0;
8971 }
d292364e 8972 start += augmentation_data_len;
19e6b90e
L
8973 }
8974
bf5117e3
NC
8975 printf ("\n%08lx %s %s FDE cie=%08lx pc=",
8976 (unsigned long)(saved_start - section_start),
8977 dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
9c41109d 8978 dwarf_vmatoa_1 (NULL, cie_id, offset_size),
604282a7 8979 (unsigned long)(cie->chunk_start - section_start));
bf5117e3 8980
604282a7
JJ
8981 if (fc->segment_size)
8982 printf ("%04lx:", segment_selector);
bf5117e3
NC
8983
8984 printf ("%s..%s\n",
8985 dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
8986 dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));
8987
19e6b90e
L
8988 if (! do_debug_frames_interp && augmentation_data_len)
8989 {
bf59c5d5 8990 display_augmentation_data (augmentation_data, augmentation_data_len);
19e6b90e
L
8991 putchar ('\n');
8992 }
8993 }
8994
8995 /* At this point, fc is the current chunk, cie (if any) is set, and
8996 we're about to interpret instructions for the chunk. */
8997 /* ??? At present we need to do this always, since this sizes the
8998 fc->col_type and fc->col_offset arrays, which we write into always.
8999 We should probably split the interpreted and non-interpreted bits
9000 into two different routines, since there's so much that doesn't
9001 really overlap between them. */
9002 if (1 || do_debug_frames_interp)
9003 {
9004 /* Start by making a pass over the chunk, allocating storage
9005 and taking note of what registers are used. */
9006 unsigned char *tmp = start;
9007
9008 while (start < block_end)
9009 {
041830e0
NC
9010 unsigned int reg, op, opa;
9011 unsigned long temp;
5929c344 9012 unsigned char * new_start;
19e6b90e
L
9013
9014 op = *start++;
9015 opa = op & 0x3f;
9016 if (op & 0xc0)
9017 op &= 0xc0;
9018
9019 /* Warning: if you add any more cases to this switch, be
9020 sure to add them to the corresponding switch below. */
9021 switch (op)
9022 {
9023 case DW_CFA_advance_loc:
9024 break;
9025 case DW_CFA_offset:
cd30bcef 9026 SKIP_ULEB (start, end);
665ce1f6
L
9027 if (frame_need_space (fc, opa) >= 0)
9028 fc->col_type[opa] = DW_CFA_undefined;
19e6b90e
L
9029 break;
9030 case DW_CFA_restore:
665ce1f6
L
9031 if (frame_need_space (fc, opa) >= 0)
9032 fc->col_type[opa] = DW_CFA_undefined;
19e6b90e
L
9033 break;
9034 case DW_CFA_set_loc:
9035 start += encoded_ptr_size;
9036 break;
9037 case DW_CFA_advance_loc1:
9038 start += 1;
9039 break;
9040 case DW_CFA_advance_loc2:
9041 start += 2;
9042 break;
9043 case DW_CFA_advance_loc4:
9044 start += 4;
9045 break;
9046 case DW_CFA_offset_extended:
12eae2d3 9047 case DW_CFA_val_offset:
cd30bcef
AM
9048 READ_ULEB (reg, start, end);
9049 SKIP_ULEB (start, end);
665ce1f6
L
9050 if (frame_need_space (fc, reg) >= 0)
9051 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
9052 break;
9053 case DW_CFA_restore_extended:
cd30bcef 9054 READ_ULEB (reg, start, end);
665ce1f6
L
9055 if (frame_need_space (fc, reg) >= 0)
9056 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
9057 break;
9058 case DW_CFA_undefined:
cd30bcef 9059 READ_ULEB (reg, start, end);
665ce1f6
L
9060 if (frame_need_space (fc, reg) >= 0)
9061 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
9062 break;
9063 case DW_CFA_same_value:
cd30bcef 9064 READ_ULEB (reg, start, end);
665ce1f6
L
9065 if (frame_need_space (fc, reg) >= 0)
9066 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
9067 break;
9068 case DW_CFA_register:
cd30bcef
AM
9069 READ_ULEB (reg, start, end);
9070 SKIP_ULEB (start, end);
665ce1f6
L
9071 if (frame_need_space (fc, reg) >= 0)
9072 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
9073 break;
9074 case DW_CFA_def_cfa:
cd30bcef
AM
9075 SKIP_ULEB (start, end);
9076 SKIP_ULEB (start, end);
19e6b90e
L
9077 break;
9078 case DW_CFA_def_cfa_register:
cd30bcef 9079 SKIP_ULEB (start, end);
19e6b90e
L
9080 break;
9081 case DW_CFA_def_cfa_offset:
cd30bcef 9082 SKIP_ULEB (start, end);
19e6b90e
L
9083 break;
9084 case DW_CFA_def_cfa_expression:
cd30bcef 9085 READ_ULEB (temp, start, end);
5929c344
NC
9086 new_start = start + temp;
9087 if (new_start < start)
041830e0
NC
9088 {
9089 warn (_("Corrupt CFA_def expression value: %lu\n"), temp);
9090 start = block_end;
9091 }
9092 else
5929c344 9093 start = new_start;
19e6b90e
L
9094 break;
9095 case DW_CFA_expression:
12eae2d3 9096 case DW_CFA_val_expression:
cd30bcef
AM
9097 READ_ULEB (reg, start, end);
9098 READ_ULEB (temp, start, end);
5929c344
NC
9099 new_start = start + temp;
9100 if (new_start < start)
041830e0 9101 {
b4eb7656 9102 /* PR 17512: file:306-192417-0.005. */
041830e0
NC
9103 warn (_("Corrupt CFA expression value: %lu\n"), temp);
9104 start = block_end;
9105 }
9106 else
5929c344 9107 start = new_start;
665ce1f6
L
9108 if (frame_need_space (fc, reg) >= 0)
9109 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
9110 break;
9111 case DW_CFA_offset_extended_sf:
12eae2d3 9112 case DW_CFA_val_offset_sf:
cd30bcef
AM
9113 READ_ULEB (reg, start, end);
9114 SKIP_SLEB (start, end);
665ce1f6
L
9115 if (frame_need_space (fc, reg) >= 0)
9116 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
9117 break;
9118 case DW_CFA_def_cfa_sf:
cd30bcef
AM
9119 SKIP_ULEB (start, end);
9120 SKIP_SLEB (start, end);
19e6b90e
L
9121 break;
9122 case DW_CFA_def_cfa_offset_sf:
cd30bcef 9123 SKIP_SLEB (start, end);
19e6b90e
L
9124 break;
9125 case DW_CFA_MIPS_advance_loc8:
9126 start += 8;
9127 break;
9128 case DW_CFA_GNU_args_size:
cd30bcef 9129 SKIP_ULEB (start, end);
19e6b90e
L
9130 break;
9131 case DW_CFA_GNU_negative_offset_extended:
cd30bcef
AM
9132 READ_ULEB (reg, start, end);
9133 SKIP_ULEB (start, end);
665ce1f6
L
9134 if (frame_need_space (fc, reg) >= 0)
9135 fc->col_type[reg] = DW_CFA_undefined;
9136 break;
19e6b90e
L
9137 default:
9138 break;
9139 }
9140 }
9141 start = tmp;
9142 }
9143
015dc7e1 9144 all_nops = true;
5b6312fd 9145
19e6b90e
L
9146 /* Now we know what registers are used, make a second pass over
9147 the chunk, this time actually printing out the info. */
9148
9149 while (start < block_end)
9150 {
362beea4 9151 unsigned char * tmp;
19e6b90e 9152 unsigned op, opa;
7f2c8a1d
NC
9153 unsigned long ul, roffs;
9154 /* Note: It is tempting to use an unsigned long for 'reg' but there
9155 are various functions, notably frame_space_needed() that assume that
9156 reg is an unsigned int. */
9157 unsigned int reg;
9158 dwarf_signed_vma l;
bf5117e3 9159 dwarf_vma ofs;
19e6b90e 9160 dwarf_vma vma;
665ce1f6 9161 const char *reg_prefix = "";
19e6b90e
L
9162
9163 op = *start++;
9164 opa = op & 0x3f;
9165 if (op & 0xc0)
9166 op &= 0xc0;
9167
5b6312fd
NC
9168 /* Make a note if something other than DW_CFA_nop happens. */
9169 if (op != DW_CFA_nop)
015dc7e1 9170 all_nops = false;
5b6312fd 9171
19e6b90e
L
9172 /* Warning: if you add any more cases to this switch, be
9173 sure to add them to the corresponding switch above. */
9174 switch (op)
9175 {
9176 case DW_CFA_advance_loc:
9177 if (do_debug_frames_interp)
9178 frame_display_row (fc, &need_col_headers, &max_regs);
9179 else
bf5117e3 9180 printf (" DW_CFA_advance_loc: %d to %s\n",
19e6b90e 9181 opa * fc->code_factor,
b4eb7656 9182 dwarf_vmatoa_1 (NULL,
bf5117e3
NC
9183 fc->pc_begin + opa * fc->code_factor,
9184 fc->ptr_size));
19e6b90e
L
9185 fc->pc_begin += opa * fc->code_factor;
9186 break;
9187
9188 case DW_CFA_offset:
cd30bcef 9189 READ_ULEB (roffs, start, end);
665ce1f6
L
9190 if (opa >= (unsigned int) fc->ncols)
9191 reg_prefix = bad_reg;
9192 if (! do_debug_frames_interp || *reg_prefix != '\0')
9193 printf (" DW_CFA_offset: %s%s at cfa%+ld\n",
9194 reg_prefix, regname (opa, 0),
9195 roffs * fc->data_factor);
9196 if (*reg_prefix == '\0')
9197 {
9198 fc->col_type[opa] = DW_CFA_offset;
9199 fc->col_offset[opa] = roffs * fc->data_factor;
9200 }
19e6b90e
L
9201 break;
9202
9203 case DW_CFA_restore:
50751e18 9204 if (opa >= (unsigned int) fc->ncols)
665ce1f6
L
9205 reg_prefix = bad_reg;
9206 if (! do_debug_frames_interp || *reg_prefix != '\0')
9207 printf (" DW_CFA_restore: %s%s\n",
9208 reg_prefix, regname (opa, 0));
50751e18
AK
9209 if (*reg_prefix != '\0')
9210 break;
9211
9212 if (opa >= (unsigned int) cie->ncols
9213 || (do_debug_frames_interp
9214 && cie->col_type[opa] == DW_CFA_unreferenced))
9215 {
9216 fc->col_type[opa] = DW_CFA_undefined;
9217 fc->col_offset[opa] = 0;
9218 }
9219 else
665ce1f6
L
9220 {
9221 fc->col_type[opa] = cie->col_type[opa];
9222 fc->col_offset[opa] = cie->col_offset[opa];
9223 }
19e6b90e
L
9224 break;
9225
9226 case DW_CFA_set_loc:
6937bb54 9227 vma = get_encoded_value (&start, fc->fde_encoding, section, block_end);
19e6b90e
L
9228 if (do_debug_frames_interp)
9229 frame_display_row (fc, &need_col_headers, &max_regs);
9230 else
bf5117e3
NC
9231 printf (" DW_CFA_set_loc: %s\n",
9232 dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
19e6b90e
L
9233 fc->pc_begin = vma;
9234 break;
9235
9236 case DW_CFA_advance_loc1:
0c588247 9237 SAFE_BYTE_GET_AND_INC (ofs, start, 1, end);
19e6b90e
L
9238 if (do_debug_frames_interp)
9239 frame_display_row (fc, &need_col_headers, &max_regs);
9240 else
bf5117e3
NC
9241 printf (" DW_CFA_advance_loc1: %ld to %s\n",
9242 (unsigned long) (ofs * fc->code_factor),
9243 dwarf_vmatoa_1 (NULL,
9244 fc->pc_begin + ofs * fc->code_factor,
9245 fc->ptr_size));
19e6b90e
L
9246 fc->pc_begin += ofs * fc->code_factor;
9247 break;
9248
9249 case DW_CFA_advance_loc2:
6937bb54 9250 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
19e6b90e
L
9251 if (do_debug_frames_interp)
9252 frame_display_row (fc, &need_col_headers, &max_regs);
9253 else
bf5117e3
NC
9254 printf (" DW_CFA_advance_loc2: %ld to %s\n",
9255 (unsigned long) (ofs * fc->code_factor),
9256 dwarf_vmatoa_1 (NULL,
9257 fc->pc_begin + ofs * fc->code_factor,
9258 fc->ptr_size));
19e6b90e
L
9259 fc->pc_begin += ofs * fc->code_factor;
9260 break;
9261
9262 case DW_CFA_advance_loc4:
6937bb54 9263 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
19e6b90e
L
9264 if (do_debug_frames_interp)
9265 frame_display_row (fc, &need_col_headers, &max_regs);
9266 else
bf5117e3
NC
9267 printf (" DW_CFA_advance_loc4: %ld to %s\n",
9268 (unsigned long) (ofs * fc->code_factor),
9269 dwarf_vmatoa_1 (NULL,
9270 fc->pc_begin + ofs * fc->code_factor,
9271 fc->ptr_size));
19e6b90e
L
9272 fc->pc_begin += ofs * fc->code_factor;
9273 break;
9274
9275 case DW_CFA_offset_extended:
cd30bcef
AM
9276 READ_ULEB (reg, start, end);
9277 READ_ULEB (roffs, start, end);
665ce1f6
L
9278 if (reg >= (unsigned int) fc->ncols)
9279 reg_prefix = bad_reg;
9280 if (! do_debug_frames_interp || *reg_prefix != '\0')
9281 printf (" DW_CFA_offset_extended: %s%s at cfa%+ld\n",
9282 reg_prefix, regname (reg, 0),
9283 roffs * fc->data_factor);
9284 if (*reg_prefix == '\0')
9285 {
9286 fc->col_type[reg] = DW_CFA_offset;
9287 fc->col_offset[reg] = roffs * fc->data_factor;
9288 }
19e6b90e
L
9289 break;
9290
12eae2d3 9291 case DW_CFA_val_offset:
cd30bcef
AM
9292 READ_ULEB (reg, start, end);
9293 READ_ULEB (roffs, start, end);
665ce1f6
L
9294 if (reg >= (unsigned int) fc->ncols)
9295 reg_prefix = bad_reg;
9296 if (! do_debug_frames_interp || *reg_prefix != '\0')
084303b8 9297 printf (" DW_CFA_val_offset: %s%s is cfa%+ld\n",
665ce1f6
L
9298 reg_prefix, regname (reg, 0),
9299 roffs * fc->data_factor);
9300 if (*reg_prefix == '\0')
9301 {
9302 fc->col_type[reg] = DW_CFA_val_offset;
9303 fc->col_offset[reg] = roffs * fc->data_factor;
9304 }
12eae2d3
JJ
9305 break;
9306
19e6b90e 9307 case DW_CFA_restore_extended:
cd30bcef 9308 READ_ULEB (reg, start, end);
50751e18 9309 if (reg >= (unsigned int) fc->ncols)
665ce1f6
L
9310 reg_prefix = bad_reg;
9311 if (! do_debug_frames_interp || *reg_prefix != '\0')
9312 printf (" DW_CFA_restore_extended: %s%s\n",
9313 reg_prefix, regname (reg, 0));
50751e18
AK
9314 if (*reg_prefix != '\0')
9315 break;
9316
9317 if (reg >= (unsigned int) cie->ncols)
9318 {
9319 fc->col_type[reg] = DW_CFA_undefined;
9320 fc->col_offset[reg] = 0;
9321 }
9322 else
665ce1f6
L
9323 {
9324 fc->col_type[reg] = cie->col_type[reg];
9325 fc->col_offset[reg] = cie->col_offset[reg];
9326 }
19e6b90e
L
9327 break;
9328
9329 case DW_CFA_undefined:
cd30bcef 9330 READ_ULEB (reg, start, end);
665ce1f6
L
9331 if (reg >= (unsigned int) fc->ncols)
9332 reg_prefix = bad_reg;
9333 if (! do_debug_frames_interp || *reg_prefix != '\0')
9334 printf (" DW_CFA_undefined: %s%s\n",
9335 reg_prefix, regname (reg, 0));
9336 if (*reg_prefix == '\0')
9337 {
9338 fc->col_type[reg] = DW_CFA_undefined;
9339 fc->col_offset[reg] = 0;
9340 }
19e6b90e
L
9341 break;
9342
9343 case DW_CFA_same_value:
cd30bcef 9344 READ_ULEB (reg, start, end);
665ce1f6
L
9345 if (reg >= (unsigned int) fc->ncols)
9346 reg_prefix = bad_reg;
9347 if (! do_debug_frames_interp || *reg_prefix != '\0')
9348 printf (" DW_CFA_same_value: %s%s\n",
9349 reg_prefix, regname (reg, 0));
9350 if (*reg_prefix == '\0')
9351 {
9352 fc->col_type[reg] = DW_CFA_same_value;
9353 fc->col_offset[reg] = 0;
9354 }
19e6b90e
L
9355 break;
9356
9357 case DW_CFA_register:
cd30bcef
AM
9358 READ_ULEB (reg, start, end);
9359 READ_ULEB (roffs, start, end);
665ce1f6
L
9360 if (reg >= (unsigned int) fc->ncols)
9361 reg_prefix = bad_reg;
9362 if (! do_debug_frames_interp || *reg_prefix != '\0')
2dc4cec1 9363 {
665ce1f6
L
9364 printf (" DW_CFA_register: %s%s in ",
9365 reg_prefix, regname (reg, 0));
2dc4cec1
L
9366 puts (regname (roffs, 0));
9367 }
665ce1f6
L
9368 if (*reg_prefix == '\0')
9369 {
9370 fc->col_type[reg] = DW_CFA_register;
9371 fc->col_offset[reg] = roffs;
9372 }
19e6b90e
L
9373 break;
9374
9375 case DW_CFA_remember_state:
9376 if (! do_debug_frames_interp)
9377 printf (" DW_CFA_remember_state\n");
3f5e193b 9378 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
b4eb7656 9379 rs->cfa_offset = fc->cfa_offset;
d71ad7fc
RC
9380 rs->cfa_reg = fc->cfa_reg;
9381 rs->ra = fc->ra;
9382 rs->cfa_exp = fc->cfa_exp;
19e6b90e 9383 rs->ncols = fc->ncols;
3f5e193b 9384 rs->col_type = (short int *) xcmalloc (rs->ncols,
b4eb7656 9385 sizeof (* rs->col_type));
d71ad7fc
RC
9386 rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
9387 memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
9388 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
19e6b90e
L
9389 rs->next = remembered_state;
9390 remembered_state = rs;
9391 break;
9392
9393 case DW_CFA_restore_state:
9394 if (! do_debug_frames_interp)
9395 printf (" DW_CFA_restore_state\n");
9396 rs = remembered_state;
9397 if (rs)
9398 {
9399 remembered_state = rs->next;
d71ad7fc
RC
9400 fc->cfa_offset = rs->cfa_offset;
9401 fc->cfa_reg = rs->cfa_reg;
b4eb7656
AM
9402 fc->ra = rs->ra;
9403 fc->cfa_exp = rs->cfa_exp;
06614111
NC
9404 if (frame_need_space (fc, rs->ncols - 1) < 0)
9405 {
1306a742 9406 warn (_("Invalid column number in saved frame state\n"));
06614111
NC
9407 fc->ncols = 0;
9408 break;
9409 }
d71ad7fc 9410 memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
19e6b90e 9411 memcpy (fc->col_offset, rs->col_offset,
d71ad7fc 9412 rs->ncols * sizeof (* rs->col_offset));
19e6b90e
L
9413 free (rs->col_type);
9414 free (rs->col_offset);
9415 free (rs);
9416 }
9417 else if (do_debug_frames_interp)
9418 printf ("Mismatched DW_CFA_restore_state\n");
9419 break;
9420
9421 case DW_CFA_def_cfa:
cd30bcef
AM
9422 READ_ULEB (fc->cfa_reg, start, end);
9423 READ_ULEB (fc->cfa_offset, start, end);
19e6b90e
L
9424 fc->cfa_exp = 0;
9425 if (! do_debug_frames_interp)
2dc4cec1 9426 printf (" DW_CFA_def_cfa: %s ofs %d\n",
c8071705 9427 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
19e6b90e
L
9428 break;
9429
9430 case DW_CFA_def_cfa_register:
cd30bcef 9431 READ_ULEB (fc->cfa_reg, start, end);
19e6b90e
L
9432 fc->cfa_exp = 0;
9433 if (! do_debug_frames_interp)
2dc4cec1
L
9434 printf (" DW_CFA_def_cfa_register: %s\n",
9435 regname (fc->cfa_reg, 0));
19e6b90e
L
9436 break;
9437
9438 case DW_CFA_def_cfa_offset:
cd30bcef 9439 READ_ULEB (fc->cfa_offset, start, end);
19e6b90e 9440 if (! do_debug_frames_interp)
c8071705 9441 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
19e6b90e
L
9442 break;
9443
9444 case DW_CFA_nop:
9445 if (! do_debug_frames_interp)
9446 printf (" DW_CFA_nop\n");
9447 break;
9448
9449 case DW_CFA_def_cfa_expression:
cd30bcef 9450 READ_ULEB (ul, start, end);
7460c0ab 9451 if (start >= block_end || ul > (unsigned long) (block_end - start))
6937bb54 9452 {
a1165289 9453 printf (_(" DW_CFA_def_cfa_expression: <corrupt len %lu>\n"), ul);
6937bb54
NC
9454 break;
9455 }
19e6b90e
L
9456 if (! do_debug_frames_interp)
9457 {
9458 printf (" DW_CFA_def_cfa_expression (");
b7807392
JJ
9459 decode_location_expression (start, eh_addr_size, 0, -1,
9460 ul, 0, section);
19e6b90e
L
9461 printf (")\n");
9462 }
9463 fc->cfa_exp = 1;
9464 start += ul;
9465 break;
9466
9467 case DW_CFA_expression:
cd30bcef
AM
9468 READ_ULEB (reg, start, end);
9469 READ_ULEB (ul, start, end);
665ce1f6
L
9470 if (reg >= (unsigned int) fc->ncols)
9471 reg_prefix = bad_reg;
6937bb54 9472 /* PR 17512: file: 069-133014-0.006. */
06614111 9473 /* PR 17512: file: 98c02eb4. */
362beea4
NC
9474 tmp = start + ul;
9475 if (start >= block_end || tmp > block_end || tmp < start)
6937bb54 9476 {
a1165289 9477 printf (_(" DW_CFA_expression: <corrupt len %lu>\n"), ul);
6937bb54
NC
9478 break;
9479 }
665ce1f6 9480 if (! do_debug_frames_interp || *reg_prefix != '\0')
19e6b90e 9481 {
665ce1f6
L
9482 printf (" DW_CFA_expression: %s%s (",
9483 reg_prefix, regname (reg, 0));
b7807392 9484 decode_location_expression (start, eh_addr_size, 0, -1,
f1c4cc75 9485 ul, 0, section);
19e6b90e
L
9486 printf (")\n");
9487 }
665ce1f6
L
9488 if (*reg_prefix == '\0')
9489 fc->col_type[reg] = DW_CFA_expression;
362beea4 9490 start = tmp;
19e6b90e
L
9491 break;
9492
12eae2d3 9493 case DW_CFA_val_expression:
cd30bcef
AM
9494 READ_ULEB (reg, start, end);
9495 READ_ULEB (ul, start, end);
665ce1f6
L
9496 if (reg >= (unsigned int) fc->ncols)
9497 reg_prefix = bad_reg;
362beea4
NC
9498 tmp = start + ul;
9499 if (start >= block_end || tmp > block_end || tmp < start)
6937bb54 9500 {
a1165289 9501 printf (" DW_CFA_val_expression: <corrupt len %lu>\n", ul);
6937bb54
NC
9502 break;
9503 }
665ce1f6 9504 if (! do_debug_frames_interp || *reg_prefix != '\0')
12eae2d3 9505 {
665ce1f6
L
9506 printf (" DW_CFA_val_expression: %s%s (",
9507 reg_prefix, regname (reg, 0));
b7807392
JJ
9508 decode_location_expression (start, eh_addr_size, 0, -1,
9509 ul, 0, section);
12eae2d3
JJ
9510 printf (")\n");
9511 }
665ce1f6
L
9512 if (*reg_prefix == '\0')
9513 fc->col_type[reg] = DW_CFA_val_expression;
362beea4 9514 start = tmp;
12eae2d3
JJ
9515 break;
9516
19e6b90e 9517 case DW_CFA_offset_extended_sf:
cd30bcef
AM
9518 READ_ULEB (reg, start, end);
9519 READ_SLEB (l, start, end);
665ce1f6
L
9520 if (frame_need_space (fc, reg) < 0)
9521 reg_prefix = bad_reg;
9522 if (! do_debug_frames_interp || *reg_prefix != '\0')
9523 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
9524 reg_prefix, regname (reg, 0),
c8071705 9525 (long)(l * fc->data_factor));
665ce1f6
L
9526 if (*reg_prefix == '\0')
9527 {
9528 fc->col_type[reg] = DW_CFA_offset;
9529 fc->col_offset[reg] = l * fc->data_factor;
9530 }
19e6b90e
L
9531 break;
9532
12eae2d3 9533 case DW_CFA_val_offset_sf:
cd30bcef
AM
9534 READ_ULEB (reg, start, end);
9535 READ_SLEB (l, start, end);
665ce1f6
L
9536 if (frame_need_space (fc, reg) < 0)
9537 reg_prefix = bad_reg;
9538 if (! do_debug_frames_interp || *reg_prefix != '\0')
084303b8 9539 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+ld\n",
665ce1f6 9540 reg_prefix, regname (reg, 0),
c8071705 9541 (long)(l * fc->data_factor));
665ce1f6
L
9542 if (*reg_prefix == '\0')
9543 {
9544 fc->col_type[reg] = DW_CFA_val_offset;
9545 fc->col_offset[reg] = l * fc->data_factor;
9546 }
12eae2d3
JJ
9547 break;
9548
19e6b90e 9549 case DW_CFA_def_cfa_sf:
cd30bcef
AM
9550 READ_ULEB (fc->cfa_reg, start, end);
9551 READ_ULEB (fc->cfa_offset, start, end);
19e6b90e
L
9552 fc->cfa_offset = fc->cfa_offset * fc->data_factor;
9553 fc->cfa_exp = 0;
9554 if (! do_debug_frames_interp)
2dc4cec1 9555 printf (" DW_CFA_def_cfa_sf: %s ofs %d\n",
c8071705 9556 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
19e6b90e
L
9557 break;
9558
9559 case DW_CFA_def_cfa_offset_sf:
cd30bcef 9560 READ_ULEB (fc->cfa_offset, start, end);
c8071705 9561 fc->cfa_offset *= fc->data_factor;
19e6b90e 9562 if (! do_debug_frames_interp)
c8071705 9563 printf (" DW_CFA_def_cfa_offset_sf: %d\n", (int) fc->cfa_offset);
19e6b90e
L
9564 break;
9565
9566 case DW_CFA_MIPS_advance_loc8:
6937bb54 9567 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
19e6b90e
L
9568 if (do_debug_frames_interp)
9569 frame_display_row (fc, &need_col_headers, &max_regs);
9570 else
bf5117e3
NC
9571 printf (" DW_CFA_MIPS_advance_loc8: %ld to %s\n",
9572 (unsigned long) (ofs * fc->code_factor),
9573 dwarf_vmatoa_1 (NULL,
9574 fc->pc_begin + ofs * fc->code_factor,
9575 fc->ptr_size));
19e6b90e
L
9576 fc->pc_begin += ofs * fc->code_factor;
9577 break;
9578
9579 case DW_CFA_GNU_window_save:
9580 if (! do_debug_frames_interp)
9581 printf (" DW_CFA_GNU_window_save\n");
9582 break;
9583
9584 case DW_CFA_GNU_args_size:
cd30bcef 9585 READ_ULEB (ul, start, end);
19e6b90e
L
9586 if (! do_debug_frames_interp)
9587 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
9588 break;
9589
9590 case DW_CFA_GNU_negative_offset_extended:
cd30bcef
AM
9591 READ_ULEB (reg, start, end);
9592 READ_SLEB (l, start, end);
7f2c8a1d 9593 l = - l;
665ce1f6
L
9594 if (frame_need_space (fc, reg) < 0)
9595 reg_prefix = bad_reg;
9596 if (! do_debug_frames_interp || *reg_prefix != '\0')
9597 printf (" DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
9598 reg_prefix, regname (reg, 0),
c8071705 9599 (long)(l * fc->data_factor));
665ce1f6
L
9600 if (*reg_prefix == '\0')
9601 {
9602 fc->col_type[reg] = DW_CFA_offset;
9603 fc->col_offset[reg] = l * fc->data_factor;
9604 }
19e6b90e
L
9605 break;
9606
9607 default:
53b8873b
NC
9608 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
9609 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
9610 else
f41e4712 9611 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
19e6b90e
L
9612 start = block_end;
9613 }
9614 }
9615
5b6312fd
NC
9616 /* Interpret the CFA - as long as it is not completely full of NOPs. */
9617 if (do_debug_frames_interp && ! all_nops)
19e6b90e
L
9618 frame_display_row (fc, &need_col_headers, &max_regs);
9619
a788aedd
AM
9620 if (fde_fc.col_type != NULL)
9621 {
9622 free (fde_fc.col_type);
9623 fde_fc.col_type = NULL;
9624 }
9625 if (fde_fc.col_offset != NULL)
9626 {
9627 free (fde_fc.col_offset);
9628 fde_fc.col_offset = NULL;
9629 }
9630
19e6b90e 9631 start = block_end;
604282a7 9632 eh_addr_size = saved_eh_addr_size;
19e6b90e
L
9633 }
9634
9635 printf ("\n");
9636
3391569f
NC
9637 while (remembered_state != NULL)
9638 {
9639 rs = remembered_state;
9640 remembered_state = rs->next;
9641 free (rs->col_type);
9642 free (rs->col_offset);
9643 rs->next = NULL; /* Paranoia. */
9644 free (rs);
9645 }
9646
9647 while (chunks != NULL)
9648 {
9649 rs = chunks;
9650 chunks = rs->next;
9651 free (rs->col_type);
9652 free (rs->col_offset);
9653 rs->next = NULL; /* Paranoia. */
9654 free (rs);
9655 }
9656
9657 while (forward_refs != NULL)
9658 {
9659 rs = forward_refs;
9660 forward_refs = rs->next;
9661 free (rs->col_type);
9662 free (rs->col_offset);
9663 rs->next = NULL; /* Paranoia. */
9664 free (rs);
9665 }
9666
19e6b90e
L
9667 return 1;
9668}
9669
9670#undef GET
19e6b90e 9671
61364358
JK
9672static int
9673display_debug_names (struct dwarf_section *section, void *file)
9674{
9675 unsigned char *hdrptr = section->start;
9676 dwarf_vma unit_length;
9677 unsigned char *unit_start;
9678 const unsigned char *const section_end = section->start + section->size;
9679 unsigned char *unit_end;
9680
015dc7e1 9681 introduce (section, false);
61364358 9682
dda8d76d 9683 load_debug_section_with_follow (str, file);
61364358
JK
9684
9685 for (; hdrptr < section_end; hdrptr = unit_end)
9686 {
9687 unsigned int offset_size;
9688 uint16_t dwarf_version, padding;
9689 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
9690 uint32_t bucket_count, name_count, abbrev_table_size;
9691 uint32_t augmentation_string_size;
9692 unsigned int i;
e98fdf1a 9693 unsigned long sec_off;
015dc7e1 9694 bool augmentation_printable;
48467cb9 9695 const char *augmentation_string;
61364358
JK
9696
9697 unit_start = hdrptr;
9698
9699 /* Get and check the length of the block. */
9700 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
9701
9702 if (unit_length == 0xffffffff)
9703 {
9704 /* This section is 64-bit DWARF. */
9705 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
9706 offset_size = 8;
9707 }
9708 else
9709 offset_size = 4;
9710 unit_end = hdrptr + unit_length;
9711
e98fdf1a
AM
9712 sec_off = hdrptr - section->start;
9713 if (sec_off + unit_length < sec_off
9714 || sec_off + unit_length > section->size)
61364358 9715 {
e98fdf1a
AM
9716 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
9717 section->name,
9718 (unsigned long) (unit_start - section->start),
9719 dwarf_vmatoa ("x", unit_length));
61364358
JK
9720 return 0;
9721 }
9722
9723 /* Get and check the version number. */
9724 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
9725 printf (_("Version %ld\n"), (long) dwarf_version);
9726
9727 /* Prior versions did not exist, and future versions may not be
9728 backwards compatible. */
9729 if (dwarf_version != 5)
9730 {
9731 warn (_("Only DWARF version 5 .debug_names "
9732 "is currently supported.\n"));
9733 return 0;
9734 }
9735
9736 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
9737 if (padding != 0)
9738 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
9739 padding);
9740
9741 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
9742 if (comp_unit_count == 0)
9743 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
9744
9745 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
9746 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
9747 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
9748 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
9749 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
9750
9751 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
9752 if (augmentation_string_size % 4 != 0)
9753 {
9754 warn (_("Augmentation string length %u must be rounded up "
9755 "to a multiple of 4 in .debug_names.\n"),
9756 augmentation_string_size);
9757 augmentation_string_size += (-augmentation_string_size) & 3;
9758 }
48467cb9 9759
61364358 9760 printf (_("Augmentation string:"));
48467cb9 9761
015dc7e1 9762 augmentation_printable = true;
48467cb9
TV
9763 augmentation_string = (const char *) hdrptr;
9764
61364358
JK
9765 for (i = 0; i < augmentation_string_size; i++)
9766 {
9767 unsigned char uc;
9768
9769 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
9770 printf (" %02x", uc);
48467cb9
TV
9771
9772 if (uc != 0 && !ISPRINT (uc))
015dc7e1 9773 augmentation_printable = false;
48467cb9
TV
9774 }
9775
9776 if (augmentation_printable)
9777 {
9778 printf (" (\"");
9779 for (i = 0;
9780 i < augmentation_string_size && augmentation_string[i];
9781 ++i)
9782 putchar (augmentation_string[i]);
9783 printf ("\")");
61364358 9784 }
61364358
JK
9785 putchar ('\n');
9786
9787 printf (_("CU table:\n"));
9788 for (i = 0; i < comp_unit_count; i++)
9789 {
9790 uint64_t cu_offset;
9791
9792 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
9793 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) cu_offset);
9794 }
9795 putchar ('\n');
9796
9797 printf (_("TU table:\n"));
9798 for (i = 0; i < local_type_unit_count; i++)
9799 {
9800 uint64_t tu_offset;
9801
9802 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
9803 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) tu_offset);
9804 }
9805 putchar ('\n');
9806
9807 printf (_("Foreign TU table:\n"));
9808 for (i = 0; i < foreign_type_unit_count; i++)
9809 {
9810 uint64_t signature;
9811
9812 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
9813 printf (_("[%3u] "), i);
9814 print_dwarf_vma (signature, 8);
9815 putchar ('\n');
9816 }
9817 putchar ('\n');
9818
9819 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
9820 hdrptr += bucket_count * sizeof (uint32_t);
9821 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
9822 hdrptr += name_count * sizeof (uint32_t);
9823 unsigned char *const name_table_string_offsets = hdrptr;
9824 hdrptr += name_count * offset_size;
9825 unsigned char *const name_table_entry_offsets = hdrptr;
9826 hdrptr += name_count * offset_size;
9827 unsigned char *const abbrev_table = hdrptr;
9828 hdrptr += abbrev_table_size;
9829 const unsigned char *const abbrev_table_end = hdrptr;
9830 unsigned char *const entry_pool = hdrptr;
9831 if (hdrptr > unit_end)
9832 {
9833 warn (_("Entry pool offset (0x%lx) exceeds unit size 0x%lx "
9834 "for unit 0x%lx in the debug_names\n"),
9835 (long) (hdrptr - section->start),
9836 (long) (unit_end - section->start),
9837 (long) (unit_start - section->start));
9838 return 0;
9839 }
9840
9841 size_t buckets_filled = 0;
9842 size_t bucketi;
9843 for (bucketi = 0; bucketi < bucket_count; bucketi++)
9844 {
9845 const uint32_t bucket = hash_table_buckets[bucketi];
9846
9847 if (bucket != 0)
9848 ++buckets_filled;
9849 }
d3a49aa8
AM
9850 printf (ngettext ("Used %zu of %lu bucket.\n",
9851 "Used %zu of %lu buckets.\n",
9852 bucket_count),
9853 buckets_filled, (unsigned long) bucket_count);
61364358 9854
0a79bef4 9855 uint32_t hash_prev = 0;
61364358
JK
9856 size_t hash_clash_count = 0;
9857 size_t longest_clash = 0;
9858 size_t this_length = 0;
9859 size_t hashi;
9860 for (hashi = 0; hashi < name_count; hashi++)
9861 {
9862 const uint32_t hash_this = hash_table_hashes[hashi];
9863
9864 if (hashi > 0)
9865 {
9866 if (hash_prev % bucket_count == hash_this % bucket_count)
9867 {
9868 ++hash_clash_count;
9869 ++this_length;
9870 longest_clash = MAX (longest_clash, this_length);
9871 }
9872 else
9873 this_length = 0;
9874 }
9875 hash_prev = hash_this;
9876 }
9877 printf (_("Out of %lu items there are %zu bucket clashes"
9878 " (longest of %zu entries).\n"),
9879 (unsigned long) name_count, hash_clash_count, longest_clash);
9880 assert (name_count == buckets_filled + hash_clash_count);
9881
9882 struct abbrev_lookup_entry
9883 {
9884 dwarf_vma abbrev_tag;
9885 unsigned char *abbrev_lookup_ptr;
9886 };
9887 struct abbrev_lookup_entry *abbrev_lookup = NULL;
9888 size_t abbrev_lookup_used = 0;
9889 size_t abbrev_lookup_allocated = 0;
9890
9891 unsigned char *abbrevptr = abbrev_table;
9892 for (;;)
9893 {
cd30bcef
AM
9894 dwarf_vma abbrev_tag;
9895
9896 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
61364358
JK
9897 if (abbrev_tag == 0)
9898 break;
9899 if (abbrev_lookup_used == abbrev_lookup_allocated)
9900 {
9901 abbrev_lookup_allocated = MAX (0x100,
9902 abbrev_lookup_allocated * 2);
9903 abbrev_lookup = xrealloc (abbrev_lookup,
9904 (abbrev_lookup_allocated
9905 * sizeof (*abbrev_lookup)));
9906 }
9907 assert (abbrev_lookup_used < abbrev_lookup_allocated);
9908 struct abbrev_lookup_entry *entry;
9909 for (entry = abbrev_lookup;
9910 entry < abbrev_lookup + abbrev_lookup_used;
9911 entry++)
9912 if (entry->abbrev_tag == abbrev_tag)
9913 {
9914 warn (_("Duplicate abbreviation tag %lu "
9915 "in unit 0x%lx in the debug_names\n"),
9916 (long) abbrev_tag, (long) (unit_start - section->start));
9917 break;
9918 }
9919 entry = &abbrev_lookup[abbrev_lookup_used++];
9920 entry->abbrev_tag = abbrev_tag;
9921 entry->abbrev_lookup_ptr = abbrevptr;
9922
9923 /* Skip DWARF tag. */
cd30bcef 9924 SKIP_ULEB (abbrevptr, abbrev_table_end);
61364358
JK
9925 for (;;)
9926 {
cd30bcef
AM
9927 dwarf_vma xindex, form;
9928
9929 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
9930 READ_ULEB (form, abbrevptr, abbrev_table_end);
1d827a72 9931 if (xindex == 0 && form == 0)
61364358
JK
9932 break;
9933 }
9934 }
9935
9936 printf (_("\nSymbol table:\n"));
9937 uint32_t namei;
9938 for (namei = 0; namei < name_count; ++namei)
9939 {
9940 uint64_t string_offset, entry_offset;
9941
9942 SAFE_BYTE_GET (string_offset,
9943 name_table_string_offsets + namei * offset_size,
9944 offset_size, unit_end);
9945 SAFE_BYTE_GET (entry_offset,
9946 name_table_entry_offsets + namei * offset_size,
9947 offset_size, unit_end);
9948
9949 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
9950 fetch_indirect_string (string_offset));
9951
9952 unsigned char *entryptr = entry_pool + entry_offset;
9953
279edac5
AM
9954 /* We need to scan first whether there is a single or multiple
9955 entries. TAGNO is -2 for the first entry, it is -1 for the
9956 initial tag read of the second entry, then it becomes 0 for the
9957 first entry for real printing etc. */
61364358
JK
9958 int tagno = -2;
9959 /* Initialize it due to a false compiler warning. */
9960 dwarf_vma second_abbrev_tag = -1;
9961 for (;;)
9962 {
cd30bcef
AM
9963 dwarf_vma abbrev_tag;
9964 dwarf_vma dwarf_tag;
9965 const struct abbrev_lookup_entry *entry;
9966
9967 READ_ULEB (abbrev_tag, entryptr, unit_end);
61364358
JK
9968 if (tagno == -1)
9969 {
9970 second_abbrev_tag = abbrev_tag;
9971 tagno = 0;
9972 entryptr = entry_pool + entry_offset;
9973 continue;
9974 }
9975 if (abbrev_tag == 0)
9976 break;
9977 if (tagno >= 0)
9978 printf ("%s<%lu>",
9979 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
9980 (unsigned long) abbrev_tag);
9981
61364358
JK
9982 for (entry = abbrev_lookup;
9983 entry < abbrev_lookup + abbrev_lookup_used;
9984 entry++)
9985 if (entry->abbrev_tag == abbrev_tag)
9986 break;
9987 if (entry >= abbrev_lookup + abbrev_lookup_used)
9988 {
9989 warn (_("Undefined abbreviation tag %lu "
9990 "in unit 0x%lx in the debug_names\n"),
9991 (long) abbrev_tag,
9992 (long) (unit_start - section->start));
9993 break;
9994 }
9995 abbrevptr = entry->abbrev_lookup_ptr;
cd30bcef 9996 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
61364358
JK
9997 if (tagno >= 0)
9998 printf (" %s", get_TAG_name (dwarf_tag));
9999 for (;;)
10000 {
cd30bcef
AM
10001 dwarf_vma xindex, form;
10002
10003 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10004 READ_ULEB (form, abbrevptr, abbrev_table_end);
1d827a72 10005 if (xindex == 0 && form == 0)
61364358
JK
10006 break;
10007
10008 if (tagno >= 0)
1d827a72 10009 printf (" %s", get_IDX_name (xindex));
ec1b0fbb
NC
10010 entryptr = read_and_display_attr_value (0, form, 0,
10011 unit_start, entryptr, unit_end,
10012 0, 0, offset_size,
61364358
JK
10013 dwarf_version, NULL,
10014 (tagno < 0), NULL,
ec1b0fbb 10015 NULL, '=', -1);
61364358
JK
10016 }
10017 ++tagno;
10018 }
10019 if (tagno <= 0)
10020 printf (_(" <no entries>"));
10021 putchar ('\n');
10022 }
10023
10024 free (abbrev_lookup);
10025 }
10026
10027 return 1;
10028}
10029
dda8d76d 10030static int
d85bf2ba
NC
10031display_debug_links (struct dwarf_section * section,
10032 void * file ATTRIBUTE_UNUSED)
dda8d76d
NC
10033{
10034 const unsigned char * filename;
10035 unsigned int filelen;
10036
015dc7e1 10037 introduce (section, false);
dda8d76d
NC
10038
10039 /* The .gnu_debuglink section is formatted as:
10040 (c-string) Filename.
10041 (padding) If needed to reach a 4 byte boundary.
10042 (uint32_t) CRC32 value.
10043
10044 The .gun_debugaltlink section is formatted as:
10045 (c-string) Filename.
10046 (binary) Build-ID. */
521d4b19 10047
dda8d76d
NC
10048 filename = section->start;
10049 filelen = strnlen ((const char *) filename, section->size);
10050 if (filelen == section->size)
10051 {
10052 warn (_("The debuglink filename is corrupt/missing\n"));
10053 return 0;
10054 }
10055
10056 printf (_(" Separate debug info file: %s\n"), filename);
10057
24d127aa 10058 if (startswith (section->name, ".gnu_debuglink"))
dda8d76d
NC
10059 {
10060 unsigned int crc32;
10061 unsigned int crc_offset;
10062
10063 crc_offset = filelen + 1;
10064 crc_offset = (crc_offset + 3) & ~3;
10065 if (crc_offset + 4 > section->size)
10066 {
10067 warn (_("CRC offset missing/truncated\n"));
10068 return 0;
10069 }
10070
10071 crc32 = byte_get (filename + crc_offset, 4);
10072
10073 printf (_(" CRC value: %#x\n"), crc32);
10074
10075 if (crc_offset + 4 < section->size)
10076 {
10077 warn (_("There are %#lx extraneous bytes at the end of the section\n"),
10078 (long)(section->size - (crc_offset + 4)));
10079 return 0;
10080 }
10081 }
24d127aa 10082 else /* startswith (section->name, ".gnu_debugaltlink") */
dda8d76d
NC
10083 {
10084 const unsigned char * build_id = section->start + filelen + 1;
10085 bfd_size_type build_id_len = section->size - (filelen + 1);
10086 bfd_size_type printed;
10087
10088 /* FIXME: Should we support smaller build-id notes ? */
10089 if (build_id_len < 0x14)
10090 {
10091 warn (_("Build-ID is too short (%#lx bytes)\n"), (long) build_id_len);
10092 return 0;
10093 }
10094
10095 printed = printf (_(" Build-ID (%#lx bytes):"), (long) build_id_len);
d85bf2ba 10096 display_data (printed, build_id, build_id_len);
dda8d76d
NC
10097 putchar ('\n');
10098 }
10099
10100 putchar ('\n');
10101 return 1;
10102}
10103
5bbdf3d5
DE
10104static int
10105display_gdb_index (struct dwarf_section *section,
10106 void *file ATTRIBUTE_UNUSED)
10107{
10108 unsigned char *start = section->start;
10109 uint32_t version;
10110 uint32_t cu_list_offset, tu_list_offset;
10111 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10112 unsigned int cu_list_elements, tu_list_elements;
10113 unsigned int address_table_size, symbol_table_slots;
10114 unsigned char *cu_list, *tu_list;
10115 unsigned char *address_table, *symbol_table, *constant_pool;
10116 unsigned int i;
10117
10118 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10119
015dc7e1 10120 introduce (section, false);
5bbdf3d5
DE
10121
10122 if (section->size < 6 * sizeof (uint32_t))
10123 {
10124 warn (_("Truncated header in the %s section.\n"), section->name);
10125 return 0;
10126 }
10127
10128 version = byte_get_little_endian (start, 4);
da88a764 10129 printf (_("Version %ld\n"), (long) version);
5bbdf3d5
DE
10130
10131 /* Prior versions are obsolete, and future versions may not be
10132 backwards compatible. */
aa170720 10133 if (version < 3 || version > 8)
5bbdf3d5 10134 {
da88a764 10135 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
5bbdf3d5
DE
10136 return 0;
10137 }
8d6eee87
TT
10138 if (version < 4)
10139 warn (_("The address table data in version 3 may be wrong.\n"));
10140 if (version < 5)
10141 warn (_("Version 4 does not support case insensitive lookups.\n"));
10142 if (version < 6)
10143 warn (_("Version 5 does not include inlined functions.\n"));
10144 if (version < 7)
10145 warn (_("Version 6 does not include symbol attributes.\n"));
aa170720
DE
10146 /* Version 7 indices generated by Gold have bad type unit references,
10147 PR binutils/15021. But we don't know if the index was generated by
10148 Gold or not, so to avoid worrying users with gdb-generated indices
10149 we say nothing for version 7 here. */
5bbdf3d5
DE
10150
10151 cu_list_offset = byte_get_little_endian (start + 4, 4);
10152 tu_list_offset = byte_get_little_endian (start + 8, 4);
10153 address_table_offset = byte_get_little_endian (start + 12, 4);
10154 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10155 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10156
10157 if (cu_list_offset > section->size
10158 || tu_list_offset > section->size
10159 || address_table_offset > section->size
10160 || symbol_table_offset > section->size
10161 || constant_pool_offset > section->size)
10162 {
10163 warn (_("Corrupt header in the %s section.\n"), section->name);
10164 return 0;
10165 }
10166
53774b7e
NC
10167 /* PR 17531: file: 418d0a8a. */
10168 if (tu_list_offset < cu_list_offset)
10169 {
10170 warn (_("TU offset (%x) is less than CU offset (%x)\n"),
10171 tu_list_offset, cu_list_offset);
10172 return 0;
10173 }
10174
5bbdf3d5 10175 cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
53774b7e
NC
10176
10177 if (address_table_offset < tu_list_offset)
10178 {
10179 warn (_("Address table offset (%x) is less than TU offset (%x)\n"),
10180 address_table_offset, tu_list_offset);
10181 return 0;
10182 }
10183
5bbdf3d5 10184 tu_list_elements = (address_table_offset - tu_list_offset) / 8;
53774b7e
NC
10185
10186 /* PR 17531: file: 18a47d3d. */
10187 if (symbol_table_offset < address_table_offset)
10188 {
13bace4a 10189 warn (_("Symbol table offset (%x) is less then Address table offset (%x)\n"),
53774b7e
NC
10190 symbol_table_offset, address_table_offset);
10191 return 0;
10192 }
10193
5bbdf3d5 10194 address_table_size = symbol_table_offset - address_table_offset;
53774b7e
NC
10195
10196 if (constant_pool_offset < symbol_table_offset)
10197 {
10198 warn (_("Constant pool offset (%x) is less than symbol table offset (%x)\n"),
10199 constant_pool_offset, symbol_table_offset);
10200 return 0;
10201 }
10202
5bbdf3d5
DE
10203 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10204
10205 cu_list = start + cu_list_offset;
10206 tu_list = start + tu_list_offset;
10207 address_table = start + address_table_offset;
10208 symbol_table = start + symbol_table_offset;
10209 constant_pool = start + constant_pool_offset;
10210
28d909e5 10211 if (address_table + address_table_size > section->start + section->size)
acff9664 10212 {
1306a742 10213 warn (_("Address table extends beyond end of section.\n"));
acff9664
NC
10214 return 0;
10215 }
b4eb7656 10216
5bbdf3d5
DE
10217 printf (_("\nCU table:\n"));
10218 for (i = 0; i < cu_list_elements; i += 2)
10219 {
10220 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
10221 uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
10222
10223 printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
10224 (unsigned long) cu_offset,
10225 (unsigned long) (cu_offset + cu_length - 1));
10226 }
10227
10228 printf (_("\nTU table:\n"));
10229 for (i = 0; i < tu_list_elements; i += 3)
10230 {
10231 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
10232 uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
10233 uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
10234
10235 printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
10236 (unsigned long) tu_offset,
10237 (unsigned long) type_offset);
10238 print_dwarf_vma (signature, 8);
10239 printf ("\n");
10240 }
10241
10242 printf (_("\nAddress table:\n"));
acff9664
NC
10243 for (i = 0; i < address_table_size && i <= address_table_size - (2 * 8 + 4);
10244 i += 2 * 8 + 4)
5bbdf3d5
DE
10245 {
10246 uint64_t low = byte_get_little_endian (address_table + i, 8);
10247 uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
10248 uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
10249
10250 print_dwarf_vma (low, 8);
10251 print_dwarf_vma (high, 8);
da88a764 10252 printf (_("%lu\n"), (unsigned long) cu_index);
5bbdf3d5
DE
10253 }
10254
10255 printf (_("\nSymbol table:\n"));
10256 for (i = 0; i < symbol_table_slots; ++i)
10257 {
10258 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10259 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10260 uint32_t num_cus, cu;
10261
10262 if (name_offset != 0
10263 || cu_vector_offset != 0)
10264 {
10265 unsigned int j;
362beea4 10266 unsigned char * adr;
5bbdf3d5 10267
362beea4 10268 adr = constant_pool + name_offset;
53774b7e 10269 /* PR 17531: file: 5b7b07ad. */
362beea4 10270 if (adr < constant_pool || adr >= section->start + section->size)
53774b7e
NC
10271 {
10272 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10273 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10274 name_offset, i);
10275 }
10276 else
acff9664
NC
10277 printf ("[%3u] %.*s:", i,
10278 (int) (section->size - (constant_pool_offset + name_offset)),
10279 constant_pool + name_offset);
53774b7e 10280
362beea4
NC
10281 adr = constant_pool + cu_vector_offset;
10282 if (adr < constant_pool || adr >= section->start + section->size - 3)
53774b7e
NC
10283 {
10284 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10285 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10286 cu_vector_offset, i);
10287 continue;
10288 }
57028622 10289
362beea4 10290 num_cus = byte_get_little_endian (adr, 4);
53774b7e 10291
362beea4 10292 adr = constant_pool + cu_vector_offset + 4 + num_cus * 4;
acff9664 10293 if (num_cus * 4 < num_cus
362beea4
NC
10294 || adr >= section->start + section->size
10295 || adr < constant_pool)
53774b7e
NC
10296 {
10297 printf ("<invalid number of CUs: %d>\n", num_cus);
acff9664 10298 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
53774b7e
NC
10299 num_cus, i);
10300 continue;
10301 }
10302
8d6eee87
TT
10303 if (num_cus > 1)
10304 printf ("\n");
f3853b34 10305
5bbdf3d5
DE
10306 for (j = 0; j < num_cus; ++j)
10307 {
7c1cef97 10308 int is_static;
8d6eee87
TT
10309 gdb_index_symbol_kind kind;
10310
5bbdf3d5 10311 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
7c1cef97 10312 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
8d6eee87
TT
10313 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10314 cu = GDB_INDEX_CU_VALUE (cu);
5bbdf3d5 10315 /* Convert to TU number if it's for a type unit. */
ad6b52dd 10316 if (cu >= cu_list_elements / 2)
8d6eee87
TT
10317 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10318 (unsigned long) (cu - cu_list_elements / 2));
5bbdf3d5 10319 else
8d6eee87
TT
10320 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10321
459d52c8
DE
10322 printf (" [%s, %s]",
10323 is_static ? _("static") : _("global"),
10324 get_gdb_index_symbol_kind_name (kind));
8d6eee87
TT
10325 if (num_cus > 1)
10326 printf ("\n");
5bbdf3d5 10327 }
8d6eee87
TT
10328 if (num_cus <= 1)
10329 printf ("\n");
5bbdf3d5
DE
10330 }
10331 }
10332
10333 return 1;
10334}
10335
657d0d47
CC
10336/* Pre-allocate enough space for the CU/TU sets needed. */
10337
10338static void
10339prealloc_cu_tu_list (unsigned int nshndx)
10340{
10341 if (shndx_pool == NULL)
10342 {
10343 shndx_pool_size = nshndx;
10344 shndx_pool_used = 0;
10345 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10346 sizeof (unsigned int));
10347 }
10348 else
10349 {
10350 shndx_pool_size = shndx_pool_used + nshndx;
10351 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10352 sizeof (unsigned int));
10353 }
10354}
10355
10356static void
10357add_shndx_to_cu_tu_entry (unsigned int shndx)
10358{
10359 if (shndx_pool_used >= shndx_pool_size)
10360 {
10361 error (_("Internal error: out of space in the shndx pool.\n"));
10362 return;
10363 }
10364 shndx_pool [shndx_pool_used++] = shndx;
10365}
10366
10367static void
10368end_cu_tu_entry (void)
10369{
10370 if (shndx_pool_used >= shndx_pool_size)
10371 {
10372 error (_("Internal error: out of space in the shndx pool.\n"));
10373 return;
10374 }
10375 shndx_pool [shndx_pool_used++] = 0;
10376}
10377
341f9135
CC
10378/* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10379
10380static const char *
10381get_DW_SECT_short_name (unsigned int dw_sect)
10382{
10383 static char buf[16];
10384
10385 switch (dw_sect)
10386 {
10387 case DW_SECT_INFO:
10388 return "info";
10389 case DW_SECT_TYPES:
10390 return "types";
10391 case DW_SECT_ABBREV:
10392 return "abbrev";
10393 case DW_SECT_LINE:
10394 return "line";
10395 case DW_SECT_LOC:
10396 return "loc";
10397 case DW_SECT_STR_OFFSETS:
10398 return "str_off";
10399 case DW_SECT_MACINFO:
10400 return "macinfo";
10401 case DW_SECT_MACRO:
10402 return "macro";
10403 default:
b4eb7656 10404 break;
341f9135
CC
10405 }
10406
10407 snprintf (buf, sizeof (buf), "%d", dw_sect);
10408 return buf;
10409}
10410
10411/* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10412 These sections are extensions for Fission.
10413 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
657d0d47
CC
10414
10415static int
10416process_cu_tu_index (struct dwarf_section *section, int do_display)
10417{
10418 unsigned char *phdr = section->start;
10419 unsigned char *limit = phdr + section->size;
10420 unsigned char *phash;
10421 unsigned char *pindex;
10422 unsigned char *ppool;
10423 unsigned int version;
341f9135 10424 unsigned int ncols = 0;
657d0d47
CC
10425 unsigned int nused;
10426 unsigned int nslots;
10427 unsigned int i;
341f9135
CC
10428 unsigned int j;
10429 dwarf_vma signature_high;
10430 dwarf_vma signature_low;
10431 char buf[64];
657d0d47 10432
6937bb54
NC
10433 /* PR 17512: file: 002-168123-0.004. */
10434 if (phdr == NULL)
10435 {
10436 warn (_("Section %s is empty\n"), section->name);
10437 return 0;
10438 }
10439 /* PR 17512: file: 002-376-0.004. */
10440 if (section->size < 24)
10441 {
72c61a0d 10442 warn (_("Section %s is too small to contain a CU/TU header\n"),
6937bb54
NC
10443 section->name);
10444 return 0;
10445 }
10446
10447 SAFE_BYTE_GET (version, phdr, 4, limit);
341f9135 10448 if (version >= 2)
6937bb54
NC
10449 SAFE_BYTE_GET (ncols, phdr + 4, 4, limit);
10450 SAFE_BYTE_GET (nused, phdr + 8, 4, limit);
10451 SAFE_BYTE_GET (nslots, phdr + 12, 4, limit);
10452
657d0d47 10453 phash = phdr + 16;
8e2e3c6c
AM
10454 pindex = phash + (size_t) nslots * 8;
10455 ppool = pindex + (size_t) nslots * 4;
57028622 10456
657d0d47
CC
10457 if (do_display)
10458 {
015dc7e1 10459 introduce (section, false);
dda8d76d 10460
8e2e3c6c 10461 printf (_(" Version: %u\n"), version);
341f9135 10462 if (version >= 2)
8e2e3c6c
AM
10463 printf (_(" Number of columns: %u\n"), ncols);
10464 printf (_(" Number of used entries: %u\n"), nused);
10465 printf (_(" Number of slots: %u\n\n"), nslots);
657d0d47
CC
10466 }
10467
8e2e3c6c
AM
10468 /* PR 17531: file: 45d69832. */
10469 if ((size_t) nslots * 8 / 8 != nslots
10470 || phash < phdr || phash > limit
10471 || pindex < phash || pindex > limit
10472 || ppool < pindex || ppool > limit)
657d0d47 10473 {
8e2e3c6c
AM
10474 warn (ngettext ("Section %s is too small for %u slot\n",
10475 "Section %s is too small for %u slots\n",
10476 nslots),
657d0d47
CC
10477 section->name, nslots);
10478 return 0;
10479 }
10480
341f9135 10481 if (version == 1)
657d0d47 10482 {
341f9135
CC
10483 if (!do_display)
10484 prealloc_cu_tu_list ((limit - ppool) / 4);
10485 for (i = 0; i < nslots; i++)
657d0d47 10486 {
341f9135
CC
10487 unsigned char *shndx_list;
10488 unsigned int shndx;
10489
6937bb54 10490 SAFE_BYTE_GET64 (phash, &signature_high, &signature_low, limit);
341f9135 10491 if (signature_high != 0 || signature_low != 0)
657d0d47 10492 {
6937bb54 10493 SAFE_BYTE_GET (j, pindex, 4, limit);
341f9135 10494 shndx_list = ppool + j * 4;
f3853b34
NC
10495 /* PR 17531: file: 705e010d. */
10496 if (shndx_list < ppool)
10497 {
10498 warn (_("Section index pool located before start of section\n"));
10499 return 0;
10500 }
10501
341f9135
CC
10502 if (do_display)
10503 printf (_(" [%3d] Signature: 0x%s Sections: "),
10504 i, dwarf_vmatoa64 (signature_high, signature_low,
10505 buf, sizeof (buf)));
10506 for (;;)
657d0d47 10507 {
341f9135
CC
10508 if (shndx_list >= limit)
10509 {
10510 warn (_("Section %s too small for shndx pool\n"),
10511 section->name);
10512 return 0;
10513 }
6937bb54 10514 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
341f9135
CC
10515 if (shndx == 0)
10516 break;
10517 if (do_display)
10518 printf (" %d", shndx);
10519 else
10520 add_shndx_to_cu_tu_entry (shndx);
10521 shndx_list += 4;
657d0d47 10522 }
657d0d47 10523 if (do_display)
341f9135 10524 printf ("\n");
657d0d47 10525 else
341f9135
CC
10526 end_cu_tu_entry ();
10527 }
10528 phash += 8;
10529 pindex += 4;
10530 }
10531 }
10532 else if (version == 2)
10533 {
10534 unsigned int val;
10535 unsigned int dw_sect;
10536 unsigned char *ph = phash;
10537 unsigned char *pi = pindex;
8e2e3c6c
AM
10538 unsigned char *poffsets = ppool + (size_t) ncols * 4;
10539 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
10540 unsigned char *pend = psizes + (size_t) nused * ncols * 4;
015dc7e1 10541 bool is_tu_index;
341f9135
CC
10542 struct cu_tu_set *this_set = NULL;
10543 unsigned int row;
10544 unsigned char *prow;
10545
10546 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
10547
362beea4 10548 /* PR 17531: file: 0dd159bf.
8e2e3c6c
AM
10549 Check for integer overflow (can occur when size_t is 32-bit)
10550 with overlarge ncols or nused values. */
4ac948a0
NC
10551 if (ncols > 0
10552 && ((size_t) ncols * 4 / 4 != ncols
10553 || (size_t) nused * ncols * 4 / ((size_t) ncols * 4) != nused
10554 || poffsets < ppool || poffsets > limit
10555 || psizes < poffsets || psizes > limit
10556 || pend < psizes || pend > limit))
341f9135
CC
10557 {
10558 warn (_("Section %s too small for offset and size tables\n"),
10559 section->name);
10560 return 0;
10561 }
10562
10563 if (do_display)
10564 {
10565 printf (_(" Offset table\n"));
10566 printf (" slot %-16s ",
10567 is_tu_index ? _("signature") : _("dwo_id"));
10568 }
10569 else
10570 {
10571 if (is_tu_index)
10572 {
10573 tu_count = nused;
72c61a0d 10574 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
341f9135 10575 this_set = tu_sets;
657d0d47 10576 }
657d0d47 10577 else
341f9135
CC
10578 {
10579 cu_count = nused;
72c61a0d 10580 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
341f9135
CC
10581 this_set = cu_sets;
10582 }
10583 }
6937bb54 10584
341f9135
CC
10585 if (do_display)
10586 {
10587 for (j = 0; j < ncols; j++)
10588 {
6937bb54 10589 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
341f9135
CC
10590 printf (" %8s", get_DW_SECT_short_name (dw_sect));
10591 }
10592 printf ("\n");
10593 }
6937bb54 10594
341f9135
CC
10595 for (i = 0; i < nslots; i++)
10596 {
6937bb54
NC
10597 SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
10598
10599 SAFE_BYTE_GET (row, pi, 4, limit);
341f9135
CC
10600 if (row != 0)
10601 {
591f7597 10602 /* PR 17531: file: a05f6ab3. */
ef77750e 10603 if (row > nused)
591f7597
NC
10604 {
10605 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
10606 row, nused);
10607 return 0;
10608 }
10609
341f9135 10610 if (!do_display)
6aea08d9
NC
10611 {
10612 size_t num_copy = sizeof (uint64_t);
10613
10614 /* PR 23064: Beware of buffer overflow. */
10615 if (ph + num_copy < limit)
10616 memcpy (&this_set[row - 1].signature, ph, num_copy);
10617 else
10618 {
10619 warn (_("Signature (%p) extends beyond end of space in section\n"), ph);
10620 return 0;
10621 }
10622 }
6937bb54 10623
341f9135 10624 prow = poffsets + (row - 1) * ncols * 4;
ffc0f143
NC
10625 /* PR 17531: file: b8ce60a8. */
10626 if (prow < poffsets || prow > limit)
10627 {
10628 warn (_("Row index (%u) * num columns (%u) > space remaining in section\n"),
10629 row, ncols);
10630 return 0;
10631 }
3aade688 10632
341f9135
CC
10633 if (do_display)
10634 printf (_(" [%3d] 0x%s"),
10635 i, dwarf_vmatoa64 (signature_high, signature_low,
10636 buf, sizeof (buf)));
10637 for (j = 0; j < ncols; j++)
10638 {
6937bb54 10639 SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
341f9135
CC
10640 if (do_display)
10641 printf (" %8d", val);
10642 else
10643 {
6937bb54 10644 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
82b1b41b
NC
10645
10646 /* PR 17531: file: 10796eb3. */
10647 if (dw_sect >= DW_SECT_MAX)
10648 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10649 else
10650 this_set [row - 1].section_offsets [dw_sect] = val;
341f9135
CC
10651 }
10652 }
6937bb54 10653
341f9135
CC
10654 if (do_display)
10655 printf ("\n");
10656 }
10657 ph += 8;
10658 pi += 4;
10659 }
10660
10661 ph = phash;
10662 pi = pindex;
10663 if (do_display)
b4eb7656 10664 {
341f9135
CC
10665 printf ("\n");
10666 printf (_(" Size table\n"));
10667 printf (" slot %-16s ",
10668 is_tu_index ? _("signature") : _("dwo_id"));
b4eb7656 10669 }
6937bb54 10670
341f9135
CC
10671 for (j = 0; j < ncols; j++)
10672 {
6937bb54 10673 SAFE_BYTE_GET (val, ppool + j * 4, 4, limit);
341f9135
CC
10674 if (do_display)
10675 printf (" %8s", get_DW_SECT_short_name (val));
10676 }
6937bb54 10677
341f9135
CC
10678 if (do_display)
10679 printf ("\n");
6937bb54 10680
341f9135
CC
10681 for (i = 0; i < nslots; i++)
10682 {
6937bb54
NC
10683 SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
10684
10685 SAFE_BYTE_GET (row, pi, 4, limit);
341f9135
CC
10686 if (row != 0)
10687 {
10688 prow = psizes + (row - 1) * ncols * 4;
6937bb54 10689
341f9135
CC
10690 if (do_display)
10691 printf (_(" [%3d] 0x%s"),
10692 i, dwarf_vmatoa64 (signature_high, signature_low,
10693 buf, sizeof (buf)));
6937bb54 10694
341f9135
CC
10695 for (j = 0; j < ncols; j++)
10696 {
6937bb54 10697 SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
341f9135
CC
10698 if (do_display)
10699 printf (" %8d", val);
10700 else
10701 {
6937bb54 10702 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
82b1b41b
NC
10703 if (dw_sect >= DW_SECT_MAX)
10704 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10705 else
341f9135
CC
10706 this_set [row - 1].section_sizes [dw_sect] = val;
10707 }
10708 }
6937bb54 10709
341f9135
CC
10710 if (do_display)
10711 printf ("\n");
10712 }
6937bb54 10713
341f9135
CC
10714 ph += 8;
10715 pi += 4;
657d0d47 10716 }
657d0d47 10717 }
341f9135 10718 else if (do_display)
6937bb54 10719 printf (_(" Unsupported version (%d)\n"), version);
657d0d47
CC
10720
10721 if (do_display)
10722 printf ("\n");
10723
10724 return 1;
10725}
10726
288f0ba2
AM
10727static int cu_tu_indexes_read = -1; /* Tri-state variable. */
10728
657d0d47
CC
10729/* Load the CU and TU indexes if present. This will build a list of
10730 section sets that we can use to associate a .debug_info.dwo section
10731 with its associated .debug_abbrev.dwo section in a .dwp file. */
10732
015dc7e1 10733static bool
657d0d47
CC
10734load_cu_tu_indexes (void *file)
10735{
10736 /* If we have already loaded (or tried to load) the CU and TU indexes
10737 then do not bother to repeat the task. */
43a444f9
NC
10738 if (cu_tu_indexes_read == -1)
10739 {
015dc7e1 10740 cu_tu_indexes_read = true;
521d4b19 10741
dda8d76d 10742 if (load_debug_section_with_follow (dwp_cu_index, file))
43a444f9 10743 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
015dc7e1 10744 cu_tu_indexes_read = false;
43a444f9 10745
dda8d76d 10746 if (load_debug_section_with_follow (dwp_tu_index, file))
43a444f9 10747 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
015dc7e1 10748 cu_tu_indexes_read = false;
43a444f9 10749 }
657d0d47 10750
015dc7e1 10751 return (bool) cu_tu_indexes_read;
657d0d47
CC
10752}
10753
10754/* Find the set of sections that includes section SHNDX. */
10755
10756unsigned int *
10757find_cu_tu_set (void *file, unsigned int shndx)
10758{
10759 unsigned int i;
10760
43a444f9
NC
10761 if (! load_cu_tu_indexes (file))
10762 return NULL;
657d0d47
CC
10763
10764 /* Find SHNDX in the shndx pool. */
10765 for (i = 0; i < shndx_pool_used; i++)
10766 if (shndx_pool [i] == shndx)
10767 break;
10768
10769 if (i >= shndx_pool_used)
10770 return NULL;
10771
10772 /* Now backup to find the first entry in the set. */
10773 while (i > 0 && shndx_pool [i - 1] != 0)
10774 i--;
10775
10776 return shndx_pool + i;
10777}
10778
10779/* Display a .debug_cu_index or .debug_tu_index section. */
10780
10781static int
10782display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
10783{
10784 return process_cu_tu_index (section, 1);
10785}
10786
19e6b90e
L
10787static int
10788display_debug_not_supported (struct dwarf_section *section,
10789 void *file ATTRIBUTE_UNUSED)
10790{
10791 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
10792 section->name);
10793
10794 return 1;
10795}
10796
1306a742
NC
10797/* Like malloc, but takes two parameters like calloc.
10798 Verifies that the first parameter is not too large.
82b1b41b 10799 Note: does *not* initialise the allocated memory to zero. */
dda8d76d 10800
19e6b90e
L
10801void *
10802cmalloc (size_t nmemb, size_t size)
10803{
10804 /* Check for overflow. */
10805 if (nmemb >= ~(size_t) 0 / size)
10806 return NULL;
82b1b41b
NC
10807
10808 return xmalloc (nmemb * size);
19e6b90e
L
10809}
10810
1306a742
NC
10811/* Like xmalloc, but takes two parameters like calloc.
10812 Verifies that the first parameter is not too large.
10813 Note: does *not* initialise the allocated memory to zero. */
dda8d76d 10814
72c61a0d 10815void *
1306a742 10816xcmalloc (size_t nmemb, size_t size)
72c61a0d
NC
10817{
10818 /* Check for overflow. */
10819 if (nmemb >= ~(size_t) 0 / size)
8490fb40
NC
10820 {
10821 fprintf (stderr,
10822 _("Attempt to allocate an array with an excessive number of elements: 0x%lx\n"),
10823 (long) nmemb);
10824 xexit (1);
10825 }
72c61a0d 10826
1306a742 10827 return xmalloc (nmemb * size);
72c61a0d
NC
10828}
10829
1306a742
NC
10830/* Like xrealloc, but takes three parameters.
10831 Verifies that the second parameter is not too large.
10832 Note: does *not* initialise any new memory to zero. */
dda8d76d 10833
19e6b90e 10834void *
1306a742 10835xcrealloc (void *ptr, size_t nmemb, size_t size)
19e6b90e
L
10836{
10837 /* Check for overflow. */
10838 if (nmemb >= ~(size_t) 0 / size)
8490fb40 10839 {
dda8d76d
NC
10840 error (_("Attempt to re-allocate an array with an excessive number of elements: 0x%lx\n"),
10841 (long) nmemb);
8490fb40
NC
10842 xexit (1);
10843 }
82b1b41b 10844
1306a742 10845 return xrealloc (ptr, nmemb * size);
19e6b90e
L
10846}
10847
1306a742 10848/* Like xcalloc, but verifies that the first parameter is not too large. */
dda8d76d 10849
19e6b90e 10850void *
1306a742 10851xcalloc2 (size_t nmemb, size_t size)
19e6b90e
L
10852{
10853 /* Check for overflow. */
10854 if (nmemb >= ~(size_t) 0 / size)
8490fb40 10855 {
dda8d76d
NC
10856 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: 0x%lx\n"),
10857 (long) nmemb);
8490fb40
NC
10858 xexit (1);
10859 }
82b1b41b 10860
1306a742 10861 return xcalloc (nmemb, size);
19e6b90e
L
10862}
10863
dda8d76d
NC
10864static unsigned long
10865calc_gnu_debuglink_crc32 (unsigned long crc,
10866 const unsigned char * buf,
10867 bfd_size_type len)
10868{
10869 static const unsigned long crc32_table[256] =
10870 {
10871 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
10872 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
10873 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
10874 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
10875 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
10876 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
10877 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
10878 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
10879 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
10880 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
10881 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
10882 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
10883 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
10884 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
10885 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
10886 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
10887 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
10888 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
10889 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
10890 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
10891 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
10892 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
10893 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
10894 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
10895 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
10896 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
10897 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
10898 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
10899 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
10900 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
10901 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
10902 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
10903 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
10904 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
10905 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
10906 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
10907 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
10908 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
10909 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
10910 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
10911 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
10912 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
10913 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
10914 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
10915 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
10916 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
10917 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
10918 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
10919 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
10920 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
10921 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
10922 0x2d02ef8d
10923 };
10924 const unsigned char *end;
10925
10926 crc = ~crc & 0xffffffff;
10927 for (end = buf + len; buf < end; ++ buf)
10928 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
10929 return ~crc & 0xffffffff;
10930}
10931
015dc7e1
AM
10932typedef bool (*check_func_type) (const char *, void *);
10933typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
dda8d76d 10934
015dc7e1 10935static bool
dda8d76d
NC
10936check_gnu_debuglink (const char * pathname, void * crc_pointer)
10937{
10938 static unsigned char buffer [8 * 1024];
10939 FILE * f;
10940 bfd_size_type count;
10941 unsigned long crc = 0;
10942 void * sep_data;
10943
10944 sep_data = open_debug_file (pathname);
10945 if (sep_data == NULL)
015dc7e1 10946 return false;
dda8d76d
NC
10947
10948 /* Yes - we are opening the file twice... */
10949 f = fopen (pathname, "rb");
10950 if (f == NULL)
10951 {
10952 /* Paranoia: This should never happen. */
10953 close_debug_file (sep_data);
10954 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
015dc7e1 10955 return false;
dda8d76d
NC
10956 }
10957
10958 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
10959 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
10960
10961 fclose (f);
10962
10963 if (crc != * (unsigned long *) crc_pointer)
10964 {
10965 close_debug_file (sep_data);
10966 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
10967 pathname);
015dc7e1 10968 return false;
dda8d76d
NC
10969 }
10970
015dc7e1 10971 return true;
dda8d76d
NC
10972}
10973
10974static const char *
10975parse_gnu_debuglink (struct dwarf_section * section, void * data)
10976{
10977 const char * name;
10978 unsigned int crc_offset;
10979 unsigned long * crc32 = (unsigned long *) data;
10980
10981 /* The name is first.
10982 The CRC value is stored after the filename, aligned up to 4 bytes. */
10983 name = (const char *) section->start;
10984
10985 crc_offset = strnlen (name, section->size) + 1;
3d64c987
NC
10986 if (crc_offset == 1)
10987 return NULL;
dda8d76d
NC
10988 crc_offset = (crc_offset + 3) & ~3;
10989 if (crc_offset + 4 > section->size)
10990 return NULL;
10991
10992 * crc32 = byte_get (section->start + crc_offset, 4);
10993 return name;
10994}
10995
015dc7e1 10996static bool
dda8d76d
NC
10997check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
10998{
10999 void * sep_data = open_debug_file (filename);
11000
11001 if (sep_data == NULL)
015dc7e1 11002 return false;
dda8d76d
NC
11003
11004 /* FIXME: We should now extract the build-id in the separate file
11005 and check it... */
11006
015dc7e1 11007 return true;
dda8d76d
NC
11008}
11009
11010typedef struct build_id_data
11011{
11012 bfd_size_type len;
11013 const unsigned char * data;
11014} Build_id_data;
11015
11016static const char *
11017parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11018{
11019 const char * name;
11020 bfd_size_type namelen;
11021 bfd_size_type id_len;
11022 Build_id_data * build_id_data;
11023
11024 /* The name is first.
11025 The build-id follows immediately, with no padding, up to the section's end. */
11026
11027 name = (const char *) section->start;
11028 namelen = strnlen (name, section->size) + 1;
3d64c987
NC
11029 if (namelen == 1)
11030 return NULL;
dda8d76d
NC
11031 if (namelen >= section->size)
11032 return NULL;
11033
11034 id_len = section->size - namelen;
11035 if (id_len < 0x14)
11036 return NULL;
11037
dfbee680 11038 build_id_data = (Build_id_data *) data;
dda8d76d
NC
11039 build_id_data->len = id_len;
11040 build_id_data->data = section->start + namelen;
11041
dda8d76d
NC
11042 return name;
11043}
11044
24841daa
NC
11045static void
11046add_separate_debug_file (const char * filename, void * handle)
11047{
11048 separate_info * i = xmalloc (sizeof * i);
11049
11050 i->filename = filename;
11051 i->handle = handle;
11052 i->next = first_separate_info;
11053 first_separate_info = i;
11054}
11055
301a9420
AM
11056#if HAVE_LIBDEBUGINFOD
11057/* Query debuginfod servers for the target debuglink or debugaltlink
11058 file. If successful, store the path of the file in filename and
11059 return TRUE, otherwise return FALSE. */
11060
015dc7e1 11061static bool
301a9420
AM
11062debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11063 char ** filename,
11064 void * file)
11065{
11066 size_t build_id_len;
11067 unsigned char * build_id;
11068
11069 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11070 {
11071 /* Get the build-id of file. */
11072 build_id = get_build_id (file);
11073 build_id_len = 0;
11074 }
11075 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11076 {
11077 /* Get the build-id of the debugaltlink file. */
11078 unsigned int filelen;
11079
11080 filelen = strnlen ((const char *)section->start, section->size);
11081 if (filelen == section->size)
11082 /* Corrupt debugaltlink. */
015dc7e1 11083 return false;
301a9420
AM
11084
11085 build_id = section->start + filelen + 1;
11086 build_id_len = section->size - (filelen + 1);
11087
11088 if (build_id_len == 0)
015dc7e1 11089 return false;
301a9420
AM
11090 }
11091 else
015dc7e1 11092 return false;
301a9420
AM
11093
11094 if (build_id)
11095 {
11096 int fd;
11097 debuginfod_client * client;
11098
11099 client = debuginfod_begin ();
11100 if (client == NULL)
015dc7e1 11101 return false;
301a9420
AM
11102
11103 /* Query debuginfod servers for the target file. If found its path
11104 will be stored in filename. */
11105 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11106 debuginfod_end (client);
11107
11108 /* Only free build_id if we allocated space for a hex string
11109 in get_build_id (). */
11110 if (build_id_len == 0)
11111 free (build_id);
11112
11113 if (fd >= 0)
11114 {
11115 /* File successfully retrieved. Close fd since we want to
11116 use open_debug_file () on filename instead. */
11117 close (fd);
015dc7e1 11118 return true;
301a9420
AM
11119 }
11120 }
11121
015dc7e1 11122 return false;
301a9420
AM
11123}
11124#endif
11125
dda8d76d
NC
11126static void *
11127load_separate_debug_info (const char * main_filename,
2b63c337 11128 struct dwarf_section * xlink,
dda8d76d
NC
11129 parse_func_type parse_func,
11130 check_func_type check_func,
301a9420
AM
11131 void * func_data,
11132 void * file ATTRIBUTE_UNUSED)
dda8d76d
NC
11133{
11134 const char * separate_filename;
24841daa 11135 char * debug_filename;
dda8d76d
NC
11136 char * canon_dir;
11137 size_t canon_dirlen;
11138 size_t dirlen;
11139
2b63c337 11140 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
dda8d76d
NC
11141 {
11142 warn (_("Corrupt debuglink section: %s\n"),
2b63c337 11143 xlink->name ? xlink->name : xlink->uncompressed_name);
ce139cd5 11144 return NULL;
dda8d76d 11145 }
521d4b19 11146
dda8d76d
NC
11147 /* Attempt to locate the separate file.
11148 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11149
11150 canon_dir = lrealpath (main_filename);
521d4b19 11151
dda8d76d
NC
11152 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11153 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11154 break;
11155 canon_dir[canon_dirlen] = '\0';
11156
11157#ifndef DEBUGDIR
11158#define DEBUGDIR "/lib/debug"
11159#endif
11160#ifndef EXTRA_DEBUG_ROOT1
11161#define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11162#endif
11163#ifndef EXTRA_DEBUG_ROOT2
11164#define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11165#endif
11166
24841daa
NC
11167 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11168 + canon_dirlen
11169 + strlen (".debug/")
dda8d76d 11170#ifdef EXTRA_DEBUG_ROOT1
24841daa 11171 + strlen (EXTRA_DEBUG_ROOT1)
dda8d76d
NC
11172#endif
11173#ifdef EXTRA_DEBUG_ROOT2
24841daa 11174 + strlen (EXTRA_DEBUG_ROOT2)
dda8d76d 11175#endif
24841daa
NC
11176 + strlen (separate_filename)
11177 + 1);
11178 if (debug_filename == NULL)
dda8d76d
NC
11179 {
11180 warn (_("Out of memory"));
3391569f 11181 free (canon_dir);
dda8d76d
NC
11182 return NULL;
11183 }
11184
11185 /* First try in the current directory. */
24841daa
NC
11186 sprintf (debug_filename, "%s", separate_filename);
11187 if (check_func (debug_filename, func_data))
dda8d76d
NC
11188 goto found;
11189
11190 /* Then try in a subdirectory called .debug. */
24841daa
NC
11191 sprintf (debug_filename, ".debug/%s", separate_filename);
11192 if (check_func (debug_filename, func_data))
dda8d76d
NC
11193 goto found;
11194
11195 /* Then try in the same directory as the original file. */
24841daa
NC
11196 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11197 if (check_func (debug_filename, func_data))
dda8d76d
NC
11198 goto found;
11199
11200 /* And the .debug subdirectory of that directory. */
24841daa
NC
11201 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11202 if (check_func (debug_filename, func_data))
dda8d76d
NC
11203 goto found;
11204
11205#ifdef EXTRA_DEBUG_ROOT1
11206 /* Try the first extra debug file root. */
24841daa
NC
11207 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11208 if (check_func (debug_filename, func_data))
dda8d76d 11209 goto found;
39f0547e
NC
11210
11211 /* Try the first extra debug file root. */
11212 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11213 if (check_func (debug_filename, func_data))
11214 goto found;
dda8d76d
NC
11215#endif
11216
11217#ifdef EXTRA_DEBUG_ROOT2
11218 /* Try the second extra debug file root. */
24841daa
NC
11219 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11220 if (check_func (debug_filename, func_data))
dda8d76d
NC
11221 goto found;
11222#endif
11223
24841daa
NC
11224 /* Then try in the global debug_filename directory. */
11225 strcpy (debug_filename, DEBUGDIR);
dda8d76d
NC
11226 dirlen = strlen (DEBUGDIR) - 1;
11227 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
24841daa
NC
11228 strcat (debug_filename, "/");
11229 strcat (debug_filename, (const char *) separate_filename);
dda8d76d 11230
24841daa 11231 if (check_func (debug_filename, func_data))
dda8d76d
NC
11232 goto found;
11233
301a9420
AM
11234#if HAVE_LIBDEBUGINFOD
11235 {
11236 char * tmp_filename;
11237
11238 if (debuginfod_fetch_separate_debug_info (xlink,
11239 & tmp_filename,
11240 file))
11241 {
11242 /* File successfully downloaded from server, replace
11243 debug_filename with the file's path. */
11244 free (debug_filename);
11245 debug_filename = tmp_filename;
11246 goto found;
11247 }
11248 }
11249#endif
11250
ecd65684
L
11251 if (do_debug_links)
11252 {
11253 /* Failed to find the file. */
11254 warn (_("could not find separate debug file '%s'\n"),
11255 separate_filename);
11256 warn (_("tried: %s\n"), debug_filename);
dda8d76d
NC
11257
11258#ifdef EXTRA_DEBUG_ROOT2
ecd65684
L
11259 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11260 separate_filename);
11261 warn (_("tried: %s\n"), debug_filename);
dda8d76d
NC
11262#endif
11263
11264#ifdef EXTRA_DEBUG_ROOT1
ecd65684
L
11265 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11266 canon_dir, separate_filename);
11267 warn (_("tried: %s\n"), debug_filename);
39f0547e 11268
ecd65684
L
11269 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11270 separate_filename);
11271 warn (_("tried: %s\n"), debug_filename);
dda8d76d
NC
11272#endif
11273
ecd65684
L
11274 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11275 separate_filename);
11276 warn (_("tried: %s\n"), debug_filename);
dda8d76d 11277
ecd65684
L
11278 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11279 warn (_("tried: %s\n"), debug_filename);
dda8d76d 11280
ecd65684
L
11281 sprintf (debug_filename, ".debug/%s", separate_filename);
11282 warn (_("tried: %s\n"), debug_filename);
dda8d76d 11283
ecd65684
L
11284 sprintf (debug_filename, "%s", separate_filename);
11285 warn (_("tried: %s\n"), debug_filename);
dda8d76d 11286
301a9420 11287#if HAVE_LIBDEBUGINFOD
ecd65684
L
11288 {
11289 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11290 if (urls == NULL)
11291 urls = "";
301a9420 11292
ecd65684
L
11293 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11294 }
301a9420 11295#endif
ecd65684 11296 }
301a9420 11297
dda8d76d 11298 free (canon_dir);
24841daa 11299 free (debug_filename);
dda8d76d
NC
11300 return NULL;
11301
11302 found:
11303 free (canon_dir);
11304
24841daa
NC
11305 void * debug_handle;
11306
dda8d76d 11307 /* Now open the file.... */
24841daa 11308 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
dda8d76d 11309 {
24841daa
NC
11310 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11311 free (debug_filename);
ce139cd5 11312 return NULL;
dda8d76d
NC
11313 }
11314
11315 /* FIXME: We do not check to see if there are any other separate debug info
11316 files that would also match. */
11317
ca0e11aa
NC
11318 if (do_debug_links)
11319 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
24841daa 11320 add_separate_debug_file (debug_filename, debug_handle);
dda8d76d 11321
24841daa 11322 /* Do not free debug_filename - it might be referenced inside
dda8d76d 11323 the structure returned by open_debug_file(). */
24841daa 11324 return debug_handle;
dda8d76d
NC
11325}
11326
d85bf2ba
NC
11327/* Attempt to load a separate dwarf object file. */
11328
11329static void *
24841daa 11330load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
d85bf2ba 11331{
24841daa
NC
11332 char * separate_filename;
11333 void * separate_handle;
d85bf2ba 11334
d9d9d8ef
TV
11335 if (IS_ABSOLUTE_PATH (name))
11336 separate_filename = strdup (name);
11337 else
11338 /* FIXME: Skip adding / if dwo_dir ends in /. */
11339 separate_filename = concat (dir, "/", name, NULL);
24841daa 11340 if (separate_filename == NULL)
d85bf2ba
NC
11341 {
11342 warn (_("Out of memory allocating dwo filename\n"));
11343 return NULL;
11344 }
11345
24841daa 11346 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
d85bf2ba 11347 {
24841daa
NC
11348 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11349 free (separate_filename);
d85bf2ba
NC
11350 return NULL;
11351 }
11352
11353 /* FIXME: We should check the dwo_id. */
11354
24841daa 11355 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
bfbfa6e7 11356
24841daa
NC
11357 add_separate_debug_file (separate_filename, separate_handle);
11358 /* Note - separate_filename will be freed in free_debug_memory(). */
11359 return separate_handle;
d85bf2ba
NC
11360}
11361
e38332c2
NC
11362static void
11363load_debug_sup_file (const char * main_filename, void * file)
11364{
11365 if (! load_debug_section (debug_sup, file))
11366 return; /* No .debug_sup section. */
11367
11368 struct dwarf_section * section;
11369 section = & debug_displays [debug_sup].section;
11370 assert (section != NULL);
11371
11372 if (section->start == NULL || section->size < 5)
11373 {
11374 warn (_(".debug_sup section is corrupt/empty\n"));
11375 return;
11376 }
11377
11378 if (section->start[2] != 0)
11379 return; /* This is a supplementary file. */
11380
11381 const char * filename = (const char *) section->start + 3;
11382 if (strnlen (filename, section->size - 3) == section->size - 3)
11383 {
11384 warn (_("filename in .debug_sup section is corrupt\n"));
11385 return;
11386 }
11387
11388 if (filename[0] != '/' && strchr (main_filename, '/'))
11389 {
11390 char * new_name;
bfbfa6e7
NC
11391 int new_len;
11392
11393 new_len = asprintf (& new_name, "%.*s/%s",
11394 (int) (strrchr (main_filename, '/') - main_filename),
11395 main_filename,
11396 filename);
11397 if (new_len < 3)
11398 {
11399 warn (_("unable to construct path for supplementary debug file"));
11400 if (new_len > -1)
11401 free (new_name);
11402 return;
11403 }
11404 filename = new_name;
11405 }
11406 else
11407 {
11408 /* PR 27796: Make sure that we pass a filename that can be free'd to
11409 add_separate_debug_file(). */
11410 filename = strdup (filename);
11411 if (filename == NULL)
11412 {
11413 warn (_("out of memory constructing filename for .debug_sup link\n"));
11414 return;
11415 }
e38332c2
NC
11416 }
11417
bfbfa6e7 11418 void * handle = open_debug_file (filename);
e38332c2
NC
11419 if (handle == NULL)
11420 {
11421 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
bfbfa6e7 11422 free ((void *) filename);
e38332c2
NC
11423 return;
11424 }
11425
11426 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
11427
11428 /* FIXME: Compare the checksums, if present. */
11429 add_separate_debug_file (filename, handle);
11430}
11431
ce139cd5
NC
11432/* Load a debuglink section and/or a debugaltlink section, if either are present.
11433 Recursively check the loaded files for more of these sections.
e38332c2
NC
11434 Also follow any links in .debug_sup sections.
11435 FIXME: Should also check for DWO_* entries in the newly loaded files. */
ce139cd5
NC
11436
11437static void
11438check_for_and_load_links (void * file, const char * filename)
11439{
11440 void * handle = NULL;
11441
11442 if (load_debug_section (gnu_debugaltlink, file))
11443 {
11444 Build_id_data build_id_data;
11445
11446 handle = load_separate_debug_info (filename,
11447 & debug_displays[gnu_debugaltlink].section,
11448 parse_gnu_debugaltlink,
11449 check_gnu_debugaltlink,
11450 & build_id_data,
11451 file);
11452 if (handle)
11453 {
11454 assert (handle == first_separate_info->handle);
11455 check_for_and_load_links (first_separate_info->handle,
11456 first_separate_info->filename);
11457 }
11458 }
11459
11460 if (load_debug_section (gnu_debuglink, file))
11461 {
11462 unsigned long crc32;
11463
11464 handle = load_separate_debug_info (filename,
11465 & debug_displays[gnu_debuglink].section,
11466 parse_gnu_debuglink,
11467 check_gnu_debuglink,
11468 & crc32,
11469 file);
11470 if (handle)
11471 {
11472 assert (handle == first_separate_info->handle);
11473 check_for_and_load_links (first_separate_info->handle,
11474 first_separate_info->filename);
11475 }
11476 }
e38332c2
NC
11477
11478 load_debug_sup_file (filename, file);
ce139cd5
NC
11479}
11480
24841daa
NC
11481/* Load the separate debug info file(s) attached to FILE, if any exist.
11482 Returns TRUE if any were found, FALSE otherwise.
11483 If TRUE is returned then the linked list starting at first_separate_info
11484 will be populated with open file handles. */
dda8d76d 11485
015dc7e1 11486bool
24841daa 11487load_separate_debug_files (void * file, const char * filename)
dda8d76d 11488{
8de3a6e2
NC
11489 /* Skip this operation if we are not interested in debug links. */
11490 if (! do_follow_links && ! do_debug_links)
015dc7e1 11491 return false;
8de3a6e2 11492
24841daa 11493 /* See if there are any dwo links. */
d85bf2ba
NC
11494 if (load_debug_section (str, file)
11495 && load_debug_section (abbrev, file)
11496 && load_debug_section (info, file))
11497 {
24841daa 11498 free_dwo_info ();
d85bf2ba 11499
50ea0877 11500 if (process_debug_info (& debug_displays[info].section, file, abbrev,
015dc7e1 11501 true, false))
d85bf2ba 11502 {
015dc7e1
AM
11503 bool introduced = false;
11504 dwo_info *dwinfo;
11505 const char *dir = NULL;
11506 const char *id = NULL;
11507 const char *name = NULL;
24841daa
NC
11508
11509 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
d85bf2ba 11510 {
50ea0877 11511 /* Accumulate NAME, DIR and ID fields. */
24841daa 11512 switch (dwinfo->type)
d85bf2ba 11513 {
24841daa 11514 case DWO_NAME:
50ea0877
NC
11515 if (name != NULL)
11516 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
11517 name = dwinfo->value;
11518 break;
11519
11520 case DWO_DIR:
11521 /* There can be multiple DW_AT_comp_dir entries in a CU,
11522 so do not complain. */
11523 dir = dwinfo->value;
11524 break;
11525
11526 case DWO_ID:
11527 if (id != NULL)
11528 warn (_("multiple DWO_IDs encountered for the same CU\n"));
11529 id = dwinfo->value;
11530 break;
11531
11532 default:
11533 error (_("Unexpected DWO INFO type"));
11534 break;
11535 }
11536
11537 /* If we have reached the end of our list, or we are changing
11538 CUs, then display the information that we have accumulated
11539 so far. */
11540 if (name != NULL
11541 && (dwinfo->next == NULL
11542 || dwinfo->next->cu_offset != dwinfo->cu_offset))
11543 {
24841daa
NC
11544 if (do_debug_links)
11545 {
11546 if (! introduced)
11547 {
11548 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
11549 debug_displays [info].section.uncompressed_name);
015dc7e1 11550 introduced = true;
24841daa 11551 }
d85bf2ba 11552
50ea0877 11553 printf (_(" Name: %s\n"), name);
24841daa
NC
11554 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
11555 if (id != NULL)
11556 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
11557 else
50ea0877 11558 printf (_(" ID: <not specified>\n"));
24841daa
NC
11559 printf ("\n\n");
11560 }
11561
11562 if (do_follow_links)
50ea0877 11563 load_dwo_file (filename, name, dir, id);
24841daa 11564
50ea0877 11565 name = dir = id = NULL;
24841daa 11566 }
d85bf2ba
NC
11567 }
11568 }
11569 }
11570
dda8d76d 11571 if (! do_follow_links)
8de3a6e2
NC
11572 /* The other debug links will be displayed by display_debug_links()
11573 so we do not need to do any further processing here. */
015dc7e1 11574 return false;
dda8d76d
NC
11575
11576 /* FIXME: We do not check for the presence of both link sections in the same file. */
dda8d76d 11577 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
d85bf2ba 11578 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
dda8d76d 11579
ce139cd5 11580 check_for_and_load_links (file, filename);
24841daa 11581 if (first_separate_info != NULL)
015dc7e1 11582 return true;
24841daa 11583
dda8d76d 11584 do_follow_links = 0;
015dc7e1 11585 return false;
521d4b19 11586}
dda8d76d 11587
19e6b90e
L
11588void
11589free_debug_memory (void)
11590{
3f5e193b 11591 unsigned int i;
19e6b90e 11592
bcd213b2 11593 free_all_abbrevs ();
19e6b90e 11594
bcd213b2
NC
11595 free (cu_abbrev_map);
11596 cu_abbrev_map = NULL;
11597 next_free_abbrev_map_entry = 0;
521d4b19 11598
288f0ba2
AM
11599 free (shndx_pool);
11600 shndx_pool = NULL;
11601 shndx_pool_size = 0;
11602 shndx_pool_used = 0;
11603 free (cu_sets);
11604 cu_sets = NULL;
11605 cu_count = 0;
11606 free (tu_sets);
11607 tu_sets = NULL;
11608 tu_count = 0;
11609
11610 memset (level_type_signed, 0, sizeof level_type_signed);
11611 cu_tu_indexes_read = -1;
11612
19e6b90e 11613 for (i = 0; i < max; i++)
3f5e193b 11614 free_debug_section ((enum dwarf_section_display_enum) i);
19e6b90e 11615
cc86f28f 11616 if (debug_information != NULL)
19e6b90e 11617 {
82fcdb39 11618 for (i = 0; i < alloc_num_debug_info_entries; i++)
19e6b90e 11619 {
82fcdb39 11620 if (debug_information [i].max_loc_offsets)
19e6b90e 11621 {
82fcdb39
AM
11622 free (debug_information [i].loc_offsets);
11623 free (debug_information [i].have_frame_base);
19e6b90e 11624 }
82fcdb39
AM
11625 if (debug_information [i].max_range_lists)
11626 free (debug_information [i].range_lists);
19e6b90e
L
11627 }
11628 free (debug_information);
11629 debug_information = NULL;
82b1b41b 11630 alloc_num_debug_info_entries = num_debug_info_entries = 0;
19e6b90e 11631 }
dda8d76d 11632
24841daa
NC
11633 separate_info * d;
11634 separate_info * next;
dda8d76d 11635
24841daa
NC
11636 for (d = first_separate_info; d != NULL; d = next)
11637 {
11638 close_debug_file (d->handle);
11639 free ((void *) d->filename);
11640 next = d->next;
11641 free ((void *) d);
dda8d76d 11642 }
24841daa 11643 first_separate_info = NULL;
521d4b19 11644
24841daa 11645 free_dwo_info ();
19e6b90e
L
11646}
11647
4cb93e3b
TG
11648void
11649dwarf_select_sections_by_names (const char *names)
11650{
11651 typedef struct
11652 {
11653 const char * option;
11654 int * variable;
f9f0e732 11655 int val;
4cb93e3b
TG
11656 }
11657 debug_dump_long_opts;
11658
11659 static const debug_dump_long_opts opts_table [] =
11660 {
11661 /* Please keep this table alpha- sorted. */
11662 { "Ranges", & do_debug_ranges, 1 },
11663 { "abbrev", & do_debug_abbrevs, 1 },
657d0d47 11664 { "addr", & do_debug_addr, 1 },
4cb93e3b 11665 { "aranges", & do_debug_aranges, 1 },
657d0d47
CC
11666 { "cu_index", & do_debug_cu_index, 1 },
11667 { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
dda8d76d 11668 { "follow-links", & do_follow_links, 1 },
4cb93e3b
TG
11669 { "frames", & do_debug_frames, 1 },
11670 { "frames-interp", & do_debug_frames_interp, 1 },
657d0d47
CC
11671 /* The special .gdb_index section. */
11672 { "gdb_index", & do_gdb_index, 1 },
4cb93e3b
TG
11673 { "info", & do_debug_info, 1 },
11674 { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility. */
dda8d76d 11675 { "links", & do_debug_links, 1 },
4cb93e3b
TG
11676 { "loc", & do_debug_loc, 1 },
11677 { "macro", & do_debug_macinfo, 1 },
c46b7066 11678 { "no-follow-links", & do_follow_links, 0 },
4cb93e3b 11679 { "pubnames", & do_debug_pubnames, 1 },
357da287 11680 { "pubtypes", & do_debug_pubtypes, 1 },
222c2bf0 11681 /* This entry is for compatibility
4cb93e3b
TG
11682 with earlier versions of readelf. */
11683 { "ranges", & do_debug_aranges, 1 },
657d0d47 11684 { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
4cb93e3b 11685 { "str", & do_debug_str, 1 },
e4b7104b 11686 { "str-offsets", & do_debug_str_offsets, 1 },
6f875884
TG
11687 /* These trace_* sections are used by Itanium VMS. */
11688 { "trace_abbrev", & do_trace_abbrevs, 1 },
11689 { "trace_aranges", & do_trace_aranges, 1 },
11690 { "trace_info", & do_trace_info, 1 },
4cb93e3b
TG
11691 { NULL, NULL, 0 }
11692 };
11693
11694 const char *p;
467c65bc 11695
4cb93e3b
TG
11696 p = names;
11697 while (*p)
11698 {
11699 const debug_dump_long_opts * entry;
467c65bc 11700
4cb93e3b
TG
11701 for (entry = opts_table; entry->option; entry++)
11702 {
11703 size_t len = strlen (entry->option);
467c65bc 11704
4cb93e3b
TG
11705 if (strncmp (p, entry->option, len) == 0
11706 && (p[len] == ',' || p[len] == '\0'))
11707 {
c46b7066 11708 * entry->variable = entry->val;
467c65bc 11709
4cb93e3b
TG
11710 /* The --debug-dump=frames-interp option also
11711 enables the --debug-dump=frames option. */
11712 if (do_debug_frames_interp)
11713 do_debug_frames = 1;
11714
11715 p += len;
11716 break;
11717 }
11718 }
467c65bc 11719
4cb93e3b
TG
11720 if (entry->option == NULL)
11721 {
11722 warn (_("Unrecognized debug option '%s'\n"), p);
11723 p = strchr (p, ',');
11724 if (p == NULL)
11725 break;
11726 }
467c65bc 11727
4cb93e3b
TG
11728 if (*p == ',')
11729 p++;
11730 }
11731}
11732
11733void
11734dwarf_select_sections_by_letters (const char *letters)
11735{
91d6fa6a 11736 unsigned int lindex = 0;
4cb93e3b 11737
91d6fa6a
NC
11738 while (letters[lindex])
11739 switch (letters[lindex++])
4cb93e3b 11740 {
dda8d76d
NC
11741 case 'A': do_debug_addr = 1; break;
11742 case 'a': do_debug_abbrevs = 1; break;
11743 case 'c': do_debug_cu_index = 1; break;
11744 case 'F': do_debug_frames_interp = 1; /* Fall through. */
11745 case 'f': do_debug_frames = 1; break;
11746 case 'g': do_gdb_index = 1; break;
11747 case 'i': do_debug_info = 1; break;
11748 case 'K': do_follow_links = 1; break;
c46b7066 11749 case 'N': do_follow_links = 0; break;
dda8d76d
NC
11750 case 'k': do_debug_links = 1; break;
11751 case 'l': do_debug_lines |= FLAG_DEBUG_LINES_RAW; break;
11752 case 'L': do_debug_lines |= FLAG_DEBUG_LINES_DECODED; break;
11753 case 'm': do_debug_macinfo = 1; break;
e4b7104b 11754 case 'O': do_debug_str_offsets = 1; break;
dda8d76d
NC
11755 case 'o': do_debug_loc = 1; break;
11756 case 'p': do_debug_pubnames = 1; break;
11757 case 'R': do_debug_ranges = 1; break;
11758 case 'r': do_debug_aranges = 1; break;
11759 case 's': do_debug_str = 1; break;
11760 case 'T': do_trace_aranges = 1; break;
11761 case 't': do_debug_pubtypes = 1; break;
11762 case 'U': do_trace_info = 1; break;
11763 case 'u': do_trace_abbrevs = 1; break;
467c65bc 11764
4cb93e3b 11765 default:
7cc78d07 11766 warn (_("Unrecognized debug option '%s'\n"), letters);
4cb93e3b
TG
11767 break;
11768 }
11769}
11770
11771void
11772dwarf_select_sections_all (void)
11773{
11774 do_debug_info = 1;
11775 do_debug_abbrevs = 1;
11776 do_debug_lines = FLAG_DEBUG_LINES_RAW;
11777 do_debug_pubnames = 1;
f9f0e732 11778 do_debug_pubtypes = 1;
4cb93e3b
TG
11779 do_debug_aranges = 1;
11780 do_debug_ranges = 1;
11781 do_debug_frames = 1;
11782 do_debug_macinfo = 1;
11783 do_debug_str = 1;
11784 do_debug_loc = 1;
5bbdf3d5 11785 do_gdb_index = 1;
6f875884
TG
11786 do_trace_info = 1;
11787 do_trace_abbrevs = 1;
11788 do_trace_aranges = 1;
657d0d47
CC
11789 do_debug_addr = 1;
11790 do_debug_cu_index = 1;
dda8d76d
NC
11791 do_follow_links = 1;
11792 do_debug_links = 1;
e4b7104b 11793 do_debug_str_offsets = 1;
4cb93e3b
TG
11794}
11795
208599d9
AM
11796#define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
11797#define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
dda8d76d
NC
11798
11799/* N.B. The order here must match the order in section_display_enum. */
11800
19e6b90e
L
11801struct dwarf_section_display debug_displays[] =
11802{
015dc7e1
AM
11803 { { ".debug_abbrev", ".zdebug_abbrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
11804 { { ".debug_aranges", ".zdebug_aranges", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
11805 { { ".debug_frame", ".zdebug_frame", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
11806 { { ".debug_info", ".zdebug_info", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
11807 { { ".debug_line", ".zdebug_line", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
11808 { { ".debug_pubnames", ".zdebug_pubnames", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
11809 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
11810 { { ".eh_frame", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
11811 { { ".debug_macinfo", ".zdebug_macinfo", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
11812 { { ".debug_macro", ".zdebug_macro", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
11813 { { ".debug_str", ".zdebug_str", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
11814 { { ".debug_line_str", ".zdebug_line_str", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
11815 { { ".debug_loc", ".zdebug_loc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
11816 { { ".debug_loclists", ".zdebug_loclists", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
11817 { { ".debug_pubtypes", ".zdebug_pubtypes", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
11818 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
11819 { { ".debug_ranges", ".zdebug_ranges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
11820 { { ".debug_rnglists", ".zdebug_rnglists", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
11821 { { ".debug_static_func", ".zdebug_static_func", NO_ABBREVS }, display_debug_not_supported, NULL, false },
11822 { { ".debug_static_vars", ".zdebug_static_vars", NO_ABBREVS }, display_debug_not_supported, NULL, false },
11823 { { ".debug_types", ".zdebug_types", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
11824 { { ".debug_weaknames", ".zdebug_weaknames", NO_ABBREVS }, display_debug_not_supported, NULL, false },
11825 { { ".gdb_index", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
11826 { { ".debug_names", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
11827 { { ".trace_info", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
11828 { { ".trace_abbrev", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
11829 { { ".trace_aranges", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
11830 { { ".debug_info.dwo", ".zdebug_info.dwo", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
11831 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
11832 { { ".debug_types.dwo", ".zdebug_types.dwo", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
11833 { { ".debug_line.dwo", ".zdebug_line.dwo", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
11834 { { ".debug_loc.dwo", ".zdebug_loc.dwo", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
11835 { { ".debug_macro.dwo", ".zdebug_macro.dwo", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
11836 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
11837 { { ".debug_str.dwo", ".zdebug_str.dwo", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
11838 { { ".debug_str_offsets", ".zdebug_str_offsets", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
11839 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
11840 { { ".debug_addr", ".zdebug_addr", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
11841 { { ".debug_cu_index", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
11842 { { ".debug_tu_index", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
11843 { { ".gnu_debuglink", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
11844 { { ".gnu_debugaltlink", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
11845 { { ".debug_sup", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
dda8d76d
NC
11846 /* Separate debug info files can containt their own .debug_str section,
11847 and this might be in *addition* to a .debug_str section already present
11848 in the main file. Hence we need to have two entries for .debug_str. */
015dc7e1 11849 { { ".debug_str", ".zdebug_str", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
19e6b90e 11850};
b451e98a
JK
11851
11852/* A static assertion. */
11853extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];