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