]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/dwarf.c
Readelf demangling test typo
[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 2576 break;
5568cc9e
NC
2577 case DW_FORM_GNU_strp_alt:
2578 add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue));
2579 break;
d85bf2ba 2580 case DW_FORM_GNU_str_index:
24841daa 2581 add_dwo_name (fetch_indexed_string (uvalue, this_set, offset_size, FALSE));
d85bf2ba
NC
2582 break;
2583 case DW_FORM_string:
24841daa 2584 add_dwo_name ((const char *) orig_data);
d85bf2ba
NC
2585 break;
2586 default:
2587 warn (_("Unsupported form (%s) for attribute %s\n"),
2588 get_FORM_name (form), get_AT_name (attribute));
d85bf2ba
NC
2589 break;
2590 }
2591 break;
2592
2593 case DW_AT_comp_dir:
2594 /* FIXME: Also extract a build-id in a CU/TU. */
2595 if (need_dwo_info)
2596 switch (form)
2597 {
2598 case DW_FORM_strp:
24841daa 2599 add_dwo_dir ((const char *) fetch_indirect_string (uvalue));
d85bf2ba 2600 break;
5568cc9e
NC
2601 case DW_FORM_GNU_strp_alt:
2602 add_dwo_dir (fetch_alt_indirect_string (uvalue));
2603 break;
d85bf2ba 2604 case DW_FORM_line_strp:
24841daa 2605 add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue));
d85bf2ba
NC
2606 break;
2607 case DW_FORM_GNU_str_index:
24841daa 2608 add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, FALSE));
d85bf2ba
NC
2609 break;
2610 case DW_FORM_string:
24841daa 2611 add_dwo_dir ((const char *) orig_data);
d85bf2ba
NC
2612 break;
2613 default:
2614 warn (_("Unsupported form (%s) for attribute %s\n"),
2615 get_FORM_name (form), get_AT_name (attribute));
d85bf2ba
NC
2616 break;
2617 }
2618 break;
2619
2620 case DW_AT_GNU_dwo_id:
2621 if (need_dwo_info)
2622 switch (form)
2623 {
2624 case DW_FORM_data8:
24841daa
NC
2625 /* FIXME: Record the length of the ID as well ? */
2626 add_dwo_id ((const char *) (data - 8));
d85bf2ba
NC
2627 break;
2628 default:
2629 warn (_("Unsupported form (%s) for attribute %s\n"),
2630 get_FORM_name (form), get_AT_name (attribute));
d85bf2ba
NC
2631 break;
2632 }
2633 break;
2634
19e6b90e
L
2635 default:
2636 break;
2637 }
2638 }
2639
4ccf1e31 2640 if (do_loc || attribute == 0)
19e6b90e
L
2641 return data;
2642
ec4d4525 2643 /* For some attributes we can display further information. */
19e6b90e
L
2644 switch (attribute)
2645 {
ec1b0fbb 2646 case DW_AT_type:
b3fe587e
AM
2647 if (level >= 0 && level < MAX_CU_NESTING
2648 && uvalue < (size_t) (end - start))
ec1b0fbb
NC
2649 {
2650 bfd_boolean is_signed = FALSE;
2651
2652 get_type_signedness (start, start + uvalue, end, pointer_size,
2653 offset_size, dwarf_version, & is_signed, FALSE);
2654 level_type_signed[level] = is_signed;
2655 }
2656 break;
2657
19e6b90e 2658 case DW_AT_inline:
2e9e81a8 2659 printf ("\t");
19e6b90e
L
2660 switch (uvalue)
2661 {
2662 case DW_INL_not_inlined:
2663 printf (_("(not inlined)"));
2664 break;
2665 case DW_INL_inlined:
2666 printf (_("(inlined)"));
2667 break;
2668 case DW_INL_declared_not_inlined:
2669 printf (_("(declared as inline but ignored)"));
2670 break;
2671 case DW_INL_declared_inlined:
2672 printf (_("(declared as inline and inlined)"));
2673 break;
2674 default:
47704ddf
KT
2675 printf (_(" (Unknown inline attribute value: %s)"),
2676 dwarf_vmatoa ("x", uvalue));
19e6b90e
L
2677 break;
2678 }
2679 break;
2680
2681 case DW_AT_language:
2e9e81a8 2682 printf ("\t");
19e6b90e
L
2683 switch (uvalue)
2684 {
4b78141a 2685 /* Ordered by the numeric value of these constants. */
19e6b90e 2686 case DW_LANG_C89: printf ("(ANSI C)"); break;
4b78141a
NC
2687 case DW_LANG_C: printf ("(non-ANSI C)"); break;
2688 case DW_LANG_Ada83: printf ("(Ada)"); break;
19e6b90e 2689 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
4b78141a
NC
2690 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
2691 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
19e6b90e
L
2692 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
2693 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
19e6b90e 2694 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
4b78141a 2695 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
19e6b90e 2696 /* DWARF 2.1 values. */
4b78141a 2697 case DW_LANG_Java: printf ("(Java)"); break;
19e6b90e
L
2698 case DW_LANG_C99: printf ("(ANSI C99)"); break;
2699 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
2700 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
4b78141a
NC
2701 /* DWARF 3 values. */
2702 case DW_LANG_PLI: printf ("(PLI)"); break;
2703 case DW_LANG_ObjC: printf ("(Objective C)"); break;
2704 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
2705 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
2706 case DW_LANG_D: printf ("(D)"); break;
2b6f5997
CC
2707 /* DWARF 4 values. */
2708 case DW_LANG_Python: printf ("(Python)"); break;
3362e0d9 2709 /* DWARF 5 values. */
2008a0db 2710 case DW_LANG_OpenCL: printf ("(OpenCL)"); break;
3362e0d9 2711 case DW_LANG_Go: printf ("(Go)"); break;
2008a0db
TT
2712 case DW_LANG_Modula3: printf ("(Modula 3)"); break;
2713 case DW_LANG_Haskell: printf ("(Haskell)"); break;
2714 case DW_LANG_C_plus_plus_03: printf ("(C++03)"); break;
8bc10620 2715 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
2008a0db
TT
2716 case DW_LANG_OCaml: printf ("(OCaml)"); break;
2717 case DW_LANG_Rust: printf ("(Rust)"); break;
6ddfe5b4 2718 case DW_LANG_C11: printf ("(C11)"); break;
2008a0db
TT
2719 case DW_LANG_Swift: printf ("(Swift)"); break;
2720 case DW_LANG_Julia: printf ("(Julia)"); break;
2721 case DW_LANG_Dylan: printf ("(Dylan)"); break;
8bc10620 2722 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
5a195044
MW
2723 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
2724 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
2008a0db 2725 case DW_LANG_RenderScript: printf ("(RenderScript)"); break;
19e6b90e
L
2726 /* MIPS extension. */
2727 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
2728 /* UPC extension. */
2729 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
2730 default:
4b78141a 2731 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
9cf03b7e 2732 printf (_("(implementation defined: %s)"),
467c65bc 2733 dwarf_vmatoa ("x", uvalue));
4b78141a 2734 else
9cf03b7e 2735 printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
19e6b90e
L
2736 break;
2737 }
2738 break;
2739
2740 case DW_AT_encoding:
2e9e81a8 2741 printf ("\t");
19e6b90e
L
2742 switch (uvalue)
2743 {
2744 case DW_ATE_void: printf ("(void)"); break;
2745 case DW_ATE_address: printf ("(machine address)"); break;
2746 case DW_ATE_boolean: printf ("(boolean)"); break;
2747 case DW_ATE_complex_float: printf ("(complex float)"); break;
2748 case DW_ATE_float: printf ("(float)"); break;
2749 case DW_ATE_signed: printf ("(signed)"); break;
2750 case DW_ATE_signed_char: printf ("(signed char)"); break;
2751 case DW_ATE_unsigned: printf ("(unsigned)"); break;
2752 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
e2a0d921 2753 /* DWARF 2.1 values: */
19e6b90e
L
2754 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
2755 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
e2a0d921
NC
2756 /* DWARF 3 values: */
2757 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
2758 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
2759 case DW_ATE_edited: printf ("(edited)"); break;
2760 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
2761 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
04914e37
NC
2762 /* DWARF 4 values: */
2763 case DW_ATE_UTF: printf ("(unicode string)"); break;
2764 /* DWARF 5 values: */
2765 case DW_ATE_UCS: printf ("(UCS)"); break;
2766 case DW_ATE_ASCII: printf ("(ASCII)"); break;
2767
e2a0d921
NC
2768 /* HP extensions: */
2769 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
2770 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
2771 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
2772 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
2773 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
2774 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
2775 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
2776
19e6b90e
L
2777 default:
2778 if (uvalue >= DW_ATE_lo_user
2779 && uvalue <= DW_ATE_hi_user)
9cf03b7e 2780 printf (_("(user defined type)"));
19e6b90e 2781 else
9cf03b7e 2782 printf (_("(unknown type)"));
19e6b90e
L
2783 break;
2784 }
2785 break;
2786
2787 case DW_AT_accessibility:
2e9e81a8 2788 printf ("\t");
19e6b90e
L
2789 switch (uvalue)
2790 {
2791 case DW_ACCESS_public: printf ("(public)"); break;
2792 case DW_ACCESS_protected: printf ("(protected)"); break;
2793 case DW_ACCESS_private: printf ("(private)"); break;
2794 default:
9cf03b7e 2795 printf (_("(unknown accessibility)"));
19e6b90e
L
2796 break;
2797 }
2798 break;
2799
2800 case DW_AT_visibility:
2e9e81a8 2801 printf ("\t");
19e6b90e
L
2802 switch (uvalue)
2803 {
2804 case DW_VIS_local: printf ("(local)"); break;
2805 case DW_VIS_exported: printf ("(exported)"); break;
2806 case DW_VIS_qualified: printf ("(qualified)"); break;
9cf03b7e 2807 default: printf (_("(unknown visibility)")); break;
19e6b90e
L
2808 }
2809 break;
2810
04914e37
NC
2811 case DW_AT_endianity:
2812 printf ("\t");
2813 switch (uvalue)
2814 {
2815 case DW_END_default: printf ("(default)"); break;
2816 case DW_END_big: printf ("(big)"); break;
2817 case DW_END_little: printf ("(little)"); break;
2818 default:
2819 if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
2820 printf (_("(user specified)"));
2821 else
2822 printf (_("(unknown endianity)"));
2823 break;
2824 }
2825 break;
2826
19e6b90e 2827 case DW_AT_virtuality:
2e9e81a8 2828 printf ("\t");
19e6b90e
L
2829 switch (uvalue)
2830 {
2831 case DW_VIRTUALITY_none: printf ("(none)"); break;
2832 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
2833 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
9cf03b7e 2834 default: printf (_("(unknown virtuality)")); break;
19e6b90e
L
2835 }
2836 break;
2837
2838 case DW_AT_identifier_case:
2e9e81a8 2839 printf ("\t");
19e6b90e
L
2840 switch (uvalue)
2841 {
2842 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
2843 case DW_ID_up_case: printf ("(up_case)"); break;
2844 case DW_ID_down_case: printf ("(down_case)"); break;
2845 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
9cf03b7e 2846 default: printf (_("(unknown case)")); break;
19e6b90e
L
2847 }
2848 break;
2849
2850 case DW_AT_calling_convention:
2e9e81a8 2851 printf ("\t");
19e6b90e
L
2852 switch (uvalue)
2853 {
2854 case DW_CC_normal: printf ("(normal)"); break;
2855 case DW_CC_program: printf ("(program)"); break;
2856 case DW_CC_nocall: printf ("(nocall)"); break;
04914e37
NC
2857 case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
2858 case DW_CC_pass_by_value: printf ("(pass by value)"); break;
2859 case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
2860 case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
19e6b90e
L
2861 default:
2862 if (uvalue >= DW_CC_lo_user
2863 && uvalue <= DW_CC_hi_user)
9cf03b7e 2864 printf (_("(user defined)"));
19e6b90e 2865 else
9cf03b7e 2866 printf (_("(unknown convention)"));
19e6b90e
L
2867 }
2868 break;
2869
2870 case DW_AT_ordering:
2e9e81a8 2871 printf ("\t");
19e6b90e
L
2872 switch (uvalue)
2873 {
04914e37 2874 case 255:
9cf03b7e 2875 case -1: printf (_("(undefined)")); break;
19e6b90e
L
2876 case 0: printf ("(row major)"); break;
2877 case 1: printf ("(column major)"); break;
2878 }
2879 break;
2880
04914e37
NC
2881 case DW_AT_decimal_sign:
2882 printf ("\t");
2883 switch (uvalue)
2884 {
2885 case DW_DS_unsigned: printf (_("(unsigned)")); break;
2886 case DW_DS_leading_overpunch: printf (_("(leading overpunch)")); break;
2887 case DW_DS_trailing_overpunch: printf (_("(trailing overpunch)")); break;
2888 case DW_DS_leading_separate: printf (_("(leading separate)")); break;
2889 case DW_DS_trailing_separate: printf (_("(trailing separate)")); break;
2890 default: printf (_("(unrecognised)")); break;
2891 }
2892 break;
2893
2894 case DW_AT_defaulted:
2895 printf ("\t");
2896 switch (uvalue)
2897 {
2898 case DW_DEFAULTED_no: printf (_("(no)")); break;
2899 case DW_DEFAULTED_in_class: printf (_("(in class)")); break;
2900 case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
2901 default: printf (_("(unrecognised)")); break;
2902 }
2903 break;
2904
2905 case DW_AT_discr_list:
2906 printf ("\t");
ec1b0fbb 2907 display_discr_list (form, uvalue, data, end, level);
04914e37
NC
2908 break;
2909
19e6b90e
L
2910 case DW_AT_frame_base:
2911 have_frame_base = 1;
1a0670f3 2912 /* Fall through. */
19e6b90e 2913 case DW_AT_location:
e2a0d921
NC
2914 case DW_AT_string_length:
2915 case DW_AT_return_addr:
19e6b90e
L
2916 case DW_AT_data_member_location:
2917 case DW_AT_vtable_elem_location:
e2a0d921
NC
2918 case DW_AT_segment:
2919 case DW_AT_static_link:
2920 case DW_AT_use_location:
bc0a77d2 2921 case DW_AT_call_value:
629e7ca8 2922 case DW_AT_GNU_call_site_value:
bc0a77d2 2923 case DW_AT_call_data_value:
629e7ca8 2924 case DW_AT_GNU_call_site_data_value:
bc0a77d2 2925 case DW_AT_call_target:
629e7ca8 2926 case DW_AT_GNU_call_site_target:
bc0a77d2 2927 case DW_AT_call_target_clobbered:
629e7ca8 2928 case DW_AT_GNU_call_site_target_clobbered:
212b6063 2929 if ((dwarf_version < 4
b4eb7656 2930 && (form == DW_FORM_data4 || form == DW_FORM_data8))
932fd279 2931 || form == DW_FORM_sec_offset)
2e9e81a8 2932 printf (_(" (location list)"));
e2a0d921 2933 /* Fall through. */
19e6b90e
L
2934 case DW_AT_allocated:
2935 case DW_AT_associated:
2936 case DW_AT_data_location:
2937 case DW_AT_stride:
2938 case DW_AT_upper_bound:
cecf136e 2939 case DW_AT_lower_bound:
19e6b90e
L
2940 if (block_start)
2941 {
2942 int need_frame_base;
2943
2e9e81a8 2944 printf ("\t(");
19e6b90e
L
2945 need_frame_base = decode_location_expression (block_start,
2946 pointer_size,
b7807392
JJ
2947 offset_size,
2948 dwarf_version,
19e6b90e 2949 uvalue,
f1c4cc75 2950 cu_offset, section);
19e6b90e
L
2951 printf (")");
2952 if (need_frame_base && !have_frame_base)
2953 printf (_(" [without DW_AT_frame_base]"));
2954 }
19e6b90e
L
2955 break;
2956
c54207d3
NC
2957 case DW_AT_data_bit_offset:
2958 case DW_AT_byte_size:
2959 case DW_AT_bit_size:
2960 case DW_AT_string_length_byte_size:
2961 case DW_AT_string_length_bit_size:
2962 case DW_AT_bit_stride:
2963 if (form == DW_FORM_exprloc)
2964 {
2965 printf ("\t(");
2966 (void) decode_location_expression (block_start, pointer_size,
2967 offset_size, dwarf_version,
2968 uvalue, cu_offset, section);
2969 printf (")");
2970 }
2971 break;
2972
ec4d4525
NC
2973 case DW_AT_import:
2974 {
a081f3cd
JJ
2975 if (form == DW_FORM_ref_sig8
2976 || form == DW_FORM_GNU_ref_alt)
b4eb7656 2977 break;
2b6f5997 2978
ec4d4525
NC
2979 if (form == DW_FORM_ref1
2980 || form == DW_FORM_ref2
a7a0b6a5
JK
2981 || form == DW_FORM_ref4
2982 || form == DW_FORM_ref_udata)
ec4d4525
NC
2983 uvalue += cu_offset;
2984
6e3d6dc1 2985 if (uvalue >= section->size)
f3853b34 2986 warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset 0x%lx is too big.\n"),
47704ddf
KT
2987 dwarf_vmatoa ("x", uvalue),
2988 (unsigned long) (orig_data - section->start));
6e3d6dc1
NC
2989 else
2990 {
2991 unsigned long abbrev_number;
cd30bcef
AM
2992 abbrev_entry *entry;
2993 unsigned char *p = section->start + uvalue;
6e3d6dc1 2994
cd30bcef 2995 READ_ULEB (abbrev_number, p, end);
cecf136e 2996
2e9e81a8 2997 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
afd6e1ff
JJ
2998 /* Don't look up abbrev for DW_FORM_ref_addr, as it very often will
2999 use different abbrev table, and we don't track .debug_info chunks
3000 yet. */
3001 if (form != DW_FORM_ref_addr)
3002 {
3003 for (entry = first_abbrev; entry != NULL; entry = entry->next)
3004 if (entry->entry == abbrev_number)
3005 break;
3006 if (entry != NULL)
3007 printf (" (%s)", get_TAG_name (entry->tag));
3008 }
6e3d6dc1
NC
3009 printf ("]");
3010 }
ec4d4525
NC
3011 }
3012 break;
3013
19e6b90e
L
3014 default:
3015 break;
3016 }
3017
3018 return data;
3019}
3020
19e6b90e 3021static unsigned char *
dda8d76d
NC
3022read_and_display_attr (unsigned long attribute,
3023 unsigned long form,
3024 dwarf_signed_vma implicit_const,
ec1b0fbb 3025 unsigned char * start,
dda8d76d
NC
3026 unsigned char * data,
3027 unsigned char * end,
3028 dwarf_vma cu_offset,
3029 dwarf_vma pointer_size,
3030 dwarf_vma offset_size,
3031 int dwarf_version,
3032 debug_info * debug_info_p,
3033 int do_loc,
3034 struct dwarf_section * section,
ec1b0fbb
NC
3035 struct cu_tu_set * this_set,
3036 int level)
19e6b90e
L
3037{
3038 if (!do_loc)
750f03b7 3039 printf (" %-18s:", get_AT_name (attribute));
ec1b0fbb
NC
3040 data = read_and_display_attr_value (attribute, form, implicit_const,
3041 start, data, end,
f6f0e17b 3042 cu_offset, pointer_size, offset_size,
19e6b90e 3043 dwarf_version, debug_info_p,
ec1b0fbb 3044 do_loc, section, this_set, ' ', level);
19e6b90e
L
3045 if (!do_loc)
3046 printf ("\n");
3047 return data;
3048}
3049
dda8d76d 3050/* Like load_debug_section, but if the ordinary call fails, and we are
24841daa
NC
3051 following debug links, then attempt to load the requested section
3052 from one of the separate debug info files. */
dda8d76d
NC
3053
3054static bfd_boolean
3055load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
24841daa 3056 void * handle)
dda8d76d 3057{
24841daa 3058 if (load_debug_section (sec_enum, handle))
dda8d76d 3059 {
24841daa
NC
3060 if (debug_displays[sec_enum].section.filename == NULL)
3061 {
3062 /* See if we can associate a filename with this section. */
3063 separate_info * i;
3064
3065 for (i = first_separate_info; i != NULL; i = i->next)
3066 if (i->handle == handle)
3067 {
3068 debug_displays[sec_enum].section.filename = i->filename;
3069 break;
3070 }
3071 }
3072
dda8d76d
NC
3073 return TRUE;
3074 }
3075
24841daa
NC
3076 if (do_follow_links)
3077 {
3078 separate_info * i;
3079
3080 for (i = first_separate_info; i != NULL; i = i->next)
3081 {
3082 if (load_debug_section (sec_enum, i->handle))
3083 {
3084 debug_displays[sec_enum].section.filename = i->filename;
3085
3086 /* FIXME: We should check to see if any of the remaining debug info
3087 files also contain this section, and, umm, do something about it. */
3088 return TRUE;
3089 }
3090 }
3091 }
dda8d76d
NC
3092
3093 return FALSE;
3094}
3095
3096static void
3097introduce (struct dwarf_section * section, bfd_boolean raw)
3098{
3099 if (raw)
3100 {
3101 if (do_follow_links && section->filename)
3102 printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3103 section->name, section->filename);
3104 else
3105 printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3106 }
3107 else
3108 {
3109 if (do_follow_links && section->filename)
3110 printf (_("Contents of the %s section (loaded from %s):\n\n"),
3111 section->name, section->filename);
3112 else
3113 printf (_("Contents of the %s section:\n\n"), section->name);
3114 }
3115}
3116
d85bf2ba
NC
3117/* Process the contents of a .debug_info section.
3118 If do_loc is TRUE then we are scanning for location lists and dwo tags
3119 and we do not want to display anything to the user.
3120 If do_types is TRUE, we are processing a .debug_types section instead of
3121 a .debug_info section.
3122 The information displayed is restricted by the values in DWARF_START_DIE
3123 and DWARF_CUTOFF_LEVEL.
3124 Returns TRUE upon success. Otherwise an error or warning message is
3125 printed and FALSE is returned. */
19e6b90e 3126
d85bf2ba
NC
3127static bfd_boolean
3128process_debug_info (struct dwarf_section * section,
3129 void * file,
3130 enum dwarf_section_display_enum abbrev_sec,
3131 bfd_boolean do_loc,
3132 bfd_boolean do_types)
19e6b90e
L
3133{
3134 unsigned char *start = section->start;
3135 unsigned char *end = start + section->size;
3136 unsigned char *section_begin;
3137 unsigned int unit;
3138 unsigned int num_units = 0;
3139
3140 if ((do_loc || do_debug_loc || do_debug_ranges)
2b6f5997
CC
3141 && num_debug_info_entries == 0
3142 && ! do_types)
19e6b90e 3143 {
767221a9 3144 dwarf_vma length;
19e6b90e
L
3145
3146 /* First scan the section to get the number of comp units. */
3147 for (section_begin = start, num_units = 0; section_begin < end;
3148 num_units ++)
3149 {
3150 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3151 will be the length. For a 64-bit DWARF section, it'll be
3152 the escape code 0xffffffff followed by an 8 byte length. */
0c588247 3153 SAFE_BYTE_GET (length, section_begin, 4, end);
19e6b90e
L
3154
3155 if (length == 0xffffffff)
3156 {
0c588247 3157 SAFE_BYTE_GET (length, section_begin + 4, 8, end);
19e6b90e
L
3158 section_begin += length + 12;
3159 }
ec4d4525
NC
3160 else if (length >= 0xfffffff0 && length < 0xffffffff)
3161 {
767221a9
NC
3162 warn (_("Reserved length value (0x%s) found in section %s\n"),
3163 dwarf_vmatoa ("x", length), section->name);
d85bf2ba 3164 return FALSE;
ec4d4525 3165 }
19e6b90e
L
3166 else
3167 section_begin += length + 4;
aca88567
NC
3168
3169 /* Negative values are illegal, they may even cause infinite
3170 looping. This can happen if we can't accurately apply
f3853b34
NC
3171 relocations to an object file, or if the file is corrupt. */
3172 if ((signed long) length <= 0 || section_begin < start)
aca88567 3173 {
767221a9
NC
3174 warn (_("Corrupt unit length (0x%s) found in section %s\n"),
3175 dwarf_vmatoa ("x", length), section->name);
d85bf2ba 3176 return FALSE;
aca88567 3177 }
19e6b90e
L
3178 }
3179
3180 if (num_units == 0)
3181 {
f41e4712 3182 error (_("No comp units in %s section ?\n"), section->name);
d85bf2ba 3183 return FALSE;
19e6b90e
L
3184 }
3185
3186 /* Then allocate an array to hold the information. */
3f5e193b 3187 debug_information = (debug_info *) cmalloc (num_units,
1306a742 3188 sizeof (* debug_information));
19e6b90e
L
3189 if (debug_information == NULL)
3190 {
f41e4712 3191 error (_("Not enough memory for a debug info array of %u entries\n"),
19e6b90e 3192 num_units);
82b1b41b 3193 alloc_num_debug_info_entries = num_debug_info_entries = 0;
d85bf2ba 3194 return FALSE;
19e6b90e 3195 }
d85bf2ba 3196
03a91817
NC
3197 /* PR 17531: file: 92ca3797.
3198 We cannot rely upon the debug_information array being initialised
3199 before it is used. A corrupt file could easily contain references
3200 to a unit for which information has not been made available. So
3201 we ensure that the array is zeroed here. */
b4eb7656
AM
3202 memset (debug_information, 0, num_units * sizeof (*debug_information));
3203
82b1b41b 3204 alloc_num_debug_info_entries = num_units;
19e6b90e
L
3205 }
3206
3207 if (!do_loc)
3208 {
dda8d76d
NC
3209 load_debug_section_with_follow (str, file);
3210 load_debug_section_with_follow (line_str, file);
3211 load_debug_section_with_follow (str_dwo, file);
3212 load_debug_section_with_follow (str_index, file);
3213 load_debug_section_with_follow (str_index_dwo, file);
3214 load_debug_section_with_follow (debug_addr, file);
19e6b90e 3215 }
e4b7104b 3216
dda8d76d 3217 load_debug_section_with_follow (abbrev_sec, file);
6f875884 3218 if (debug_displays [abbrev_sec].section.start == NULL)
19e6b90e
L
3219 {
3220 warn (_("Unable to locate %s section!\n"),
dda8d76d 3221 debug_displays [abbrev_sec].section.uncompressed_name);
d85bf2ba 3222 return FALSE;
19e6b90e
L
3223 }
3224
dda8d76d
NC
3225 if (!do_loc && dwarf_start_die == 0)
3226 introduce (section, FALSE);
3227
19e6b90e
L
3228 for (section_begin = start, unit = 0; start < end; unit++)
3229 {
3230 DWARF2_Internal_CompUnit compunit;
3231 unsigned char *hdrptr;
19e6b90e 3232 unsigned char *tags;
fd2f0033 3233 int level, last_level, saved_level;
467c65bc 3234 dwarf_vma cu_offset;
e98fdf1a 3235 unsigned long sec_off;
bf5117e3 3236 unsigned int offset_size;
19485196 3237 unsigned int initial_length_size;
74bc6052
CC
3238 dwarf_vma signature_high = 0;
3239 dwarf_vma signature_low = 0;
767221a9 3240 dwarf_vma type_offset = 0;
341f9135
CC
3241 struct cu_tu_set *this_set;
3242 dwarf_vma abbrev_base;
3243 size_t abbrev_size;
19e6b90e
L
3244
3245 hdrptr = start;
3246
0c588247 3247 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
19e6b90e
L
3248
3249 if (compunit.cu_length == 0xffffffff)
3250 {
0c588247 3251 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
19e6b90e
L
3252 offset_size = 8;
3253 initial_length_size = 12;
3254 }
3255 else
3256 {
3257 offset_size = 4;
3258 initial_length_size = 4;
3259 }
3260
0c588247 3261 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end);
19e6b90e
L
3262
3263 cu_offset = start - section_begin;
19e6b90e 3264
341f9135
CC
3265 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3266
77145576
JK
3267 if (compunit.cu_version < 5)
3268 {
3269 compunit.cu_unit_type = DW_UT_compile;
3270 /* Initialize it due to a false compiler warning. */
3271 compunit.cu_pointer_size = -1;
3272 }
3273 else
3274 {
3275 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end);
3276 do_types = (compunit.cu_unit_type == DW_UT_type);
3277
3278 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
3279 }
3280
0c588247 3281 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end);
19e6b90e 3282
341f9135
CC
3283 if (this_set == NULL)
3284 {
3285 abbrev_base = 0;
3286 abbrev_size = debug_displays [abbrev_sec].section.size;
3287 }
3288 else
3289 {
3290 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3291 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3292 }
3293
77145576
JK
3294 if (compunit.cu_version < 5)
3295 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
3296
f41e4712
NC
3297 /* PR 17512: file: 001-108546-0.001:0.1. */
3298 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3299 {
3300 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3301 compunit.cu_pointer_size, offset_size);
3302 compunit.cu_pointer_size = offset_size;
3303 }
2b6f5997
CC
3304
3305 if (do_types)
b4eb7656 3306 {
0c588247 3307 SAFE_BYTE_GET64 (hdrptr, &signature_high, &signature_low, end);
f048b142 3308 hdrptr += 8;
0c588247 3309 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end);
b4eb7656 3310 }
2b6f5997 3311
ae7e7825
NC
3312 if (dwarf_start_die > (cu_offset + compunit.cu_length
3313 + initial_length_size))
3314 {
3315 start = section_begin + cu_offset + compunit.cu_length
3316 + initial_length_size;
3317 continue;
3318 }
3319
19e6b90e 3320 if ((do_loc || do_debug_loc || do_debug_ranges)
2b6f5997 3321 && num_debug_info_entries == 0
c4b2f181 3322 && alloc_num_debug_info_entries > unit
2b6f5997 3323 && ! do_types)
19e6b90e
L
3324 {
3325 debug_information [unit].cu_offset = cu_offset;
3326 debug_information [unit].pointer_size
3327 = compunit.cu_pointer_size;
b7807392
JJ
3328 debug_information [unit].offset_size = offset_size;
3329 debug_information [unit].dwarf_version = compunit.cu_version;
19e6b90e 3330 debug_information [unit].base_address = 0;
4723351a
CC
3331 debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
3332 debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
19e6b90e
L
3333 debug_information [unit].loc_offsets = NULL;
3334 debug_information [unit].have_frame_base = NULL;
3335 debug_information [unit].max_loc_offsets = 0;
3336 debug_information [unit].num_loc_offsets = 0;
3337 debug_information [unit].range_lists = NULL;
3338 debug_information [unit].max_range_lists= 0;
3339 debug_information [unit].num_range_lists = 0;
3340 }
3341
fd2f0033 3342 if (!do_loc && dwarf_start_die == 0)
19e6b90e 3343 {
47704ddf
KT
3344 printf (_(" Compilation Unit @ offset 0x%s:\n"),
3345 dwarf_vmatoa ("x", cu_offset));
3346 printf (_(" Length: 0x%s (%s)\n"),
3347 dwarf_vmatoa ("x", compunit.cu_length),
49e7b350 3348 offset_size == 8 ? "64-bit" : "32-bit");
19e6b90e 3349 printf (_(" Version: %d\n"), compunit.cu_version);
7282333f
AM
3350 printf (_(" Abbrev Offset: 0x%s\n"),
3351 dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
19e6b90e 3352 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
2b6f5997
CC
3353 if (do_types)
3354 {
74bc6052
CC
3355 char buf[64];
3356
3357 printf (_(" Signature: 0x%s\n"),
3358 dwarf_vmatoa64 (signature_high, signature_low,
3359 buf, sizeof (buf)));
3360 printf (_(" Type Offset: 0x%s\n"),
3361 dwarf_vmatoa ("x", type_offset));
2b6f5997 3362 }
341f9135
CC
3363 if (this_set != NULL)
3364 {
3365 dwarf_vma *offsets = this_set->section_offsets;
3366 size_t *sizes = this_set->section_sizes;
3367
3368 printf (_(" Section contributions:\n"));
3369 printf (_(" .debug_abbrev.dwo: 0x%s 0x%s\n"),
3370 dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
3371 dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
3372 printf (_(" .debug_line.dwo: 0x%s 0x%s\n"),
3373 dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
3374 dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
3375 printf (_(" .debug_loc.dwo: 0x%s 0x%s\n"),
3376 dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
3377 dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
3378 printf (_(" .debug_str_offsets.dwo: 0x%s 0x%s\n"),
3379 dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
3380 dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
3381 }
19e6b90e
L
3382 }
3383
e98fdf1a
AM
3384 sec_off = cu_offset + initial_length_size;
3385 if (sec_off + compunit.cu_length < sec_off
3386 || sec_off + compunit.cu_length > section->size)
460c89ff 3387 {
e98fdf1a
AM
3388 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
3389 section->name,
3390 (unsigned long) cu_offset,
19485196
NC
3391 dwarf_vmatoa ("x", compunit.cu_length));
3392 num_units = unit;
3393 break;
3394 }
3395
460c89ff
NS
3396 tags = hdrptr;
3397 start += compunit.cu_length + initial_length_size;
3398
77145576 3399 if (compunit.cu_version < 2 || compunit.cu_version > 5)
19e6b90e 3400 {
47704ddf
KT
3401 warn (_("CU at offset %s contains corrupt or "
3402 "unsupported version number: %d.\n"),
3403 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
19e6b90e
L
3404 continue;
3405 }
3406
77145576
JK
3407 if (compunit.cu_unit_type != DW_UT_compile
3408 && compunit.cu_unit_type != DW_UT_type)
3409 {
3410 warn (_("CU at offset %s contains corrupt or "
3411 "unsupported unit type: %d.\n"),
3412 dwarf_vmatoa ("x", cu_offset), compunit.cu_unit_type);
3413 continue;
3414 }
3415
19e6b90e
L
3416 free_abbrevs ();
3417
d493b283 3418 /* Process the abbrevs used by this compilation unit. */
341f9135 3419 if (compunit.cu_abbrev_offset >= abbrev_size)
ec4d4525
NC
3420 warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
3421 (unsigned long) compunit.cu_abbrev_offset,
341f9135 3422 (unsigned long) abbrev_size);
b4eb7656 3423 /* PR 17531: file:4bcd9ce9. */
a0a3b04c
L
3424 else if ((abbrev_base + abbrev_size)
3425 > debug_displays [abbrev_sec].section.size)
3426 warn (_("Debug info is corrupted, abbrev size (%lx) is larger than abbrev section size (%lx)\n"),
3427 (unsigned long) abbrev_base + abbrev_size,
3428 (unsigned long) debug_displays [abbrev_sec].section.size);
460c89ff
NS
3429 else
3430 process_abbrev_section
341f9135
CC
3431 (((unsigned char *) debug_displays [abbrev_sec].section.start
3432 + abbrev_base + compunit.cu_abbrev_offset),
3433 ((unsigned char *) debug_displays [abbrev_sec].section.start
3434 + abbrev_base + abbrev_size));
19e6b90e
L
3435
3436 level = 0;
fd2f0033
TT
3437 last_level = level;
3438 saved_level = -1;
19e6b90e
L
3439 while (tags < start)
3440 {
19e6b90e 3441 unsigned long abbrev_number;
ec4d4525 3442 unsigned long die_offset;
19e6b90e
L
3443 abbrev_entry *entry;
3444 abbrev_attr *attr;
fd2f0033 3445 int do_printing = 1;
19e6b90e 3446
ec4d4525
NC
3447 die_offset = tags - section_begin;
3448
cd30bcef 3449 READ_ULEB (abbrev_number, tags, start);
19e6b90e 3450
eb7cc021
JK
3451 /* A null DIE marks the end of a list of siblings or it may also be
3452 a section padding. */
19e6b90e
L
3453 if (abbrev_number == 0)
3454 {
eb7cc021
JK
3455 /* Check if it can be a section padding for the last CU. */
3456 if (level == 0 && start == end)
3457 {
3458 unsigned char *chk;
3459
3460 for (chk = tags; chk < start; chk++)
3461 if (*chk != 0)
3462 break;
3463 if (chk == start)
3464 break;
3465 }
3466
4337774f
TT
3467 if (!do_loc && die_offset >= dwarf_start_die
3468 && (dwarf_cutoff_level == -1
3469 || level < dwarf_cutoff_level))
399c99f7
L
3470 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
3471 level, die_offset);
3472
19e6b90e 3473 --level;
ec4d4525
NC
3474 if (level < 0)
3475 {
3476 static unsigned num_bogus_warns = 0;
3477
3478 if (num_bogus_warns < 3)
3479 {
4723351a
CC
3480 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
3481 die_offset, section->name);
ec4d4525
NC
3482 num_bogus_warns ++;
3483 if (num_bogus_warns == 3)
3484 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
3485 }
3486 }
fd2f0033 3487 if (dwarf_start_die != 0 && level < saved_level)
d85bf2ba 3488 return TRUE;
19e6b90e
L
3489 continue;
3490 }
3491
4b78141a 3492 if (!do_loc)
fd2f0033
TT
3493 {
3494 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
3495 do_printing = 0;
3496 else
3497 {
3498 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
3499 saved_level = level;
3500 do_printing = (dwarf_cutoff_level == -1
3501 || level < dwarf_cutoff_level);
3502 if (do_printing)
3503 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
3504 level, die_offset, abbrev_number);
3505 else if (dwarf_cutoff_level == -1
3506 || last_level < dwarf_cutoff_level)
3507 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
3508 last_level = level;
3509 }
3510 }
cecf136e 3511
19e6b90e
L
3512 /* Scan through the abbreviation list until we reach the
3513 correct entry. */
3514 for (entry = first_abbrev;
3515 entry && entry->entry != abbrev_number;
3516 entry = entry->next)
3517 continue;
3518
3519 if (entry == NULL)
3520 {
fd2f0033 3521 if (!do_loc && do_printing)
4b78141a
NC
3522 {
3523 printf ("\n");
3524 fflush (stdout);
3525 }
f3853b34 3526 warn (_("DIE at offset 0x%lx refers to abbreviation number %lu which does not exist\n"),
cc86f28f 3527 die_offset, abbrev_number);
d85bf2ba 3528 return FALSE;
19e6b90e
L
3529 }
3530
fd2f0033 3531 if (!do_loc && do_printing)
cc5914eb 3532 printf (" (%s)\n", get_TAG_name (entry->tag));
cecf136e 3533
19e6b90e
L
3534 switch (entry->tag)
3535 {
3536 default:
3537 need_base_address = 0;
3538 break;
3539 case DW_TAG_compile_unit:
d85bf2ba
NC
3540 need_base_address = 1;
3541 need_dwo_info = do_loc;
19e6b90e
L
3542 break;
3543 case DW_TAG_entry_point:
19e6b90e
L
3544 case DW_TAG_subprogram:
3545 need_base_address = 0;
3546 /* Assuming that there is no DW_AT_frame_base. */
3547 have_frame_base = 0;
3548 break;
3549 }
3550
9f272209
AO
3551 debug_info *debug_info_p =
3552 (debug_information && unit < alloc_num_debug_info_entries)
3553 ? debug_information + unit : NULL;
3554
3555 assert (!debug_info_p
3556 || (debug_info_p->num_loc_offsets
3557 == debug_info_p->num_loc_views));
3558
399c99f7
L
3559 for (attr = entry->first_attr;
3560 attr && attr->attribute;
3561 attr = attr->next)
4b78141a 3562 {
fd2f0033 3563 if (! do_loc && do_printing)
4b78141a 3564 /* Show the offset from where the tag was extracted. */
fd2f0033 3565 printf (" <%lx>", (unsigned long)(tags - section_begin));
4b78141a
NC
3566 tags = read_and_display_attr (attr->attribute,
3567 attr->form,
77145576 3568 attr->implicit_const,
ec1b0fbb 3569 section_begin,
341f9135 3570 tags,
f6f0e17b 3571 end,
341f9135 3572 cu_offset,
4b78141a
NC
3573 compunit.cu_pointer_size,
3574 offset_size,
3575 compunit.cu_version,
9f272209 3576 debug_info_p,
341f9135
CC
3577 do_loc || ! do_printing,
3578 section,
ec1b0fbb
NC
3579 this_set,
3580 level);
4b78141a 3581 }
cecf136e 3582
9f272209
AO
3583 /* If a locview attribute appears before a location one,
3584 make sure we don't associate it with an earlier
3585 loclist. */
3586 if (debug_info_p)
3587 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
3588 {
3589 case 1:
3590 debug_info_p->loc_views [debug_info_p->num_loc_views] = vm1;
3591 debug_info_p->num_loc_views++;
3592 assert (debug_info_p->num_loc_views
3593 == debug_info_p->num_loc_offsets);
3594 break;
3595
3596 case 0:
3597 break;
3598
3599 case -1:
3600 warn(_("DIE has locviews without loclist\n"));
3601 debug_info_p->num_loc_views--;
3602 break;
3603
3604 default:
3605 assert (0);
3606 }
3607
b4eb7656
AM
3608 if (entry->children)
3609 ++level;
3610 }
19e6b90e 3611 }
cecf136e 3612
19e6b90e
L
3613 /* Set num_debug_info_entries here so that it can be used to check if
3614 we need to process .debug_loc and .debug_ranges sections. */
3615 if ((do_loc || do_debug_loc || do_debug_ranges)
2b6f5997
CC
3616 && num_debug_info_entries == 0
3617 && ! do_types)
82b1b41b 3618 {
b4eb7656 3619 if (num_units > alloc_num_debug_info_entries)
82b1b41b
NC
3620 num_debug_info_entries = alloc_num_debug_info_entries;
3621 else
3622 num_debug_info_entries = num_units;
3623 }
cecf136e 3624
19e6b90e 3625 if (!do_loc)
467c65bc 3626 printf ("\n");
cecf136e 3627
d85bf2ba 3628 return TRUE;
19e6b90e
L
3629}
3630
3631/* Locate and scan the .debug_info section in the file and record the pointer
3632 sizes and offsets for the compilation units in it. Usually an executable
3633 will have just one pointer size, but this is not guaranteed, and so we try
3634 not to make any assumptions. Returns zero upon failure, or the number of
3635 compilation units upon success. */
3636
3637static unsigned int
3638load_debug_info (void * file)
3639{
1febe64d 3640 /* If we have already tried and failed to load the .debug_info
657d0d47 3641 section then do not bother to repeat the task. */
cc86f28f 3642 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
1febe64d
NC
3643 return 0;
3644
19e6b90e
L
3645 /* If we already have the information there is nothing else to do. */
3646 if (num_debug_info_entries > 0)
3647 return num_debug_info_entries;
3648
341f9135 3649 /* If this is a DWARF package file, load the CU and TU indexes. */
43a444f9 3650 (void) load_cu_tu_indexes (file);
341f9135 3651
dda8d76d 3652 if (load_debug_section_with_follow (info, file)
d85bf2ba 3653 && process_debug_info (&debug_displays [info].section, file, abbrev, TRUE, FALSE))
19e6b90e 3654 return num_debug_info_entries;
82b1b41b 3655
dda8d76d 3656 if (load_debug_section_with_follow (info_dwo, file)
82b1b41b 3657 && process_debug_info (&debug_displays [info_dwo].section, file,
d85bf2ba 3658 abbrev_dwo, TRUE, FALSE))
4723351a 3659 return num_debug_info_entries;
1febe64d 3660
cc86f28f 3661 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
1febe64d 3662 return 0;
19e6b90e
L
3663}
3664
b40bf0a2
NC
3665/* Read a DWARF .debug_line section header starting at DATA.
3666 Upon success returns an updated DATA pointer and the LINFO
3667 structure and the END_OF_SEQUENCE pointer will be filled in.
3668 Otherwise returns NULL. */
19e6b90e 3669
b40bf0a2
NC
3670static unsigned char *
3671read_debug_line_header (struct dwarf_section * section,
3672 unsigned char * data,
3673 unsigned char * end,
3674 DWARF2_Internal_LineInfo * linfo,
3675 unsigned char ** end_of_sequence)
3676{
3677 unsigned char *hdrptr;
b40bf0a2 3678 unsigned int initial_length_size;
19e6b90e 3679
b40bf0a2
NC
3680 /* Extract information from the Line Number Program Header.
3681 (section 6.2.4 in the Dwarf3 doc). */
6937bb54 3682 hdrptr = data;
19e6b90e 3683
b40bf0a2
NC
3684 /* Get and check the length of the block. */
3685 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
19e6b90e 3686
b40bf0a2 3687 if (linfo->li_length == 0xffffffff)
f41e4712
NC
3688 {
3689 /* This section is 64-bit DWARF 3. */
b40bf0a2 3690 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
77145576 3691 linfo->li_offset_size = 8;
f41e4712
NC
3692 initial_length_size = 12;
3693 }
3694 else
3695 {
77145576 3696 linfo->li_offset_size = 4;
f41e4712
NC
3697 initial_length_size = 4;
3698 }
19e6b90e 3699
b40bf0a2 3700 if (linfo->li_length + initial_length_size > section->size)
f41e4712 3701 {
8fcc61b4
NC
3702 /* If the length field has a relocation against it, then we should
3703 not complain if it is inaccurate (and probably negative). This
3704 happens in object files when the .debug_line section is actually
3705 comprised of several different .debug_line.* sections, (some of
3706 which may be removed by linker garbage collection), and a relocation
3707 is used to compute the correct length once that is done. */
77145576 3708 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
b40bf0a2 3709 {
8fcc61b4 3710 linfo->li_length = (end - data) - initial_length_size;
b40bf0a2
NC
3711 }
3712 else
3713 {
8fcc61b4
NC
3714 warn (_("The length field (0x%lx) in the debug_line header is wrong - the section is too small\n"),
3715 (long) linfo->li_length);
b40bf0a2
NC
3716 return NULL;
3717 }
f41e4712 3718 }
19e6b90e 3719
b40bf0a2
NC
3720 /* Get and check the version number. */
3721 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
3722
3723 if (linfo->li_version != 2
3724 && linfo->li_version != 3
77145576
JK
3725 && linfo->li_version != 4
3726 && linfo->li_version != 5)
f41e4712 3727 {
77145576
JK
3728 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
3729 "is currently supported.\n"));
b40bf0a2 3730 return NULL;
f41e4712 3731 }
19e6b90e 3732
77145576
JK
3733 if (linfo->li_version >= 5)
3734 {
5496f3c6 3735 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
77145576 3736
5496f3c6
NC
3737 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
3738 if (linfo->li_segment_size != 0)
77145576
JK
3739 {
3740 warn (_("The %s section contains "
3741 "unsupported segment selector size: %d.\n"),
5496f3c6
NC
3742 section->name, linfo->li_segment_size);
3743 return NULL;
77145576
JK
3744 }
3745 }
3746
3747 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
3748 linfo->li_offset_size, end);
b40bf0a2 3749 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
0c588247 3750
b40bf0a2 3751 if (linfo->li_version >= 4)
f41e4712 3752 {
b40bf0a2 3753 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
0c588247 3754
b40bf0a2 3755 if (linfo->li_max_ops_per_insn == 0)
f41e4712
NC
3756 {
3757 warn (_("Invalid maximum operations per insn.\n"));
b40bf0a2 3758 return NULL;
a233b20c 3759 }
f41e4712
NC
3760 }
3761 else
b40bf0a2 3762 linfo->li_max_ops_per_insn = 1;
0c588247 3763
b40bf0a2 3764 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
65879393 3765 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
b40bf0a2
NC
3766 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
3767 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
19e6b90e 3768
b40bf0a2 3769 * end_of_sequence = data + linfo->li_length + initial_length_size;
b4eb7656 3770 /* PR 17512: file:002-117414-0.004. */
6937bb54
NC
3771 if (* end_of_sequence > end)
3772 {
4c219c2e
AM
3773 warn (_("Line length %s extends beyond end of section\n"),
3774 dwarf_vmatoa ("u", linfo->li_length));
6937bb54
NC
3775 * end_of_sequence = end;
3776 return NULL;
3777 }
3778
b40bf0a2
NC
3779 return hdrptr;
3780}
19e6b90e 3781
77145576 3782static unsigned char *
dda8d76d
NC
3783display_formatted_table (unsigned char * data,
3784 unsigned char * start,
3785 unsigned char * end,
3786 const DWARF2_Internal_LineInfo * linfo,
3787 struct dwarf_section * section,
7b8d9e8c 3788 bfd_boolean is_dir)
77145576
JK
3789{
3790 unsigned char *format_start, format_count, *format, formati;
3791 dwarf_vma data_count, datai;
cd30bcef 3792 unsigned int namepass, last_entry = 0;
5496f3c6
NC
3793 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
3794
77145576 3795 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
546cb2d8
NC
3796 if (do_checks && format_count > 5)
3797 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
3798 table_name, format_count);
3799
77145576
JK
3800 format_start = data;
3801 for (formati = 0; formati < format_count; formati++)
3802 {
cd30bcef
AM
3803 SKIP_ULEB (data, end);
3804 SKIP_ULEB (data, end);
77145576
JK
3805 if (data == end)
3806 {
5496f3c6 3807 warn (_("%s: Corrupt format description entry\n"), table_name);
77145576
JK
3808 return data;
3809 }
3810 }
3811
cd30bcef 3812 READ_ULEB (data_count, data, end);
546cb2d8 3813 if (data_count == 0)
77145576 3814 {
546cb2d8 3815 printf (_("\n The %s is empty.\n"), table_name);
77145576
JK
3816 return data;
3817 }
546cb2d8 3818 else if (data == end)
77145576 3819 {
546cb2d8
NC
3820 warn (_("%s: Corrupt entry count - expected %s but none found\n"),
3821 table_name, dwarf_vmatoa ("x", data_count));
77145576
JK
3822 return data;
3823 }
546cb2d8 3824
5496f3c6
NC
3825 else if (format_count == 0)
3826 {
3827 warn (_("%s: format count is zero, but the table is not empty\n"),
3828 table_name);
3829 return end;
3830 }
77145576 3831
5496f3c6
NC
3832 printf (_("\n The %s (offset 0x%lx, lines %s, columns %u):\n"),
3833 table_name, (long) (data - start), dwarf_vmatoa ("u", data_count),
3834 format_count);
77145576
JK
3835
3836 printf (_(" Entry"));
3837 /* Delay displaying name as the last entry for better screen layout. */
3838 for (namepass = 0; namepass < 2; namepass++)
3839 {
3840 format = format_start;
3841 for (formati = 0; formati < format_count; formati++)
3842 {
3843 dwarf_vma content_type;
3844
cd30bcef 3845 READ_ULEB (content_type, format, end);
77145576
JK
3846 if ((content_type == DW_LNCT_path) == (namepass == 1))
3847 switch (content_type)
3848 {
3849 case DW_LNCT_path:
3850 printf (_("\tName"));
3851 break;
3852 case DW_LNCT_directory_index:
3853 printf (_("\tDir"));
3854 break;
3855 case DW_LNCT_timestamp:
3856 printf (_("\tTime"));
3857 break;
3858 case DW_LNCT_size:
3859 printf (_("\tSize"));
3860 break;
3861 case DW_LNCT_MD5:
5496f3c6 3862 printf (_("\tMD5\t\t\t"));
77145576
JK
3863 break;
3864 default:
3865 printf (_("\t(Unknown format content type %s)"),
3866 dwarf_vmatoa ("u", content_type));
3867 }
cd30bcef 3868 SKIP_ULEB (format, end);
77145576
JK
3869 }
3870 }
3871 putchar ('\n');
3872
3873 for (datai = 0; datai < data_count; datai++)
3874 {
3875 unsigned char *datapass = data;
3876
3877 printf (" %d", last_entry++);
3878 /* Delay displaying name as the last entry for better screen layout. */
3879 for (namepass = 0; namepass < 2; namepass++)
3880 {
3881 format = format_start;
3882 data = datapass;
3883 for (formati = 0; formati < format_count; formati++)
3884 {
3885 dwarf_vma content_type, form;
3886
cd30bcef
AM
3887 READ_ULEB (content_type, format, end);
3888 READ_ULEB (form, format, end);
3889 data = read_and_display_attr_value (0, form, 0, start, data, end,
3890 0, 0, linfo->li_offset_size,
77145576
JK
3891 linfo->li_version, NULL,
3892 ((content_type == DW_LNCT_path) != (namepass == 1)),
ec1b0fbb 3893 section, NULL, '\t', -1);
77145576
JK
3894 }
3895 }
5496f3c6
NC
3896
3897 if (data == end && (datai < data_count - 1))
77145576 3898 {
5496f3c6 3899 warn (_("\n%s: Corrupt entries list\n"), table_name);
77145576
JK
3900 return data;
3901 }
3902 putchar ('\n');
3903 }
3904 return data;
3905}
3906
b40bf0a2 3907static int
dda8d76d
NC
3908display_debug_lines_raw (struct dwarf_section * section,
3909 unsigned char * data,
3910 unsigned char * end,
3911 void * file)
b40bf0a2
NC
3912{
3913 unsigned char *start = section->start;
ba8826a8 3914 int verbose_view = 0;
19e6b90e 3915
dda8d76d 3916 introduce (section, TRUE);
19e6b90e 3917
b40bf0a2
NC
3918 while (data < end)
3919 {
3920 static DWARF2_Internal_LineInfo saved_linfo;
3921 DWARF2_Internal_LineInfo linfo;
3922 unsigned char *standard_opcodes;
3923 unsigned char *end_of_sequence;
fe59e83d 3924 int i;
19e6b90e 3925
4925cdd7
NC
3926 if (const_strneq (section->name, ".debug_line.")
3927 /* Note: the following does not apply to .debug_line.dwo sections.
3928 These are full debug_line sections. */
3929 && strcmp (section->name, ".debug_line.dwo") != 0)
19e6b90e 3930 {
b40bf0a2
NC
3931 /* Sections named .debug_line.<foo> are fragments of a .debug_line
3932 section containing just the Line Number Statements. They are
3933 created by the assembler and intended to be used alongside gcc's
3934 -ffunction-sections command line option. When the linker's
3935 garbage collection decides to discard a .text.<foo> section it
3936 can then also discard the line number information in .debug_line.<foo>.
3937
4925cdd7 3938 Since the section is a fragment it does not have the details
b40bf0a2 3939 needed to fill out a LineInfo structure, so instead we use the
4925cdd7 3940 details from the last full debug_line section that we processed. */
b40bf0a2
NC
3941 end_of_sequence = end;
3942 standard_opcodes = NULL;
3943 linfo = saved_linfo;
058037d3
NC
3944 /* PR 17531: file: 0522b371. */
3945 if (linfo.li_line_range == 0)
3946 {
1306a742 3947 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
058037d3
NC
3948 return 0;
3949 }
b40bf0a2 3950 reset_state_machine (linfo.li_default_is_stmt);
19e6b90e 3951 }
19e6b90e
L
3952 else
3953 {
b40bf0a2 3954 unsigned char * hdrptr;
19e6b90e 3955
b40bf0a2
NC
3956 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
3957 & end_of_sequence)) == NULL)
3958 return 0;
19e6b90e 3959
b40bf0a2
NC
3960 printf (_(" Offset: 0x%lx\n"), (long)(data - start));
3961 printf (_(" Length: %ld\n"), (long) linfo.li_length);
3962 printf (_(" DWARF Version: %d\n"), linfo.li_version);
5496f3c6
NC
3963 if (linfo.li_version >= 5)
3964 {
3965 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
3966 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
3967 }
77ef8654 3968 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
b40bf0a2
NC
3969 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
3970 if (linfo.li_version >= 4)
3971 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
3972 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
3973 printf (_(" Line Base: %d\n"), linfo.li_line_base);
3974 printf (_(" Line Range: %d\n"), linfo.li_line_range);
3975 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
19e6b90e 3976
0a9d414a
NC
3977 /* PR 17512: file: 1665-6428-0.004. */
3978 if (linfo.li_line_range == 0)
3979 {
3980 warn (_("Line range of 0 is invalid, using 1 instead\n"));
3981 linfo.li_line_range = 1;
3982 }
77ef8654 3983
b40bf0a2 3984 reset_state_machine (linfo.li_default_is_stmt);
19e6b90e 3985
b40bf0a2
NC
3986 /* Display the contents of the Opcodes table. */
3987 standard_opcodes = hdrptr;
19e6b90e 3988
6937bb54
NC
3989 /* PR 17512: file: 002-417945-0.004. */
3990 if (standard_opcodes + linfo.li_opcode_base >= end)
3991 {
3992 warn (_("Line Base extends beyond end of section\n"));
3993 return 0;
3994 }
3995
b40bf0a2 3996 printf (_("\n Opcodes:\n"));
19e6b90e 3997
b40bf0a2 3998 for (i = 1; i < linfo.li_opcode_base; i++)
d3a49aa8
AM
3999 printf (ngettext (" Opcode %d has %d arg\n",
4000 " Opcode %d has %d args\n",
4001 standard_opcodes[i - 1]),
4002 i, standard_opcodes[i - 1]);
19e6b90e 4003
b40bf0a2
NC
4004 /* Display the contents of the Directory table. */
4005 data = standard_opcodes + linfo.li_opcode_base - 1;
19e6b90e 4006
77145576 4007 if (linfo.li_version >= 5)
b40bf0a2 4008 {
dda8d76d 4009 load_debug_section_with_follow (line_str, file);
19e6b90e 4010
77145576 4011 data = display_formatted_table (data, start, end, &linfo, section,
7b8d9e8c 4012 TRUE);
77145576 4013 data = display_formatted_table (data, start, end, &linfo, section,
7b8d9e8c 4014 FALSE);
77145576
JK
4015 }
4016 else
4017 {
4018 if (*data == 0)
4019 printf (_("\n The Directory Table is empty.\n"));
4020 else
a233b20c 4021 {
77145576 4022 unsigned int last_dir_entry = 0;
6937bb54 4023
77145576
JK
4024 printf (_("\n The Directory Table (offset 0x%lx):\n"),
4025 (long)(data - start));
19e6b90e 4026
77145576
JK
4027 while (data < end && *data != 0)
4028 {
4029 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
19e6b90e 4030
77145576
JK
4031 data += strnlen ((char *) data, end - data) + 1;
4032 }
19e6b90e 4033
77145576
JK
4034 /* PR 17512: file: 002-132094-0.004. */
4035 if (data >= end - 1)
4036 break;
4037 }
19e6b90e 4038
77145576
JK
4039 /* Skip the NUL at the end of the table. */
4040 data++;
19e6b90e 4041
77145576
JK
4042 /* Display the contents of the File Name table. */
4043 if (*data == 0)
4044 printf (_("\n The File Name Table is empty.\n"));
4045 else
4046 {
4047 printf (_("\n The File Name Table (offset 0x%lx):\n"),
4048 (long)(data - start));
4049 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
19e6b90e 4050
77145576 4051 while (data < end && *data != 0)
b40bf0a2 4052 {
77145576 4053 unsigned char *name;
cd30bcef 4054 dwarf_vma val;
77145576
JK
4055
4056 printf (" %d\t", ++state_machine_regs.last_file_entry);
4057 name = data;
4058 data += strnlen ((char *) data, end - data) + 1;
4059
cd30bcef
AM
4060 READ_ULEB (val, data, end);
4061 printf ("%s\t", dwarf_vmatoa ("u", val));
4062 READ_ULEB (val, data, end);
4063 printf ("%s\t", dwarf_vmatoa ("u", val));
4064 READ_ULEB (val, data, end);
4065 printf ("%s\t", dwarf_vmatoa ("u", val));
77145576
JK
4066 printf ("%.*s\n", (int)(end - name), name);
4067
4068 if (data == end)
4069 {
4070 warn (_("Corrupt file name table entry\n"));
4071 break;
4072 }
b40bf0a2 4073 }
a233b20c 4074 }
77145576
JK
4075
4076 /* Skip the NUL at the end of the table. */
4077 data++;
b40bf0a2 4078 }
19e6b90e 4079
b40bf0a2
NC
4080 putchar ('\n');
4081 saved_linfo = linfo;
4082 }
19e6b90e 4083
b40bf0a2
NC
4084 /* Now display the statements. */
4085 if (data >= end_of_sequence)
4086 printf (_(" No Line Number Statements.\n"));
4087 else
4088 {
4089 printf (_(" Line Number Statements:\n"));
19e6b90e 4090
b40bf0a2
NC
4091 while (data < end_of_sequence)
4092 {
4093 unsigned char op_code;
4094 dwarf_signed_vma adv;
4095 dwarf_vma uladv;
19e6b90e 4096
fe59e83d
CC
4097 printf (" [0x%08lx]", (long)(data - start));
4098
b40bf0a2 4099 op_code = *data++;
19e6b90e 4100
b40bf0a2 4101 if (op_code >= linfo.li_opcode_base)
19e6b90e 4102 {
b40bf0a2
NC
4103 op_code -= linfo.li_opcode_base;
4104 uladv = (op_code / linfo.li_line_range);
4105 if (linfo.li_max_ops_per_insn == 1)
4106 {
4107 uladv *= linfo.li_min_insn_length;
4108 state_machine_regs.address += uladv;
ba8826a8
AO
4109 if (uladv)
4110 state_machine_regs.view = 0;
b40bf0a2 4111 printf (_(" Special opcode %d: "
ba8826a8 4112 "advance Address by %s to 0x%s%s"),
b40bf0a2 4113 op_code, dwarf_vmatoa ("u", uladv),
ba8826a8
AO
4114 dwarf_vmatoa ("x", state_machine_regs.address),
4115 verbose_view && uladv
4116 ? _(" (reset view)") : "");
b40bf0a2
NC
4117 }
4118 else
4119 {
ba8826a8
AO
4120 unsigned addrdelta
4121 = ((state_machine_regs.op_index + uladv)
b40bf0a2
NC
4122 / linfo.li_max_ops_per_insn)
4123 * linfo.li_min_insn_length;
ba8826a8
AO
4124
4125 state_machine_regs.address += addrdelta;
b40bf0a2
NC
4126 state_machine_regs.op_index
4127 = (state_machine_regs.op_index + uladv)
4128 % linfo.li_max_ops_per_insn;
ba8826a8
AO
4129 if (addrdelta)
4130 state_machine_regs.view = 0;
b40bf0a2 4131 printf (_(" Special opcode %d: "
ba8826a8 4132 "advance Address by %s to 0x%s[%d]%s"),
b40bf0a2
NC
4133 op_code, dwarf_vmatoa ("u", uladv),
4134 dwarf_vmatoa ("x", state_machine_regs.address),
ba8826a8
AO
4135 state_machine_regs.op_index,
4136 verbose_view && addrdelta
4137 ? _(" (reset view)") : "");
b40bf0a2
NC
4138 }
4139 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4140 state_machine_regs.line += adv;
ba8826a8 4141 printf (_(" and Line by %s to %d"),
b40bf0a2 4142 dwarf_vmatoa ("d", adv), state_machine_regs.line);
ba8826a8
AO
4143 if (verbose_view || state_machine_regs.view)
4144 printf (_(" (view %u)\n"), state_machine_regs.view);
4145 else
4146 putchar ('\n');
4147 state_machine_regs.view++;
19e6b90e 4148 }
cd30bcef
AM
4149 else
4150 switch (op_code)
4151 {
4152 case DW_LNS_extended_op:
4153 data += process_extended_line_op (data,
4154 linfo.li_default_is_stmt,
4155 end);
4156 break;
4157
4158 case DW_LNS_copy:
4159 printf (_(" Copy"));
4160 if (verbose_view || state_machine_regs.view)
4161 printf (_(" (view %u)\n"), state_machine_regs.view);
4162 else
4163 putchar ('\n');
4164 state_machine_regs.view++;
4165 break;
4166
4167 case DW_LNS_advance_pc:
4168 READ_ULEB (uladv, data, end);
4169 if (linfo.li_max_ops_per_insn == 1)
4170 {
4171 uladv *= linfo.li_min_insn_length;
4172 state_machine_regs.address += uladv;
4173 if (uladv)
4174 state_machine_regs.view = 0;
4175 printf (_(" Advance PC by %s to 0x%s%s\n"),
4176 dwarf_vmatoa ("u", uladv),
4177 dwarf_vmatoa ("x", state_machine_regs.address),
4178 verbose_view && uladv
4179 ? _(" (reset view)") : "");
4180 }
4181 else
4182 {
4183 unsigned addrdelta
4184 = ((state_machine_regs.op_index + uladv)
4185 / linfo.li_max_ops_per_insn)
4186 * linfo.li_min_insn_length;
4187 state_machine_regs.address
4188 += addrdelta;
4189 state_machine_regs.op_index
4190 = (state_machine_regs.op_index + uladv)
4191 % linfo.li_max_ops_per_insn;
4192 if (addrdelta)
4193 state_machine_regs.view = 0;
4194 printf (_(" Advance PC by %s to 0x%s[%d]%s\n"),
4195 dwarf_vmatoa ("u", uladv),
4196 dwarf_vmatoa ("x", state_machine_regs.address),
4197 state_machine_regs.op_index,
4198 verbose_view && addrdelta
4199 ? _(" (reset view)") : "");
4200 }
4201 break;
4202
4203 case DW_LNS_advance_line:
4204 READ_SLEB (adv, data, end);
4205 state_machine_regs.line += adv;
4206 printf (_(" Advance Line by %s to %d\n"),
4207 dwarf_vmatoa ("d", adv),
4208 state_machine_regs.line);
4209 break;
4210
4211 case DW_LNS_set_file:
4212 READ_ULEB (uladv, data, end);
4213 printf (_(" Set File Name to entry %s in the File Name Table\n"),
4214 dwarf_vmatoa ("u", uladv));
4215 state_machine_regs.file = uladv;
4216 break;
4217
4218 case DW_LNS_set_column:
4219 READ_ULEB (uladv, data, end);
4220 printf (_(" Set column to %s\n"),
4221 dwarf_vmatoa ("u", uladv));
4222 state_machine_regs.column = uladv;
4223 break;
4224
4225 case DW_LNS_negate_stmt:
4226 adv = state_machine_regs.is_stmt;
4227 adv = ! adv;
4228 printf (_(" Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
4229 state_machine_regs.is_stmt = adv;
4230 break;
4231
4232 case DW_LNS_set_basic_block:
4233 printf (_(" Set basic block\n"));
4234 state_machine_regs.basic_block = 1;
4235 break;
4236
4237 case DW_LNS_const_add_pc:
4238 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4239 if (linfo.li_max_ops_per_insn)
4240 {
4241 uladv *= linfo.li_min_insn_length;
4242 state_machine_regs.address += uladv;
4243 if (uladv)
4244 state_machine_regs.view = 0;
4245 printf (_(" Advance PC by constant %s to 0x%s%s\n"),
4246 dwarf_vmatoa ("u", uladv),
4247 dwarf_vmatoa ("x", state_machine_regs.address),
4248 verbose_view && uladv
4249 ? _(" (reset view)") : "");
4250 }
4251 else
4252 {
4253 unsigned addrdelta
4254 = ((state_machine_regs.op_index + uladv)
4255 / linfo.li_max_ops_per_insn)
4256 * linfo.li_min_insn_length;
4257 state_machine_regs.address
4258 += addrdelta;
4259 state_machine_regs.op_index
4260 = (state_machine_regs.op_index + uladv)
4261 % linfo.li_max_ops_per_insn;
4262 if (addrdelta)
4263 state_machine_regs.view = 0;
4264 printf (_(" Advance PC by constant %s to 0x%s[%d]%s\n"),
4265 dwarf_vmatoa ("u", uladv),
4266 dwarf_vmatoa ("x", state_machine_regs.address),
4267 state_machine_regs.op_index,
4268 verbose_view && addrdelta
4269 ? _(" (reset view)") : "");
4270 }
4271 break;
4272
4273 case DW_LNS_fixed_advance_pc:
4274 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4275 state_machine_regs.address += uladv;
4276 state_machine_regs.op_index = 0;
4277 printf (_(" Advance PC by fixed size amount %s to 0x%s\n"),
4278 dwarf_vmatoa ("u", uladv),
4279 dwarf_vmatoa ("x", state_machine_regs.address));
4280 /* Do NOT reset view. */
4281 break;
4282
4283 case DW_LNS_set_prologue_end:
4284 printf (_(" Set prologue_end to true\n"));
4285 break;
4286
4287 case DW_LNS_set_epilogue_begin:
4288 printf (_(" Set epilogue_begin to true\n"));
4289 break;
4290
4291 case DW_LNS_set_isa:
4292 READ_ULEB (uladv, data, end);
4293 printf (_(" Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
4294 break;
4295
4296 default:
4297 printf (_(" Unknown opcode %d with operands: "), op_code);
4298
4299 if (standard_opcodes != NULL)
4300 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4301 {
4302 READ_ULEB (uladv, data, end);
4303 printf ("0x%s%s", dwarf_vmatoa ("x", uladv),
4304 i == 1 ? "" : ", ");
4305 }
4306 putchar ('\n');
4307 break;
4308 }
19e6b90e 4309 }
b40bf0a2 4310 putchar ('\n');
19e6b90e 4311 }
19e6b90e
L
4312 }
4313
4314 return 1;
4315}
4316
a262ae96
NC
4317typedef struct
4318{
467c65bc
NC
4319 unsigned char *name;
4320 unsigned int directory_index;
4321 unsigned int modification_date;
4322 unsigned int length;
a262ae96
NC
4323} File_Entry;
4324
4325/* Output a decoded representation of the .debug_line section. */
4326
4327static int
dda8d76d 4328display_debug_lines_decoded (struct dwarf_section * section,
ec1b0fbb 4329 unsigned char * start,
dda8d76d
NC
4330 unsigned char * data,
4331 unsigned char * end,
4332 void * fileptr)
a262ae96 4333{
b40bf0a2
NC
4334 static DWARF2_Internal_LineInfo saved_linfo;
4335
dda8d76d 4336 introduce (section, FALSE);
a262ae96
NC
4337
4338 while (data < end)
4339 {
4340 /* This loop amounts to one iteration per compilation unit. */
91d6fa6a 4341 DWARF2_Internal_LineInfo linfo;
a262ae96
NC
4342 unsigned char *standard_opcodes;
4343 unsigned char *end_of_sequence;
a262ae96
NC
4344 int i;
4345 File_Entry *file_table = NULL;
143a3db0 4346 unsigned int n_files = 0;
a262ae96 4347 unsigned char **directory_table = NULL;
77145576 4348 dwarf_vma n_directories = 0;
a262ae96 4349
4925cdd7
NC
4350 if (const_strneq (section->name, ".debug_line.")
4351 /* Note: the following does not apply to .debug_line.dwo sections.
4352 These are full debug_line sections. */
4353 && strcmp (section->name, ".debug_line.dwo") != 0)
b4eb7656 4354 {
4925cdd7 4355 /* See comment in display_debug_lines_raw(). */
b40bf0a2
NC
4356 end_of_sequence = end;
4357 standard_opcodes = NULL;
4358 linfo = saved_linfo;
058037d3
NC
4359 /* PR 17531: file: 0522b371. */
4360 if (linfo.li_line_range == 0)
4361 {
1306a742 4362 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
058037d3
NC
4363 return 0;
4364 }
b40bf0a2 4365 reset_state_machine (linfo.li_default_is_stmt);
b4eb7656 4366 }
a262ae96 4367 else
b4eb7656 4368 {
b40bf0a2 4369 unsigned char *hdrptr;
a262ae96 4370
b40bf0a2
NC
4371 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4372 & end_of_sequence)) == NULL)
a233b20c 4373 return 0;
0c588247 4374
058037d3
NC
4375 /* PR 17531: file: 0522b371. */
4376 if (linfo.li_line_range == 0)
4377 {
4378 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4379 linfo.li_line_range = 1;
4380 }
b40bf0a2 4381 reset_state_machine (linfo.li_default_is_stmt);
a262ae96 4382
b40bf0a2
NC
4383 /* Save a pointer to the contents of the Opcodes table. */
4384 standard_opcodes = hdrptr;
a262ae96 4385
b40bf0a2
NC
4386 /* Traverse the Directory table just to count entries. */
4387 data = standard_opcodes + linfo.li_opcode_base - 1;
d8024a91
NC
4388 /* PR 20440 */
4389 if (data >= end)
4390 {
4391 warn (_("opcode base of %d extends beyond end of section\n"),
4392 linfo.li_opcode_base);
4393 return 0;
4394 }
4395
77145576 4396 if (linfo.li_version >= 5)
b40bf0a2 4397 {
77145576
JK
4398 unsigned char *format_start, format_count, *format;
4399 dwarf_vma formati, entryi;
a262ae96 4400
dda8d76d 4401 load_debug_section_with_follow (line_str, fileptr);
77145576
JK
4402
4403 /* Skip directories format. */
4404 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
546cb2d8
NC
4405 if (do_checks && format_count > 1)
4406 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
4407 format_count);
77145576
JK
4408 format_start = data;
4409 for (formati = 0; formati < format_count; formati++)
b40bf0a2 4410 {
cd30bcef
AM
4411 SKIP_ULEB (data, end);
4412 SKIP_ULEB (data, end);
b40bf0a2 4413 }
a262ae96 4414
cd30bcef 4415 READ_ULEB (n_directories, data, end);
77145576 4416 if (data == end)
d8024a91 4417 {
77145576 4418 warn (_("Corrupt directories list\n"));
d8024a91
NC
4419 break;
4420 }
4421
070b775f
NC
4422 if (n_directories == 0)
4423 directory_table = NULL;
4424 else
4425 directory_table = (unsigned char **)
4426 xmalloc (n_directories * sizeof (unsigned char *));
a262ae96 4427
77145576 4428 for (entryi = 0; entryi < n_directories; entryi++)
b40bf0a2 4429 {
77145576 4430 unsigned char **pathp = &directory_table[entryi];
a262ae96 4431
77145576
JK
4432 format = format_start;
4433 for (formati = 0; formati < format_count; formati++)
4434 {
4435 dwarf_vma content_type, form;
4436 dwarf_vma uvalue;
4437
cd30bcef
AM
4438 READ_ULEB (content_type, format, end);
4439 READ_ULEB (form, format, end);
77145576
JK
4440 if (data == end)
4441 {
4442 warn (_("Corrupt directories list\n"));
4443 break;
4444 }
4445 switch (content_type)
4446 {
4447 case DW_LNCT_path:
4448 switch (form)
4449 {
4450 case DW_FORM_string:
4451 *pathp = data;
4452 break;
4453 case DW_FORM_line_strp:
4454 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
4455 end);
4456 /* Remove const by the cast. */
4457 *pathp = (unsigned char *)
4458 fetch_indirect_line_string (uvalue);
4459 break;
4460 }
4461 break;
4462 }
cd30bcef
AM
4463 data = read_and_display_attr_value (0, form, 0, start,
4464 data, end, 0, 0,
77145576
JK
4465 linfo.li_offset_size,
4466 linfo.li_version,
4467 NULL, 1, section,
ec1b0fbb 4468 NULL, '\t', -1);
77145576
JK
4469 }
4470 if (data == end)
4471 {
4472 warn (_("Corrupt directories list\n"));
4473 break;
4474 }
4475 }
a262ae96 4476
77145576
JK
4477 /* Skip files format. */
4478 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
546cb2d8
NC
4479 if (do_checks && format_count > 5)
4480 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
4481 format_count);
77145576
JK
4482 format_start = data;
4483 for (formati = 0; formati < format_count; formati++)
b40bf0a2 4484 {
cd30bcef
AM
4485 SKIP_ULEB (data, end);
4486 SKIP_ULEB (data, end);
b40bf0a2 4487 }
a262ae96 4488
cd30bcef 4489 READ_ULEB (n_files, data, end);
070b775f 4490 if (data == end && n_files > 0)
d8024a91 4491 {
77145576 4492 warn (_("Corrupt file name list\n"));
d8024a91
NC
4493 break;
4494 }
4495
070b775f
NC
4496 if (n_files == 0)
4497 file_table = NULL;
4498 else
4499 file_table = (File_Entry *) xcalloc (1, n_files
4500 * sizeof (File_Entry));
a262ae96 4501
77145576 4502 for (entryi = 0; entryi < n_files; entryi++)
b40bf0a2 4503 {
77145576 4504 File_Entry *file = &file_table[entryi];
a262ae96 4505
77145576
JK
4506 format = format_start;
4507 for (formati = 0; formati < format_count; formati++)
4508 {
4509 dwarf_vma content_type, form;
4510 dwarf_vma uvalue;
cd30bcef 4511 unsigned char *tmp;
77145576 4512
cd30bcef
AM
4513 READ_ULEB (content_type, format, end);
4514 READ_ULEB (form, format, end);
77145576
JK
4515 if (data == end)
4516 {
4517 warn (_("Corrupt file name list\n"));
4518 break;
4519 }
4520 switch (content_type)
4521 {
4522 case DW_LNCT_path:
4523 switch (form)
4524 {
4525 case DW_FORM_string:
4526 file->name = data;
4527 break;
4528 case DW_FORM_line_strp:
4529 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
4530 end);
4531 /* Remove const by the cast. */
4532 file->name = (unsigned char *)
4533 fetch_indirect_line_string (uvalue);
4534 break;
4535 }
4536 break;
4537 case DW_LNCT_directory_index:
4538 switch (form)
4539 {
4540 case DW_FORM_data1:
4541 SAFE_BYTE_GET (file->directory_index, data, 1,
4542 end);
4543 break;
4544 case DW_FORM_data2:
4545 SAFE_BYTE_GET (file->directory_index, data, 2,
4546 end);
4547 break;
4548 case DW_FORM_udata:
cd30bcef
AM
4549 tmp = data;
4550 READ_ULEB (file->directory_index, tmp, end);
77145576
JK
4551 break;
4552 }
4553 break;
4554 }
cd30bcef
AM
4555 data = read_and_display_attr_value (0, form, 0, start,
4556 data, end, 0, 0,
77145576
JK
4557 linfo.li_offset_size,
4558 linfo.li_version,
4559 NULL, 1, section,
ec1b0fbb 4560 NULL, '\t', -1);
77145576
JK
4561 }
4562 if (data == end)
4563 {
4564 warn (_("Corrupt file name list\n"));
4565 break;
4566 }
4567 }
4568 }
4569 else
4570 {
4571 if (*data != 0)
b40bf0a2 4572 {
77145576
JK
4573 unsigned char *ptr_directory_table = data;
4574
4575 while (data < end && *data != 0)
4576 {
4577 data += strnlen ((char *) data, end - data) + 1;
4578 n_directories++;
4579 }
4580
4581 /* PR 20440 */
4582 if (data >= end)
4583 {
4584 warn (_("directory table ends unexpectedly\n"));
4585 n_directories = 0;
4586 break;
4587 }
4588
4589 /* Go through the directory table again to save the directories. */
4590 directory_table = (unsigned char **)
4591 xmalloc (n_directories * sizeof (unsigned char *));
4592
4593 i = 0;
4594 while (*ptr_directory_table != 0)
4595 {
4596 directory_table[i] = ptr_directory_table;
4597 ptr_directory_table += strnlen ((char *) ptr_directory_table,
4598 ptr_directory_table - end) + 1;
4599 i++;
4600 }
b40bf0a2 4601 }
77145576
JK
4602 /* Skip the NUL at the end of the table. */
4603 data++;
4604
4605 /* Traverse the File Name table just to count the entries. */
4606 if (data < end && *data != 0)
b40bf0a2 4607 {
77145576
JK
4608 unsigned char *ptr_file_name_table = data;
4609
4610 while (data < end && *data != 0)
db9537d2 4611 {
cd30bcef
AM
4612 /* Skip Name, directory index, last modification
4613 time and length of file. */
77145576 4614 data += strnlen ((char *) data, end - data) + 1;
cd30bcef
AM
4615 SKIP_ULEB (data, end);
4616 SKIP_ULEB (data, end);
4617 SKIP_ULEB (data, end);
77145576 4618 n_files++;
db9537d2 4619 }
a262ae96 4620
77145576
JK
4621 if (data >= end)
4622 {
4623 warn (_("file table ends unexpectedly\n"));
4624 n_files = 0;
4625 break;
4626 }
4627
4628 /* Go through the file table again to save the strings. */
4629 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
0c588247 4630
77145576
JK
4631 i = 0;
4632 while (*ptr_file_name_table != 0)
4633 {
77145576
JK
4634 file_table[i].name = ptr_file_name_table;
4635 ptr_file_name_table += strnlen ((char *) ptr_file_name_table,
4636 end - ptr_file_name_table) + 1;
4637
4638 /* We are not interested in directory, time or size. */
cd30bcef
AM
4639 READ_ULEB (file_table[i].directory_index,
4640 ptr_file_name_table, end);
4641 READ_ULEB (file_table[i].modification_date,
4642 ptr_file_name_table, end);
4643 READ_ULEB (file_table[i].length,
4644 ptr_file_name_table, end);
77145576
JK
4645 i++;
4646 }
4647 i = 0;
b40bf0a2 4648 }
77145576
JK
4649
4650 /* Skip the NUL at the end of the table. */
4651 data++;
b40bf0a2 4652 }
cc5914eb 4653
77145576 4654 /* Print the Compilation Unit's name and a header. */
f082820d 4655 if (file_table == NULL)
070b775f 4656 printf (_("CU: No directory table\n"));
f082820d
AM
4657 else if (directory_table == NULL)
4658 printf (_("CU: %s:\n"), file_table[0].name);
77145576
JK
4659 else
4660 {
4661 unsigned int ix = file_table[0].directory_index;
4662 const char *directory;
4663
4664 if (ix == 0)
4665 directory = ".";
4666 /* PR 20439 */
4667 else if (n_directories == 0)
4668 directory = _("<unknown>");
4669 else if (ix > n_directories)
4670 {
4671 warn (_("directory index %u > number of directories %s\n"),
4672 ix, dwarf_vmatoa ("u", n_directories));
4673 directory = _("<corrupt>");
4674 }
4675 else
4676 directory = (char *) directory_table[ix - 1];
4677
4678 if (do_wide || strlen (directory) < 76)
4679 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
4680 else
4681 printf ("%s:\n", file_table[0].name);
77145576 4682 }
a262ae96 4683
070b775f
NC
4684 if (n_files > 0)
4685 printf (_("File name Line number Starting address View Stmt\n"));
4686 else
4687 printf (_("CU: Empty file name table\n"));
b40bf0a2
NC
4688 saved_linfo = linfo;
4689 }
a262ae96
NC
4690
4691 /* This loop iterates through the Dwarf Line Number Program. */
4692 while (data < end_of_sequence)
b4eb7656 4693 {
a262ae96 4694 unsigned char op_code;
ba8826a8 4695 int xop;
b4eb7656
AM
4696 int adv;
4697 unsigned long int uladv;
b4eb7656 4698 int is_special_opcode = 0;
a262ae96 4699
b4eb7656 4700 op_code = *data++;
ba8826a8 4701 xop = op_code;
a262ae96 4702
b4eb7656 4703 if (op_code >= linfo.li_opcode_base)
a262ae96 4704 {
91d6fa6a 4705 op_code -= linfo.li_opcode_base;
a233b20c
JJ
4706 uladv = (op_code / linfo.li_line_range);
4707 if (linfo.li_max_ops_per_insn == 1)
4708 {
4709 uladv *= linfo.li_min_insn_length;
4710 state_machine_regs.address += uladv;
ba8826a8
AO
4711 if (uladv)
4712 state_machine_regs.view = 0;
a233b20c
JJ
4713 }
4714 else
4715 {
ba8826a8
AO
4716 unsigned addrdelta
4717 = ((state_machine_regs.op_index + uladv)
4718 / linfo.li_max_ops_per_insn)
b40bf0a2 4719 * linfo.li_min_insn_length;
ba8826a8
AO
4720 state_machine_regs.address
4721 += addrdelta;
a233b20c
JJ
4722 state_machine_regs.op_index
4723 = (state_machine_regs.op_index + uladv)
b40bf0a2 4724 % linfo.li_max_ops_per_insn;
ba8826a8
AO
4725 if (addrdelta)
4726 state_machine_regs.view = 0;
a233b20c 4727 }
a262ae96 4728
b4eb7656
AM
4729 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4730 state_machine_regs.line += adv;
4731 is_special_opcode = 1;
ba8826a8 4732 /* Increment view after printing this row. */
b4eb7656 4733 }
cd30bcef
AM
4734 else
4735 switch (op_code)
4736 {
4737 case DW_LNS_extended_op:
4738 {
4739 unsigned int ext_op_code_len;
4740 unsigned char ext_op_code;
4741 unsigned char *op_code_end;
4742 unsigned char *op_code_data = data;
4743
4744 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
4745 op_code_end = op_code_data + ext_op_code_len;
4746 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
4747 {
4748 warn (_("Badly formed extended line op encountered!\n"));
4749 break;
4750 }
4751 ext_op_code = *op_code_data++;
4752 xop = ext_op_code;
4753 xop = -xop;
4754
4755 switch (ext_op_code)
4756 {
4757 case DW_LNE_end_sequence:
4758 /* Reset stuff after printing this row. */
4759 break;
4760 case DW_LNE_set_address:
4761 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
4762 op_code_data,
4763 op_code_end - op_code_data,
4764 op_code_end);
4765 state_machine_regs.op_index = 0;
4766 state_machine_regs.view = 0;
4767 break;
4768 case DW_LNE_define_file:
4769 file_table = (File_Entry *) xrealloc
4770 (file_table, (n_files + 1) * sizeof (File_Entry));
4771
4772 ++state_machine_regs.last_file_entry;
4773 /* Source file name. */
4774 file_table[n_files].name = op_code_data;
4775 op_code_data += strlen ((char *) op_code_data) + 1;
4776 /* Directory index. */
4777 READ_ULEB (file_table[n_files].directory_index,
4778 op_code_data, op_code_end);
4779 /* Last modification time. */
4780 READ_ULEB (file_table[n_files].modification_date,
4781 op_code_data, op_code_end);
4782 /* File length. */
4783 READ_ULEB (file_table[n_files].length,
4784 op_code_data, op_code_end);
4785 n_files++;
4786 break;
4787
4788 case DW_LNE_set_discriminator:
4789 case DW_LNE_HP_set_sequence:
4790 /* Simply ignored. */
4791 break;
4792
4793 default:
4794 printf (_("UNKNOWN (%u): length %ld\n"),
27653fba 4795 ext_op_code, (long int) (op_code_data - data));
cd30bcef
AM
4796 break;
4797 }
4798 data = op_code_end;
4799 break;
4800 }
4801 case DW_LNS_copy:
4802 /* Increment view after printing this row. */
4803 break;
4804
4805 case DW_LNS_advance_pc:
4806 READ_ULEB (uladv, data, end);
4807 if (linfo.li_max_ops_per_insn == 1)
4808 {
4809 uladv *= linfo.li_min_insn_length;
4810 state_machine_regs.address += uladv;
4811 if (uladv)
4812 state_machine_regs.view = 0;
4813 }
4814 else
4815 {
4816 unsigned addrdelta
4817 = ((state_machine_regs.op_index + uladv)
4818 / linfo.li_max_ops_per_insn)
4819 * linfo.li_min_insn_length;
4820 state_machine_regs.address
4821 += addrdelta;
4822 state_machine_regs.op_index
4823 = (state_machine_regs.op_index + uladv)
4824 % linfo.li_max_ops_per_insn;
4825 if (addrdelta)
4826 state_machine_regs.view = 0;
4827 }
4828 break;
4829
4830 case DW_LNS_advance_line:
4831 READ_SLEB (adv, data, end);
4832 state_machine_regs.line += adv;
4833 break;
4834
4835 case DW_LNS_set_file:
4836 READ_ULEB (uladv, data, end);
4837 state_machine_regs.file = uladv;
4838
4839 {
4840 unsigned file = state_machine_regs.file - 1;
4841 unsigned dir;
4842
4843 if (file_table == NULL || n_files == 0)
4844 printf (_("\n [Use file table entry %d]\n"), file);
4845 /* PR 20439 */
4846 else if (file >= n_files)
4847 {
4848 warn (_("file index %u > number of files %u\n"), file + 1, n_files);
4849 printf (_("\n <over large file table index %u>"), file);
4850 }
4851 else if ((dir = file_table[file].directory_index) == 0)
4852 /* If directory index is 0, that means current directory. */
4853 printf ("\n./%s:[++]\n", file_table[file].name);
4854 else if (directory_table == NULL || n_directories == 0)
4855 printf (_("\n [Use file %s in directory table entry %d]\n"),
4856 file_table[file].name, dir);
4857 /* PR 20439 */
4858 else if (dir > n_directories)
4859 {
4860 warn (_("directory index %u > number of directories %s\n"),
4861 dir, dwarf_vmatoa ("u", n_directories));
4862 printf (_("\n <over large directory table entry %u>\n"), dir);
4863 }
4864 else
4865 printf ("\n%s/%s:\n",
4866 /* The directory index starts counting at 1. */
4867 directory_table[dir - 1], file_table[file].name);
4868 }
4869 break;
4870
4871 case DW_LNS_set_column:
4872 READ_ULEB (uladv, data, end);
4873 state_machine_regs.column = uladv;
4874 break;
4875
4876 case DW_LNS_negate_stmt:
4877 adv = state_machine_regs.is_stmt;
4878 adv = ! adv;
4879 state_machine_regs.is_stmt = adv;
4880 break;
4881
4882 case DW_LNS_set_basic_block:
4883 state_machine_regs.basic_block = 1;
4884 break;
4885
4886 case DW_LNS_const_add_pc:
4887 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4888 if (linfo.li_max_ops_per_insn == 1)
4889 {
4890 uladv *= linfo.li_min_insn_length;
4891 state_machine_regs.address += uladv;
4892 if (uladv)
4893 state_machine_regs.view = 0;
4894 }
4895 else
4896 {
4897 unsigned addrdelta
4898 = ((state_machine_regs.op_index + uladv)
4899 / linfo.li_max_ops_per_insn)
4900 * linfo.li_min_insn_length;
4901 state_machine_regs.address
4902 += addrdelta;
4903 state_machine_regs.op_index
4904 = (state_machine_regs.op_index + uladv)
4905 % linfo.li_max_ops_per_insn;
4906 if (addrdelta)
4907 state_machine_regs.view = 0;
4908 }
4909 break;
4910
4911 case DW_LNS_fixed_advance_pc:
4912 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4913 state_machine_regs.address += uladv;
4914 state_machine_regs.op_index = 0;
4915 /* Do NOT reset view. */
4916 break;
4917
4918 case DW_LNS_set_prologue_end:
4919 break;
4920
4921 case DW_LNS_set_epilogue_begin:
4922 break;
4923
4924 case DW_LNS_set_isa:
4925 READ_ULEB (uladv, data, end);
4926 printf (_(" Set ISA to %lu\n"), uladv);
4927 break;
4928
4929 default:
4930 printf (_(" Unknown opcode %d with operands: "), op_code);
4931
4932 if (standard_opcodes != NULL)
4933 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4934 {
4935 dwarf_vma val;
4936
4937 READ_ULEB (val, data, end);
4938 printf ("0x%s%s", dwarf_vmatoa ("x", val),
4939 i == 1 ? "" : ", ");
4940 }
4941 putchar ('\n');
4942 break;
4943 }
a262ae96 4944
b4eb7656
AM
4945 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
4946 to the DWARF address/line matrix. */
ba8826a8
AO
4947 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
4948 || (xop == DW_LNS_copy))
b4eb7656
AM
4949 {
4950 const unsigned int MAX_FILENAME_LENGTH = 35;
4951 char *fileName;
4952 char *newFileName = NULL;
4953 size_t fileNameLength;
b40bf0a2
NC
4954
4955 if (file_table)
db9537d2
NC
4956 {
4957 unsigned indx = state_machine_regs.file - 1;
4958 /* PR 20439 */
4959 if (indx >= n_files)
4960 {
4961 warn (_("corrupt file index %u encountered\n"), indx);
4962 fileName = _("<corrupt>");
4963 }
4964 else
4965 fileName = (char *) file_table[indx].name;
4966 }
b40bf0a2 4967 else
db9537d2 4968 fileName = _("<unknown>");
b40bf0a2
NC
4969
4970 fileNameLength = strlen (fileName);
a262ae96 4971
b4eb7656
AM
4972 if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
4973 {
4974 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
4975 /* Truncate file name */
4976 strncpy (newFileName,
4977 fileName + fileNameLength - MAX_FILENAME_LENGTH,
4978 MAX_FILENAME_LENGTH + 1);
e1104d08
NC
4979 /* FIXME: This is to pacify gcc-10 which can warn that the
4980 strncpy above might leave a non-NUL terminated string
4981 in newFileName. It won't, but gcc's analysis doesn't
4982 quite go far enough to discover this. */
4983 newFileName[MAX_FILENAME_LENGTH] = 0;
b4eb7656
AM
4984 }
4985 else
4986 {
4987 newFileName = (char *) xmalloc (fileNameLength + 1);
4988 strncpy (newFileName, fileName, fileNameLength + 1);
4989 }
4990
af2b3186
TV
4991 /* A row with end_seq set to true has a meaningful address, but
4992 the other information in the same row is not significant.
4993 In such a row, print line as "-", and don't print
4994 view/is_stmt. */
b4eb7656
AM
4995 if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
4996 {
a233b20c 4997 if (linfo.li_max_ops_per_insn == 1)
af2b3186
TV
4998 {
4999 if (xop == -DW_LNE_end_sequence)
5000 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x",
5001 newFileName, "-",
5002 state_machine_regs.address);
5003 else
5004 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x",
5005 newFileName, state_machine_regs.line,
5006 state_machine_regs.address);
5007 }
a233b20c 5008 else
af2b3186
TV
5009 {
5010 if (xop == -DW_LNE_end_sequence)
5011 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x[%d]",
5012 newFileName, "-",
5013 state_machine_regs.address,
5014 state_machine_regs.op_index);
5015 else
5016 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x[%d]",
5017 newFileName, state_machine_regs.line,
5018 state_machine_regs.address,
5019 state_machine_regs.op_index);
5020 }
b4eb7656
AM
5021 }
5022 else
5023 {
a233b20c 5024 if (linfo.li_max_ops_per_insn == 1)
af2b3186
TV
5025 {
5026 if (xop == -DW_LNE_end_sequence)
5027 printf ("%s %11s %#18" DWARF_VMA_FMT "x",
5028 newFileName, "-",
5029 state_machine_regs.address);
5030 else
5031 printf ("%s %11d %#18" DWARF_VMA_FMT "x",
5032 newFileName, state_machine_regs.line,
5033 state_machine_regs.address);
5034 }
a233b20c 5035 else
af2b3186
TV
5036 {
5037 if (xop == -DW_LNE_end_sequence)
5038 printf ("%s %11s %#18" DWARF_VMA_FMT "x[%d]",
5039 newFileName, "-",
5040 state_machine_regs.address,
5041 state_machine_regs.op_index);
5042 else
5043 printf ("%s %11d %#18" DWARF_VMA_FMT "x[%d]",
5044 newFileName, state_machine_regs.line,
5045 state_machine_regs.address,
5046 state_machine_regs.op_index);
5047 }
b4eb7656 5048 }
a262ae96 5049
af2b3186
TV
5050 if (xop != -DW_LNE_end_sequence)
5051 {
5052 if (state_machine_regs.view)
5053 printf (" %6u", state_machine_regs.view);
5054 else
5055 printf (" ");
17f6ade2 5056
af2b3186
TV
5057 if (state_machine_regs.is_stmt)
5058 printf (" x");
5059 }
17f6ade2
JD
5060
5061 putchar ('\n');
ba8826a8
AO
5062 state_machine_regs.view++;
5063
5064 if (xop == -DW_LNE_end_sequence)
5065 {
5066 reset_state_machine (linfo.li_default_is_stmt);
5067 putchar ('\n');
5068 }
a262ae96 5069
b4eb7656
AM
5070 free (newFileName);
5071 }
5072 }
b40bf0a2
NC
5073
5074 if (file_table)
5075 {
5076 free (file_table);
5077 file_table = NULL;
5078 n_files = 0;
5079 }
5080
5081 if (directory_table)
5082 {
5083 free (directory_table);
5084 directory_table = NULL;
5085 n_directories = 0;
5086 }
5087
a262ae96
NC
5088 putchar ('\n');
5089 }
5090
5091 return 1;
5092}
5093
5094static int
77145576 5095display_debug_lines (struct dwarf_section *section, void *file)
a262ae96
NC
5096{
5097 unsigned char *data = section->start;
5098 unsigned char *end = data + section->size;
4cb93e3b
TG
5099 int retValRaw = 1;
5100 int retValDecoded = 1;
a262ae96 5101
008f4c78
NC
5102 if (do_debug_lines == 0)
5103 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5104
4cb93e3b 5105 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
77145576 5106 retValRaw = display_debug_lines_raw (section, data, end, file);
a262ae96 5107
4cb93e3b 5108 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
ec1b0fbb 5109 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
a262ae96 5110
4cb93e3b 5111 if (!retValRaw || !retValDecoded)
a262ae96
NC
5112 return 0;
5113
5114 return 1;
5115}
5116
6e3d6dc1
NC
5117static debug_info *
5118find_debug_info_for_offset (unsigned long offset)
5119{
5120 unsigned int i;
5121
5122 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5123 return NULL;
5124
5125 for (i = 0; i < num_debug_info_entries; i++)
5126 if (debug_information[i].cu_offset == offset)
5127 return debug_information + i;
5128
5129 return NULL;
5130}
5131
459d52c8
DE
5132static const char *
5133get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5134{
5135 /* See gdb/gdb-index.h. */
5136 static const char * const kinds[] =
5137 {
5138 N_ ("no info"),
5139 N_ ("type"),
5140 N_ ("variable"),
5141 N_ ("function"),
5142 N_ ("other"),
5143 N_ ("unused5"),
5144 N_ ("unused6"),
5145 N_ ("unused7")
5146 };
5147
5148 return _ (kinds[kind]);
5149}
5150
19e6b90e 5151static int
459d52c8
DE
5152display_debug_pubnames_worker (struct dwarf_section *section,
5153 void *file ATTRIBUTE_UNUSED,
5154 int is_gnu)
19e6b90e 5155{
91d6fa6a 5156 DWARF2_Internal_PubNames names;
19e6b90e
L
5157 unsigned char *start = section->start;
5158 unsigned char *end = start + section->size;
5159
6e3d6dc1
NC
5160 /* It does not matter if this load fails,
5161 we test for that later on. */
5162 load_debug_info (file);
5163
dda8d76d 5164 introduce (section, FALSE);
19e6b90e
L
5165
5166 while (start < end)
5167 {
5168 unsigned char *data;
e98fdf1a 5169 unsigned long sec_off;
bf5117e3 5170 unsigned int offset_size, initial_length_size;
19e6b90e 5171
e98fdf1a 5172 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
91d6fa6a 5173 if (names.pn_length == 0xffffffff)
19e6b90e 5174 {
e98fdf1a 5175 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
19e6b90e
L
5176 offset_size = 8;
5177 initial_length_size = 12;
5178 }
5179 else
5180 {
5181 offset_size = 4;
5182 initial_length_size = 4;
5183 }
5184
e98fdf1a
AM
5185 sec_off = start - section->start;
5186 if (sec_off + names.pn_length < sec_off
5187 || sec_off + names.pn_length > section->size)
5188 {
5189 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
5190 section->name,
5191 sec_off - initial_length_size,
5192 dwarf_vmatoa ("x", names.pn_length));
5193 break;
5194 }
5195
5196 data = start;
5197 start += names.pn_length;
5198
0c588247
NC
5199 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, end);
5200 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, end);
6e3d6dc1
NC
5201
5202 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5203 && num_debug_info_entries > 0
91d6fa6a 5204 && find_debug_info_for_offset (names.pn_offset) == NULL)
6e3d6dc1 5205 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
47704ddf 5206 (unsigned long) names.pn_offset, section->name);
cecf136e 5207
0c588247 5208 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, end);
19e6b90e 5209
058037d3
NC
5210 printf (_(" Length: %ld\n"),
5211 (long) names.pn_length);
5212 printf (_(" Version: %d\n"),
5213 names.pn_version);
5214 printf (_(" Offset into .debug_info section: 0x%lx\n"),
5215 (unsigned long) names.pn_offset);
5216 printf (_(" Size of area in .debug_info section: %ld\n"),
5217 (long) names.pn_size);
19e6b90e 5218
91d6fa6a 5219 if (names.pn_version != 2 && names.pn_version != 3)
19e6b90e
L
5220 {
5221 static int warned = 0;
5222
5223 if (! warned)
5224 {
5225 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5226 warned = 1;
5227 }
5228
5229 continue;
5230 }
5231
459d52c8
DE
5232 if (is_gnu)
5233 printf (_("\n Offset Kind Name\n"));
5234 else
5235 printf (_("\n Offset\tName\n"));
19e6b90e 5236
e98fdf1a 5237 while (1)
19e6b90e 5238 {
f41e4712 5239 bfd_size_type maxprint;
e98fdf1a 5240 dwarf_vma offset;
f41e4712 5241
0c588247 5242 SAFE_BYTE_GET (offset, data, offset_size, end);
19e6b90e 5243
e98fdf1a
AM
5244 if (offset == 0)
5245 break;
b4eb7656 5246
e98fdf1a
AM
5247 data += offset_size;
5248 if (data >= end)
5249 break;
5250 maxprint = (end - data) - 1;
f41e4712 5251
e98fdf1a
AM
5252 if (is_gnu)
5253 {
5254 unsigned int kind_data;
5255 gdb_index_symbol_kind kind;
5256 const char *kind_name;
5257 int is_static;
5258
5259 SAFE_BYTE_GET (kind_data, data, 1, end);
5260 data++;
5261 maxprint --;
5262 /* GCC computes the kind as the upper byte in the CU index
5263 word, and then right shifts it by the CU index size.
5264 Left shift KIND to where the gdb-index.h accessor macros
5265 can use it. */
5266 kind_data <<= GDB_INDEX_CU_BITSIZE;
5267 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5268 kind_name = get_gdb_index_symbol_kind_name (kind);
5269 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5270 printf (" %-6lx %s,%-10s %.*s\n",
5271 (unsigned long) offset, is_static ? _("s") : _("g"),
5272 kind_name, (int) maxprint, data);
19e6b90e 5273 }
e98fdf1a
AM
5274 else
5275 printf (" %-6lx\t%.*s\n",
5276 (unsigned long) offset, (int) maxprint, data);
5277
5278 data += strnlen ((char *) data, maxprint) + 1;
5279 if (data >= end)
5280 break;
19e6b90e 5281 }
19e6b90e
L
5282 }
5283
5284 printf ("\n");
5285 return 1;
5286}
5287
459d52c8
DE
5288static int
5289display_debug_pubnames (struct dwarf_section *section, void *file)
5290{
5291 return display_debug_pubnames_worker (section, file, 0);
5292}
5293
5294static int
5295display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5296{
5297 return display_debug_pubnames_worker (section, file, 1);
5298}
5299
19e6b90e
L
5300static int
5301display_debug_macinfo (struct dwarf_section *section,
5302 void *file ATTRIBUTE_UNUSED)
5303{
5304 unsigned char *start = section->start;
5305 unsigned char *end = start + section->size;
5306 unsigned char *curr = start;
19e6b90e
L
5307 enum dwarf_macinfo_record_type op;
5308
dda8d76d 5309 introduce (section, FALSE);
19e6b90e
L
5310
5311 while (curr < end)
5312 {
5313 unsigned int lineno;
0c588247 5314 const unsigned char *string;
19e6b90e 5315
3f5e193b 5316 op = (enum dwarf_macinfo_record_type) *curr;
19e6b90e
L
5317 curr++;
5318
5319 switch (op)
5320 {
5321 case DW_MACINFO_start_file:
5322 {
5323 unsigned int filenum;
5324
cd30bcef
AM
5325 READ_ULEB (lineno, curr, end);
5326 READ_ULEB (filenum, curr, end);
19e6b90e
L
5327 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5328 lineno, filenum);
5329 }
5330 break;
5331
5332 case DW_MACINFO_end_file:
5333 printf (_(" DW_MACINFO_end_file\n"));
5334 break;
5335
5336 case DW_MACINFO_define:
cd30bcef 5337 READ_ULEB (lineno, curr, end);
0c588247
NC
5338 string = curr;
5339 curr += strnlen ((char *) string, end - string) + 1;
19e6b90e
L
5340 printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
5341 lineno, string);
5342 break;
5343
5344 case DW_MACINFO_undef:
cd30bcef 5345 READ_ULEB (lineno, curr, end);
0c588247
NC
5346 string = curr;
5347 curr += strnlen ((char *) string, end - string) + 1;
19e6b90e
L
5348 printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
5349 lineno, string);
5350 break;
5351
5352 case DW_MACINFO_vendor_ext:
5353 {
5354 unsigned int constant;
5355
cd30bcef 5356 READ_ULEB (constant, curr, end);
0c588247
NC
5357 string = curr;
5358 curr += strnlen ((char *) string, end - string) + 1;
19e6b90e
L
5359 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
5360 constant, string);
5361 }
5362 break;
5363 }
5364 }
5365
5366 return 1;
5367}
5368
4ccf1e31
JJ
5369/* Given LINE_OFFSET into the .debug_line section, attempt to return
5370 filename and dirname corresponding to file name table entry with index
5371 FILEIDX. Return NULL on failure. */
5372
5373static unsigned char *
f6f0e17b
NC
5374get_line_filename_and_dirname (dwarf_vma line_offset,
5375 dwarf_vma fileidx,
4ccf1e31
JJ
5376 unsigned char **dir_name)
5377{
5378 struct dwarf_section *section = &debug_displays [line].section;
5379 unsigned char *hdrptr, *dirtable, *file_name;
5380 unsigned int offset_size, initial_length_size;
cd30bcef 5381 unsigned int version, opcode_base;
4ccf1e31 5382 dwarf_vma length, diridx;
f6f0e17b 5383 const unsigned char * end;
4ccf1e31
JJ
5384
5385 *dir_name = NULL;
5386 if (section->start == NULL
5387 || line_offset >= section->size
5388 || fileidx == 0)
5389 return NULL;
5390
5391 hdrptr = section->start + line_offset;
f6f0e17b 5392 end = section->start + section->size;
0c588247
NC
5393
5394 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
4ccf1e31
JJ
5395 if (length == 0xffffffff)
5396 {
5397 /* This section is 64-bit DWARF 3. */
0c588247 5398 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
4ccf1e31
JJ
5399 offset_size = 8;
5400 initial_length_size = 12;
5401 }
5402 else
5403 {
5404 offset_size = 4;
5405 initial_length_size = 4;
5406 }
e98fdf1a
AM
5407 if (length + initial_length_size < length
5408 || length + initial_length_size > section->size)
4ccf1e31 5409 return NULL;
0c588247
NC
5410
5411 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
4ccf1e31
JJ
5412 if (version != 2 && version != 3 && version != 4)
5413 return NULL;
5414 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
5415 if (version >= 4)
5416 hdrptr++; /* Skip max_ops_per_insn. */
5417 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
0c588247
NC
5418
5419 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
4ccf1e31
JJ
5420 if (opcode_base == 0)
5421 return NULL;
0c588247 5422
4ccf1e31 5423 hdrptr += opcode_base - 1;
5c1c468d
NC
5424 if (hdrptr >= end)
5425 return NULL;
5426
4ccf1e31
JJ
5427 dirtable = hdrptr;
5428 /* Skip over dirname table. */
5429 while (*hdrptr != '\0')
5c1c468d
NC
5430 {
5431 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
5432 if (hdrptr >= end)
5433 return NULL;
5434 }
4ccf1e31 5435 hdrptr++; /* Skip the NUL at the end of the table. */
5c1c468d 5436
4ccf1e31 5437 /* Now skip over preceding filename table entries. */
5c1c468d 5438 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
4ccf1e31 5439 {
0c588247 5440 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
cd30bcef
AM
5441 SKIP_ULEB (hdrptr, end);
5442 SKIP_ULEB (hdrptr, end);
5443 SKIP_ULEB (hdrptr, end);
4ccf1e31 5444 }
5c1c468d 5445 if (hdrptr >= end || *hdrptr == '\0')
4ccf1e31 5446 return NULL;
5c1c468d 5447
4ccf1e31 5448 file_name = hdrptr;
0c588247 5449 hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
5c1c468d
NC
5450 if (hdrptr >= end)
5451 return NULL;
cd30bcef 5452 READ_ULEB (diridx, hdrptr, end);
4ccf1e31
JJ
5453 if (diridx == 0)
5454 return file_name;
5c1c468d 5455 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
0c588247 5456 dirtable += strnlen ((char *) dirtable, end - dirtable) + 1;
5c1c468d 5457 if (dirtable >= end || *dirtable == '\0')
4ccf1e31
JJ
5458 return NULL;
5459 *dir_name = dirtable;
5460 return file_name;
5461}
5462
5463static int
5464display_debug_macro (struct dwarf_section *section,
5465 void *file)
5466{
5467 unsigned char *start = section->start;
5468 unsigned char *end = start + section->size;
5469 unsigned char *curr = start;
5470 unsigned char *extended_op_buf[256];
4ccf1e31 5471
dda8d76d
NC
5472 load_debug_section_with_follow (str, file);
5473 load_debug_section_with_follow (line, file);
e4b7104b 5474 load_debug_section_with_follow (str_index, file);
4ccf1e31 5475
dda8d76d 5476 introduce (section, FALSE);
4ccf1e31
JJ
5477
5478 while (curr < end)
5479 {
5480 unsigned int lineno, version, flags;
5481 unsigned int offset_size = 4;
0c588247 5482 const unsigned char *string;
4ccf1e31
JJ
5483 dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
5484 unsigned char **extended_ops = NULL;
5485
0c588247 5486 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
7a7e1061 5487 if (version != 4 && version != 5)
4ccf1e31 5488 {
7a7e1061 5489 error (_("Only GNU extension to DWARF 4 or 5 of %s is currently supported.\n"),
4ccf1e31
JJ
5490 section->name);
5491 return 0;
5492 }
5493
0c588247 5494 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
4ccf1e31
JJ
5495 if (flags & 1)
5496 offset_size = 8;
5497 printf (_(" Offset: 0x%lx\n"),
5498 (unsigned long) sec_offset);
5499 printf (_(" Version: %d\n"), version);
5500 printf (_(" Offset size: %d\n"), offset_size);
5501 if (flags & 2)
5502 {
0c588247 5503 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
4ccf1e31
JJ
5504 printf (_(" Offset into .debug_line: 0x%lx\n"),
5505 (unsigned long) line_offset);
5506 }
5507 if (flags & 4)
5508 {
0c588247 5509 unsigned int i, count, op;
4ccf1e31 5510 dwarf_vma nargs, n;
0c588247
NC
5511
5512 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
bf5117e3 5513
4ccf1e31
JJ
5514 memset (extended_op_buf, 0, sizeof (extended_op_buf));
5515 extended_ops = extended_op_buf;
5516 if (count)
5517 {
5518 printf (_(" Extension opcode arguments:\n"));
5519 for (i = 0; i < count; i++)
5520 {
0c588247 5521 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4ccf1e31 5522 extended_ops[op] = curr;
cd30bcef 5523 READ_ULEB (nargs, curr, end);
4ccf1e31 5524 if (nargs == 0)
7a7e1061 5525 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
4ccf1e31
JJ
5526 else
5527 {
7a7e1061 5528 printf (_(" DW_MACRO_%02x arguments: "), op);
4ccf1e31
JJ
5529 for (n = 0; n < nargs; n++)
5530 {
0c588247
NC
5531 unsigned int form;
5532
5533 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
4ccf1e31
JJ
5534 printf ("%s%s", get_FORM_name (form),
5535 n == nargs - 1 ? "\n" : ", ");
5536 switch (form)
5537 {
5538 case DW_FORM_data1:
5539 case DW_FORM_data2:
5540 case DW_FORM_data4:
5541 case DW_FORM_data8:
5542 case DW_FORM_sdata:
5543 case DW_FORM_udata:
5544 case DW_FORM_block:
5545 case DW_FORM_block1:
5546 case DW_FORM_block2:
5547 case DW_FORM_block4:
5548 case DW_FORM_flag:
5549 case DW_FORM_string:
5550 case DW_FORM_strp:
5551 case DW_FORM_sec_offset:
5552 break;
5553 default:
5554 error (_("Invalid extension opcode form %s\n"),
5555 get_FORM_name (form));
5556 return 0;
5557 }
5558 }
5559 }
5560 }
5561 }
5562 }
5563 printf ("\n");
5564
5565 while (1)
5566 {
5567 unsigned int op;
5568
5569 if (curr >= end)
5570 {
5571 error (_(".debug_macro section not zero terminated\n"));
5572 return 0;
5573 }
5574
0c588247 5575 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4ccf1e31
JJ
5576 if (op == 0)
5577 break;
5578
5579 switch (op)
5580 {
e4b7104b
NC
5581 case DW_MACRO_define:
5582 READ_ULEB (lineno, curr, end);
5583 string = curr;
5584 curr += strnlen ((char *) string, end - string) + 1;
5585 printf (_(" DW_MACRO_define - lineno : %d macro : %s\n"),
5586 lineno, string);
5587 break;
5588
5589 case DW_MACRO_undef:
5590 READ_ULEB (lineno, curr, end);
5591 string = curr;
5592 curr += strnlen ((char *) string, end - string) + 1;
5593 printf (_(" DW_MACRO_undef - lineno : %d macro : %s\n"),
5594 lineno, string);
5595 break;
5596
7a7e1061 5597 case DW_MACRO_start_file:
4ccf1e31
JJ
5598 {
5599 unsigned int filenum;
5600 unsigned char *file_name = NULL, *dir_name = NULL;
5601
cd30bcef
AM
5602 READ_ULEB (lineno, curr, end);
5603 READ_ULEB (filenum, curr, end);
4ccf1e31
JJ
5604
5605 if ((flags & 2) == 0)
7a7e1061 5606 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
4ccf1e31
JJ
5607 else
5608 file_name
5609 = get_line_filename_and_dirname (line_offset, filenum,
5610 &dir_name);
5611 if (file_name == NULL)
7a7e1061 5612 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
4ccf1e31
JJ
5613 lineno, filenum);
5614 else
7a7e1061 5615 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
4ccf1e31
JJ
5616 lineno, filenum,
5617 dir_name != NULL ? (const char *) dir_name : "",
5618 dir_name != NULL ? "/" : "", file_name);
5619 }
5620 break;
5621
7a7e1061
JK
5622 case DW_MACRO_end_file:
5623 printf (_(" DW_MACRO_end_file\n"));
4ccf1e31
JJ
5624 break;
5625
7a7e1061 5626 case DW_MACRO_define_strp:
cd30bcef 5627 READ_ULEB (lineno, curr, end);
0c588247 5628 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4ccf1e31 5629 string = fetch_indirect_string (offset);
7a7e1061 5630 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
4ccf1e31
JJ
5631 lineno, string);
5632 break;
5633
7a7e1061 5634 case DW_MACRO_undef_strp:
cd30bcef 5635 READ_ULEB (lineno, curr, end);
0c588247 5636 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4ccf1e31 5637 string = fetch_indirect_string (offset);
7a7e1061 5638 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
4ccf1e31
JJ
5639 lineno, string);
5640 break;
5641
7a7e1061 5642 case DW_MACRO_import:
0c588247 5643 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 5644 printf (_(" DW_MACRO_import - offset : 0x%lx\n"),
4ccf1e31
JJ
5645 (unsigned long) offset);
5646 break;
5647
7a7e1061 5648 case DW_MACRO_define_sup:
cd30bcef 5649 READ_ULEB (lineno, curr, end);
0c588247 5650 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 5651 printf (_(" DW_MACRO_define_sup - lineno : %d macro offset : 0x%lx\n"),
a081f3cd
JJ
5652 lineno, (unsigned long) offset);
5653 break;
5654
7a7e1061 5655 case DW_MACRO_undef_sup:
cd30bcef 5656 READ_ULEB (lineno, curr, end);
0c588247 5657 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 5658 printf (_(" DW_MACRO_undef_sup - lineno : %d macro offset : 0x%lx\n"),
a081f3cd
JJ
5659 lineno, (unsigned long) offset);
5660 break;
5661
7a7e1061 5662 case DW_MACRO_import_sup:
0c588247 5663 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
7a7e1061 5664 printf (_(" DW_MACRO_import_sup - offset : 0x%lx\n"),
a081f3cd
JJ
5665 (unsigned long) offset);
5666 break;
5667
e4b7104b
NC
5668 case DW_MACRO_define_strx:
5669 case DW_MACRO_undef_strx:
5670 READ_ULEB (lineno, curr, end);
5671 READ_ULEB (offset, curr, end);
5672 string = (const unsigned char *)
5673 fetch_indexed_string (offset, NULL, offset_size, FALSE);
5674 if (op == DW_MACRO_define_strx)
5675 printf (" DW_MACRO_define_strx ");
5676 else
5677 printf (" DW_MACRO_undef_strx ");
5678 if (do_wide)
39f381cb 5679 printf (_("(with offset %s) "), dwarf_vmatoa ("x", offset));
e4b7104b
NC
5680 printf (_("lineno : %d macro : %s\n"),
5681 lineno, string);
5682 break;
5683
4ccf1e31 5684 default:
e4b7104b
NC
5685 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
5686 {
5687 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
5688 break;
5689 }
5690
4ccf1e31
JJ
5691 if (extended_ops == NULL || extended_ops[op] == NULL)
5692 {
5693 error (_(" Unknown macro opcode %02x seen\n"), op);
5694 return 0;
5695 }
5696 else
5697 {
5698 /* Skip over unhandled opcodes. */
5699 dwarf_vma nargs, n;
5700 unsigned char *desc = extended_ops[op];
cd30bcef 5701 READ_ULEB (nargs, desc, end);
4ccf1e31
JJ
5702 if (nargs == 0)
5703 {
7a7e1061 5704 printf (_(" DW_MACRO_%02x\n"), op);
4ccf1e31
JJ
5705 break;
5706 }
7a7e1061 5707 printf (_(" DW_MACRO_%02x -"), op);
4ccf1e31
JJ
5708 for (n = 0; n < nargs; n++)
5709 {
0c588247
NC
5710 int val;
5711
77145576 5712 /* DW_FORM_implicit_const is not expected here. */
0c588247 5713 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
4ccf1e31 5714 curr
77145576 5715 = read_and_display_attr_value (0, val, 0,
ec1b0fbb 5716 start, curr, end, 0, 0, offset_size,
341f9135 5717 version, NULL, 0, NULL,
ec1b0fbb 5718 NULL, ' ', -1);
4ccf1e31
JJ
5719 if (n != nargs - 1)
5720 printf (",");
5721 }
5722 printf ("\n");
5723 }
5724 break;
5725 }
5726 }
5727
5728 printf ("\n");
b4eb7656 5729 }
4ccf1e31
JJ
5730
5731 return 1;
5732}
5733
19e6b90e
L
5734static int
5735display_debug_abbrev (struct dwarf_section *section,
5736 void *file ATTRIBUTE_UNUSED)
5737{
5738 abbrev_entry *entry;
5739 unsigned char *start = section->start;
5740 unsigned char *end = start + section->size;
5741
dda8d76d 5742 introduce (section, FALSE);
19e6b90e
L
5743
5744 do
5745 {
7282333f
AM
5746 unsigned char *last;
5747
19e6b90e
L
5748 free_abbrevs ();
5749
7282333f 5750 last = start;
19e6b90e
L
5751 start = process_abbrev_section (start, end);
5752
5753 if (first_abbrev == NULL)
5754 continue;
5755
7282333f 5756 printf (_(" Number TAG (0x%lx)\n"), (long) (last - section->start));
19e6b90e
L
5757
5758 for (entry = first_abbrev; entry; entry = entry->next)
5759 {
5760 abbrev_attr *attr;
5761
cc5914eb 5762 printf (" %ld %s [%s]\n",
19e6b90e
L
5763 entry->entry,
5764 get_TAG_name (entry->tag),
5765 entry->children ? _("has children") : _("no children"));
5766
5767 for (attr = entry->first_attr; attr; attr = attr->next)
77145576
JK
5768 {
5769 printf (" %-18s %s",
5770 get_AT_name (attr->attribute),
5771 get_FORM_name (attr->form));
5772 if (attr->form == DW_FORM_implicit_const)
5773 printf (": %" BFD_VMA_FMT "d", attr->implicit_const);
5774 putchar ('\n');
5775 }
19e6b90e
L
5776 }
5777 }
5778 while (start);
5779
5780 printf ("\n");
5781
5782 return 1;
5783}
5784
42bcef4a
AB
5785/* Return true when ADDR is the maximum address, when addresses are
5786 POINTER_SIZE bytes long. */
5787
5788static bfd_boolean
5789is_max_address (dwarf_vma addr, unsigned int pointer_size)
5790{
5791 dwarf_vma mask = ~(~(dwarf_vma) 1 << (pointer_size * 8 - 1));
5792 return ((addr & mask) == mask);
5793}
5794
9f272209
AO
5795/* Display a view pair list starting at *VSTART_PTR and ending at
5796 VLISTEND within SECTION. */
5797
5798static void
5799display_view_pair_list (struct dwarf_section *section,
5800 unsigned char **vstart_ptr,
5801 unsigned int debug_info_entry,
5802 unsigned char *vlistend)
5803{
5804 unsigned char *vstart = *vstart_ptr;
5805 unsigned char *section_end = section->start + section->size;
5806 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
5807
5808 if (vlistend < section_end)
5809 section_end = vlistend;
5810
5811 putchar ('\n');
5812
5813 while (vstart < section_end)
5814 {
5815 dwarf_vma off = vstart - section->start;
5816 dwarf_vma vbegin, vend;
5817
cd30bcef 5818 READ_ULEB (vbegin, vstart, section_end);
9f272209 5819 if (vstart == section_end)
cd30bcef 5820 break;
9f272209 5821
cd30bcef 5822 READ_ULEB (vend, vstart, section_end);
9f272209
AO
5823 printf (" %8.8lx ", (unsigned long) off);
5824
5825 print_dwarf_view (vbegin, pointer_size, 1);
5826 print_dwarf_view (vend, pointer_size, 1);
5827 printf (_("location view pair\n"));
5828 }
5829
5830 putchar ('\n');
5831 *vstart_ptr = vstart;
5832}
5833
4723351a
CC
5834/* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
5835
5836static void
5837display_loc_list (struct dwarf_section *section,
b4eb7656
AM
5838 unsigned char **start_ptr,
5839 unsigned int debug_info_entry,
359ca075
JK
5840 dwarf_vma offset,
5841 dwarf_vma base_address,
9f272209 5842 unsigned char **vstart_ptr,
b4eb7656 5843 int has_frame_base)
4723351a 5844{
9f272209 5845 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
4723351a 5846 unsigned char *section_end = section->start + section->size;
82b1b41b
NC
5847 unsigned long cu_offset;
5848 unsigned int pointer_size;
5849 unsigned int offset_size;
5850 int dwarf_version;
4723351a
CC
5851
5852 dwarf_vma begin;
5853 dwarf_vma end;
5854 unsigned short length;
5855 int need_frame_base;
5856
82b1b41b
NC
5857 if (debug_info_entry >= num_debug_info_entries)
5858 {
5859 warn (_("No debug information available for loc lists of entry: %u\n"),
5860 debug_info_entry);
5861 return;
5862 }
b4eb7656 5863
82b1b41b
NC
5864 cu_offset = debug_information [debug_info_entry].cu_offset;
5865 pointer_size = debug_information [debug_info_entry].pointer_size;
5866 offset_size = debug_information [debug_info_entry].offset_size;
5867 dwarf_version = debug_information [debug_info_entry].dwarf_version;
b4eb7656 5868
f41e4712
NC
5869 if (pointer_size < 2 || pointer_size > 8)
5870 {
5871 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
5872 pointer_size, debug_info_entry);
5873 return;
5874 }
5875
4723351a
CC
5876 while (1)
5877 {
359ca075 5878 dwarf_vma off = offset + (start - *start_ptr);
9f272209 5879 dwarf_vma vbegin = vm1, vend = vm1;
d1c4b12b 5880
4723351a 5881 if (start + 2 * pointer_size > section_end)
b4eb7656
AM
5882 {
5883 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 5884 (unsigned long) offset);
b4eb7656
AM
5885 break;
5886 }
4723351a 5887
359ca075 5888 printf (" %8.8lx ", (unsigned long) off);
fab128ef 5889
0c588247
NC
5890 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
5891 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
4723351a 5892
4723351a 5893 if (begin == 0 && end == 0)
b4eb7656 5894 {
d1c4b12b
NC
5895 /* PR 18374: In a object file we can have a location list that
5896 starts with a begin and end of 0 because there are relocations
5897 that need to be applied to the addresses. Actually applying
5898 the relocations now does not help as they will probably resolve
5899 to 0, since the object file has not been fully linked. Real
5900 end of list markers will not have any relocations against them. */
5901 if (! reloc_at (section, off)
5902 && ! reloc_at (section, off + pointer_size))
5903 {
5904 printf (_("<End of list>\n"));
5905 break;
5906 }
b4eb7656 5907 }
4723351a
CC
5908
5909 /* Check base address specifiers. */
42bcef4a
AB
5910 if (is_max_address (begin, pointer_size)
5911 && !is_max_address (end, pointer_size))
b4eb7656
AM
5912 {
5913 base_address = end;
5914 print_dwarf_vma (begin, pointer_size);
5915 print_dwarf_vma (end, pointer_size);
5916 printf (_("(base address)\n"));
5917 continue;
5918 }
4723351a 5919
9f272209
AO
5920 if (vstart)
5921 {
9f272209
AO
5922 off = offset + (vstart - *start_ptr);
5923
cd30bcef 5924 READ_ULEB (vbegin, vstart, section_end);
9f272209
AO
5925 print_dwarf_view (vbegin, pointer_size, 1);
5926
cd30bcef 5927 READ_ULEB (vend, vstart, section_end);
9f272209
AO
5928 print_dwarf_view (vend, pointer_size, 1);
5929
5930 printf (_("views at %8.8lx for:\n %*s "),
5931 (unsigned long) off, 8, "");
5932 }
5933
4723351a 5934 if (start + 2 > section_end)
b4eb7656
AM
5935 {
5936 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 5937 (unsigned long) offset);
b4eb7656
AM
5938 break;
5939 }
4723351a 5940
0c588247 5941 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4723351a
CC
5942
5943 if (start + length > section_end)
b4eb7656
AM
5944 {
5945 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 5946 (unsigned long) offset);
b4eb7656
AM
5947 break;
5948 }
4723351a
CC
5949
5950 print_dwarf_vma (begin + base_address, pointer_size);
5951 print_dwarf_vma (end + base_address, pointer_size);
5952
5953 putchar ('(');
5954 need_frame_base = decode_location_expression (start,
b4eb7656
AM
5955 pointer_size,
5956 offset_size,
5957 dwarf_version,
5958 length,
5959 cu_offset, section);
4723351a
CC
5960 putchar (')');
5961
5962 if (need_frame_base && !has_frame_base)
b4eb7656 5963 printf (_(" [without DW_AT_frame_base]"));
4723351a 5964
9f272209 5965 if (begin == end && vbegin == vend)
b4eb7656 5966 fputs (_(" (start == end)"), stdout);
9f272209 5967 else if (begin > end || (begin == end && vbegin > vend))
b4eb7656 5968 fputs (_(" (start > end)"), stdout);
4723351a
CC
5969
5970 putchar ('\n');
5971
5972 start += length;
5973 }
5974
5975 *start_ptr = start;
9f272209 5976 *vstart_ptr = vstart;
4723351a
CC
5977}
5978
77145576
JK
5979/* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
5980
5981static void
5982display_loclists_list (struct dwarf_section *section,
5983 unsigned char **start_ptr,
5984 unsigned int debug_info_entry,
5985 dwarf_vma offset,
5986 dwarf_vma base_address,
9f272209 5987 unsigned char **vstart_ptr,
77145576
JK
5988 int has_frame_base)
5989{
9f272209 5990 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
77145576
JK
5991 unsigned char *section_end = section->start + section->size;
5992 unsigned long cu_offset;
5993 unsigned int pointer_size;
5994 unsigned int offset_size;
5995 int dwarf_version;
77145576 5996
9dfd0db9 5997 /* Initialize it due to a false compiler warning. */
9f272209
AO
5998 dwarf_vma begin = -1, vbegin = -1;
5999 dwarf_vma end = -1, vend = -1;
77145576
JK
6000 dwarf_vma length;
6001 int need_frame_base;
6002
6003 if (debug_info_entry >= num_debug_info_entries)
6004 {
6005 warn (_("No debug information available for "
6006 "loclists lists of entry: %u\n"),
6007 debug_info_entry);
6008 return;
6009 }
6010
6011 cu_offset = debug_information [debug_info_entry].cu_offset;
6012 pointer_size = debug_information [debug_info_entry].pointer_size;
6013 offset_size = debug_information [debug_info_entry].offset_size;
6014 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6015
6016 if (pointer_size < 2 || pointer_size > 8)
6017 {
6018 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6019 pointer_size, debug_info_entry);
6020 return;
6021 }
6022
6023 while (1)
6024 {
6025 dwarf_vma off = offset + (start - *start_ptr);
6026 enum dwarf_location_list_entry_type llet;
6027
6028 if (start + 1 > section_end)
6029 {
6030 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6031 (unsigned long) offset);
6032 break;
6033 }
6034
6035 printf (" %8.8lx ", (unsigned long) off);
6036
6037 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6038
9f272209
AO
6039 if (vstart && llet == DW_LLE_offset_pair)
6040 {
6041 off = offset + (vstart - *start_ptr);
6042
cd30bcef 6043 READ_ULEB (vbegin, vstart, section_end);
9f272209
AO
6044 print_dwarf_view (vbegin, pointer_size, 1);
6045
cd30bcef 6046 READ_ULEB (vend, vstart, section_end);
9f272209
AO
6047 print_dwarf_view (vend, pointer_size, 1);
6048
6049 printf (_("views at %8.8lx for:\n %*s "),
6050 (unsigned long) off, 8, "");
6051 }
6052
77145576
JK
6053 switch (llet)
6054 {
6055 case DW_LLE_end_of_list:
6056 printf (_("<End of list>\n"));
6057 break;
6058 case DW_LLE_offset_pair:
cd30bcef
AM
6059 READ_ULEB (begin, start, section_end);
6060 READ_ULEB (end, start, section_end);
77145576
JK
6061 break;
6062 case DW_LLE_base_address:
6063 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6064 section_end);
6065 print_dwarf_vma (base_address, pointer_size);
6066 printf (_("(base address)\n"));
6067 break;
9f272209
AO
6068#ifdef DW_LLE_view_pair
6069 case DW_LLE_view_pair:
6070 if (vstart)
6071 printf (_("View pair entry in loclist with locviews attribute\n"));
cd30bcef 6072 READ_ULEB (vbegin, start, section_end);
9f272209
AO
6073 print_dwarf_view (vbegin, pointer_size, 1);
6074
cd30bcef 6075 READ_ULEB (vend, start, section_end);
9f272209
AO
6076 print_dwarf_view (vend, pointer_size, 1);
6077
6078 printf (_("views for:\n"));
6079 continue;
6080#endif
77145576
JK
6081 default:
6082 error (_("Invalid location list entry type %d\n"), llet);
6083 return;
6084 }
6085 if (llet == DW_LLE_end_of_list)
6086 break;
6087 if (llet != DW_LLE_offset_pair)
6088 continue;
6089
6090 if (start + 2 > section_end)
6091 {
6092 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6093 (unsigned long) offset);
6094 break;
6095 }
6096
cd30bcef 6097 READ_ULEB (length, start, section_end);
77145576
JK
6098
6099 print_dwarf_vma (begin + base_address, pointer_size);
6100 print_dwarf_vma (end + base_address, pointer_size);
6101
6102 putchar ('(');
6103 need_frame_base = decode_location_expression (start,
6104 pointer_size,
6105 offset_size,
6106 dwarf_version,
6107 length,
6108 cu_offset, section);
6109 putchar (')');
6110
6111 if (need_frame_base && !has_frame_base)
6112 printf (_(" [without DW_AT_frame_base]"));
6113
9f272209 6114 if (begin == end && vbegin == vend)
77145576 6115 fputs (_(" (start == end)"), stdout);
9f272209 6116 else if (begin > end || (begin == end && vbegin > vend))
77145576
JK
6117 fputs (_(" (start > end)"), stdout);
6118
6119 putchar ('\n');
6120
6121 start += length;
9f272209 6122 vbegin = vend = -1;
77145576
JK
6123 }
6124
9f272209
AO
6125 if (vbegin != vm1 || vend != vm1)
6126 printf (_("Trailing view pair not used in a range"));
6127
77145576 6128 *start_ptr = start;
9f272209 6129 *vstart_ptr = vstart;
77145576
JK
6130}
6131
fab128ef
CC
6132/* Print a .debug_addr table index in decimal, surrounded by square brackets,
6133 right-adjusted in a field of length LEN, and followed by a space. */
6134
6135static void
6136print_addr_index (unsigned int idx, unsigned int len)
6137{
6138 static char buf[15];
6139 snprintf (buf, sizeof (buf), "[%d]", idx);
341f9135 6140 printf ("%*s ", len, buf);
fab128ef
CC
6141}
6142
4723351a
CC
6143/* Display a location list from a .dwo section. It uses address indexes rather
6144 than embedded addresses. This code closely follows display_loc_list, but the
6145 two are sufficiently different that combining things is very ugly. */
6146
6147static void
6148display_loc_list_dwo (struct dwarf_section *section,
b4eb7656
AM
6149 unsigned char **start_ptr,
6150 unsigned int debug_info_entry,
359ca075 6151 dwarf_vma offset,
9f272209 6152 unsigned char **vstart_ptr,
b4eb7656 6153 int has_frame_base)
4723351a 6154{
9f272209 6155 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
4723351a 6156 unsigned char *section_end = section->start + section->size;
82b1b41b
NC
6157 unsigned long cu_offset;
6158 unsigned int pointer_size;
6159 unsigned int offset_size;
6160 int dwarf_version;
4723351a
CC
6161 int entry_type;
6162 unsigned short length;
6163 int need_frame_base;
fab128ef 6164 unsigned int idx;
4723351a 6165
82b1b41b
NC
6166 if (debug_info_entry >= num_debug_info_entries)
6167 {
6168 warn (_("No debug information for loc lists of entry: %u\n"),
6169 debug_info_entry);
6170 return;
6171 }
6172
6173 cu_offset = debug_information [debug_info_entry].cu_offset;
6174 pointer_size = debug_information [debug_info_entry].pointer_size;
6175 offset_size = debug_information [debug_info_entry].offset_size;
6176 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6177
f41e4712
NC
6178 if (pointer_size < 2 || pointer_size > 8)
6179 {
6180 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6181 pointer_size, debug_info_entry);
6182 return;
6183 }
6184
4723351a
CC
6185 while (1)
6186 {
359ca075 6187 printf (" %8.8lx ", (unsigned long) (offset + (start - *start_ptr)));
4723351a 6188
fab128ef 6189 if (start >= section_end)
b4eb7656
AM
6190 {
6191 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6192 (unsigned long) offset);
b4eb7656
AM
6193 break;
6194 }
4723351a 6195
0c588247 6196 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
9f272209
AO
6197
6198 if (vstart)
6199 switch (entry_type)
6200 {
6201 default:
6202 break;
6203
6204 case 2:
6205 case 3:
6206 case 4:
6207 {
6208 dwarf_vma view;
6209 dwarf_vma off = offset + (vstart - *start_ptr);
6210
cd30bcef 6211 READ_ULEB (view, vstart, section_end);
9f272209
AO
6212 print_dwarf_view (view, 8, 1);
6213
cd30bcef 6214 READ_ULEB (view, vstart, section_end);
9f272209
AO
6215 print_dwarf_view (view, 8, 1);
6216
6217 printf (_("views at %8.8lx for:\n %*s "),
6218 (unsigned long) off, 8, "");
6219
6220 }
6221 break;
6222 }
6223
4723351a 6224 switch (entry_type)
b4eb7656
AM
6225 {
6226 case 0: /* A terminating entry. */
6227 *start_ptr = start;
9f272209 6228 *vstart_ptr = vstart;
b4eb7656
AM
6229 printf (_("<End of list>\n"));
6230 return;
6231 case 1: /* A base-address entry. */
cd30bcef 6232 READ_ULEB (idx, start, section_end);
b4eb7656 6233 print_addr_index (idx, 8);
9f272209 6234 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
b4eb7656
AM
6235 printf (_("(base address selection entry)\n"));
6236 continue;
6237 case 2: /* A start/end entry. */
cd30bcef 6238 READ_ULEB (idx, start, section_end);
b4eb7656 6239 print_addr_index (idx, 8);
cd30bcef 6240 READ_ULEB (idx, start, section_end);
b4eb7656
AM
6241 print_addr_index (idx, 8);
6242 break;
6243 case 3: /* A start/length entry. */
cd30bcef 6244 READ_ULEB (idx, start, section_end);
b4eb7656
AM
6245 print_addr_index (idx, 8);
6246 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6247 printf ("%08x ", idx);
6248 break;
6249 case 4: /* An offset pair entry. */
6250 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6251 printf ("%08x ", idx);
6252 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6253 printf ("%08x ", idx);
6254 break;
6255 default:
6256 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6257 *start_ptr = start;
9f272209 6258 *vstart_ptr = vstart;
b4eb7656
AM
6259 return;
6260 }
4723351a
CC
6261
6262 if (start + 2 > section_end)
b4eb7656
AM
6263 {
6264 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6265 (unsigned long) offset);
b4eb7656
AM
6266 break;
6267 }
4723351a 6268
0c588247 6269 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4723351a 6270 if (start + length > section_end)
b4eb7656
AM
6271 {
6272 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
359ca075 6273 (unsigned long) offset);
b4eb7656
AM
6274 break;
6275 }
4723351a
CC
6276
6277 putchar ('(');
6278 need_frame_base = decode_location_expression (start,
b4eb7656
AM
6279 pointer_size,
6280 offset_size,
6281 dwarf_version,
6282 length,
6283 cu_offset, section);
4723351a
CC
6284 putchar (')');
6285
6286 if (need_frame_base && !has_frame_base)
b4eb7656 6287 printf (_(" [without DW_AT_frame_base]"));
4723351a
CC
6288
6289 putchar ('\n');
6290
6291 start += length;
6292 }
6293
6294 *start_ptr = start;
9f272209 6295 *vstart_ptr = vstart;
4723351a
CC
6296}
6297
9f272209
AO
6298/* Sort array of indexes in ascending order of loc_offsets[idx] and
6299 loc_views. */
51d0d03f 6300
9f272209 6301static dwarf_vma *loc_offsets, *loc_views;
51d0d03f
JJ
6302
6303static int
6304loc_offsets_compar (const void *ap, const void *bp)
6305{
6306 dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
6307 dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
6308
9f272209
AO
6309 int ret = (a > b) - (b > a);
6310 if (ret)
6311 return ret;
6312
6313 a = loc_views[*(const unsigned int *) ap];
6314 b = loc_views[*(const unsigned int *) bp];
6315
6316 ret = (a > b) - (b > a);
6317
6318 return ret;
51d0d03f
JJ
6319}
6320
19e6b90e
L
6321static int
6322display_debug_loc (struct dwarf_section *section, void *file)
6323{
9f272209 6324 unsigned char *start = section->start, *vstart = NULL;
19e6b90e
L
6325 unsigned long bytes;
6326 unsigned char *section_begin = start;
6327 unsigned int num_loc_list = 0;
6328 unsigned long last_offset = 0;
9f272209 6329 unsigned long last_view = 0;
19e6b90e
L
6330 unsigned int first = 0;
6331 unsigned int i;
6332 unsigned int j;
6333 int seen_first_offset = 0;
51d0d03f 6334 int locs_sorted = 1;
9f272209 6335 unsigned char *next = start, *vnext = vstart;
51d0d03f 6336 unsigned int *array = NULL;
4723351a 6337 const char *suffix = strrchr (section->name, '.');
24841daa 6338 bfd_boolean is_dwo = FALSE;
77145576
JK
6339 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
6340 dwarf_vma expected_start = 0;
4723351a
CC
6341
6342 if (suffix && strcmp (suffix, ".dwo") == 0)
24841daa 6343 is_dwo = TRUE;
19e6b90e
L
6344
6345 bytes = section->size;
19e6b90e
L
6346
6347 if (bytes == 0)
6348 {
6349 printf (_("\nThe %s section is empty.\n"), section->name);
6350 return 0;
6351 }
6352
77145576
JK
6353 if (is_loclists)
6354 {
6355 unsigned char *hdrptr = section_begin;
6356 dwarf_vma ll_length;
6357 unsigned short ll_version;
6358 unsigned char *end = section_begin + section->size;
6359 unsigned char address_size, segment_selector_size;
6360 uint32_t offset_entry_count;
6361
6362 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
6363 if (ll_length == 0xffffffff)
6364 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
6365
6366 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
6367 if (ll_version != 5)
6368 {
6369 warn (_("The %s section contains corrupt or "
6370 "unsupported version number: %d.\n"),
6371 section->name, ll_version);
6372 return 0;
6373 }
6374
6375 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
6376
6377 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
6378 if (segment_selector_size != 0)
6379 {
6380 warn (_("The %s section contains "
6381 "unsupported segment selector size: %d.\n"),
6382 section->name, segment_selector_size);
6383 return 0;
6384 }
6385
6386 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
6387 if (offset_entry_count != 0)
6388 {
6389 warn (_("The %s section contains "
6390 "unsupported offset entry count: %d.\n"),
6391 section->name, offset_entry_count);
6392 return 0;
6393 }
6394
6395 expected_start = hdrptr - section_begin;
6396 }
6397
1febe64d
NC
6398 if (load_debug_info (file) == 0)
6399 {
6400 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
6401 section->name);
6402 return 0;
6403 }
19e6b90e
L
6404
6405 /* Check the order of location list in .debug_info section. If
6406 offsets of location lists are in the ascending order, we can
6407 use `debug_information' directly. */
6408 for (i = 0; i < num_debug_info_entries; i++)
6409 {
6410 unsigned int num;
6411
6412 num = debug_information [i].num_loc_offsets;
51d0d03f
JJ
6413 if (num > num_loc_list)
6414 num_loc_list = num;
19e6b90e
L
6415
6416 /* Check if we can use `debug_information' directly. */
51d0d03f 6417 if (locs_sorted && num != 0)
19e6b90e
L
6418 {
6419 if (!seen_first_offset)
6420 {
6421 /* This is the first location list. */
6422 last_offset = debug_information [i].loc_offsets [0];
9f272209 6423 last_view = debug_information [i].loc_views [0];
19e6b90e
L
6424 first = i;
6425 seen_first_offset = 1;
6426 j = 1;
6427 }
6428 else
6429 j = 0;
6430
6431 for (; j < num; j++)
6432 {
6433 if (last_offset >
9f272209
AO
6434 debug_information [i].loc_offsets [j]
6435 || (last_offset == debug_information [i].loc_offsets [j]
6436 && last_view > debug_information [i].loc_views [j]))
19e6b90e 6437 {
51d0d03f 6438 locs_sorted = 0;
19e6b90e
L
6439 break;
6440 }
6441 last_offset = debug_information [i].loc_offsets [j];
9f272209 6442 last_view = debug_information [i].loc_views [j];
19e6b90e
L
6443 }
6444 }
6445 }
6446
19e6b90e
L
6447 if (!seen_first_offset)
6448 error (_("No location lists in .debug_info section!\n"));
6449
d4bfc77b 6450 if (debug_information [first].num_loc_offsets > 0
9f272209
AO
6451 && debug_information [first].loc_offsets [0] != expected_start
6452 && debug_information [first].loc_views [0] != expected_start)
47704ddf
KT
6453 warn (_("Location lists in %s section start at 0x%s\n"),
6454 section->name,
6455 dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
19e6b90e 6456
51d0d03f
JJ
6457 if (!locs_sorted)
6458 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
dda8d76d
NC
6459
6460 introduce (section, FALSE);
6461
d1c4b12b
NC
6462 if (reloc_at (section, 0))
6463 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
dda8d76d 6464
d1c4b12b 6465 printf (_(" Offset Begin End Expression\n"));
19e6b90e
L
6466
6467 seen_first_offset = 0;
6468 for (i = first; i < num_debug_info_entries; i++)
6469 {
9f272209 6470 dwarf_vma offset, voffset;
359ca075 6471 dwarf_vma base_address;
d1c4b12b 6472 unsigned int k;
19e6b90e
L
6473 int has_frame_base;
6474
51d0d03f
JJ
6475 if (!locs_sorted)
6476 {
6477 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
6478 array[k] = k;
6479 loc_offsets = debug_information [i].loc_offsets;
9f272209 6480 loc_views = debug_information [i].loc_views;
51d0d03f
JJ
6481 qsort (array, debug_information [i].num_loc_offsets,
6482 sizeof (*array), loc_offsets_compar);
6483 }
19e6b90e 6484
9f272209 6485 int adjacent_view_loclists = 1;
51d0d03f 6486 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
19e6b90e 6487 {
51d0d03f
JJ
6488 j = locs_sorted ? k : array[k];
6489 if (k
9f272209 6490 && (debug_information [i].loc_offsets [locs_sorted
51d0d03f 6491 ? k - 1 : array [k - 1]]
9f272209
AO
6492 == debug_information [i].loc_offsets [j])
6493 && (debug_information [i].loc_views [locs_sorted
6494 ? k - 1 : array [k - 1]]
6495 == debug_information [i].loc_views [j]))
51d0d03f 6496 continue;
19e6b90e 6497 has_frame_base = debug_information [i].have_frame_base [j];
d493b283 6498 offset = debug_information [i].loc_offsets [j];
19e6b90e 6499 next = section_begin + offset;
9f272209
AO
6500 voffset = debug_information [i].loc_views [j];
6501 if (voffset != vm1)
6502 vnext = section_begin + voffset;
6503 else
6504 vnext = NULL;
19e6b90e
L
6505 base_address = debug_information [i].base_address;
6506
9f272209
AO
6507 if (vnext && vnext < next)
6508 {
6509 vstart = vnext;
6510 display_view_pair_list (section, &vstart, i, next);
6511 if (start == vnext)
6512 start = vstart;
6513 }
6514
6515 if (!seen_first_offset || !adjacent_view_loclists)
19e6b90e
L
6516 seen_first_offset = 1;
6517 else
6518 {
6519 if (start < next)
6520 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
0af1713e 6521 (unsigned long) (start - section_begin),
c8071705 6522 (unsigned long) offset);
19e6b90e
L
6523 else if (start > next)
6524 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
0af1713e 6525 (unsigned long) (start - section_begin),
c8071705 6526 (unsigned long) offset);
19e6b90e
L
6527 }
6528 start = next;
9f272209 6529 vstart = vnext;
19e6b90e
L
6530
6531 if (offset >= bytes)
6532 {
6533 warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
359ca075 6534 (unsigned long) offset);
19e6b90e
L
6535 continue;
6536 }
6537
9f272209
AO
6538 if (vnext && voffset >= bytes)
6539 {
6540 warn (_("View Offset 0x%lx is bigger than .debug_loc section size.\n"),
6541 (unsigned long) voffset);
6542 continue;
6543 }
6544
77145576
JK
6545 if (!is_loclists)
6546 {
6547 if (is_dwo)
6548 display_loc_list_dwo (section, &start, i, offset,
9f272209 6549 &vstart, has_frame_base);
77145576
JK
6550 else
6551 display_loc_list (section, &start, i, offset, base_address,
9f272209 6552 &vstart, has_frame_base);
77145576 6553 }
b4eb7656 6554 else
77145576
JK
6555 {
6556 if (is_dwo)
6557 warn (_("DWO is not yet supported.\n"));
6558 else
6559 display_loclists_list (section, &start, i, offset, base_address,
9f272209
AO
6560 &vstart, has_frame_base);
6561 }
6562
6563 /* FIXME: this arrangement is quite simplistic. Nothing
6564 requires locview lists to be adjacent to corresponding
6565 loclists, and a single loclist could be augmented by
6566 different locview lists, and vice-versa, unlikely as it
6567 is that it would make sense to do so. Hopefully we'll
6568 have view pair support built into loclists before we ever
6569 need to address all these possibilities. */
6570 if (adjacent_view_loclists && vnext
6571 && vnext != start && vstart != next)
6572 {
6573 adjacent_view_loclists = 0;
6574 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
77145576 6575 }
9f272209
AO
6576
6577 if (vnext && vnext == start)
6578 display_view_pair_list (section, &start, i, vstart);
19e6b90e
L
6579 }
6580 }
031cd65f 6581
4723351a 6582 if (start < section->start + section->size)
d3a49aa8
AM
6583 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
6584 "There are %ld unused bytes at the end of section %s\n",
6585 (long) (section->start + section->size - start)),
4723351a 6586 (long) (section->start + section->size - start), section->name);
98fb390a 6587 putchar ('\n');
51d0d03f 6588 free (array);
19e6b90e
L
6589 return 1;
6590}
6591
6592static int
6593display_debug_str (struct dwarf_section *section,
6594 void *file ATTRIBUTE_UNUSED)
6595{
6596 unsigned char *start = section->start;
6597 unsigned long bytes = section->size;
6598 dwarf_vma addr = section->address;
6599
6600 if (bytes == 0)
6601 {
6602 printf (_("\nThe %s section is empty.\n"), section->name);
6603 return 0;
6604 }
6605
dda8d76d 6606 introduce (section, FALSE);
19e6b90e
L
6607
6608 while (bytes)
6609 {
6610 int j;
6611 int k;
6612 int lbytes;
6613
6614 lbytes = (bytes > 16 ? 16 : bytes);
6615
6616 printf (" 0x%8.8lx ", (unsigned long) addr);
6617
6618 for (j = 0; j < 16; j++)
6619 {
6620 if (j < lbytes)
6621 printf ("%2.2x", start[j]);
6622 else
6623 printf (" ");
6624
6625 if ((j & 3) == 3)
6626 printf (" ");
6627 }
6628
6629 for (j = 0; j < lbytes; j++)
6630 {
6631 k = start[j];
6632 if (k >= ' ' && k < 0x80)
6633 printf ("%c", k);
6634 else
6635 printf (".");
6636 }
6637
6638 putchar ('\n');
6639
6640 start += lbytes;
6641 addr += lbytes;
6642 bytes -= lbytes;
6643 }
6644
6645 putchar ('\n');
6646
6647 return 1;
6648}
6649
19e6b90e
L
6650static int
6651display_debug_info (struct dwarf_section *section, void *file)
6652{
d85bf2ba 6653 return process_debug_info (section, file, section->abbrev_sec, FALSE, FALSE);
19e6b90e
L
6654}
6655
2b6f5997
CC
6656static int
6657display_debug_types (struct dwarf_section *section, void *file)
6658{
d85bf2ba 6659 return process_debug_info (section, file, section->abbrev_sec, FALSE, TRUE);
6f875884
TG
6660}
6661
6662static int
6663display_trace_info (struct dwarf_section *section, void *file)
6664{
d85bf2ba 6665 return process_debug_info (section, file, section->abbrev_sec, FALSE, TRUE);
2b6f5997 6666}
19e6b90e
L
6667
6668static int
6669display_debug_aranges (struct dwarf_section *section,
6670 void *file ATTRIBUTE_UNUSED)
6671{
6672 unsigned char *start = section->start;
6673 unsigned char *end = start + section->size;
6674
dda8d76d 6675 introduce (section, FALSE);
19e6b90e 6676
6e3d6dc1
NC
6677 /* It does not matter if this load fails,
6678 we test for that later on. */
6679 load_debug_info (file);
6680
19e6b90e
L
6681 while (start < end)
6682 {
6683 unsigned char *hdrptr;
6684 DWARF2_Internal_ARange arange;
91d6fa6a 6685 unsigned char *addr_ranges;
2d9472a2
NC
6686 dwarf_vma length;
6687 dwarf_vma address;
e98fdf1a 6688 unsigned long sec_off;
53b8873b 6689 unsigned char address_size;
19e6b90e 6690 int excess;
bf5117e3
NC
6691 unsigned int offset_size;
6692 unsigned int initial_length_size;
19e6b90e
L
6693
6694 hdrptr = start;
6695
0c588247 6696 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
19e6b90e
L
6697 if (arange.ar_length == 0xffffffff)
6698 {
0c588247 6699 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
19e6b90e
L
6700 offset_size = 8;
6701 initial_length_size = 12;
6702 }
6703 else
6704 {
6705 offset_size = 4;
6706 initial_length_size = 4;
6707 }
6708
e98fdf1a
AM
6709 sec_off = hdrptr - section->start;
6710 if (sec_off + arange.ar_length < sec_off
6711 || sec_off + arange.ar_length > section->size)
6712 {
6713 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
6714 section->name,
6715 sec_off - initial_length_size,
6716 dwarf_vmatoa ("x", arange.ar_length));
6717 break;
6718 }
6719
0c588247
NC
6720 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end);
6721 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size, end);
19e6b90e 6722
6e3d6dc1
NC
6723 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
6724 && num_debug_info_entries > 0
6725 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
6726 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
47704ddf 6727 (unsigned long) arange.ar_info_offset, section->name);
6e3d6dc1 6728
0c588247
NC
6729 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end);
6730 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end);
19e6b90e
L
6731
6732 if (arange.ar_version != 2 && arange.ar_version != 3)
6733 {
67f101ee
NC
6734 /* PR 19872: A version number of 0 probably means that there is
6735 padding at the end of the .debug_aranges section. Gold puts
6736 it there when performing an incremental link, for example.
6737 So do not generate a warning in this case. */
6738 if (arange.ar_version)
6739 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
19e6b90e
L
6740 break;
6741 }
6742
47704ddf
KT
6743 printf (_(" Length: %ld\n"),
6744 (long) arange.ar_length);
19e6b90e 6745 printf (_(" Version: %d\n"), arange.ar_version);
47704ddf
KT
6746 printf (_(" Offset into .debug_info: 0x%lx\n"),
6747 (unsigned long) arange.ar_info_offset);
19e6b90e
L
6748 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
6749 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
6750
53b8873b
NC
6751 address_size = arange.ar_pointer_size + arange.ar_segment_size;
6752
f41e4712
NC
6753 /* PR 17512: file: 001-108546-0.001:0.1. */
6754 if (address_size == 0 || address_size > 8)
b3681d67
L
6755 {
6756 error (_("Invalid address size in %s section!\n"),
6757 section->name);
6758 break;
6759 }
6760
53b8873b
NC
6761 /* The DWARF spec does not require that the address size be a power
6762 of two, but we do. This will have to change if we ever encounter
6763 an uneven architecture. */
6764 if ((address_size & (address_size - 1)) != 0)
6765 {
6766 warn (_("Pointer size + Segment size is not a power of two.\n"));
6767 break;
6768 }
cecf136e 6769
209c9a13
NC
6770 if (address_size > 4)
6771 printf (_("\n Address Length\n"));
6772 else
6773 printf (_("\n Address Length\n"));
19e6b90e 6774
91d6fa6a 6775 addr_ranges = hdrptr;
19e6b90e 6776
53b8873b
NC
6777 /* Must pad to an alignment boundary that is twice the address size. */
6778 excess = (hdrptr - start) % (2 * address_size);
19e6b90e 6779 if (excess)
91d6fa6a 6780 addr_ranges += (2 * address_size) - excess;
19e6b90e 6781
e98fdf1a 6782 start += arange.ar_length + initial_length_size;
1617e571 6783
91d6fa6a 6784 while (addr_ranges + 2 * address_size <= start)
19e6b90e 6785 {
0c588247
NC
6786 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size, end);
6787 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size, end);
19e6b90e 6788
80c35038 6789 printf (" ");
2d9472a2
NC
6790 print_dwarf_vma (address, address_size);
6791 print_dwarf_vma (length, address_size);
6792 putchar ('\n');
19e6b90e 6793 }
19e6b90e
L
6794 }
6795
6796 printf ("\n");
6797
6798 return 1;
6799}
6800
4723351a
CC
6801/* Comparison function for qsort. */
6802static int
6803comp_addr_base (const void * v0, const void * v1)
6804{
d367307b
AM
6805 debug_info *info0 = *(debug_info **) v0;
6806 debug_info *info1 = *(debug_info **) v1;
4723351a
CC
6807 return info0->addr_base - info1->addr_base;
6808}
6809
6810/* Display the debug_addr section. */
6811static int
6812display_debug_addr (struct dwarf_section *section,
b4eb7656 6813 void *file)
4723351a
CC
6814{
6815 debug_info **debug_addr_info;
6816 unsigned char *entry;
6817 unsigned char *end;
6818 unsigned int i;
6819 unsigned int count;
6820
6821 if (section->size == 0)
6822 {
6823 printf (_("\nThe %s section is empty.\n"), section->name);
6824 return 0;
6825 }
6826
6827 if (load_debug_info (file) == 0)
6828 {
6829 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
6830 section->name);
6831 return 0;
6832 }
6833
dda8d76d 6834 introduce (section, FALSE);
4723351a 6835
1306a742
NC
6836 /* PR 17531: file: cf38d01b.
6837 We use xcalloc because a corrupt file may not have initialised all of the
6838 fields in the debug_info structure, which means that the sort below might
6839 try to move uninitialised data. */
6840 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
b4eb7656 6841 sizeof (debug_info *));
4723351a
CC
6842
6843 count = 0;
6844 for (i = 0; i < num_debug_info_entries; i++)
82b1b41b 6845 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
1306a742
NC
6846 {
6847 /* PR 17531: file: cf38d01b. */
6848 if (debug_information[i].addr_base >= section->size)
6849 warn (_("Corrupt address base (%lx) found in debug section %u\n"),
6850 (unsigned long) debug_information[i].addr_base, i);
6851 else
6852 debug_addr_info [count++] = debug_information + i;
6853 }
4723351a
CC
6854
6855 /* Add a sentinel to make iteration convenient. */
6856 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
6857 debug_addr_info [count]->addr_base = section->size;
4723351a 6858 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
1306a742 6859
4723351a
CC
6860 for (i = 0; i < count; i++)
6861 {
6862 unsigned int idx;
fab128ef 6863 unsigned int address_size = debug_addr_info [i]->pointer_size;
4723351a
CC
6864
6865 printf (_(" For compilation unit at offset 0x%s:\n"),
b4eb7656 6866 dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
4723351a 6867
fab128ef 6868 printf (_("\tIndex\tAddress\n"));
4723351a
CC
6869 entry = section->start + debug_addr_info [i]->addr_base;
6870 end = section->start + debug_addr_info [i + 1]->addr_base;
6871 idx = 0;
6872 while (entry < end)
b4eb7656
AM
6873 {
6874 dwarf_vma base = byte_get (entry, address_size);
6875 printf (_("\t%d:\t"), idx);
6876 print_dwarf_vma (base, address_size);
6877 printf ("\n");
6878 entry += address_size;
6879 idx++;
6880 }
4723351a
CC
6881 }
6882 printf ("\n");
6883
6884 free (debug_addr_info);
6885 return 1;
6886}
6887
6888/* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
24841daa 6889
4723351a
CC
6890static int
6891display_debug_str_offsets (struct dwarf_section *section,
b4eb7656 6892 void *file ATTRIBUTE_UNUSED)
4723351a 6893{
9f27c364
HPN
6894 unsigned long idx;
6895
4723351a
CC
6896 if (section->size == 0)
6897 {
6898 printf (_("\nThe %s section is empty.\n"), section->name);
6899 return 0;
6900 }
e4b7104b
NC
6901
6902 unsigned char *start = section->start;
6903 unsigned char *end = start + section->size;
6904 unsigned char *curr = start;
6905
39f381cb
NC
6906 const char * suffix = strrchr (section->name, '.');
6907 bfd_boolean dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? TRUE : FALSE;
6908
6909 if (dwo)
6910 load_debug_section_with_follow (str_dwo, file);
6911 else
6912 load_debug_section_with_follow (str, file);
e4b7104b
NC
6913
6914 introduce (section, FALSE);
6915
6916 while (curr < end)
6917 {
6918 dwarf_vma length;
6919 dwarf_vma entry_length;
6920
6921 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
6922 /* FIXME: We assume that this means 64-bit DWARF is being used. */
6923 if (length == 0xffffffff)
6924 {
6925 SAFE_BYTE_GET (length, curr, 8, end);
6926 entry_length = 8;
6927 }
6928 else
6929 entry_length = 4;
6930
39f381cb
NC
6931 if (length == 0)
6932 {
6933 /* This is probably an old style .debug_str_offset section which
6934 just contains offsets and no header (and the first offset is 0). */
6935 length = section->size;
6936 curr = section->start;
e4b7104b 6937
39f381cb
NC
6938 printf (_(" Length: %#lx\n"), (unsigned long) length);
6939 printf (_(" Index Offset [String]\n"));
6940 }
6941 else
6942 {
6943 int version;
6944 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6945 if (version != 5)
6946 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
6947
6948 int padding;
6949 SAFE_BYTE_GET_AND_INC (padding, curr, 2, end);
6950 if (padding != 0)
6951 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
6952
6953 printf (_(" Length: %#lx\n"), (unsigned long) length);
6954 printf (_(" Version: %#lx\n"), (unsigned long) version);
6955 printf (_(" Index Offset [String]\n"));
6956 }
e4b7104b 6957
9f27c364 6958 for (idx = 0; length >= entry_length && curr < end; idx++)
e4b7104b
NC
6959 {
6960 dwarf_vma offset;
6961 const unsigned char * string;
6962
6963 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, end);
39f381cb
NC
6964 if (dwo)
6965 string = (const unsigned char *)
9f27c364 6966 fetch_indexed_string (idx, NULL, entry_length, dwo);
39f381cb
NC
6967 else
6968 string = fetch_indirect_string (offset);
6969
9f27c364 6970 printf (" %8lu %8s %s\n", idx, dwarf_vmatoa ("x", offset),
e4b7104b
NC
6971 string);
6972 }
6973 }
39f381cb 6974
4723351a
CC
6975 return 1;
6976}
6977
01a8f077
JK
6978/* Each debug_information[x].range_lists[y] gets this representation for
6979 sorting purposes. */
6980
6981struct range_entry
467c65bc
NC
6982{
6983 /* The debug_information[x].range_lists[y] value. */
359ca075 6984 dwarf_vma ranges_offset;
01a8f077 6985
467c65bc
NC
6986 /* Original debug_information to find parameters of the data. */
6987 debug_info *debug_info_p;
6988};
01a8f077
JK
6989
6990/* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
6991
6992static int
6993range_entry_compar (const void *ap, const void *bp)
6994{
3f5e193b
NC
6995 const struct range_entry *a_re = (const struct range_entry *) ap;
6996 const struct range_entry *b_re = (const struct range_entry *) bp;
359ca075
JK
6997 const dwarf_vma a = a_re->ranges_offset;
6998 const dwarf_vma b = b_re->ranges_offset;
01a8f077
JK
6999
7000 return (a > b) - (b > a);
7001}
7002
77145576
JK
7003static void
7004display_debug_ranges_list (unsigned char *start, unsigned char *finish,
7005 unsigned int pointer_size, unsigned long offset,
7006 unsigned long base_address)
7007{
7008 while (start < finish)
7009 {
7010 dwarf_vma begin;
7011 dwarf_vma end;
7012
7013 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7014 if (start >= finish)
7015 break;
7016 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7017
d11ae95e 7018
77145576
JK
7019 printf (" %8.8lx ", offset);
7020
7021 if (begin == 0 && end == 0)
7022 {
7023 printf (_("<End of list>\n"));
7024 break;
7025 }
7026
7027 /* Check base address specifiers. */
7028 if (is_max_address (begin, pointer_size)
7029 && !is_max_address (end, pointer_size))
7030 {
7031 base_address = end;
7032 print_dwarf_vma (begin, pointer_size);
7033 print_dwarf_vma (end, pointer_size);
7034 printf ("(base address)\n");
7035 continue;
7036 }
7037
7038 print_dwarf_vma (begin + base_address, pointer_size);
7039 print_dwarf_vma (end + base_address, pointer_size);
7040
7041 if (begin == end)
7042 fputs (_("(start == end)"), stdout);
7043 else if (begin > end)
7044 fputs (_("(start > end)"), stdout);
7045
7046 putchar ('\n');
7047 }
7048}
7049
7050static void
7051display_debug_rnglists_list (unsigned char *start, unsigned char *finish,
7052 unsigned int pointer_size, unsigned long offset,
7053 unsigned long base_address)
7054{
7055 unsigned char *next = start;
7056
7057 while (1)
7058 {
7059 unsigned long off = offset + (start - next);
7060 enum dwarf_range_list_entry rlet;
9dfd0db9
JK
7061 /* Initialize it due to a false compiler warning. */
7062 dwarf_vma begin = -1, length, end = -1;
77145576
JK
7063
7064 if (start + 1 > finish)
7065 {
7066 warn (_("Range list starting at offset 0x%lx is not terminated.\n"),
7067 offset);
7068 break;
7069 }
7070
7071 printf (" %8.8lx ", off);
7072
7073 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
7074
7075 switch (rlet)
7076 {
7077 case DW_RLE_end_of_list:
7078 printf (_("<End of list>\n"));
7079 break;
7080 case DW_RLE_base_address:
7081 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
7082 print_dwarf_vma (base_address, pointer_size);
7083 printf (_("(base address)\n"));
7084 break;
7085 case DW_RLE_start_length:
7086 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
cd30bcef 7087 READ_ULEB (length, start, finish);
77145576
JK
7088 end = begin + length;
7089 break;
7090 case DW_RLE_offset_pair:
cd30bcef
AM
7091 READ_ULEB (begin, start, finish);
7092 READ_ULEB (end, start, finish);
77145576
JK
7093 break;
7094 case DW_RLE_start_end:
7095 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7096 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7097 break;
7098 default:
7099 error (_("Invalid range list entry type %d\n"), rlet);
7100 rlet = DW_RLE_end_of_list;
7101 break;
7102 }
7103 if (rlet == DW_RLE_end_of_list)
7104 break;
7105 if (rlet == DW_RLE_base_address)
7106 continue;
7107
7108 print_dwarf_vma (begin + base_address, pointer_size);
7109 print_dwarf_vma (end + base_address, pointer_size);
7110
7111 if (begin == end)
7112 fputs (_("(start == end)"), stdout);
7113 else if (begin > end)
7114 fputs (_("(start > end)"), stdout);
7115
7116 putchar ('\n');
7117 }
7118}
7119
19e6b90e
L
7120static int
7121display_debug_ranges (struct dwarf_section *section,
7122 void *file ATTRIBUTE_UNUSED)
7123{
7124 unsigned char *start = section->start;
a2ff7a4b 7125 unsigned char *last_start = start;
f6f0e17b 7126 unsigned long bytes = section->size;
19e6b90e 7127 unsigned char *section_begin = start;
f6f0e17b 7128 unsigned char *finish = start + bytes;
01a8f077
JK
7129 unsigned int num_range_list, i;
7130 struct range_entry *range_entries, *range_entry_fill;
77145576
JK
7131 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
7132 /* Initialize it due to a false compiler warning. */
7133 unsigned char address_size = 0;
cb4c35cf 7134 dwarf_vma last_offset = 0;
19e6b90e 7135
19e6b90e
L
7136 if (bytes == 0)
7137 {
7138 printf (_("\nThe %s section is empty.\n"), section->name);
7139 return 0;
7140 }
7141
77145576
JK
7142 if (is_rnglists)
7143 {
7144 dwarf_vma initial_length;
7145 unsigned int initial_length_size;
7146 unsigned char segment_selector_size;
7147 unsigned int offset_size, offset_entry_count;
7148 unsigned short version;
7149
7150 /* Get and check the length of the block. */
7151 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
7152
7153 if (initial_length == 0xffffffff)
7154 {
7155 /* This section is 64-bit DWARF 3. */
7156 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
7157 offset_size = 8;
7158 initial_length_size = 12;
7159 }
7160 else
7161 {
7162 offset_size = 4;
7163 initial_length_size = 4;
7164 }
7165
7166 if (initial_length + initial_length_size > section->size)
7167 {
7168 /* If the length field has a relocation against it, then we should
7169 not complain if it is inaccurate (and probably negative).
7170 It is copied from .debug_line handling code. */
7171 if (reloc_at (section, (start - section->start) - offset_size))
7172 {
7173 initial_length = (finish - start) - initial_length_size;
7174 }
7175 else
7176 {
7177 warn (_("The length field (0x%lx) in the debug_rnglists header is wrong - the section is too small\n"),
7178 (long) initial_length);
7179 return 0;
7180 }
7181 }
7182
7183 /* Get and check the version number. */
7184 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
7185
7186 if (version != 5)
7187 {
7188 warn (_("Only DWARF version 5 debug_rnglists info "
7189 "is currently supported.\n"));
7190 return 0;
7191 }
7192
7193 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
7194
7195 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
7196 if (segment_selector_size != 0)
7197 {
7198 warn (_("The %s section contains "
7199 "unsupported segment selector size: %d.\n"),
7200 section->name, segment_selector_size);
7201 return 0;
7202 }
7203
7204 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
7205 if (offset_entry_count != 0)
7206 {
7207 warn (_("The %s section contains "
7208 "unsupported offset entry count: %u.\n"),
7209 section->name, offset_entry_count);
7210 return 0;
7211 }
7212 }
7213
1febe64d
NC
7214 if (load_debug_info (file) == 0)
7215 {
7216 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7217 section->name);
7218 return 0;
7219 }
19e6b90e 7220
01a8f077 7221 num_range_list = 0;
19e6b90e 7222 for (i = 0; i < num_debug_info_entries; i++)
01a8f077 7223 num_range_list += debug_information [i].num_range_lists;
19e6b90e 7224
01a8f077 7225 if (num_range_list == 0)
4723351a
CC
7226 {
7227 /* This can happen when the file was compiled with -gsplit-debug
b4eb7656 7228 which removes references to range lists from the primary .o file. */
4723351a
CC
7229 printf (_("No range lists in .debug_info section.\n"));
7230 return 1;
7231 }
19e6b90e 7232
3f5e193b
NC
7233 range_entries = (struct range_entry *)
7234 xmalloc (sizeof (*range_entries) * num_range_list);
01a8f077 7235 range_entry_fill = range_entries;
19e6b90e 7236
01a8f077
JK
7237 for (i = 0; i < num_debug_info_entries; i++)
7238 {
7239 debug_info *debug_info_p = &debug_information[i];
7240 unsigned int j;
7241
7242 for (j = 0; j < debug_info_p->num_range_lists; j++)
7243 {
7244 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
7245 range_entry_fill->debug_info_p = debug_info_p;
7246 range_entry_fill++;
19e6b90e
L
7247 }
7248 }
7249
01a8f077
JK
7250 qsort (range_entries, num_range_list, sizeof (*range_entries),
7251 range_entry_compar);
19e6b90e 7252
d493b283 7253 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
19e6b90e 7254 warn (_("Range lists in %s section start at 0x%lx\n"),
359ca075 7255 section->name, (unsigned long) range_entries[0].ranges_offset);
19e6b90e 7256
dda8d76d
NC
7257 introduce (section, FALSE);
7258
19e6b90e
L
7259 printf (_(" Offset Begin End\n"));
7260
01a8f077 7261 for (i = 0; i < num_range_list; i++)
19e6b90e 7262 {
01a8f077
JK
7263 struct range_entry *range_entry = &range_entries[i];
7264 debug_info *debug_info_p = range_entry->debug_info_p;
19e6b90e 7265 unsigned int pointer_size;
359ca075 7266 dwarf_vma offset;
01a8f077 7267 unsigned char *next;
359ca075 7268 dwarf_vma base_address;
19e6b90e 7269
77145576 7270 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
d493b283 7271 offset = range_entry->ranges_offset;
01a8f077
JK
7272 next = section_begin + offset;
7273 base_address = debug_info_p->base_address;
cecf136e 7274
f41e4712
NC
7275 /* PR 17512: file: 001-101485-0.001:0.1. */
7276 if (pointer_size < 2 || pointer_size > 8)
7277 {
7278 warn (_("Corrupt pointer size (%d) in debug entry at offset %8.8lx\n"),
359ca075 7279 pointer_size, (unsigned long) offset);
f41e4712
NC
7280 continue;
7281 }
b4eb7656 7282
d11ae95e
NC
7283 if (next < section_begin || next >= finish)
7284 {
7285 warn (_("Corrupt offset (%#8.8lx) in range entry %u\n"),
7286 (unsigned long) offset, i);
7287 continue;
7288 }
7289
cb4c35cf
AB
7290 /* If multiple DWARF entities reference the same range then we will
7291 have multiple entries in the `range_entries' list for the same
7292 offset. Thanks to the sort above these will all be consecutive in
7293 the `range_entries' list, so we can easily ignore duplicates
7294 here. */
7295 if (i > 0 && last_offset == offset)
7296 continue;
7297 last_offset = offset;
7298
4723351a 7299 if (dwarf_check != 0 && i > 0)
19e6b90e 7300 {
01a8f077
JK
7301 if (start < next)
7302 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
7303 (unsigned long) (start - section_begin),
7304 (unsigned long) (next - section_begin), section->name);
7305 else if (start > next)
a2ff7a4b
AM
7306 {
7307 if (next == last_start)
7308 continue;
7309 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
7310 (unsigned long) (start - section_begin),
7311 (unsigned long) (next - section_begin), section->name);
7312 }
01a8f077 7313 }
d11ae95e 7314
01a8f077 7315 start = next;
a2ff7a4b 7316 last_start = next;
19e6b90e 7317
77145576
JK
7318 (is_rnglists ? display_debug_rnglists_list : display_debug_ranges_list)
7319 (start, finish, pointer_size, offset, base_address);
19e6b90e
L
7320 }
7321 putchar ('\n');
01a8f077
JK
7322
7323 free (range_entries);
7324
19e6b90e
L
7325 return 1;
7326}
7327
7328typedef struct Frame_Chunk
7329{
7330 struct Frame_Chunk *next;
7331 unsigned char *chunk_start;
a1165289 7332 unsigned int ncols;
19e6b90e
L
7333 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
7334 short int *col_type;
7335 int *col_offset;
7336 char *augmentation;
7337 unsigned int code_factor;
7338 int data_factor;
bf5117e3
NC
7339 dwarf_vma pc_begin;
7340 dwarf_vma pc_range;
32ef3000 7341 unsigned int cfa_reg;
c8071705 7342 dwarf_vma cfa_offset;
a1165289 7343 unsigned int ra;
19e6b90e
L
7344 unsigned char fde_encoding;
7345 unsigned char cfa_exp;
604282a7
JJ
7346 unsigned char ptr_size;
7347 unsigned char segment_size;
19e6b90e
L
7348}
7349Frame_Chunk;
7350
1296bc99
AB
7351typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
7352static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
665ce1f6
L
7353static const char *const *dwarf_regnames;
7354static unsigned int dwarf_regnames_count;
7355
1296bc99 7356
19e6b90e
L
7357/* A marker for a col_type that means this column was never referenced
7358 in the frame info. */
7359#define DW_CFA_unreferenced (-1)
7360
a1165289 7361/* Return 0 if no more space is needed, 1 if more space is needed,
665ce1f6
L
7362 -1 for invalid reg. */
7363
7364static int
7365frame_need_space (Frame_Chunk *fc, unsigned int reg)
19e6b90e 7366{
a1165289 7367 unsigned int prev = fc->ncols;
19e6b90e 7368
665ce1f6
L
7369 if (reg < (unsigned int) fc->ncols)
7370 return 0;
7371
d9acf707 7372 if (dwarf_regnames_count > 0
665ce1f6
L
7373 && reg > dwarf_regnames_count)
7374 return -1;
19e6b90e
L
7375
7376 fc->ncols = reg + 1;
a1165289
NC
7377 /* PR 17512: file: 10450-2643-0.004.
7378 If reg == -1 then this can happen... */
7379 if (fc->ncols == 0)
7380 return -1;
7381
06614111 7382 /* PR 17512: file: 2844a11d. */
d9acf707 7383 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
06614111
NC
7384 {
7385 error (_("Unfeasibly large register number: %u\n"), reg);
7386 fc->ncols = 0;
7387 /* FIXME: 1024 is an arbitrary limit. Increase it if
7388 we ever encounter a valid binary that exceeds it. */
7389 return -1;
7390 }
7391
3f5e193b 7392 fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
b4eb7656 7393 sizeof (short int));
3f5e193b 7394 fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
b4eb7656 7395 /* PR 17512: file:002-10025-0.005. */
041830e0
NC
7396 if (fc->col_type == NULL || fc->col_offset == NULL)
7397 {
7398 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
7399 fc->ncols);
7400 fc->ncols = 0;
7401 return -1;
7402 }
19e6b90e
L
7403
7404 while (prev < fc->ncols)
7405 {
7406 fc->col_type[prev] = DW_CFA_unreferenced;
7407 fc->col_offset[prev] = 0;
7408 prev++;
7409 }
665ce1f6 7410 return 1;
19e6b90e
L
7411}
7412
2dc4cec1
L
7413static const char *const dwarf_regnames_i386[] =
7414{
43234a1e
L
7415 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
7416 "esp", "ebp", "esi", "edi", /* 4 - 7 */
7417 "eip", "eflags", NULL, /* 8 - 10 */
7418 "st0", "st1", "st2", "st3", /* 11 - 14 */
7419 "st4", "st5", "st6", "st7", /* 15 - 18 */
7420 NULL, NULL, /* 19 - 20 */
7421 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
7422 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
7423 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
7424 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
7425 "fcw", "fsw", "mxcsr", /* 37 - 39 */
7426 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
7427 "tr", "ldtr", /* 48 - 49 */
7428 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
7429 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
7430 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
7431 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
7432 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
7433 NULL, NULL, NULL, /* 90 - 92 */
7434 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
2dc4cec1
L
7435};
7436
3d875af5
L
7437static const char *const dwarf_regnames_iamcu[] =
7438{
7439 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
7440 "esp", "ebp", "esi", "edi", /* 4 - 7 */
7441 "eip", "eflags", NULL, /* 8 - 10 */
7442 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
7443 NULL, NULL, /* 19 - 20 */
7444 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
7445 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
7446 NULL, NULL, NULL, /* 37 - 39 */
7447 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
7448 "tr", "ldtr", /* 48 - 49 */
7449 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
7450 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
7451 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
7452 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
7453 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
7454 NULL, NULL, NULL, /* 90 - 92 */
7455 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
7456};
7457
99f6fdd9 7458static void
b129eb0e
RH
7459init_dwarf_regnames_i386 (void)
7460{
7461 dwarf_regnames = dwarf_regnames_i386;
7462 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
1296bc99 7463 dwarf_regnames_lookup_func = regname_internal_by_table_only;
b129eb0e
RH
7464}
7465
99f6fdd9 7466static void
3d875af5
L
7467init_dwarf_regnames_iamcu (void)
7468{
7469 dwarf_regnames = dwarf_regnames_iamcu;
7470 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
1296bc99 7471 dwarf_regnames_lookup_func = regname_internal_by_table_only;
3d875af5
L
7472}
7473
2dc4cec1
L
7474static const char *const dwarf_regnames_x86_64[] =
7475{
7476 "rax", "rdx", "rcx", "rbx",
7477 "rsi", "rdi", "rbp", "rsp",
7478 "r8", "r9", "r10", "r11",
7479 "r12", "r13", "r14", "r15",
7480 "rip",
7481 "xmm0", "xmm1", "xmm2", "xmm3",
7482 "xmm4", "xmm5", "xmm6", "xmm7",
7483 "xmm8", "xmm9", "xmm10", "xmm11",
7484 "xmm12", "xmm13", "xmm14", "xmm15",
7485 "st0", "st1", "st2", "st3",
7486 "st4", "st5", "st6", "st7",
7487 "mm0", "mm1", "mm2", "mm3",
7488 "mm4", "mm5", "mm6", "mm7",
7489 "rflags",
7490 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
7491 "fs.base", "gs.base", NULL, NULL,
7492 "tr", "ldtr",
43234a1e
L
7493 "mxcsr", "fcw", "fsw",
7494 "xmm16", "xmm17", "xmm18", "xmm19",
7495 "xmm20", "xmm21", "xmm22", "xmm23",
7496 "xmm24", "xmm25", "xmm26", "xmm27",
7497 "xmm28", "xmm29", "xmm30", "xmm31",
7498 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
7499 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
7500 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
7501 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
7502 NULL, NULL, NULL, /* 115 - 117 */
7503 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
2dc4cec1
L
7504};
7505
99f6fdd9 7506static void
b129eb0e
RH
7507init_dwarf_regnames_x86_64 (void)
7508{
7509 dwarf_regnames = dwarf_regnames_x86_64;
7510 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
1296bc99 7511 dwarf_regnames_lookup_func = regname_internal_by_table_only;
b129eb0e
RH
7512}
7513
4ee22035
RH
7514static const char *const dwarf_regnames_aarch64[] =
7515{
b4eb7656
AM
7516 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
7517 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
4ee22035
RH
7518 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
7519 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
7520 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
fab7c86e
TC
7521 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
7522 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
7523 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
b4eb7656
AM
7524 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
7525 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
4ee22035
RH
7526 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
7527 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
fab7c86e
TC
7528 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
7529 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
7530 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
7531 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
4ee22035
RH
7532};
7533
99f6fdd9 7534static void
4ee22035
RH
7535init_dwarf_regnames_aarch64 (void)
7536{
7537 dwarf_regnames = dwarf_regnames_aarch64;
7538 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
1296bc99 7539 dwarf_regnames_lookup_func = regname_internal_by_table_only;
4ee22035
RH
7540}
7541
d6bb17b0
AA
7542static const char *const dwarf_regnames_s390[] =
7543{
7544 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
7545 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7546 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7547 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
7548 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
7549 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
7550 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
7551 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
7552 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
7553 "pswm", "pswa",
7554 NULL, NULL,
7555 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
7556 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
7557};
7558
99f6fdd9 7559static void
d6bb17b0
AA
7560init_dwarf_regnames_s390 (void)
7561{
7562 dwarf_regnames = dwarf_regnames_s390;
7563 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
1296bc99 7564 dwarf_regnames_lookup_func = regname_internal_by_table_only;
d6bb17b0
AA
7565}
7566
5bb0830d
AB
7567static const char *const dwarf_regnames_riscv[] =
7568{
7569 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
7570 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
7571 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
7572 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
7573 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
7574 "fs0", "fs1", /* 40 - 41 */
7575 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
7576 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
7577 "fs10", "fs11", /* 58 - 59 */
7578 "ft8", "ft9", "ft10", "ft11" /* 60 - 63 */
7579};
7580
4762fe62
AB
7581/* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
7582 the large number of CSRs. */
7583
7584static const char *
7585regname_internal_riscv (unsigned int regno)
7586{
7587 const char *name = NULL;
7588
7589 /* Lookup in the table first, this covers GPR and FPR. */
7590 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
7591 name = dwarf_regnames_riscv [regno];
7592 else if (regno >= 4096 && regno <= 8191)
7593 {
7594 /* This might be a CSR, these live in a sparse number space from 4096
7595 to 8191 These numbers are defined in the RISC-V ELF ABI
7596 document. */
7597 switch (regno)
7598 {
8f595e9b
NC
7599#define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
7600 case VALUE + 4096: name = #NAME; break;
4762fe62
AB
7601#include "opcode/riscv-opc.h"
7602#undef DECLARE_CSR
7603
7604 default:
7605 {
7606 static char csr_name[10];
7607 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
7608 name = csr_name;
7609 }
7610 break;
7611 }
7612 }
7613
7614 return name;
7615}
7616
99f6fdd9 7617static void
5bb0830d
AB
7618init_dwarf_regnames_riscv (void)
7619{
4762fe62
AB
7620 dwarf_regnames = NULL;
7621 dwarf_regnames_count = 8192;
7622 dwarf_regnames_lookup_func = regname_internal_riscv;
5bb0830d
AB
7623}
7624
2dc4cec1 7625void
955ff7fc 7626init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
2dc4cec1 7627{
1296bc99
AB
7628 dwarf_regnames_lookup_func = NULL;
7629
2dc4cec1
L
7630 switch (e_machine)
7631 {
7632 case EM_386:
b129eb0e 7633 init_dwarf_regnames_i386 ();
2dc4cec1
L
7634 break;
7635
3d875af5
L
7636 case EM_IAMCU:
7637 init_dwarf_regnames_iamcu ();
7638 break;
7639
2dc4cec1 7640 case EM_X86_64:
7f502d6c 7641 case EM_L1OM:
7a9068fe 7642 case EM_K1OM:
b129eb0e 7643 init_dwarf_regnames_x86_64 ();
2dc4cec1
L
7644 break;
7645
4ee22035
RH
7646 case EM_AARCH64:
7647 init_dwarf_regnames_aarch64 ();
7648 break;
7649
d6bb17b0
AA
7650 case EM_S390:
7651 init_dwarf_regnames_s390 ();
7652 break;
7653
5bb0830d
AB
7654 case EM_RISCV:
7655 init_dwarf_regnames_riscv ();
7656 break;
7657
2dc4cec1
L
7658 default:
7659 break;
7660 }
7661}
7662
229a22cf
AB
7663/* Initialize the DWARF register name lookup state based on the
7664 architecture and specific machine type of a BFD. */
7665
7666void
7667init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
7668 unsigned long mach)
7669{
1296bc99
AB
7670 dwarf_regnames_lookup_func = NULL;
7671
229a22cf
AB
7672 switch (arch)
7673 {
7674 case bfd_arch_i386:
7675 switch (mach)
7676 {
7677 case bfd_mach_x86_64:
7678 case bfd_mach_x86_64_intel_syntax:
229a22cf
AB
7679 case bfd_mach_x64_32:
7680 case bfd_mach_x64_32_intel_syntax:
229a22cf
AB
7681 init_dwarf_regnames_x86_64 ();
7682 break;
7683
7684 default:
7685 init_dwarf_regnames_i386 ();
7686 break;
7687 }
7688 break;
7689
7690 case bfd_arch_iamcu:
7691 init_dwarf_regnames_iamcu ();
7692 break;
7693
7694 case bfd_arch_aarch64:
7695 init_dwarf_regnames_aarch64();
7696 break;
7697
7698 case bfd_arch_s390:
7699 init_dwarf_regnames_s390 ();
7700 break;
7701
7702 case bfd_arch_riscv:
7703 init_dwarf_regnames_riscv ();
7704 break;
7705
7706 default:
7707 break;
7708 }
7709}
7710
2dc4cec1 7711static const char *
1296bc99 7712regname_internal_by_table_only (unsigned int regno)
2dc4cec1 7713{
1296bc99 7714 if (dwarf_regnames != NULL
2dc4cec1
L
7715 && regno < dwarf_regnames_count
7716 && dwarf_regnames [regno] != NULL)
1296bc99
AB
7717 return dwarf_regnames [regno];
7718
7719 return NULL;
7720}
7721
7722static const char *
7723regname (unsigned int regno, int name_only_p)
7724{
7725 static char reg[64];
7726
7727 const char *name = NULL;
7728
7729 if (dwarf_regnames_lookup_func != NULL)
7730 name = dwarf_regnames_lookup_func (regno);
7731
7732 if (name != NULL)
2dc4cec1 7733 {
1296bc99
AB
7734 if (name_only_p)
7735 return name;
7736 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
2dc4cec1
L
7737 }
7738 else
7739 snprintf (reg, sizeof (reg), "r%d", regno);
7740 return reg;
7741}
7742
19e6b90e 7743static void
a1165289 7744frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
19e6b90e 7745{
a1165289 7746 unsigned int r;
19e6b90e
L
7747 char tmp[100];
7748
d8024a91 7749 if (*max_regs != fc->ncols)
19e6b90e
L
7750 *max_regs = fc->ncols;
7751
7752 if (*need_col_headers)
7753 {
91d6fa6a 7754 static const char *sloc = " LOC";
2dc4cec1 7755
19e6b90e
L
7756 *need_col_headers = 0;
7757
91d6fa6a 7758 printf ("%-*s CFA ", eh_addr_size * 2, sloc);
19e6b90e
L
7759
7760 for (r = 0; r < *max_regs; r++)
7761 if (fc->col_type[r] != DW_CFA_unreferenced)
7762 {
7763 if (r == fc->ra)
50751e18 7764 printf ("ra ");
19e6b90e 7765 else
2dc4cec1 7766 printf ("%-5s ", regname (r, 1));
19e6b90e
L
7767 }
7768
7769 printf ("\n");
7770 }
7771
bf5117e3 7772 print_dwarf_vma (fc->pc_begin, eh_addr_size);
19e6b90e
L
7773 if (fc->cfa_exp)
7774 strcpy (tmp, "exp");
7775 else
c8071705 7776 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
19e6b90e
L
7777 printf ("%-8s ", tmp);
7778
7779 for (r = 0; r < fc->ncols; r++)
7780 {
7781 if (fc->col_type[r] != DW_CFA_unreferenced)
7782 {
7783 switch (fc->col_type[r])
7784 {
7785 case DW_CFA_undefined:
7786 strcpy (tmp, "u");
7787 break;
7788 case DW_CFA_same_value:
7789 strcpy (tmp, "s");
7790 break;
7791 case DW_CFA_offset:
7792 sprintf (tmp, "c%+d", fc->col_offset[r]);
7793 break;
12eae2d3
JJ
7794 case DW_CFA_val_offset:
7795 sprintf (tmp, "v%+d", fc->col_offset[r]);
7796 break;
19e6b90e 7797 case DW_CFA_register:
2dc4cec1 7798 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
19e6b90e
L
7799 break;
7800 case DW_CFA_expression:
7801 strcpy (tmp, "exp");
7802 break;
12eae2d3
JJ
7803 case DW_CFA_val_expression:
7804 strcpy (tmp, "vexp");
7805 break;
19e6b90e
L
7806 default:
7807 strcpy (tmp, "n/a");
7808 break;
7809 }
2dc4cec1 7810 printf ("%-5s ", tmp);
19e6b90e
L
7811 }
7812 }
7813 printf ("\n");
7814}
7815
49727e46 7816#define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
19e6b90e 7817
49727e46
AM
7818static unsigned char *
7819read_cie (unsigned char *start, unsigned char *end,
7820 Frame_Chunk **p_cie, int *p_version,
bf59c5d5 7821 bfd_size_type *p_aug_len, unsigned char **p_aug)
49727e46
AM
7822{
7823 int version;
7824 Frame_Chunk *fc;
49727e46 7825 unsigned char *augmentation_data = NULL;
bf59c5d5 7826 bfd_size_type augmentation_data_len = 0;
49727e46 7827
041830e0 7828 * p_cie = NULL;
f41e4712
NC
7829 /* PR 17512: file: 001-228113-0.004. */
7830 if (start >= end)
7831 return end;
7832
49727e46
AM
7833 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
7834 memset (fc, 0, sizeof (Frame_Chunk));
7835
7836 fc->col_type = (short int *) xmalloc (sizeof (short int));
7837 fc->col_offset = (int *) xmalloc (sizeof (int));
7838
7839 version = *start++;
7840
7841 fc->augmentation = (char *) start;
f41e4712
NC
7842 /* PR 17512: file: 001-228113-0.004.
7843 Skip past augmentation name, but avoid running off the end of the data. */
7844 while (start < end)
7845 if (* start ++ == '\0')
7846 break;
7847 if (start == end)
7848 {
7849 warn (_("No terminator for augmentation name\n"));
442a6ce8 7850 goto fail;
f41e4712 7851 }
49727e46
AM
7852
7853 if (strcmp (fc->augmentation, "eh") == 0)
7854 start += eh_addr_size;
7855
7856 if (version >= 4)
7857 {
7858 GET (fc->ptr_size, 1);
77ef8654
NC
7859 if (fc->ptr_size < 1 || fc->ptr_size > 8)
7860 {
7861 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
442a6ce8 7862 goto fail;
77ef8654
NC
7863 }
7864
49727e46 7865 GET (fc->segment_size, 1);
77ef8654
NC
7866 /* PR 17512: file: e99d2804. */
7867 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
7868 {
7869 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
442a6ce8 7870 goto fail;
77ef8654
NC
7871 }
7872
49727e46
AM
7873 eh_addr_size = fc->ptr_size;
7874 }
7875 else
7876 {
7877 fc->ptr_size = eh_addr_size;
7878 fc->segment_size = 0;
7879 }
442a6ce8 7880
cd30bcef
AM
7881 READ_ULEB (fc->code_factor, start, end);
7882 READ_SLEB (fc->data_factor, start, end);
442a6ce8 7883
49727e46
AM
7884 if (version == 1)
7885 {
7886 GET (fc->ra, 1);
7887 }
7888 else
7889 {
cd30bcef 7890 READ_ULEB (fc->ra, start, end);
49727e46
AM
7891 }
7892
7893 if (fc->augmentation[0] == 'z')
7894 {
cd30bcef 7895 READ_ULEB (augmentation_data_len, start, end);
49727e46 7896 augmentation_data = start;
a1165289 7897 /* PR 17512: file: 11042-2589-0.004. */
bf59c5d5 7898 if (augmentation_data_len > (bfd_size_type) (end - start))
a1165289 7899 {
bf59c5d5
NC
7900 warn (_("Augmentation data too long: 0x%s, expected at most %#lx\n"),
7901 dwarf_vmatoa ("x", augmentation_data_len),
7902 (unsigned long) (end - start));
442a6ce8 7903 goto fail;
a1165289 7904 }
9c0f3d3f 7905 start += augmentation_data_len;
49727e46
AM
7906 }
7907
7908 if (augmentation_data_len)
7909 {
058037d3
NC
7910 unsigned char *p;
7911 unsigned char *q;
7912 unsigned char *qend;
b4eb7656 7913
49727e46
AM
7914 p = (unsigned char *) fc->augmentation + 1;
7915 q = augmentation_data;
058037d3
NC
7916 qend = q + augmentation_data_len;
7917
9c0f3d3f 7918 while (p < end && q < qend)
49727e46
AM
7919 {
7920 if (*p == 'L')
7921 q++;
7922 else if (*p == 'P')
7923 q += 1 + size_of_encoded_value (*q);
7924 else if (*p == 'R')
7925 fc->fde_encoding = *q++;
7926 else if (*p == 'S')
7927 ;
09038062
ST
7928 else if (*p == 'B')
7929 ;
49727e46
AM
7930 else
7931 break;
7932 p++;
7933 }
c361b9ac
NC
7934 /* Note - it is OK if this loop terminates with q < qend.
7935 Padding may have been inserted to align the end of the CIE. */
49727e46
AM
7936 }
7937
7938 *p_cie = fc;
7939 if (p_version)
7940 *p_version = version;
7941 if (p_aug_len)
7942 {
7943 *p_aug_len = augmentation_data_len;
7944 *p_aug = augmentation_data;
7945 }
7946 return start;
442a6ce8
NC
7947
7948 fail:
7949 free (fc->col_offset);
7950 free (fc->col_type);
7951 free (fc);
7952 return end;
49727e46
AM
7953}
7954
d85bf2ba
NC
7955/* Prints out the contents on the DATA array formatted as unsigned bytes.
7956 If do_wide is not enabled, then formats the output to fit into 80 columns.
7957 PRINTED contains the number of characters already written to the current
7958 output line. */
bf59c5d5
NC
7959
7960static void
d85bf2ba
NC
7961display_data (bfd_size_type printed,
7962 const unsigned char * data,
7963 const bfd_size_type len)
bf59c5d5 7964{
d85bf2ba
NC
7965 if (do_wide || len < ((80 - printed) / 3))
7966 for (printed = 0; printed < len; ++printed)
7967 printf (" %02x", data[printed]);
bf59c5d5
NC
7968 else
7969 {
d85bf2ba 7970 for (printed = 0; printed < len; ++printed)
bf59c5d5 7971 {
d85bf2ba 7972 if (printed % (80 / 3) == 0)
bf59c5d5 7973 putchar ('\n');
d85bf2ba 7974 printf (" %02x", data[printed]);
bf59c5d5
NC
7975 }
7976 }
d85bf2ba
NC
7977}
7978
7979/* Prints out the contents on the augmentation data array.
7980 If do_wide is not enabled, then formats the output to fit into 80 columns. */
7981
7982static void
7983display_augmentation_data (const unsigned char * data, const bfd_size_type len)
7984{
7985 bfd_size_type i;
7986
7987 i = printf (_(" Augmentation data: "));
7988 display_data (i, data, len);
bf59c5d5
NC
7989}
7990
19e6b90e
L
7991static int
7992display_debug_frames (struct dwarf_section *section,
7993 void *file ATTRIBUTE_UNUSED)
7994{
7995 unsigned char *start = section->start;
7996 unsigned char *end = start + section->size;
7997 unsigned char *section_start = start;
3391569f
NC
7998 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
7999 Frame_Chunk *remembered_state = NULL;
19e6b90e 8000 Frame_Chunk *rs;
3391569f 8001 bfd_boolean is_eh = strcmp (section->name, ".eh_frame") == 0;
a1165289 8002 unsigned int max_regs = 0;
665ce1f6 8003 const char *bad_reg = _("bad register: ");
77ef8654 8004 unsigned int saved_eh_addr_size = eh_addr_size;
19e6b90e 8005
dda8d76d 8006 introduce (section, FALSE);
19e6b90e
L
8007
8008 while (start < end)
8009 {
8010 unsigned char *saved_start;
8011 unsigned char *block_end;
bf5117e3
NC
8012 dwarf_vma length;
8013 dwarf_vma cie_id;
19e6b90e
L
8014 Frame_Chunk *fc;
8015 Frame_Chunk *cie;
8016 int need_col_headers = 1;
8017 unsigned char *augmentation_data = NULL;
bf59c5d5 8018 bfd_size_type augmentation_data_len = 0;
bf5117e3
NC
8019 unsigned int encoded_ptr_size = saved_eh_addr_size;
8020 unsigned int offset_size;
8021 unsigned int initial_length_size;
5b6312fd 8022 bfd_boolean all_nops;
a788aedd 8023 static Frame_Chunk fde_fc;
19e6b90e
L
8024
8025 saved_start = start;
19e6b90e 8026
0c588247 8027 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
041830e0 8028
19e6b90e
L
8029 if (length == 0)
8030 {
8031 printf ("\n%08lx ZERO terminator\n\n",
8032 (unsigned long)(saved_start - section_start));
6937bb54
NC
8033 /* Skip any zero terminators that directly follow.
8034 A corrupt section size could have loaded a whole
8035 slew of zero filled memory bytes. eg
8036 PR 17512: file: 070-19381-0.004. */
8037 while (start < end && * start == 0)
8038 ++ start;
b758e50f 8039 continue;
19e6b90e
L
8040 }
8041
8042 if (length == 0xffffffff)
8043 {
0c588247 8044 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
19e6b90e
L
8045 offset_size = 8;
8046 initial_length_size = 12;
8047 }
8048 else
8049 {
8050 offset_size = 4;
8051 initial_length_size = 4;
8052 }
8053
8054 block_end = saved_start + length + initial_length_size;
041830e0 8055 if (block_end > end || block_end < start)
53b8873b 8056 {
bf5117e3
NC
8057 warn ("Invalid length 0x%s in FDE at %#08lx\n",
8058 dwarf_vmatoa_1 (NULL, length, offset_size),
8059 (unsigned long) (saved_start - section_start));
53b8873b
NC
8060 block_end = end;
8061 }
0c588247
NC
8062
8063 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, end);
19e6b90e 8064
846a11e4
NC
8065 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
8066 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
19e6b90e
L
8067 {
8068 int version;
a1165289 8069 unsigned int mreg;
19e6b90e 8070
49727e46
AM
8071 start = read_cie (start, end, &cie, &version,
8072 &augmentation_data_len, &augmentation_data);
041830e0
NC
8073 /* PR 17512: file: 027-135133-0.005. */
8074 if (cie == NULL)
8075 break;
a1165289 8076
49727e46 8077 fc = cie;
19e6b90e
L
8078 fc->next = chunks;
8079 chunks = fc;
8080 fc->chunk_start = saved_start;
a1165289 8081 mreg = max_regs > 0 ? max_regs - 1 : 0;
49727e46
AM
8082 if (mreg < fc->ra)
8083 mreg = fc->ra;
06614111
NC
8084 if (frame_need_space (fc, mreg) < 0)
8085 break;
49727e46
AM
8086 if (fc->fde_encoding)
8087 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
19e6b90e 8088
bf5117e3
NC
8089 printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
8090 print_dwarf_vma (length, fc->ptr_size);
9c41109d 8091 print_dwarf_vma (cie_id, offset_size);
bf5117e3 8092
19e6b90e 8093 if (do_debug_frames_interp)
bf5117e3
NC
8094 {
8095 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
8096 fc->code_factor, fc->data_factor, fc->ra);
8097 }
19e6b90e
L
8098 else
8099 {
bf5117e3 8100 printf ("CIE\n");
19e6b90e
L
8101 printf (" Version: %d\n", version);
8102 printf (" Augmentation: \"%s\"\n", fc->augmentation);
604282a7
JJ
8103 if (version >= 4)
8104 {
8105 printf (" Pointer Size: %u\n", fc->ptr_size);
8106 printf (" Segment Size: %u\n", fc->segment_size);
8107 }
19e6b90e
L
8108 printf (" Code alignment factor: %u\n", fc->code_factor);
8109 printf (" Data alignment factor: %d\n", fc->data_factor);
8110 printf (" Return address column: %d\n", fc->ra);
8111
8112 if (augmentation_data_len)
bf59c5d5 8113 display_augmentation_data (augmentation_data, augmentation_data_len);
a1165289 8114
19e6b90e
L
8115 putchar ('\n');
8116 }
19e6b90e
L
8117 }
8118 else
8119 {
8120 unsigned char *look_for;
604282a7 8121 unsigned long segment_selector;
19e6b90e 8122
49727e46
AM
8123 if (is_eh)
8124 {
8125 dwarf_vma sign = (dwarf_vma) 1 << (offset_size * 8 - 1);
8126 look_for = start - 4 - ((cie_id ^ sign) - sign);
8127 }
8128 else
8129 look_for = section_start + cie_id;
19e6b90e 8130
49727e46
AM
8131 if (look_for <= saved_start)
8132 {
8133 for (cie = chunks; cie ; cie = cie->next)
8134 if (cie->chunk_start == look_for)
8135 break;
8136 }
8137 else
8138 {
8139 for (cie = forward_refs; cie ; cie = cie->next)
8140 if (cie->chunk_start == look_for)
8141 break;
8142 if (!cie)
8143 {
8144 unsigned int off_size;
8145 unsigned char *cie_scan;
19e6b90e 8146
49727e46
AM
8147 cie_scan = look_for;
8148 off_size = 4;
8149 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
8150 if (length == 0xffffffff)
8151 {
8152 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
8153 off_size = 8;
8154 }
8155 if (length != 0)
8156 {
8157 dwarf_vma c_id;
8158
8159 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size, end);
8160 if (is_eh
8161 ? c_id == 0
8162 : ((off_size == 4 && c_id == DW_CIE_ID)
8163 || (off_size == 8 && c_id == DW64_CIE_ID)))
8164 {
8165 int version;
a1165289 8166 unsigned int mreg;
49727e46
AM
8167
8168 read_cie (cie_scan, end, &cie, &version,
8169 &augmentation_data_len, &augmentation_data);
a1165289
NC
8170 /* PR 17512: file: 3450-2098-0.004. */
8171 if (cie == NULL)
8172 {
8173 warn (_("Failed to read CIE information\n"));
8174 break;
8175 }
49727e46
AM
8176 cie->next = forward_refs;
8177 forward_refs = cie;
8178 cie->chunk_start = look_for;
a1165289 8179 mreg = max_regs > 0 ? max_regs - 1 : 0;
49727e46
AM
8180 if (mreg < cie->ra)
8181 mreg = cie->ra;
06614111
NC
8182 if (frame_need_space (cie, mreg) < 0)
8183 {
8184 warn (_("Invalid max register\n"));
8185 break;
8186 }
49727e46
AM
8187 if (cie->fde_encoding)
8188 encoded_ptr_size
8189 = size_of_encoded_value (cie->fde_encoding);
8190 }
8191 }
8192 }
8193 }
8194
8195 fc = &fde_fc;
8196 memset (fc, 0, sizeof (Frame_Chunk));
19e6b90e
L
8197
8198 if (!cie)
8199 {
bf5117e3
NC
8200 warn ("Invalid CIE pointer 0x%s in FDE at %#08lx\n",
8201 dwarf_vmatoa_1 (NULL, cie_id, offset_size),
8202 (unsigned long) (saved_start - section_start));
19e6b90e 8203 fc->ncols = 0;
3f5e193b
NC
8204 fc->col_type = (short int *) xmalloc (sizeof (short int));
8205 fc->col_offset = (int *) xmalloc (sizeof (int));
06614111
NC
8206 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
8207 {
8208 warn (_("Invalid max register\n"));
8209 break;
8210 }
19e6b90e
L
8211 cie = fc;
8212 fc->augmentation = "";
8213 fc->fde_encoding = 0;
604282a7
JJ
8214 fc->ptr_size = eh_addr_size;
8215 fc->segment_size = 0;
19e6b90e
L
8216 }
8217 else
8218 {
8219 fc->ncols = cie->ncols;
3f5e193b
NC
8220 fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
8221 fc->col_offset = (int *) xcmalloc (fc->ncols, sizeof (int));
19e6b90e
L
8222 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
8223 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
8224 fc->augmentation = cie->augmentation;
604282a7
JJ
8225 fc->ptr_size = cie->ptr_size;
8226 eh_addr_size = cie->ptr_size;
8227 fc->segment_size = cie->segment_size;
19e6b90e
L
8228 fc->code_factor = cie->code_factor;
8229 fc->data_factor = cie->data_factor;
8230 fc->cfa_reg = cie->cfa_reg;
8231 fc->cfa_offset = cie->cfa_offset;
8232 fc->ra = cie->ra;
06614111
NC
8233 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
8234 {
8235 warn (_("Invalid max register\n"));
8236 break;
8237 }
19e6b90e
L
8238 fc->fde_encoding = cie->fde_encoding;
8239 }
8240
8241 if (fc->fde_encoding)
8242 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8243
604282a7
JJ
8244 segment_selector = 0;
8245 if (fc->segment_size)
c8071705
NC
8246 {
8247 if (fc->segment_size > sizeof (segment_selector))
8248 {
8249 /* PR 17512: file: 9e196b3e. */
8250 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
8251 fc->segment_size = 4;
8252 }
8253 SAFE_BYTE_GET_AND_INC (segment_selector, start, fc->segment_size, end);
8254 }
041830e0
NC
8255
8256 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section, end);
19e6b90e 8257
0c588247
NC
8258 /* FIXME: It appears that sometimes the final pc_range value is
8259 encoded in less than encoded_ptr_size bytes. See the x86_64
8260 run of the "objcopy on compressed debug sections" test for an
8261 example of this. */
8262 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size, end);
bf5117e3 8263
19e6b90e
L
8264 if (cie->augmentation[0] == 'z')
8265 {
cd30bcef 8266 READ_ULEB (augmentation_data_len, start, end);
19e6b90e 8267 augmentation_data = start;
bf59c5d5 8268 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
d292364e 8269 if (augmentation_data_len > (bfd_size_type) (end - start))
0a9d414a 8270 {
d292364e
AM
8271 warn (_("Augmentation data too long: 0x%s, "
8272 "expected at most %#lx\n"),
8273 dwarf_vmatoa ("x", augmentation_data_len),
8274 (unsigned long) (end - start));
0a9d414a
NC
8275 start = end;
8276 augmentation_data = NULL;
8277 augmentation_data_len = 0;
8278 }
d292364e 8279 start += augmentation_data_len;
19e6b90e
L
8280 }
8281
bf5117e3
NC
8282 printf ("\n%08lx %s %s FDE cie=%08lx pc=",
8283 (unsigned long)(saved_start - section_start),
8284 dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
9c41109d 8285 dwarf_vmatoa_1 (NULL, cie_id, offset_size),
604282a7 8286 (unsigned long)(cie->chunk_start - section_start));
bf5117e3 8287
604282a7
JJ
8288 if (fc->segment_size)
8289 printf ("%04lx:", segment_selector);
bf5117e3
NC
8290
8291 printf ("%s..%s\n",
8292 dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
8293 dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));
8294
19e6b90e
L
8295 if (! do_debug_frames_interp && augmentation_data_len)
8296 {
bf59c5d5 8297 display_augmentation_data (augmentation_data, augmentation_data_len);
19e6b90e
L
8298 putchar ('\n');
8299 }
8300 }
8301
8302 /* At this point, fc is the current chunk, cie (if any) is set, and
8303 we're about to interpret instructions for the chunk. */
8304 /* ??? At present we need to do this always, since this sizes the
8305 fc->col_type and fc->col_offset arrays, which we write into always.
8306 We should probably split the interpreted and non-interpreted bits
8307 into two different routines, since there's so much that doesn't
8308 really overlap between them. */
8309 if (1 || do_debug_frames_interp)
8310 {
8311 /* Start by making a pass over the chunk, allocating storage
8312 and taking note of what registers are used. */
8313 unsigned char *tmp = start;
8314
8315 while (start < block_end)
8316 {
041830e0
NC
8317 unsigned int reg, op, opa;
8318 unsigned long temp;
5929c344 8319 unsigned char * new_start;
19e6b90e
L
8320
8321 op = *start++;
8322 opa = op & 0x3f;
8323 if (op & 0xc0)
8324 op &= 0xc0;
8325
8326 /* Warning: if you add any more cases to this switch, be
8327 sure to add them to the corresponding switch below. */
8328 switch (op)
8329 {
8330 case DW_CFA_advance_loc:
8331 break;
8332 case DW_CFA_offset:
cd30bcef 8333 SKIP_ULEB (start, end);
665ce1f6
L
8334 if (frame_need_space (fc, opa) >= 0)
8335 fc->col_type[opa] = DW_CFA_undefined;
19e6b90e
L
8336 break;
8337 case DW_CFA_restore:
665ce1f6
L
8338 if (frame_need_space (fc, opa) >= 0)
8339 fc->col_type[opa] = DW_CFA_undefined;
19e6b90e
L
8340 break;
8341 case DW_CFA_set_loc:
8342 start += encoded_ptr_size;
8343 break;
8344 case DW_CFA_advance_loc1:
8345 start += 1;
8346 break;
8347 case DW_CFA_advance_loc2:
8348 start += 2;
8349 break;
8350 case DW_CFA_advance_loc4:
8351 start += 4;
8352 break;
8353 case DW_CFA_offset_extended:
12eae2d3 8354 case DW_CFA_val_offset:
cd30bcef
AM
8355 READ_ULEB (reg, start, end);
8356 SKIP_ULEB (start, end);
665ce1f6
L
8357 if (frame_need_space (fc, reg) >= 0)
8358 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
8359 break;
8360 case DW_CFA_restore_extended:
cd30bcef 8361 READ_ULEB (reg, start, end);
665ce1f6
L
8362 if (frame_need_space (fc, reg) >= 0)
8363 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
8364 break;
8365 case DW_CFA_undefined:
cd30bcef 8366 READ_ULEB (reg, start, end);
665ce1f6
L
8367 if (frame_need_space (fc, reg) >= 0)
8368 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
8369 break;
8370 case DW_CFA_same_value:
cd30bcef 8371 READ_ULEB (reg, start, end);
665ce1f6
L
8372 if (frame_need_space (fc, reg) >= 0)
8373 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
8374 break;
8375 case DW_CFA_register:
cd30bcef
AM
8376 READ_ULEB (reg, start, end);
8377 SKIP_ULEB (start, end);
665ce1f6
L
8378 if (frame_need_space (fc, reg) >= 0)
8379 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
8380 break;
8381 case DW_CFA_def_cfa:
cd30bcef
AM
8382 SKIP_ULEB (start, end);
8383 SKIP_ULEB (start, end);
19e6b90e
L
8384 break;
8385 case DW_CFA_def_cfa_register:
cd30bcef 8386 SKIP_ULEB (start, end);
19e6b90e
L
8387 break;
8388 case DW_CFA_def_cfa_offset:
cd30bcef 8389 SKIP_ULEB (start, end);
19e6b90e
L
8390 break;
8391 case DW_CFA_def_cfa_expression:
cd30bcef 8392 READ_ULEB (temp, start, end);
5929c344
NC
8393 new_start = start + temp;
8394 if (new_start < start)
041830e0
NC
8395 {
8396 warn (_("Corrupt CFA_def expression value: %lu\n"), temp);
8397 start = block_end;
8398 }
8399 else
5929c344 8400 start = new_start;
19e6b90e
L
8401 break;
8402 case DW_CFA_expression:
12eae2d3 8403 case DW_CFA_val_expression:
cd30bcef
AM
8404 READ_ULEB (reg, start, end);
8405 READ_ULEB (temp, start, end);
5929c344
NC
8406 new_start = start + temp;
8407 if (new_start < start)
041830e0 8408 {
b4eb7656 8409 /* PR 17512: file:306-192417-0.005. */
041830e0
NC
8410 warn (_("Corrupt CFA expression value: %lu\n"), temp);
8411 start = block_end;
8412 }
8413 else
5929c344 8414 start = new_start;
665ce1f6
L
8415 if (frame_need_space (fc, reg) >= 0)
8416 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
8417 break;
8418 case DW_CFA_offset_extended_sf:
12eae2d3 8419 case DW_CFA_val_offset_sf:
cd30bcef
AM
8420 READ_ULEB (reg, start, end);
8421 SKIP_SLEB (start, end);
665ce1f6
L
8422 if (frame_need_space (fc, reg) >= 0)
8423 fc->col_type[reg] = DW_CFA_undefined;
19e6b90e
L
8424 break;
8425 case DW_CFA_def_cfa_sf:
cd30bcef
AM
8426 SKIP_ULEB (start, end);
8427 SKIP_SLEB (start, end);
19e6b90e
L
8428 break;
8429 case DW_CFA_def_cfa_offset_sf:
cd30bcef 8430 SKIP_SLEB (start, end);
19e6b90e
L
8431 break;
8432 case DW_CFA_MIPS_advance_loc8:
8433 start += 8;
8434 break;
8435 case DW_CFA_GNU_args_size:
cd30bcef 8436 SKIP_ULEB (start, end);
19e6b90e
L
8437 break;
8438 case DW_CFA_GNU_negative_offset_extended:
cd30bcef
AM
8439 READ_ULEB (reg, start, end);
8440 SKIP_ULEB (start, end);
665ce1f6
L
8441 if (frame_need_space (fc, reg) >= 0)
8442 fc->col_type[reg] = DW_CFA_undefined;
8443 break;
19e6b90e
L
8444 default:
8445 break;
8446 }
8447 }
8448 start = tmp;
8449 }
8450
5b6312fd
NC
8451 all_nops = TRUE;
8452
19e6b90e
L
8453 /* Now we know what registers are used, make a second pass over
8454 the chunk, this time actually printing out the info. */
8455
8456 while (start < block_end)
8457 {
362beea4 8458 unsigned char * tmp;
19e6b90e 8459 unsigned op, opa;
7f2c8a1d
NC
8460 unsigned long ul, roffs;
8461 /* Note: It is tempting to use an unsigned long for 'reg' but there
8462 are various functions, notably frame_space_needed() that assume that
8463 reg is an unsigned int. */
8464 unsigned int reg;
8465 dwarf_signed_vma l;
bf5117e3 8466 dwarf_vma ofs;
19e6b90e 8467 dwarf_vma vma;
665ce1f6 8468 const char *reg_prefix = "";
19e6b90e
L
8469
8470 op = *start++;
8471 opa = op & 0x3f;
8472 if (op & 0xc0)
8473 op &= 0xc0;
8474
5b6312fd
NC
8475 /* Make a note if something other than DW_CFA_nop happens. */
8476 if (op != DW_CFA_nop)
8477 all_nops = FALSE;
8478
19e6b90e
L
8479 /* Warning: if you add any more cases to this switch, be
8480 sure to add them to the corresponding switch above. */
8481 switch (op)
8482 {
8483 case DW_CFA_advance_loc:
8484 if (do_debug_frames_interp)
8485 frame_display_row (fc, &need_col_headers, &max_regs);
8486 else
bf5117e3 8487 printf (" DW_CFA_advance_loc: %d to %s\n",
19e6b90e 8488 opa * fc->code_factor,
b4eb7656 8489 dwarf_vmatoa_1 (NULL,
bf5117e3
NC
8490 fc->pc_begin + opa * fc->code_factor,
8491 fc->ptr_size));
19e6b90e
L
8492 fc->pc_begin += opa * fc->code_factor;
8493 break;
8494
8495 case DW_CFA_offset:
cd30bcef 8496 READ_ULEB (roffs, start, end);
665ce1f6
L
8497 if (opa >= (unsigned int) fc->ncols)
8498 reg_prefix = bad_reg;
8499 if (! do_debug_frames_interp || *reg_prefix != '\0')
8500 printf (" DW_CFA_offset: %s%s at cfa%+ld\n",
8501 reg_prefix, regname (opa, 0),
8502 roffs * fc->data_factor);
8503 if (*reg_prefix == '\0')
8504 {
8505 fc->col_type[opa] = DW_CFA_offset;
8506 fc->col_offset[opa] = roffs * fc->data_factor;
8507 }
19e6b90e
L
8508 break;
8509
8510 case DW_CFA_restore:
50751e18 8511 if (opa >= (unsigned int) fc->ncols)
665ce1f6
L
8512 reg_prefix = bad_reg;
8513 if (! do_debug_frames_interp || *reg_prefix != '\0')
8514 printf (" DW_CFA_restore: %s%s\n",
8515 reg_prefix, regname (opa, 0));
50751e18
AK
8516 if (*reg_prefix != '\0')
8517 break;
8518
8519 if (opa >= (unsigned int) cie->ncols
8520 || (do_debug_frames_interp
8521 && cie->col_type[opa] == DW_CFA_unreferenced))
8522 {
8523 fc->col_type[opa] = DW_CFA_undefined;
8524 fc->col_offset[opa] = 0;
8525 }
8526 else
665ce1f6
L
8527 {
8528 fc->col_type[opa] = cie->col_type[opa];
8529 fc->col_offset[opa] = cie->col_offset[opa];
8530 }
19e6b90e
L
8531 break;
8532
8533 case DW_CFA_set_loc:
6937bb54 8534 vma = get_encoded_value (&start, fc->fde_encoding, section, block_end);
19e6b90e
L
8535 if (do_debug_frames_interp)
8536 frame_display_row (fc, &need_col_headers, &max_regs);
8537 else
bf5117e3
NC
8538 printf (" DW_CFA_set_loc: %s\n",
8539 dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
19e6b90e
L
8540 fc->pc_begin = vma;
8541 break;
8542
8543 case DW_CFA_advance_loc1:
0c588247 8544 SAFE_BYTE_GET_AND_INC (ofs, start, 1, end);
19e6b90e
L
8545 if (do_debug_frames_interp)
8546 frame_display_row (fc, &need_col_headers, &max_regs);
8547 else
bf5117e3
NC
8548 printf (" DW_CFA_advance_loc1: %ld to %s\n",
8549 (unsigned long) (ofs * fc->code_factor),
8550 dwarf_vmatoa_1 (NULL,
8551 fc->pc_begin + ofs * fc->code_factor,
8552 fc->ptr_size));
19e6b90e
L
8553 fc->pc_begin += ofs * fc->code_factor;
8554 break;
8555
8556 case DW_CFA_advance_loc2:
6937bb54 8557 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
19e6b90e
L
8558 if (do_debug_frames_interp)
8559 frame_display_row (fc, &need_col_headers, &max_regs);
8560 else
bf5117e3
NC
8561 printf (" DW_CFA_advance_loc2: %ld to %s\n",
8562 (unsigned long) (ofs * fc->code_factor),
8563 dwarf_vmatoa_1 (NULL,
8564 fc->pc_begin + ofs * fc->code_factor,
8565 fc->ptr_size));
19e6b90e
L
8566 fc->pc_begin += ofs * fc->code_factor;
8567 break;
8568
8569 case DW_CFA_advance_loc4:
6937bb54 8570 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
19e6b90e
L
8571 if (do_debug_frames_interp)
8572 frame_display_row (fc, &need_col_headers, &max_regs);
8573 else
bf5117e3
NC
8574 printf (" DW_CFA_advance_loc4: %ld to %s\n",
8575 (unsigned long) (ofs * fc->code_factor),
8576 dwarf_vmatoa_1 (NULL,
8577 fc->pc_begin + ofs * fc->code_factor,
8578 fc->ptr_size));
19e6b90e
L
8579 fc->pc_begin += ofs * fc->code_factor;
8580 break;
8581
8582 case DW_CFA_offset_extended:
cd30bcef
AM
8583 READ_ULEB (reg, start, end);
8584 READ_ULEB (roffs, start, end);
665ce1f6
L
8585 if (reg >= (unsigned int) fc->ncols)
8586 reg_prefix = bad_reg;
8587 if (! do_debug_frames_interp || *reg_prefix != '\0')
8588 printf (" DW_CFA_offset_extended: %s%s at cfa%+ld\n",
8589 reg_prefix, regname (reg, 0),
8590 roffs * fc->data_factor);
8591 if (*reg_prefix == '\0')
8592 {
8593 fc->col_type[reg] = DW_CFA_offset;
8594 fc->col_offset[reg] = roffs * fc->data_factor;
8595 }
19e6b90e
L
8596 break;
8597
12eae2d3 8598 case DW_CFA_val_offset:
cd30bcef
AM
8599 READ_ULEB (reg, start, end);
8600 READ_ULEB (roffs, start, end);
665ce1f6
L
8601 if (reg >= (unsigned int) fc->ncols)
8602 reg_prefix = bad_reg;
8603 if (! do_debug_frames_interp || *reg_prefix != '\0')
084303b8 8604 printf (" DW_CFA_val_offset: %s%s is cfa%+ld\n",
665ce1f6
L
8605 reg_prefix, regname (reg, 0),
8606 roffs * fc->data_factor);
8607 if (*reg_prefix == '\0')
8608 {
8609 fc->col_type[reg] = DW_CFA_val_offset;
8610 fc->col_offset[reg] = roffs * fc->data_factor;
8611 }
12eae2d3
JJ
8612 break;
8613
19e6b90e 8614 case DW_CFA_restore_extended:
cd30bcef 8615 READ_ULEB (reg, start, end);
50751e18 8616 if (reg >= (unsigned int) fc->ncols)
665ce1f6
L
8617 reg_prefix = bad_reg;
8618 if (! do_debug_frames_interp || *reg_prefix != '\0')
8619 printf (" DW_CFA_restore_extended: %s%s\n",
8620 reg_prefix, regname (reg, 0));
50751e18
AK
8621 if (*reg_prefix != '\0')
8622 break;
8623
8624 if (reg >= (unsigned int) cie->ncols)
8625 {
8626 fc->col_type[reg] = DW_CFA_undefined;
8627 fc->col_offset[reg] = 0;
8628 }
8629 else
665ce1f6
L
8630 {
8631 fc->col_type[reg] = cie->col_type[reg];
8632 fc->col_offset[reg] = cie->col_offset[reg];
8633 }
19e6b90e
L
8634 break;
8635
8636 case DW_CFA_undefined:
cd30bcef 8637 READ_ULEB (reg, start, end);
665ce1f6
L
8638 if (reg >= (unsigned int) fc->ncols)
8639 reg_prefix = bad_reg;
8640 if (! do_debug_frames_interp || *reg_prefix != '\0')
8641 printf (" DW_CFA_undefined: %s%s\n",
8642 reg_prefix, regname (reg, 0));
8643 if (*reg_prefix == '\0')
8644 {
8645 fc->col_type[reg] = DW_CFA_undefined;
8646 fc->col_offset[reg] = 0;
8647 }
19e6b90e
L
8648 break;
8649
8650 case DW_CFA_same_value:
cd30bcef 8651 READ_ULEB (reg, start, end);
665ce1f6
L
8652 if (reg >= (unsigned int) fc->ncols)
8653 reg_prefix = bad_reg;
8654 if (! do_debug_frames_interp || *reg_prefix != '\0')
8655 printf (" DW_CFA_same_value: %s%s\n",
8656 reg_prefix, regname (reg, 0));
8657 if (*reg_prefix == '\0')
8658 {
8659 fc->col_type[reg] = DW_CFA_same_value;
8660 fc->col_offset[reg] = 0;
8661 }
19e6b90e
L
8662 break;
8663
8664 case DW_CFA_register:
cd30bcef
AM
8665 READ_ULEB (reg, start, end);
8666 READ_ULEB (roffs, start, end);
665ce1f6
L
8667 if (reg >= (unsigned int) fc->ncols)
8668 reg_prefix = bad_reg;
8669 if (! do_debug_frames_interp || *reg_prefix != '\0')
2dc4cec1 8670 {
665ce1f6
L
8671 printf (" DW_CFA_register: %s%s in ",
8672 reg_prefix, regname (reg, 0));
2dc4cec1
L
8673 puts (regname (roffs, 0));
8674 }
665ce1f6
L
8675 if (*reg_prefix == '\0')
8676 {
8677 fc->col_type[reg] = DW_CFA_register;
8678 fc->col_offset[reg] = roffs;
8679 }
19e6b90e
L
8680 break;
8681
8682 case DW_CFA_remember_state:
8683 if (! do_debug_frames_interp)
8684 printf (" DW_CFA_remember_state\n");
3f5e193b 8685 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
b4eb7656 8686 rs->cfa_offset = fc->cfa_offset;
d71ad7fc
RC
8687 rs->cfa_reg = fc->cfa_reg;
8688 rs->ra = fc->ra;
8689 rs->cfa_exp = fc->cfa_exp;
19e6b90e 8690 rs->ncols = fc->ncols;
3f5e193b 8691 rs->col_type = (short int *) xcmalloc (rs->ncols,
b4eb7656 8692 sizeof (* rs->col_type));
d71ad7fc
RC
8693 rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
8694 memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
8695 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
19e6b90e
L
8696 rs->next = remembered_state;
8697 remembered_state = rs;
8698 break;
8699
8700 case DW_CFA_restore_state:
8701 if (! do_debug_frames_interp)
8702 printf (" DW_CFA_restore_state\n");
8703 rs = remembered_state;
8704 if (rs)
8705 {
8706 remembered_state = rs->next;
d71ad7fc
RC
8707 fc->cfa_offset = rs->cfa_offset;
8708 fc->cfa_reg = rs->cfa_reg;
b4eb7656
AM
8709 fc->ra = rs->ra;
8710 fc->cfa_exp = rs->cfa_exp;
06614111
NC
8711 if (frame_need_space (fc, rs->ncols - 1) < 0)
8712 {
1306a742 8713 warn (_("Invalid column number in saved frame state\n"));
06614111
NC
8714 fc->ncols = 0;
8715 break;
8716 }
d71ad7fc 8717 memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
19e6b90e 8718 memcpy (fc->col_offset, rs->col_offset,
d71ad7fc 8719 rs->ncols * sizeof (* rs->col_offset));
19e6b90e
L
8720 free (rs->col_type);
8721 free (rs->col_offset);
8722 free (rs);
8723 }
8724 else if (do_debug_frames_interp)
8725 printf ("Mismatched DW_CFA_restore_state\n");
8726 break;
8727
8728 case DW_CFA_def_cfa:
cd30bcef
AM
8729 READ_ULEB (fc->cfa_reg, start, end);
8730 READ_ULEB (fc->cfa_offset, start, end);
19e6b90e
L
8731 fc->cfa_exp = 0;
8732 if (! do_debug_frames_interp)
2dc4cec1 8733 printf (" DW_CFA_def_cfa: %s ofs %d\n",
c8071705 8734 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
19e6b90e
L
8735 break;
8736
8737 case DW_CFA_def_cfa_register:
cd30bcef 8738 READ_ULEB (fc->cfa_reg, start, end);
19e6b90e
L
8739 fc->cfa_exp = 0;
8740 if (! do_debug_frames_interp)
2dc4cec1
L
8741 printf (" DW_CFA_def_cfa_register: %s\n",
8742 regname (fc->cfa_reg, 0));
19e6b90e
L
8743 break;
8744
8745 case DW_CFA_def_cfa_offset:
cd30bcef 8746 READ_ULEB (fc->cfa_offset, start, end);
19e6b90e 8747 if (! do_debug_frames_interp)
c8071705 8748 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
19e6b90e
L
8749 break;
8750
8751 case DW_CFA_nop:
8752 if (! do_debug_frames_interp)
8753 printf (" DW_CFA_nop\n");
8754 break;
8755
8756 case DW_CFA_def_cfa_expression:
cd30bcef 8757 READ_ULEB (ul, start, end);
7460c0ab 8758 if (start >= block_end || ul > (unsigned long) (block_end - start))
6937bb54 8759 {
a1165289 8760 printf (_(" DW_CFA_def_cfa_expression: <corrupt len %lu>\n"), ul);
6937bb54
NC
8761 break;
8762 }
19e6b90e
L
8763 if (! do_debug_frames_interp)
8764 {
8765 printf (" DW_CFA_def_cfa_expression (");
b7807392
JJ
8766 decode_location_expression (start, eh_addr_size, 0, -1,
8767 ul, 0, section);
19e6b90e
L
8768 printf (")\n");
8769 }
8770 fc->cfa_exp = 1;
8771 start += ul;
8772 break;
8773
8774 case DW_CFA_expression:
cd30bcef
AM
8775 READ_ULEB (reg, start, end);
8776 READ_ULEB (ul, start, end);
665ce1f6
L
8777 if (reg >= (unsigned int) fc->ncols)
8778 reg_prefix = bad_reg;
6937bb54 8779 /* PR 17512: file: 069-133014-0.006. */
06614111 8780 /* PR 17512: file: 98c02eb4. */
362beea4
NC
8781 tmp = start + ul;
8782 if (start >= block_end || tmp > block_end || tmp < start)
6937bb54 8783 {
a1165289 8784 printf (_(" DW_CFA_expression: <corrupt len %lu>\n"), ul);
6937bb54
NC
8785 break;
8786 }
665ce1f6 8787 if (! do_debug_frames_interp || *reg_prefix != '\0')
19e6b90e 8788 {
665ce1f6
L
8789 printf (" DW_CFA_expression: %s%s (",
8790 reg_prefix, regname (reg, 0));
b7807392 8791 decode_location_expression (start, eh_addr_size, 0, -1,
f1c4cc75 8792 ul, 0, section);
19e6b90e
L
8793 printf (")\n");
8794 }
665ce1f6
L
8795 if (*reg_prefix == '\0')
8796 fc->col_type[reg] = DW_CFA_expression;
362beea4 8797 start = tmp;
19e6b90e
L
8798 break;
8799
12eae2d3 8800 case DW_CFA_val_expression:
cd30bcef
AM
8801 READ_ULEB (reg, start, end);
8802 READ_ULEB (ul, start, end);
665ce1f6
L
8803 if (reg >= (unsigned int) fc->ncols)
8804 reg_prefix = bad_reg;
362beea4
NC
8805 tmp = start + ul;
8806 if (start >= block_end || tmp > block_end || tmp < start)
6937bb54 8807 {
a1165289 8808 printf (" DW_CFA_val_expression: <corrupt len %lu>\n", ul);
6937bb54
NC
8809 break;
8810 }
665ce1f6 8811 if (! do_debug_frames_interp || *reg_prefix != '\0')
12eae2d3 8812 {
665ce1f6
L
8813 printf (" DW_CFA_val_expression: %s%s (",
8814 reg_prefix, regname (reg, 0));
b7807392
JJ
8815 decode_location_expression (start, eh_addr_size, 0, -1,
8816 ul, 0, section);
12eae2d3
JJ
8817 printf (")\n");
8818 }
665ce1f6
L
8819 if (*reg_prefix == '\0')
8820 fc->col_type[reg] = DW_CFA_val_expression;
362beea4 8821 start = tmp;
12eae2d3
JJ
8822 break;
8823
19e6b90e 8824 case DW_CFA_offset_extended_sf:
cd30bcef
AM
8825 READ_ULEB (reg, start, end);
8826 READ_SLEB (l, start, end);
665ce1f6
L
8827 if (frame_need_space (fc, reg) < 0)
8828 reg_prefix = bad_reg;
8829 if (! do_debug_frames_interp || *reg_prefix != '\0')
8830 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
8831 reg_prefix, regname (reg, 0),
c8071705 8832 (long)(l * fc->data_factor));
665ce1f6
L
8833 if (*reg_prefix == '\0')
8834 {
8835 fc->col_type[reg] = DW_CFA_offset;
8836 fc->col_offset[reg] = l * fc->data_factor;
8837 }
19e6b90e
L
8838 break;
8839
12eae2d3 8840 case DW_CFA_val_offset_sf:
cd30bcef
AM
8841 READ_ULEB (reg, start, end);
8842 READ_SLEB (l, start, end);
665ce1f6
L
8843 if (frame_need_space (fc, reg) < 0)
8844 reg_prefix = bad_reg;
8845 if (! do_debug_frames_interp || *reg_prefix != '\0')
084303b8 8846 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+ld\n",
665ce1f6 8847 reg_prefix, regname (reg, 0),
c8071705 8848 (long)(l * fc->data_factor));
665ce1f6
L
8849 if (*reg_prefix == '\0')
8850 {
8851 fc->col_type[reg] = DW_CFA_val_offset;
8852 fc->col_offset[reg] = l * fc->data_factor;
8853 }
12eae2d3
JJ
8854 break;
8855
19e6b90e 8856 case DW_CFA_def_cfa_sf:
cd30bcef
AM
8857 READ_ULEB (fc->cfa_reg, start, end);
8858 READ_ULEB (fc->cfa_offset, start, end);
19e6b90e
L
8859 fc->cfa_offset = fc->cfa_offset * fc->data_factor;
8860 fc->cfa_exp = 0;
8861 if (! do_debug_frames_interp)
2dc4cec1 8862 printf (" DW_CFA_def_cfa_sf: %s ofs %d\n",
c8071705 8863 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
19e6b90e
L
8864 break;
8865
8866 case DW_CFA_def_cfa_offset_sf:
cd30bcef 8867 READ_ULEB (fc->cfa_offset, start, end);
c8071705 8868 fc->cfa_offset *= fc->data_factor;
19e6b90e 8869 if (! do_debug_frames_interp)
c8071705 8870 printf (" DW_CFA_def_cfa_offset_sf: %d\n", (int) fc->cfa_offset);
19e6b90e
L
8871 break;
8872
8873 case DW_CFA_MIPS_advance_loc8:
6937bb54 8874 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
19e6b90e
L
8875 if (do_debug_frames_interp)
8876 frame_display_row (fc, &need_col_headers, &max_regs);
8877 else
bf5117e3
NC
8878 printf (" DW_CFA_MIPS_advance_loc8: %ld to %s\n",
8879 (unsigned long) (ofs * fc->code_factor),
8880 dwarf_vmatoa_1 (NULL,
8881 fc->pc_begin + ofs * fc->code_factor,
8882 fc->ptr_size));
19e6b90e
L
8883 fc->pc_begin += ofs * fc->code_factor;
8884 break;
8885
8886 case DW_CFA_GNU_window_save:
8887 if (! do_debug_frames_interp)
8888 printf (" DW_CFA_GNU_window_save\n");
8889 break;
8890
8891 case DW_CFA_GNU_args_size:
cd30bcef 8892 READ_ULEB (ul, start, end);
19e6b90e
L
8893 if (! do_debug_frames_interp)
8894 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
8895 break;
8896
8897 case DW_CFA_GNU_negative_offset_extended:
cd30bcef
AM
8898 READ_ULEB (reg, start, end);
8899 READ_SLEB (l, start, end);
7f2c8a1d 8900 l = - l;
665ce1f6
L
8901 if (frame_need_space (fc, reg) < 0)
8902 reg_prefix = bad_reg;
8903 if (! do_debug_frames_interp || *reg_prefix != '\0')
8904 printf (" DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
8905 reg_prefix, regname (reg, 0),
c8071705 8906 (long)(l * fc->data_factor));
665ce1f6
L
8907 if (*reg_prefix == '\0')
8908 {
8909 fc->col_type[reg] = DW_CFA_offset;
8910 fc->col_offset[reg] = l * fc->data_factor;
8911 }
19e6b90e
L
8912 break;
8913
8914 default:
53b8873b
NC
8915 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
8916 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
8917 else
f41e4712 8918 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
19e6b90e
L
8919 start = block_end;
8920 }
8921 }
8922
5b6312fd
NC
8923 /* Interpret the CFA - as long as it is not completely full of NOPs. */
8924 if (do_debug_frames_interp && ! all_nops)
19e6b90e
L
8925 frame_display_row (fc, &need_col_headers, &max_regs);
8926
a788aedd
AM
8927 if (fde_fc.col_type != NULL)
8928 {
8929 free (fde_fc.col_type);
8930 fde_fc.col_type = NULL;
8931 }
8932 if (fde_fc.col_offset != NULL)
8933 {
8934 free (fde_fc.col_offset);
8935 fde_fc.col_offset = NULL;
8936 }
8937
19e6b90e 8938 start = block_end;
604282a7 8939 eh_addr_size = saved_eh_addr_size;
19e6b90e
L
8940 }
8941
8942 printf ("\n");
8943
3391569f
NC
8944 while (remembered_state != NULL)
8945 {
8946 rs = remembered_state;
8947 remembered_state = rs->next;
8948 free (rs->col_type);
8949 free (rs->col_offset);
8950 rs->next = NULL; /* Paranoia. */
8951 free (rs);
8952 }
8953
8954 while (chunks != NULL)
8955 {
8956 rs = chunks;
8957 chunks = rs->next;
8958 free (rs->col_type);
8959 free (rs->col_offset);
8960 rs->next = NULL; /* Paranoia. */
8961 free (rs);
8962 }
8963
8964 while (forward_refs != NULL)
8965 {
8966 rs = forward_refs;
8967 forward_refs = rs->next;
8968 free (rs->col_type);
8969 free (rs->col_offset);
8970 rs->next = NULL; /* Paranoia. */
8971 free (rs);
8972 }
8973
19e6b90e
L
8974 return 1;
8975}
8976
8977#undef GET
19e6b90e 8978
61364358
JK
8979static int
8980display_debug_names (struct dwarf_section *section, void *file)
8981{
8982 unsigned char *hdrptr = section->start;
8983 dwarf_vma unit_length;
8984 unsigned char *unit_start;
8985 const unsigned char *const section_end = section->start + section->size;
8986 unsigned char *unit_end;
8987
dda8d76d 8988 introduce (section, FALSE);
61364358 8989
dda8d76d 8990 load_debug_section_with_follow (str, file);
61364358
JK
8991
8992 for (; hdrptr < section_end; hdrptr = unit_end)
8993 {
8994 unsigned int offset_size;
8995 uint16_t dwarf_version, padding;
8996 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
8997 uint32_t bucket_count, name_count, abbrev_table_size;
8998 uint32_t augmentation_string_size;
8999 unsigned int i;
e98fdf1a 9000 unsigned long sec_off;
48467cb9
TV
9001 bfd_boolean augmentation_printable;
9002 const char *augmentation_string;
61364358
JK
9003
9004 unit_start = hdrptr;
9005
9006 /* Get and check the length of the block. */
9007 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
9008
9009 if (unit_length == 0xffffffff)
9010 {
9011 /* This section is 64-bit DWARF. */
9012 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
9013 offset_size = 8;
9014 }
9015 else
9016 offset_size = 4;
9017 unit_end = hdrptr + unit_length;
9018
e98fdf1a
AM
9019 sec_off = hdrptr - section->start;
9020 if (sec_off + unit_length < sec_off
9021 || sec_off + unit_length > section->size)
61364358 9022 {
e98fdf1a
AM
9023 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
9024 section->name,
9025 (unsigned long) (unit_start - section->start),
9026 dwarf_vmatoa ("x", unit_length));
61364358
JK
9027 return 0;
9028 }
9029
9030 /* Get and check the version number. */
9031 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
9032 printf (_("Version %ld\n"), (long) dwarf_version);
9033
9034 /* Prior versions did not exist, and future versions may not be
9035 backwards compatible. */
9036 if (dwarf_version != 5)
9037 {
9038 warn (_("Only DWARF version 5 .debug_names "
9039 "is currently supported.\n"));
9040 return 0;
9041 }
9042
9043 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
9044 if (padding != 0)
9045 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
9046 padding);
9047
9048 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
9049 if (comp_unit_count == 0)
9050 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
9051
9052 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
9053 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
9054 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
9055 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
9056 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
9057
9058 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
9059 if (augmentation_string_size % 4 != 0)
9060 {
9061 warn (_("Augmentation string length %u must be rounded up "
9062 "to a multiple of 4 in .debug_names.\n"),
9063 augmentation_string_size);
9064 augmentation_string_size += (-augmentation_string_size) & 3;
9065 }
48467cb9 9066
61364358 9067 printf (_("Augmentation string:"));
48467cb9
TV
9068
9069 augmentation_printable = TRUE;
9070 augmentation_string = (const char *) hdrptr;
9071
61364358
JK
9072 for (i = 0; i < augmentation_string_size; i++)
9073 {
9074 unsigned char uc;
9075
9076 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
9077 printf (" %02x", uc);
48467cb9
TV
9078
9079 if (uc != 0 && !ISPRINT (uc))
9080 augmentation_printable = FALSE;
9081 }
9082
9083 if (augmentation_printable)
9084 {
9085 printf (" (\"");
9086 for (i = 0;
9087 i < augmentation_string_size && augmentation_string[i];
9088 ++i)
9089 putchar (augmentation_string[i]);
9090 printf ("\")");
61364358 9091 }
61364358
JK
9092 putchar ('\n');
9093
9094 printf (_("CU table:\n"));
9095 for (i = 0; i < comp_unit_count; i++)
9096 {
9097 uint64_t cu_offset;
9098
9099 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
9100 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) cu_offset);
9101 }
9102 putchar ('\n');
9103
9104 printf (_("TU table:\n"));
9105 for (i = 0; i < local_type_unit_count; i++)
9106 {
9107 uint64_t tu_offset;
9108
9109 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
9110 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) tu_offset);
9111 }
9112 putchar ('\n');
9113
9114 printf (_("Foreign TU table:\n"));
9115 for (i = 0; i < foreign_type_unit_count; i++)
9116 {
9117 uint64_t signature;
9118
9119 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
9120 printf (_("[%3u] "), i);
9121 print_dwarf_vma (signature, 8);
9122 putchar ('\n');
9123 }
9124 putchar ('\n');
9125
9126 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
9127 hdrptr += bucket_count * sizeof (uint32_t);
9128 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
9129 hdrptr += name_count * sizeof (uint32_t);
9130 unsigned char *const name_table_string_offsets = hdrptr;
9131 hdrptr += name_count * offset_size;
9132 unsigned char *const name_table_entry_offsets = hdrptr;
9133 hdrptr += name_count * offset_size;
9134 unsigned char *const abbrev_table = hdrptr;
9135 hdrptr += abbrev_table_size;
9136 const unsigned char *const abbrev_table_end = hdrptr;
9137 unsigned char *const entry_pool = hdrptr;
9138 if (hdrptr > unit_end)
9139 {
9140 warn (_("Entry pool offset (0x%lx) exceeds unit size 0x%lx "
9141 "for unit 0x%lx in the debug_names\n"),
9142 (long) (hdrptr - section->start),
9143 (long) (unit_end - section->start),
9144 (long) (unit_start - section->start));
9145 return 0;
9146 }
9147
9148 size_t buckets_filled = 0;
9149 size_t bucketi;
9150 for (bucketi = 0; bucketi < bucket_count; bucketi++)
9151 {
9152 const uint32_t bucket = hash_table_buckets[bucketi];
9153
9154 if (bucket != 0)
9155 ++buckets_filled;
9156 }
d3a49aa8
AM
9157 printf (ngettext ("Used %zu of %lu bucket.\n",
9158 "Used %zu of %lu buckets.\n",
9159 bucket_count),
9160 buckets_filled, (unsigned long) bucket_count);
61364358 9161
0a79bef4 9162 uint32_t hash_prev = 0;
61364358
JK
9163 size_t hash_clash_count = 0;
9164 size_t longest_clash = 0;
9165 size_t this_length = 0;
9166 size_t hashi;
9167 for (hashi = 0; hashi < name_count; hashi++)
9168 {
9169 const uint32_t hash_this = hash_table_hashes[hashi];
9170
9171 if (hashi > 0)
9172 {
9173 if (hash_prev % bucket_count == hash_this % bucket_count)
9174 {
9175 ++hash_clash_count;
9176 ++this_length;
9177 longest_clash = MAX (longest_clash, this_length);
9178 }
9179 else
9180 this_length = 0;
9181 }
9182 hash_prev = hash_this;
9183 }
9184 printf (_("Out of %lu items there are %zu bucket clashes"
9185 " (longest of %zu entries).\n"),
9186 (unsigned long) name_count, hash_clash_count, longest_clash);
9187 assert (name_count == buckets_filled + hash_clash_count);
9188
9189 struct abbrev_lookup_entry
9190 {
9191 dwarf_vma abbrev_tag;
9192 unsigned char *abbrev_lookup_ptr;
9193 };
9194 struct abbrev_lookup_entry *abbrev_lookup = NULL;
9195 size_t abbrev_lookup_used = 0;
9196 size_t abbrev_lookup_allocated = 0;
9197
9198 unsigned char *abbrevptr = abbrev_table;
9199 for (;;)
9200 {
cd30bcef
AM
9201 dwarf_vma abbrev_tag;
9202
9203 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
61364358
JK
9204 if (abbrev_tag == 0)
9205 break;
9206 if (abbrev_lookup_used == abbrev_lookup_allocated)
9207 {
9208 abbrev_lookup_allocated = MAX (0x100,
9209 abbrev_lookup_allocated * 2);
9210 abbrev_lookup = xrealloc (abbrev_lookup,
9211 (abbrev_lookup_allocated
9212 * sizeof (*abbrev_lookup)));
9213 }
9214 assert (abbrev_lookup_used < abbrev_lookup_allocated);
9215 struct abbrev_lookup_entry *entry;
9216 for (entry = abbrev_lookup;
9217 entry < abbrev_lookup + abbrev_lookup_used;
9218 entry++)
9219 if (entry->abbrev_tag == abbrev_tag)
9220 {
9221 warn (_("Duplicate abbreviation tag %lu "
9222 "in unit 0x%lx in the debug_names\n"),
9223 (long) abbrev_tag, (long) (unit_start - section->start));
9224 break;
9225 }
9226 entry = &abbrev_lookup[abbrev_lookup_used++];
9227 entry->abbrev_tag = abbrev_tag;
9228 entry->abbrev_lookup_ptr = abbrevptr;
9229
9230 /* Skip DWARF tag. */
cd30bcef 9231 SKIP_ULEB (abbrevptr, abbrev_table_end);
61364358
JK
9232 for (;;)
9233 {
cd30bcef
AM
9234 dwarf_vma xindex, form;
9235
9236 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
9237 READ_ULEB (form, abbrevptr, abbrev_table_end);
1d827a72 9238 if (xindex == 0 && form == 0)
61364358
JK
9239 break;
9240 }
9241 }
9242
9243 printf (_("\nSymbol table:\n"));
9244 uint32_t namei;
9245 for (namei = 0; namei < name_count; ++namei)
9246 {
9247 uint64_t string_offset, entry_offset;
9248
9249 SAFE_BYTE_GET (string_offset,
9250 name_table_string_offsets + namei * offset_size,
9251 offset_size, unit_end);
9252 SAFE_BYTE_GET (entry_offset,
9253 name_table_entry_offsets + namei * offset_size,
9254 offset_size, unit_end);
9255
9256 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
9257 fetch_indirect_string (string_offset));
9258
9259 unsigned char *entryptr = entry_pool + entry_offset;
9260
279edac5
AM
9261 /* We need to scan first whether there is a single or multiple
9262 entries. TAGNO is -2 for the first entry, it is -1 for the
9263 initial tag read of the second entry, then it becomes 0 for the
9264 first entry for real printing etc. */
61364358
JK
9265 int tagno = -2;
9266 /* Initialize it due to a false compiler warning. */
9267 dwarf_vma second_abbrev_tag = -1;
9268 for (;;)
9269 {
cd30bcef
AM
9270 dwarf_vma abbrev_tag;
9271 dwarf_vma dwarf_tag;
9272 const struct abbrev_lookup_entry *entry;
9273
9274 READ_ULEB (abbrev_tag, entryptr, unit_end);
61364358
JK
9275 if (tagno == -1)
9276 {
9277 second_abbrev_tag = abbrev_tag;
9278 tagno = 0;
9279 entryptr = entry_pool + entry_offset;
9280 continue;
9281 }
9282 if (abbrev_tag == 0)
9283 break;
9284 if (tagno >= 0)
9285 printf ("%s<%lu>",
9286 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
9287 (unsigned long) abbrev_tag);
9288
61364358
JK
9289 for (entry = abbrev_lookup;
9290 entry < abbrev_lookup + abbrev_lookup_used;
9291 entry++)
9292 if (entry->abbrev_tag == abbrev_tag)
9293 break;
9294 if (entry >= abbrev_lookup + abbrev_lookup_used)
9295 {
9296 warn (_("Undefined abbreviation tag %lu "
9297 "in unit 0x%lx in the debug_names\n"),
9298 (long) abbrev_tag,
9299 (long) (unit_start - section->start));
9300 break;
9301 }
9302 abbrevptr = entry->abbrev_lookup_ptr;
cd30bcef 9303 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
61364358
JK
9304 if (tagno >= 0)
9305 printf (" %s", get_TAG_name (dwarf_tag));
9306 for (;;)
9307 {
cd30bcef
AM
9308 dwarf_vma xindex, form;
9309
9310 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
9311 READ_ULEB (form, abbrevptr, abbrev_table_end);
1d827a72 9312 if (xindex == 0 && form == 0)
61364358
JK
9313 break;
9314
9315 if (tagno >= 0)
1d827a72 9316 printf (" %s", get_IDX_name (xindex));
ec1b0fbb
NC
9317 entryptr = read_and_display_attr_value (0, form, 0,
9318 unit_start, entryptr, unit_end,
9319 0, 0, offset_size,
61364358
JK
9320 dwarf_version, NULL,
9321 (tagno < 0), NULL,
ec1b0fbb 9322 NULL, '=', -1);
61364358
JK
9323 }
9324 ++tagno;
9325 }
9326 if (tagno <= 0)
9327 printf (_(" <no entries>"));
9328 putchar ('\n');
9329 }
9330
9331 free (abbrev_lookup);
9332 }
9333
9334 return 1;
9335}
9336
dda8d76d 9337static int
d85bf2ba
NC
9338display_debug_links (struct dwarf_section * section,
9339 void * file ATTRIBUTE_UNUSED)
dda8d76d
NC
9340{
9341 const unsigned char * filename;
9342 unsigned int filelen;
9343
9344 introduce (section, FALSE);
9345
9346 /* The .gnu_debuglink section is formatted as:
9347 (c-string) Filename.
9348 (padding) If needed to reach a 4 byte boundary.
9349 (uint32_t) CRC32 value.
9350
9351 The .gun_debugaltlink section is formatted as:
9352 (c-string) Filename.
9353 (binary) Build-ID. */
9354
9355 filename = section->start;
9356 filelen = strnlen ((const char *) filename, section->size);
9357 if (filelen == section->size)
9358 {
9359 warn (_("The debuglink filename is corrupt/missing\n"));
9360 return 0;
9361 }
9362
9363 printf (_(" Separate debug info file: %s\n"), filename);
9364
9365 if (const_strneq (section->name, ".gnu_debuglink"))
9366 {
9367 unsigned int crc32;
9368 unsigned int crc_offset;
9369
9370 crc_offset = filelen + 1;
9371 crc_offset = (crc_offset + 3) & ~3;
9372 if (crc_offset + 4 > section->size)
9373 {
9374 warn (_("CRC offset missing/truncated\n"));
9375 return 0;
9376 }
9377
9378 crc32 = byte_get (filename + crc_offset, 4);
9379
9380 printf (_(" CRC value: %#x\n"), crc32);
9381
9382 if (crc_offset + 4 < section->size)
9383 {
9384 warn (_("There are %#lx extraneous bytes at the end of the section\n"),
9385 (long)(section->size - (crc_offset + 4)));
9386 return 0;
9387 }
9388 }
9389 else /* const_strneq (section->name, ".gnu_debugaltlink") */
9390 {
9391 const unsigned char * build_id = section->start + filelen + 1;
9392 bfd_size_type build_id_len = section->size - (filelen + 1);
9393 bfd_size_type printed;
9394
9395 /* FIXME: Should we support smaller build-id notes ? */
9396 if (build_id_len < 0x14)
9397 {
9398 warn (_("Build-ID is too short (%#lx bytes)\n"), (long) build_id_len);
9399 return 0;
9400 }
9401
9402 printed = printf (_(" Build-ID (%#lx bytes):"), (long) build_id_len);
d85bf2ba 9403 display_data (printed, build_id, build_id_len);
dda8d76d
NC
9404 putchar ('\n');
9405 }
9406
9407 putchar ('\n');
9408 return 1;
9409}
9410
5bbdf3d5
DE
9411static int
9412display_gdb_index (struct dwarf_section *section,
9413 void *file ATTRIBUTE_UNUSED)
9414{
9415 unsigned char *start = section->start;
9416 uint32_t version;
9417 uint32_t cu_list_offset, tu_list_offset;
9418 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
9419 unsigned int cu_list_elements, tu_list_elements;
9420 unsigned int address_table_size, symbol_table_slots;
9421 unsigned char *cu_list, *tu_list;
9422 unsigned char *address_table, *symbol_table, *constant_pool;
9423 unsigned int i;
9424
9425 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
9426
dda8d76d 9427 introduce (section, FALSE);
5bbdf3d5
DE
9428
9429 if (section->size < 6 * sizeof (uint32_t))
9430 {
9431 warn (_("Truncated header in the %s section.\n"), section->name);
9432 return 0;
9433 }
9434
9435 version = byte_get_little_endian (start, 4);
da88a764 9436 printf (_("Version %ld\n"), (long) version);
5bbdf3d5
DE
9437
9438 /* Prior versions are obsolete, and future versions may not be
9439 backwards compatible. */
aa170720 9440 if (version < 3 || version > 8)
5bbdf3d5 9441 {
da88a764 9442 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
5bbdf3d5
DE
9443 return 0;
9444 }
8d6eee87
TT
9445 if (version < 4)
9446 warn (_("The address table data in version 3 may be wrong.\n"));
9447 if (version < 5)
9448 warn (_("Version 4 does not support case insensitive lookups.\n"));
9449 if (version < 6)
9450 warn (_("Version 5 does not include inlined functions.\n"));
9451 if (version < 7)
9452 warn (_("Version 6 does not include symbol attributes.\n"));
aa170720
DE
9453 /* Version 7 indices generated by Gold have bad type unit references,
9454 PR binutils/15021. But we don't know if the index was generated by
9455 Gold or not, so to avoid worrying users with gdb-generated indices
9456 we say nothing for version 7 here. */
5bbdf3d5
DE
9457
9458 cu_list_offset = byte_get_little_endian (start + 4, 4);
9459 tu_list_offset = byte_get_little_endian (start + 8, 4);
9460 address_table_offset = byte_get_little_endian (start + 12, 4);
9461 symbol_table_offset = byte_get_little_endian (start + 16, 4);
9462 constant_pool_offset = byte_get_little_endian (start + 20, 4);
9463
9464 if (cu_list_offset > section->size
9465 || tu_list_offset > section->size
9466 || address_table_offset > section->size
9467 || symbol_table_offset > section->size
9468 || constant_pool_offset > section->size)
9469 {
9470 warn (_("Corrupt header in the %s section.\n"), section->name);
9471 return 0;
9472 }
9473
53774b7e
NC
9474 /* PR 17531: file: 418d0a8a. */
9475 if (tu_list_offset < cu_list_offset)
9476 {
9477 warn (_("TU offset (%x) is less than CU offset (%x)\n"),
9478 tu_list_offset, cu_list_offset);
9479 return 0;
9480 }
9481
5bbdf3d5 9482 cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
53774b7e
NC
9483
9484 if (address_table_offset < tu_list_offset)
9485 {
9486 warn (_("Address table offset (%x) is less than TU offset (%x)\n"),
9487 address_table_offset, tu_list_offset);
9488 return 0;
9489 }
9490
5bbdf3d5 9491 tu_list_elements = (address_table_offset - tu_list_offset) / 8;
53774b7e
NC
9492
9493 /* PR 17531: file: 18a47d3d. */
9494 if (symbol_table_offset < address_table_offset)
9495 {
13bace4a 9496 warn (_("Symbol table offset (%x) is less then Address table offset (%x)\n"),
53774b7e
NC
9497 symbol_table_offset, address_table_offset);
9498 return 0;
9499 }
9500
5bbdf3d5 9501 address_table_size = symbol_table_offset - address_table_offset;
53774b7e
NC
9502
9503 if (constant_pool_offset < symbol_table_offset)
9504 {
9505 warn (_("Constant pool offset (%x) is less than symbol table offset (%x)\n"),
9506 constant_pool_offset, symbol_table_offset);
9507 return 0;
9508 }
9509
5bbdf3d5
DE
9510 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
9511
9512 cu_list = start + cu_list_offset;
9513 tu_list = start + tu_list_offset;
9514 address_table = start + address_table_offset;
9515 symbol_table = start + symbol_table_offset;
9516 constant_pool = start + constant_pool_offset;
9517
28d909e5 9518 if (address_table + address_table_size > section->start + section->size)
acff9664 9519 {
1306a742 9520 warn (_("Address table extends beyond end of section.\n"));
acff9664
NC
9521 return 0;
9522 }
b4eb7656 9523
5bbdf3d5
DE
9524 printf (_("\nCU table:\n"));
9525 for (i = 0; i < cu_list_elements; i += 2)
9526 {
9527 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
9528 uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
9529
9530 printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
9531 (unsigned long) cu_offset,
9532 (unsigned long) (cu_offset + cu_length - 1));
9533 }
9534
9535 printf (_("\nTU table:\n"));
9536 for (i = 0; i < tu_list_elements; i += 3)
9537 {
9538 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
9539 uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
9540 uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
9541
9542 printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
9543 (unsigned long) tu_offset,
9544 (unsigned long) type_offset);
9545 print_dwarf_vma (signature, 8);
9546 printf ("\n");
9547 }
9548
9549 printf (_("\nAddress table:\n"));
acff9664
NC
9550 for (i = 0; i < address_table_size && i <= address_table_size - (2 * 8 + 4);
9551 i += 2 * 8 + 4)
5bbdf3d5
DE
9552 {
9553 uint64_t low = byte_get_little_endian (address_table + i, 8);
9554 uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
9555 uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
9556
9557 print_dwarf_vma (low, 8);
9558 print_dwarf_vma (high, 8);
da88a764 9559 printf (_("%lu\n"), (unsigned long) cu_index);
5bbdf3d5
DE
9560 }
9561
9562 printf (_("\nSymbol table:\n"));
9563 for (i = 0; i < symbol_table_slots; ++i)
9564 {
9565 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
9566 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
9567 uint32_t num_cus, cu;
9568
9569 if (name_offset != 0
9570 || cu_vector_offset != 0)
9571 {
9572 unsigned int j;
362beea4 9573 unsigned char * adr;
5bbdf3d5 9574
362beea4 9575 adr = constant_pool + name_offset;
53774b7e 9576 /* PR 17531: file: 5b7b07ad. */
362beea4 9577 if (adr < constant_pool || adr >= section->start + section->size)
53774b7e
NC
9578 {
9579 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
9580 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
9581 name_offset, i);
9582 }
9583 else
acff9664
NC
9584 printf ("[%3u] %.*s:", i,
9585 (int) (section->size - (constant_pool_offset + name_offset)),
9586 constant_pool + name_offset);
53774b7e 9587
362beea4
NC
9588 adr = constant_pool + cu_vector_offset;
9589 if (adr < constant_pool || adr >= section->start + section->size - 3)
53774b7e
NC
9590 {
9591 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
9592 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
9593 cu_vector_offset, i);
9594 continue;
9595 }
57028622 9596
362beea4 9597 num_cus = byte_get_little_endian (adr, 4);
53774b7e 9598
362beea4 9599 adr = constant_pool + cu_vector_offset + 4 + num_cus * 4;
acff9664 9600 if (num_cus * 4 < num_cus
362beea4
NC
9601 || adr >= section->start + section->size
9602 || adr < constant_pool)
53774b7e
NC
9603 {
9604 printf ("<invalid number of CUs: %d>\n", num_cus);
acff9664 9605 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
53774b7e
NC
9606 num_cus, i);
9607 continue;
9608 }
9609
8d6eee87
TT
9610 if (num_cus > 1)
9611 printf ("\n");
f3853b34 9612
5bbdf3d5
DE
9613 for (j = 0; j < num_cus; ++j)
9614 {
7c1cef97 9615 int is_static;
8d6eee87
TT
9616 gdb_index_symbol_kind kind;
9617
5bbdf3d5 9618 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
7c1cef97 9619 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
8d6eee87
TT
9620 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
9621 cu = GDB_INDEX_CU_VALUE (cu);
5bbdf3d5 9622 /* Convert to TU number if it's for a type unit. */
ad6b52dd 9623 if (cu >= cu_list_elements / 2)
8d6eee87
TT
9624 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
9625 (unsigned long) (cu - cu_list_elements / 2));
5bbdf3d5 9626 else
8d6eee87
TT
9627 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
9628
459d52c8
DE
9629 printf (" [%s, %s]",
9630 is_static ? _("static") : _("global"),
9631 get_gdb_index_symbol_kind_name (kind));
8d6eee87
TT
9632 if (num_cus > 1)
9633 printf ("\n");
5bbdf3d5 9634 }
8d6eee87
TT
9635 if (num_cus <= 1)
9636 printf ("\n");
5bbdf3d5
DE
9637 }
9638 }
9639
9640 return 1;
9641}
9642
657d0d47
CC
9643/* Pre-allocate enough space for the CU/TU sets needed. */
9644
9645static void
9646prealloc_cu_tu_list (unsigned int nshndx)
9647{
9648 if (shndx_pool == NULL)
9649 {
9650 shndx_pool_size = nshndx;
9651 shndx_pool_used = 0;
9652 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
9653 sizeof (unsigned int));
9654 }
9655 else
9656 {
9657 shndx_pool_size = shndx_pool_used + nshndx;
9658 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
9659 sizeof (unsigned int));
9660 }
9661}
9662
9663static void
9664add_shndx_to_cu_tu_entry (unsigned int shndx)
9665{
9666 if (shndx_pool_used >= shndx_pool_size)
9667 {
9668 error (_("Internal error: out of space in the shndx pool.\n"));
9669 return;
9670 }
9671 shndx_pool [shndx_pool_used++] = shndx;
9672}
9673
9674static void
9675end_cu_tu_entry (void)
9676{
9677 if (shndx_pool_used >= shndx_pool_size)
9678 {
9679 error (_("Internal error: out of space in the shndx pool.\n"));
9680 return;
9681 }
9682 shndx_pool [shndx_pool_used++] = 0;
9683}
9684
341f9135
CC
9685/* Return the short name of a DWARF section given by a DW_SECT enumerator. */
9686
9687static const char *
9688get_DW_SECT_short_name (unsigned int dw_sect)
9689{
9690 static char buf[16];
9691
9692 switch (dw_sect)
9693 {
9694 case DW_SECT_INFO:
9695 return "info";
9696 case DW_SECT_TYPES:
9697 return "types";
9698 case DW_SECT_ABBREV:
9699 return "abbrev";
9700 case DW_SECT_LINE:
9701 return "line";
9702 case DW_SECT_LOC:
9703 return "loc";
9704 case DW_SECT_STR_OFFSETS:
9705 return "str_off";
9706 case DW_SECT_MACINFO:
9707 return "macinfo";
9708 case DW_SECT_MACRO:
9709 return "macro";
9710 default:
b4eb7656 9711 break;
341f9135
CC
9712 }
9713
9714 snprintf (buf, sizeof (buf), "%d", dw_sect);
9715 return buf;
9716}
9717
9718/* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
9719 These sections are extensions for Fission.
9720 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
657d0d47
CC
9721
9722static int
9723process_cu_tu_index (struct dwarf_section *section, int do_display)
9724{
9725 unsigned char *phdr = section->start;
9726 unsigned char *limit = phdr + section->size;
9727 unsigned char *phash;
9728 unsigned char *pindex;
9729 unsigned char *ppool;
9730 unsigned int version;
341f9135 9731 unsigned int ncols = 0;
657d0d47
CC
9732 unsigned int nused;
9733 unsigned int nslots;
9734 unsigned int i;
341f9135
CC
9735 unsigned int j;
9736 dwarf_vma signature_high;
9737 dwarf_vma signature_low;
9738 char buf[64];
657d0d47 9739
6937bb54
NC
9740 /* PR 17512: file: 002-168123-0.004. */
9741 if (phdr == NULL)
9742 {
9743 warn (_("Section %s is empty\n"), section->name);
9744 return 0;
9745 }
9746 /* PR 17512: file: 002-376-0.004. */
9747 if (section->size < 24)
9748 {
72c61a0d 9749 warn (_("Section %s is too small to contain a CU/TU header\n"),
6937bb54
NC
9750 section->name);
9751 return 0;
9752 }
9753
9754 SAFE_BYTE_GET (version, phdr, 4, limit);
341f9135 9755 if (version >= 2)
6937bb54
NC
9756 SAFE_BYTE_GET (ncols, phdr + 4, 4, limit);
9757 SAFE_BYTE_GET (nused, phdr + 8, 4, limit);
9758 SAFE_BYTE_GET (nslots, phdr + 12, 4, limit);
9759
657d0d47 9760 phash = phdr + 16;
8e2e3c6c
AM
9761 pindex = phash + (size_t) nslots * 8;
9762 ppool = pindex + (size_t) nslots * 4;
57028622 9763
657d0d47
CC
9764 if (do_display)
9765 {
dda8d76d
NC
9766 introduce (section, FALSE);
9767
8e2e3c6c 9768 printf (_(" Version: %u\n"), version);
341f9135 9769 if (version >= 2)
8e2e3c6c
AM
9770 printf (_(" Number of columns: %u\n"), ncols);
9771 printf (_(" Number of used entries: %u\n"), nused);
9772 printf (_(" Number of slots: %u\n\n"), nslots);
657d0d47
CC
9773 }
9774
8e2e3c6c
AM
9775 /* PR 17531: file: 45d69832. */
9776 if ((size_t) nslots * 8 / 8 != nslots
9777 || phash < phdr || phash > limit
9778 || pindex < phash || pindex > limit
9779 || ppool < pindex || ppool > limit)
657d0d47 9780 {
8e2e3c6c
AM
9781 warn (ngettext ("Section %s is too small for %u slot\n",
9782 "Section %s is too small for %u slots\n",
9783 nslots),
657d0d47
CC
9784 section->name, nslots);
9785 return 0;
9786 }
9787
341f9135 9788 if (version == 1)
657d0d47 9789 {
341f9135
CC
9790 if (!do_display)
9791 prealloc_cu_tu_list ((limit - ppool) / 4);
9792 for (i = 0; i < nslots; i++)
657d0d47 9793 {
341f9135
CC
9794 unsigned char *shndx_list;
9795 unsigned int shndx;
9796
6937bb54 9797 SAFE_BYTE_GET64 (phash, &signature_high, &signature_low, limit);
341f9135 9798 if (signature_high != 0 || signature_low != 0)
657d0d47 9799 {
6937bb54 9800 SAFE_BYTE_GET (j, pindex, 4, limit);
341f9135 9801 shndx_list = ppool + j * 4;
f3853b34
NC
9802 /* PR 17531: file: 705e010d. */
9803 if (shndx_list < ppool)
9804 {
9805 warn (_("Section index pool located before start of section\n"));
9806 return 0;
9807 }
9808
341f9135
CC
9809 if (do_display)
9810 printf (_(" [%3d] Signature: 0x%s Sections: "),
9811 i, dwarf_vmatoa64 (signature_high, signature_low,
9812 buf, sizeof (buf)));
9813 for (;;)
657d0d47 9814 {
341f9135
CC
9815 if (shndx_list >= limit)
9816 {
9817 warn (_("Section %s too small for shndx pool\n"),
9818 section->name);
9819 return 0;
9820 }
6937bb54 9821 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
341f9135
CC
9822 if (shndx == 0)
9823 break;
9824 if (do_display)
9825 printf (" %d", shndx);
9826 else
9827 add_shndx_to_cu_tu_entry (shndx);
9828 shndx_list += 4;
657d0d47 9829 }
657d0d47 9830 if (do_display)
341f9135 9831 printf ("\n");
657d0d47 9832 else
341f9135
CC
9833 end_cu_tu_entry ();
9834 }
9835 phash += 8;
9836 pindex += 4;
9837 }
9838 }
9839 else if (version == 2)
9840 {
9841 unsigned int val;
9842 unsigned int dw_sect;
9843 unsigned char *ph = phash;
9844 unsigned char *pi = pindex;
8e2e3c6c
AM
9845 unsigned char *poffsets = ppool + (size_t) ncols * 4;
9846 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
9847 unsigned char *pend = psizes + (size_t) nused * ncols * 4;
341f9135
CC
9848 bfd_boolean is_tu_index;
9849 struct cu_tu_set *this_set = NULL;
9850 unsigned int row;
9851 unsigned char *prow;
9852
9853 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
9854
362beea4 9855 /* PR 17531: file: 0dd159bf.
8e2e3c6c
AM
9856 Check for integer overflow (can occur when size_t is 32-bit)
9857 with overlarge ncols or nused values. */
4ac948a0
NC
9858 if (ncols > 0
9859 && ((size_t) ncols * 4 / 4 != ncols
9860 || (size_t) nused * ncols * 4 / ((size_t) ncols * 4) != nused
9861 || poffsets < ppool || poffsets > limit
9862 || psizes < poffsets || psizes > limit
9863 || pend < psizes || pend > limit))
341f9135
CC
9864 {
9865 warn (_("Section %s too small for offset and size tables\n"),
9866 section->name);
9867 return 0;
9868 }
9869
9870 if (do_display)
9871 {
9872 printf (_(" Offset table\n"));
9873 printf (" slot %-16s ",
9874 is_tu_index ? _("signature") : _("dwo_id"));
9875 }
9876 else
9877 {
9878 if (is_tu_index)
9879 {
9880 tu_count = nused;
72c61a0d 9881 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
341f9135 9882 this_set = tu_sets;
657d0d47 9883 }
657d0d47 9884 else
341f9135
CC
9885 {
9886 cu_count = nused;
72c61a0d 9887 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
341f9135
CC
9888 this_set = cu_sets;
9889 }
9890 }
6937bb54 9891
341f9135
CC
9892 if (do_display)
9893 {
9894 for (j = 0; j < ncols; j++)
9895 {
6937bb54 9896 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
341f9135
CC
9897 printf (" %8s", get_DW_SECT_short_name (dw_sect));
9898 }
9899 printf ("\n");
9900 }
6937bb54 9901
341f9135
CC
9902 for (i = 0; i < nslots; i++)
9903 {
6937bb54
NC
9904 SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
9905
9906 SAFE_BYTE_GET (row, pi, 4, limit);
341f9135
CC
9907 if (row != 0)
9908 {
591f7597 9909 /* PR 17531: file: a05f6ab3. */
ef77750e 9910 if (row > nused)
591f7597
NC
9911 {
9912 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
9913 row, nused);
9914 return 0;
9915 }
9916
341f9135 9917 if (!do_display)
6aea08d9
NC
9918 {
9919 size_t num_copy = sizeof (uint64_t);
9920
9921 /* PR 23064: Beware of buffer overflow. */
9922 if (ph + num_copy < limit)
9923 memcpy (&this_set[row - 1].signature, ph, num_copy);
9924 else
9925 {
9926 warn (_("Signature (%p) extends beyond end of space in section\n"), ph);
9927 return 0;
9928 }
9929 }
6937bb54 9930
341f9135 9931 prow = poffsets + (row - 1) * ncols * 4;
ffc0f143
NC
9932 /* PR 17531: file: b8ce60a8. */
9933 if (prow < poffsets || prow > limit)
9934 {
9935 warn (_("Row index (%u) * num columns (%u) > space remaining in section\n"),
9936 row, ncols);
9937 return 0;
9938 }
3aade688 9939
341f9135
CC
9940 if (do_display)
9941 printf (_(" [%3d] 0x%s"),
9942 i, dwarf_vmatoa64 (signature_high, signature_low,
9943 buf, sizeof (buf)));
9944 for (j = 0; j < ncols; j++)
9945 {
6937bb54 9946 SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
341f9135
CC
9947 if (do_display)
9948 printf (" %8d", val);
9949 else
9950 {
6937bb54 9951 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
82b1b41b
NC
9952
9953 /* PR 17531: file: 10796eb3. */
9954 if (dw_sect >= DW_SECT_MAX)
9955 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
9956 else
9957 this_set [row - 1].section_offsets [dw_sect] = val;
341f9135
CC
9958 }
9959 }
6937bb54 9960
341f9135
CC
9961 if (do_display)
9962 printf ("\n");
9963 }
9964 ph += 8;
9965 pi += 4;
9966 }
9967
9968 ph = phash;
9969 pi = pindex;
9970 if (do_display)
b4eb7656 9971 {
341f9135
CC
9972 printf ("\n");
9973 printf (_(" Size table\n"));
9974 printf (" slot %-16s ",
9975 is_tu_index ? _("signature") : _("dwo_id"));
b4eb7656 9976 }
6937bb54 9977
341f9135
CC
9978 for (j = 0; j < ncols; j++)
9979 {
6937bb54 9980 SAFE_BYTE_GET (val, ppool + j * 4, 4, limit);
341f9135
CC
9981 if (do_display)
9982 printf (" %8s", get_DW_SECT_short_name (val));
9983 }
6937bb54 9984
341f9135
CC
9985 if (do_display)
9986 printf ("\n");
6937bb54 9987
341f9135
CC
9988 for (i = 0; i < nslots; i++)
9989 {
6937bb54
NC
9990 SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
9991
9992 SAFE_BYTE_GET (row, pi, 4, limit);
341f9135
CC
9993 if (row != 0)
9994 {
9995 prow = psizes + (row - 1) * ncols * 4;
6937bb54 9996
341f9135
CC
9997 if (do_display)
9998 printf (_(" [%3d] 0x%s"),
9999 i, dwarf_vmatoa64 (signature_high, signature_low,
10000 buf, sizeof (buf)));
6937bb54 10001
341f9135
CC
10002 for (j = 0; j < ncols; j++)
10003 {
6937bb54 10004 SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
341f9135
CC
10005 if (do_display)
10006 printf (" %8d", val);
10007 else
10008 {
6937bb54 10009 SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
82b1b41b
NC
10010 if (dw_sect >= DW_SECT_MAX)
10011 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10012 else
341f9135
CC
10013 this_set [row - 1].section_sizes [dw_sect] = val;
10014 }
10015 }
6937bb54 10016
341f9135
CC
10017 if (do_display)
10018 printf ("\n");
10019 }
6937bb54 10020
341f9135
CC
10021 ph += 8;
10022 pi += 4;
657d0d47 10023 }
657d0d47 10024 }
341f9135 10025 else if (do_display)
6937bb54 10026 printf (_(" Unsupported version (%d)\n"), version);
657d0d47
CC
10027
10028 if (do_display)
10029 printf ("\n");
10030
10031 return 1;
10032}
10033
10034/* Load the CU and TU indexes if present. This will build a list of
10035 section sets that we can use to associate a .debug_info.dwo section
10036 with its associated .debug_abbrev.dwo section in a .dwp file. */
10037
43a444f9 10038static bfd_boolean
657d0d47
CC
10039load_cu_tu_indexes (void *file)
10040{
43a444f9
NC
10041 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
10042
657d0d47
CC
10043 /* If we have already loaded (or tried to load) the CU and TU indexes
10044 then do not bother to repeat the task. */
43a444f9
NC
10045 if (cu_tu_indexes_read == -1)
10046 {
10047 cu_tu_indexes_read = TRUE;
10048
dda8d76d 10049 if (load_debug_section_with_follow (dwp_cu_index, file))
43a444f9
NC
10050 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
10051 cu_tu_indexes_read = FALSE;
10052
dda8d76d 10053 if (load_debug_section_with_follow (dwp_tu_index, file))
43a444f9
NC
10054 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
10055 cu_tu_indexes_read = FALSE;
10056 }
657d0d47 10057
43a444f9 10058 return (bfd_boolean) cu_tu_indexes_read;
657d0d47
CC
10059}
10060
10061/* Find the set of sections that includes section SHNDX. */
10062
10063unsigned int *
10064find_cu_tu_set (void *file, unsigned int shndx)
10065{
10066 unsigned int i;
10067
43a444f9
NC
10068 if (! load_cu_tu_indexes (file))
10069 return NULL;
657d0d47
CC
10070
10071 /* Find SHNDX in the shndx pool. */
10072 for (i = 0; i < shndx_pool_used; i++)
10073 if (shndx_pool [i] == shndx)
10074 break;
10075
10076 if (i >= shndx_pool_used)
10077 return NULL;
10078
10079 /* Now backup to find the first entry in the set. */
10080 while (i > 0 && shndx_pool [i - 1] != 0)
10081 i--;
10082
10083 return shndx_pool + i;
10084}
10085
10086/* Display a .debug_cu_index or .debug_tu_index section. */
10087
10088static int
10089display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
10090{
10091 return process_cu_tu_index (section, 1);
10092}
10093
19e6b90e
L
10094static int
10095display_debug_not_supported (struct dwarf_section *section,
10096 void *file ATTRIBUTE_UNUSED)
10097{
10098 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
10099 section->name);
10100
10101 return 1;
10102}
10103
1306a742
NC
10104/* Like malloc, but takes two parameters like calloc.
10105 Verifies that the first parameter is not too large.
82b1b41b 10106 Note: does *not* initialise the allocated memory to zero. */
dda8d76d 10107
19e6b90e
L
10108void *
10109cmalloc (size_t nmemb, size_t size)
10110{
10111 /* Check for overflow. */
10112 if (nmemb >= ~(size_t) 0 / size)
10113 return NULL;
82b1b41b
NC
10114
10115 return xmalloc (nmemb * size);
19e6b90e
L
10116}
10117
1306a742
NC
10118/* Like xmalloc, but takes two parameters like calloc.
10119 Verifies that the first parameter is not too large.
10120 Note: does *not* initialise the allocated memory to zero. */
dda8d76d 10121
72c61a0d 10122void *
1306a742 10123xcmalloc (size_t nmemb, size_t size)
72c61a0d
NC
10124{
10125 /* Check for overflow. */
10126 if (nmemb >= ~(size_t) 0 / size)
8490fb40
NC
10127 {
10128 fprintf (stderr,
10129 _("Attempt to allocate an array with an excessive number of elements: 0x%lx\n"),
10130 (long) nmemb);
10131 xexit (1);
10132 }
72c61a0d 10133
1306a742 10134 return xmalloc (nmemb * size);
72c61a0d
NC
10135}
10136
1306a742
NC
10137/* Like xrealloc, but takes three parameters.
10138 Verifies that the second parameter is not too large.
10139 Note: does *not* initialise any new memory to zero. */
dda8d76d 10140
19e6b90e 10141void *
1306a742 10142xcrealloc (void *ptr, size_t nmemb, size_t size)
19e6b90e
L
10143{
10144 /* Check for overflow. */
10145 if (nmemb >= ~(size_t) 0 / size)
8490fb40 10146 {
dda8d76d
NC
10147 error (_("Attempt to re-allocate an array with an excessive number of elements: 0x%lx\n"),
10148 (long) nmemb);
8490fb40
NC
10149 xexit (1);
10150 }
82b1b41b 10151
1306a742 10152 return xrealloc (ptr, nmemb * size);
19e6b90e
L
10153}
10154
1306a742 10155/* Like xcalloc, but verifies that the first parameter is not too large. */
dda8d76d 10156
19e6b90e 10157void *
1306a742 10158xcalloc2 (size_t nmemb, size_t size)
19e6b90e
L
10159{
10160 /* Check for overflow. */
10161 if (nmemb >= ~(size_t) 0 / size)
8490fb40 10162 {
dda8d76d
NC
10163 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: 0x%lx\n"),
10164 (long) nmemb);
8490fb40
NC
10165 xexit (1);
10166 }
82b1b41b 10167
1306a742 10168 return xcalloc (nmemb, size);
19e6b90e
L
10169}
10170
dda8d76d
NC
10171static unsigned long
10172calc_gnu_debuglink_crc32 (unsigned long crc,
10173 const unsigned char * buf,
10174 bfd_size_type len)
10175{
10176 static const unsigned long crc32_table[256] =
10177 {
10178 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
10179 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
10180 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
10181 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
10182 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
10183 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
10184 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
10185 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
10186 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
10187 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
10188 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
10189 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
10190 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
10191 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
10192 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
10193 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
10194 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
10195 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
10196 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
10197 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
10198 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
10199 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
10200 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
10201 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
10202 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
10203 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
10204 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
10205 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
10206 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
10207 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
10208 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
10209 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
10210 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
10211 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
10212 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
10213 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
10214 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
10215 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
10216 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
10217 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
10218 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
10219 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
10220 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
10221 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
10222 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
10223 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
10224 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
10225 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
10226 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
10227 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
10228 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
10229 0x2d02ef8d
10230 };
10231 const unsigned char *end;
10232
10233 crc = ~crc & 0xffffffff;
10234 for (end = buf + len; buf < end; ++ buf)
10235 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
10236 return ~crc & 0xffffffff;
10237}
10238
10239typedef bfd_boolean (* check_func_type) (const char *, void *);
10240typedef const char * (* parse_func_type) (struct dwarf_section *, void *);
10241
10242static bfd_boolean
10243check_gnu_debuglink (const char * pathname, void * crc_pointer)
10244{
10245 static unsigned char buffer [8 * 1024];
10246 FILE * f;
10247 bfd_size_type count;
10248 unsigned long crc = 0;
10249 void * sep_data;
10250
10251 sep_data = open_debug_file (pathname);
10252 if (sep_data == NULL)
10253 return FALSE;
10254
10255 /* Yes - we are opening the file twice... */
10256 f = fopen (pathname, "rb");
10257 if (f == NULL)
10258 {
10259 /* Paranoia: This should never happen. */
10260 close_debug_file (sep_data);
10261 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
10262 return FALSE;
10263 }
10264
10265 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
10266 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
10267
10268 fclose (f);
10269
10270 if (crc != * (unsigned long *) crc_pointer)
10271 {
10272 close_debug_file (sep_data);
10273 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
10274 pathname);
10275 return FALSE;
10276 }
10277
10278 return TRUE;
10279}
10280
10281static const char *
10282parse_gnu_debuglink (struct dwarf_section * section, void * data)
10283{
10284 const char * name;
10285 unsigned int crc_offset;
10286 unsigned long * crc32 = (unsigned long *) data;
10287
10288 /* The name is first.
10289 The CRC value is stored after the filename, aligned up to 4 bytes. */
10290 name = (const char *) section->start;
10291
39f0547e 10292
dda8d76d
NC
10293 crc_offset = strnlen (name, section->size) + 1;
10294 crc_offset = (crc_offset + 3) & ~3;
10295 if (crc_offset + 4 > section->size)
10296 return NULL;
10297
10298 * crc32 = byte_get (section->start + crc_offset, 4);
10299 return name;
10300}
10301
10302static bfd_boolean
10303check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
10304{
10305 void * sep_data = open_debug_file (filename);
10306
10307 if (sep_data == NULL)
10308 return FALSE;
10309
10310 /* FIXME: We should now extract the build-id in the separate file
10311 and check it... */
10312
10313 return TRUE;
10314}
10315
10316typedef struct build_id_data
10317{
10318 bfd_size_type len;
10319 const unsigned char * data;
10320} Build_id_data;
10321
10322static const char *
10323parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
10324{
10325 const char * name;
10326 bfd_size_type namelen;
10327 bfd_size_type id_len;
10328 Build_id_data * build_id_data;
10329
10330 /* The name is first.
10331 The build-id follows immediately, with no padding, up to the section's end. */
10332
10333 name = (const char *) section->start;
10334 namelen = strnlen (name, section->size) + 1;
10335 if (namelen >= section->size)
10336 return NULL;
10337
10338 id_len = section->size - namelen;
10339 if (id_len < 0x14)
10340 return NULL;
10341
10342 build_id_data = calloc (1, sizeof * build_id_data);
10343 if (build_id_data == NULL)
10344 return NULL;
10345
10346 build_id_data->len = id_len;
10347 build_id_data->data = section->start + namelen;
10348
10349 * (Build_id_data **) data = build_id_data;
10350
10351 return name;
10352}
10353
24841daa
NC
10354static void
10355add_separate_debug_file (const char * filename, void * handle)
10356{
10357 separate_info * i = xmalloc (sizeof * i);
10358
10359 i->filename = filename;
10360 i->handle = handle;
10361 i->next = first_separate_info;
10362 first_separate_info = i;
10363}
10364
301a9420
AM
10365#if HAVE_LIBDEBUGINFOD
10366/* Query debuginfod servers for the target debuglink or debugaltlink
10367 file. If successful, store the path of the file in filename and
10368 return TRUE, otherwise return FALSE. */
10369
10370static bfd_boolean
10371debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
10372 char ** filename,
10373 void * file)
10374{
10375 size_t build_id_len;
10376 unsigned char * build_id;
10377
10378 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
10379 {
10380 /* Get the build-id of file. */
10381 build_id = get_build_id (file);
10382 build_id_len = 0;
10383 }
10384 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
10385 {
10386 /* Get the build-id of the debugaltlink file. */
10387 unsigned int filelen;
10388
10389 filelen = strnlen ((const char *)section->start, section->size);
10390 if (filelen == section->size)
10391 /* Corrupt debugaltlink. */
10392 return FALSE;
10393
10394 build_id = section->start + filelen + 1;
10395 build_id_len = section->size - (filelen + 1);
10396
10397 if (build_id_len == 0)
10398 return FALSE;
10399 }
10400 else
10401 return FALSE;
10402
10403 if (build_id)
10404 {
10405 int fd;
10406 debuginfod_client * client;
10407
10408 client = debuginfod_begin ();
10409 if (client == NULL)
10410 return FALSE;
10411
10412 /* Query debuginfod servers for the target file. If found its path
10413 will be stored in filename. */
10414 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
10415 debuginfod_end (client);
10416
10417 /* Only free build_id if we allocated space for a hex string
10418 in get_build_id (). */
10419 if (build_id_len == 0)
10420 free (build_id);
10421
10422 if (fd >= 0)
10423 {
10424 /* File successfully retrieved. Close fd since we want to
10425 use open_debug_file () on filename instead. */
10426 close (fd);
10427 return TRUE;
10428 }
10429 }
10430
10431 return FALSE;
10432}
10433#endif
10434
dda8d76d
NC
10435static void *
10436load_separate_debug_info (const char * main_filename,
2b63c337 10437 struct dwarf_section * xlink,
dda8d76d
NC
10438 parse_func_type parse_func,
10439 check_func_type check_func,
301a9420
AM
10440 void * func_data,
10441 void * file ATTRIBUTE_UNUSED)
dda8d76d
NC
10442{
10443 const char * separate_filename;
24841daa 10444 char * debug_filename;
dda8d76d
NC
10445 char * canon_dir;
10446 size_t canon_dirlen;
10447 size_t dirlen;
10448
2b63c337 10449 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
dda8d76d
NC
10450 {
10451 warn (_("Corrupt debuglink section: %s\n"),
2b63c337 10452 xlink->name ? xlink->name : xlink->uncompressed_name);
dda8d76d
NC
10453 return FALSE;
10454 }
10455
10456 /* Attempt to locate the separate file.
10457 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
10458
10459 canon_dir = lrealpath (main_filename);
10460
10461 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
10462 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
10463 break;
10464 canon_dir[canon_dirlen] = '\0';
10465
10466#ifndef DEBUGDIR
10467#define DEBUGDIR "/lib/debug"
10468#endif
10469#ifndef EXTRA_DEBUG_ROOT1
10470#define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
10471#endif
10472#ifndef EXTRA_DEBUG_ROOT2
10473#define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
10474#endif
10475
24841daa
NC
10476 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
10477 + canon_dirlen
10478 + strlen (".debug/")
dda8d76d 10479#ifdef EXTRA_DEBUG_ROOT1
24841daa 10480 + strlen (EXTRA_DEBUG_ROOT1)
dda8d76d
NC
10481#endif
10482#ifdef EXTRA_DEBUG_ROOT2
24841daa 10483 + strlen (EXTRA_DEBUG_ROOT2)
dda8d76d 10484#endif
24841daa
NC
10485 + strlen (separate_filename)
10486 + 1);
10487 if (debug_filename == NULL)
dda8d76d
NC
10488 {
10489 warn (_("Out of memory"));
3391569f 10490 free (canon_dir);
dda8d76d
NC
10491 return NULL;
10492 }
10493
10494 /* First try in the current directory. */
24841daa
NC
10495 sprintf (debug_filename, "%s", separate_filename);
10496 if (check_func (debug_filename, func_data))
dda8d76d
NC
10497 goto found;
10498
10499 /* Then try in a subdirectory called .debug. */
24841daa
NC
10500 sprintf (debug_filename, ".debug/%s", separate_filename);
10501 if (check_func (debug_filename, func_data))
dda8d76d
NC
10502 goto found;
10503
10504 /* Then try in the same directory as the original file. */
24841daa
NC
10505 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
10506 if (check_func (debug_filename, func_data))
dda8d76d
NC
10507 goto found;
10508
10509 /* And the .debug subdirectory of that directory. */
24841daa
NC
10510 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
10511 if (check_func (debug_filename, func_data))
dda8d76d
NC
10512 goto found;
10513
10514#ifdef EXTRA_DEBUG_ROOT1
10515 /* Try the first extra debug file root. */
24841daa
NC
10516 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
10517 if (check_func (debug_filename, func_data))
dda8d76d 10518 goto found;
39f0547e
NC
10519
10520 /* Try the first extra debug file root. */
10521 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
10522 if (check_func (debug_filename, func_data))
10523 goto found;
dda8d76d
NC
10524#endif
10525
10526#ifdef EXTRA_DEBUG_ROOT2
10527 /* Try the second extra debug file root. */
24841daa
NC
10528 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
10529 if (check_func (debug_filename, func_data))
dda8d76d
NC
10530 goto found;
10531#endif
10532
24841daa
NC
10533 /* Then try in the global debug_filename directory. */
10534 strcpy (debug_filename, DEBUGDIR);
dda8d76d
NC
10535 dirlen = strlen (DEBUGDIR) - 1;
10536 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
24841daa
NC
10537 strcat (debug_filename, "/");
10538 strcat (debug_filename, (const char *) separate_filename);
dda8d76d 10539
24841daa 10540 if (check_func (debug_filename, func_data))
dda8d76d
NC
10541 goto found;
10542
301a9420
AM
10543#if HAVE_LIBDEBUGINFOD
10544 {
10545 char * tmp_filename;
10546
10547 if (debuginfod_fetch_separate_debug_info (xlink,
10548 & tmp_filename,
10549 file))
10550 {
10551 /* File successfully downloaded from server, replace
10552 debug_filename with the file's path. */
10553 free (debug_filename);
10554 debug_filename = tmp_filename;
10555 goto found;
10556 }
10557 }
10558#endif
10559
dda8d76d
NC
10560 /* Failed to find the file. */
10561 warn (_("could not find separate debug file '%s'\n"), separate_filename);
24841daa 10562 warn (_("tried: %s\n"), debug_filename);
dda8d76d
NC
10563
10564#ifdef EXTRA_DEBUG_ROOT2
24841daa
NC
10565 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
10566 warn (_("tried: %s\n"), debug_filename);
dda8d76d
NC
10567#endif
10568
10569#ifdef EXTRA_DEBUG_ROOT1
39f0547e
NC
10570 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
10571 warn (_("tried: %s\n"), debug_filename);
10572
24841daa
NC
10573 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
10574 warn (_("tried: %s\n"), debug_filename);
dda8d76d
NC
10575#endif
10576
24841daa
NC
10577 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
10578 warn (_("tried: %s\n"), debug_filename);
dda8d76d 10579
24841daa
NC
10580 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
10581 warn (_("tried: %s\n"), debug_filename);
dda8d76d 10582
24841daa
NC
10583 sprintf (debug_filename, ".debug/%s", separate_filename);
10584 warn (_("tried: %s\n"), debug_filename);
dda8d76d 10585
24841daa
NC
10586 sprintf (debug_filename, "%s", separate_filename);
10587 warn (_("tried: %s\n"), debug_filename);
dda8d76d 10588
301a9420
AM
10589#if HAVE_LIBDEBUGINFOD
10590 {
10591 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
10592 if (urls == NULL)
10593 urls = "";
10594
10595 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
10596 }
10597#endif
10598
dda8d76d 10599 free (canon_dir);
24841daa 10600 free (debug_filename);
dda8d76d
NC
10601 return NULL;
10602
10603 found:
10604 free (canon_dir);
10605
24841daa
NC
10606 void * debug_handle;
10607
dda8d76d 10608 /* Now open the file.... */
24841daa 10609 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
dda8d76d 10610 {
24841daa
NC
10611 warn (_("failed to open separate debug file: %s\n"), debug_filename);
10612 free (debug_filename);
dda8d76d
NC
10613 return FALSE;
10614 }
10615
10616 /* FIXME: We do not check to see if there are any other separate debug info
10617 files that would also match. */
10618
24841daa
NC
10619 printf (_("%s: Found separate debug info file: %s\n\n"), main_filename, debug_filename);
10620 add_separate_debug_file (debug_filename, debug_handle);
dda8d76d 10621
24841daa 10622 /* Do not free debug_filename - it might be referenced inside
dda8d76d 10623 the structure returned by open_debug_file(). */
24841daa 10624 return debug_handle;
dda8d76d
NC
10625}
10626
d85bf2ba
NC
10627/* Attempt to load a separate dwarf object file. */
10628
10629static void *
24841daa 10630load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
d85bf2ba 10631{
24841daa
NC
10632 char * separate_filename;
10633 void * separate_handle;
d85bf2ba
NC
10634
10635 /* FIXME: Skip adding / if dwo_dir ends in /. */
24841daa
NC
10636 separate_filename = concat (dir, "/", name, NULL);
10637 if (separate_filename == NULL)
d85bf2ba
NC
10638 {
10639 warn (_("Out of memory allocating dwo filename\n"));
10640 return NULL;
10641 }
10642
24841daa 10643 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
d85bf2ba 10644 {
24841daa
NC
10645 warn (_("Unable to load dwo file: %s\n"), separate_filename);
10646 free (separate_filename);
d85bf2ba
NC
10647 return NULL;
10648 }
10649
10650 /* FIXME: We should check the dwo_id. */
10651
24841daa
NC
10652 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
10653 add_separate_debug_file (separate_filename, separate_handle);
10654 /* Note - separate_filename will be freed in free_debug_memory(). */
10655 return separate_handle;
d85bf2ba
NC
10656}
10657
24841daa
NC
10658/* Load the separate debug info file(s) attached to FILE, if any exist.
10659 Returns TRUE if any were found, FALSE otherwise.
10660 If TRUE is returned then the linked list starting at first_separate_info
10661 will be populated with open file handles. */
dda8d76d 10662
24841daa
NC
10663bfd_boolean
10664load_separate_debug_files (void * file, const char * filename)
dda8d76d 10665{
8de3a6e2
NC
10666 /* Skip this operation if we are not interested in debug links. */
10667 if (! do_follow_links && ! do_debug_links)
24841daa 10668 return FALSE;
8de3a6e2 10669
24841daa 10670 /* See if there are any dwo links. */
d85bf2ba
NC
10671 if (load_debug_section (str, file)
10672 && load_debug_section (abbrev, file)
10673 && load_debug_section (info, file))
10674 {
24841daa 10675 free_dwo_info ();
d85bf2ba
NC
10676
10677 if (process_debug_info (& debug_displays[info].section, file, abbrev, TRUE, FALSE))
10678 {
24841daa
NC
10679 bfd_boolean introduced = FALSE;
10680 dwo_info * dwinfo;
10681 const char * dir = NULL;
10682 const char * id = NULL;
10683
10684 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
d85bf2ba 10685 {
24841daa 10686 switch (dwinfo->type)
d85bf2ba 10687 {
24841daa
NC
10688 case DWO_NAME:
10689 if (do_debug_links)
10690 {
10691 if (! introduced)
10692 {
10693 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
10694 debug_displays [info].section.uncompressed_name);
10695 introduced = TRUE;
10696 }
d85bf2ba 10697
24841daa
NC
10698 printf (_(" Name: %s\n"), dwinfo->value);
10699 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
10700 if (id != NULL)
10701 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
10702 else
10703 printf (_(" ID: <unknown>\n"));
10704 printf ("\n\n");
10705 }
10706
10707 if (do_follow_links)
10708 load_dwo_file (filename, dwinfo->value, dir, id);
10709 break;
10710
10711 case DWO_DIR:
10712 dir = dwinfo->value;
10713 break;
10714
10715 case DWO_ID:
10716 id = dwinfo->value;
10717 break;
10718
10719 default:
10720 error (_("Unexpected DWO INFO type"));
10721 break;
10722 }
d85bf2ba
NC
10723 }
10724 }
10725 }
10726
dda8d76d 10727 if (! do_follow_links)
8de3a6e2
NC
10728 /* The other debug links will be displayed by display_debug_links()
10729 so we do not need to do any further processing here. */
24841daa 10730 return FALSE;
dda8d76d
NC
10731
10732 /* FIXME: We do not check for the presence of both link sections in the same file. */
10733 /* FIXME: We do not check the separate debug info file to see if it too contains debuglinks. */
10734 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
d85bf2ba 10735 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
dda8d76d 10736
dda8d76d
NC
10737 if (load_debug_section (gnu_debugaltlink, file))
10738 {
10739 Build_id_data * build_id_data;
10740
24841daa
NC
10741 load_separate_debug_info (filename,
10742 & debug_displays[gnu_debugaltlink].section,
10743 parse_gnu_debugaltlink,
10744 check_gnu_debugaltlink,
301a9420
AM
10745 & build_id_data,
10746 file);
dda8d76d
NC
10747 }
10748
10749 if (load_debug_section (gnu_debuglink, file))
10750 {
10751 unsigned long crc32;
10752
24841daa
NC
10753 load_separate_debug_info (filename,
10754 & debug_displays[gnu_debuglink].section,
10755 parse_gnu_debuglink,
10756 check_gnu_debuglink,
301a9420
AM
10757 & crc32,
10758 file);
dda8d76d
NC
10759 }
10760
24841daa
NC
10761 if (first_separate_info != NULL)
10762 return TRUE;
10763
dda8d76d 10764 do_follow_links = 0;
24841daa 10765 return FALSE;
dda8d76d
NC
10766}
10767
19e6b90e
L
10768void
10769free_debug_memory (void)
10770{
3f5e193b 10771 unsigned int i;
19e6b90e
L
10772
10773 free_abbrevs ();
10774
10775 for (i = 0; i < max; i++)
3f5e193b 10776 free_debug_section ((enum dwarf_section_display_enum) i);
19e6b90e 10777
cc86f28f 10778 if (debug_information != NULL)
19e6b90e 10779 {
cc86f28f 10780 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
19e6b90e 10781 {
cc86f28f 10782 for (i = 0; i < num_debug_info_entries; i++)
19e6b90e 10783 {
cc86f28f
NC
10784 if (!debug_information [i].max_loc_offsets)
10785 {
10786 free (debug_information [i].loc_offsets);
10787 free (debug_information [i].have_frame_base);
10788 }
10789 if (!debug_information [i].max_range_lists)
10790 free (debug_information [i].range_lists);
19e6b90e 10791 }
19e6b90e
L
10792 }
10793 free (debug_information);
10794 debug_information = NULL;
82b1b41b 10795 alloc_num_debug_info_entries = num_debug_info_entries = 0;
19e6b90e 10796 }
dda8d76d 10797
24841daa
NC
10798 separate_info * d;
10799 separate_info * next;
dda8d76d 10800
24841daa
NC
10801 for (d = first_separate_info; d != NULL; d = next)
10802 {
10803 close_debug_file (d->handle);
10804 free ((void *) d->filename);
10805 next = d->next;
10806 free ((void *) d);
dda8d76d 10807 }
24841daa
NC
10808 first_separate_info = NULL;
10809
10810 free_dwo_info ();
19e6b90e
L
10811}
10812
4cb93e3b
TG
10813void
10814dwarf_select_sections_by_names (const char *names)
10815{
10816 typedef struct
10817 {
10818 const char * option;
10819 int * variable;
f9f0e732 10820 int val;
4cb93e3b
TG
10821 }
10822 debug_dump_long_opts;
10823
10824 static const debug_dump_long_opts opts_table [] =
10825 {
10826 /* Please keep this table alpha- sorted. */
10827 { "Ranges", & do_debug_ranges, 1 },
10828 { "abbrev", & do_debug_abbrevs, 1 },
657d0d47 10829 { "addr", & do_debug_addr, 1 },
4cb93e3b 10830 { "aranges", & do_debug_aranges, 1 },
657d0d47
CC
10831 { "cu_index", & do_debug_cu_index, 1 },
10832 { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
dda8d76d 10833 { "follow-links", & do_follow_links, 1 },
4cb93e3b
TG
10834 { "frames", & do_debug_frames, 1 },
10835 { "frames-interp", & do_debug_frames_interp, 1 },
657d0d47
CC
10836 /* The special .gdb_index section. */
10837 { "gdb_index", & do_gdb_index, 1 },
4cb93e3b
TG
10838 { "info", & do_debug_info, 1 },
10839 { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility. */
dda8d76d 10840 { "links", & do_debug_links, 1 },
4cb93e3b
TG
10841 { "loc", & do_debug_loc, 1 },
10842 { "macro", & do_debug_macinfo, 1 },
10843 { "pubnames", & do_debug_pubnames, 1 },
357da287 10844 { "pubtypes", & do_debug_pubtypes, 1 },
222c2bf0 10845 /* This entry is for compatibility
4cb93e3b
TG
10846 with earlier versions of readelf. */
10847 { "ranges", & do_debug_aranges, 1 },
657d0d47 10848 { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
4cb93e3b 10849 { "str", & do_debug_str, 1 },
e4b7104b 10850 { "str-offsets", & do_debug_str_offsets, 1 },
6f875884
TG
10851 /* These trace_* sections are used by Itanium VMS. */
10852 { "trace_abbrev", & do_trace_abbrevs, 1 },
10853 { "trace_aranges", & do_trace_aranges, 1 },
10854 { "trace_info", & do_trace_info, 1 },
4cb93e3b
TG
10855 { NULL, NULL, 0 }
10856 };
10857
10858 const char *p;
467c65bc 10859
4cb93e3b
TG
10860 p = names;
10861 while (*p)
10862 {
10863 const debug_dump_long_opts * entry;
467c65bc 10864
4cb93e3b
TG
10865 for (entry = opts_table; entry->option; entry++)
10866 {
10867 size_t len = strlen (entry->option);
467c65bc 10868
4cb93e3b
TG
10869 if (strncmp (p, entry->option, len) == 0
10870 && (p[len] == ',' || p[len] == '\0'))
10871 {
10872 * entry->variable |= entry->val;
467c65bc 10873
4cb93e3b
TG
10874 /* The --debug-dump=frames-interp option also
10875 enables the --debug-dump=frames option. */
10876 if (do_debug_frames_interp)
10877 do_debug_frames = 1;
10878
10879 p += len;
10880 break;
10881 }
10882 }
467c65bc 10883
4cb93e3b
TG
10884 if (entry->option == NULL)
10885 {
10886 warn (_("Unrecognized debug option '%s'\n"), p);
10887 p = strchr (p, ',');
10888 if (p == NULL)
10889 break;
10890 }
467c65bc 10891
4cb93e3b
TG
10892 if (*p == ',')
10893 p++;
10894 }
10895}
10896
10897void
10898dwarf_select_sections_by_letters (const char *letters)
10899{
91d6fa6a 10900 unsigned int lindex = 0;
4cb93e3b 10901
91d6fa6a
NC
10902 while (letters[lindex])
10903 switch (letters[lindex++])
4cb93e3b 10904 {
dda8d76d
NC
10905 case 'A': do_debug_addr = 1; break;
10906 case 'a': do_debug_abbrevs = 1; break;
10907 case 'c': do_debug_cu_index = 1; break;
10908 case 'F': do_debug_frames_interp = 1; /* Fall through. */
10909 case 'f': do_debug_frames = 1; break;
10910 case 'g': do_gdb_index = 1; break;
10911 case 'i': do_debug_info = 1; break;
10912 case 'K': do_follow_links = 1; break;
10913 case 'k': do_debug_links = 1; break;
10914 case 'l': do_debug_lines |= FLAG_DEBUG_LINES_RAW; break;
10915 case 'L': do_debug_lines |= FLAG_DEBUG_LINES_DECODED; break;
10916 case 'm': do_debug_macinfo = 1; break;
e4b7104b 10917 case 'O': do_debug_str_offsets = 1; break;
dda8d76d
NC
10918 case 'o': do_debug_loc = 1; break;
10919 case 'p': do_debug_pubnames = 1; break;
10920 case 'R': do_debug_ranges = 1; break;
10921 case 'r': do_debug_aranges = 1; break;
10922 case 's': do_debug_str = 1; break;
10923 case 'T': do_trace_aranges = 1; break;
10924 case 't': do_debug_pubtypes = 1; break;
10925 case 'U': do_trace_info = 1; break;
10926 case 'u': do_trace_abbrevs = 1; break;
467c65bc 10927
4cb93e3b 10928 default:
7cc78d07 10929 warn (_("Unrecognized debug option '%s'\n"), letters);
4cb93e3b
TG
10930 break;
10931 }
10932}
10933
10934void
10935dwarf_select_sections_all (void)
10936{
10937 do_debug_info = 1;
10938 do_debug_abbrevs = 1;
10939 do_debug_lines = FLAG_DEBUG_LINES_RAW;
10940 do_debug_pubnames = 1;
f9f0e732 10941 do_debug_pubtypes = 1;
4cb93e3b
TG
10942 do_debug_aranges = 1;
10943 do_debug_ranges = 1;
10944 do_debug_frames = 1;
10945 do_debug_macinfo = 1;
10946 do_debug_str = 1;
10947 do_debug_loc = 1;
5bbdf3d5 10948 do_gdb_index = 1;
6f875884
TG
10949 do_trace_info = 1;
10950 do_trace_abbrevs = 1;
10951 do_trace_aranges = 1;
657d0d47
CC
10952 do_debug_addr = 1;
10953 do_debug_cu_index = 1;
dda8d76d
NC
10954 do_follow_links = 1;
10955 do_debug_links = 1;
e4b7104b 10956 do_debug_str_offsets = 1;
4cb93e3b
TG
10957}
10958
dda8d76d
NC
10959#define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0, NULL
10960#define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0, NULL
10961
10962/* N.B. The order here must match the order in section_display_enum. */
10963
19e6b90e
L
10964struct dwarf_section_display debug_displays[] =
10965{
dda8d76d
NC
10966 { { ".debug_abbrev", ".zdebug_abbrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, FALSE },
10967 { { ".debug_aranges", ".zdebug_aranges", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, TRUE },
10968 { { ".debug_frame", ".zdebug_frame", NO_ABBREVS }, display_debug_frames, &do_debug_frames, TRUE },
10969 { { ".debug_info", ".zdebug_info", ABBREV (abbrev)}, display_debug_info, &do_debug_info, TRUE },
10970 { { ".debug_line", ".zdebug_line", NO_ABBREVS }, display_debug_lines, &do_debug_lines, TRUE },
10971 { { ".debug_pubnames", ".zdebug_pubnames", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, FALSE },
10972 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, FALSE },
10973 { { ".eh_frame", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, TRUE },
10974 { { ".debug_macinfo", ".zdebug_macinfo", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, FALSE },
10975 { { ".debug_macro", ".zdebug_macro", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, TRUE },
10976 { { ".debug_str", ".zdebug_str", NO_ABBREVS }, display_debug_str, &do_debug_str, FALSE },
10977 { { ".debug_line_str", ".zdebug_line_str", NO_ABBREVS }, display_debug_str, &do_debug_str, FALSE },
10978 { { ".debug_loc", ".zdebug_loc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, TRUE },
10979 { { ".debug_loclists", ".zdebug_loclists", NO_ABBREVS }, display_debug_loc, &do_debug_loc, TRUE },
10980 { { ".debug_pubtypes", ".zdebug_pubtypes", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, FALSE },
10981 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, FALSE },
10982 { { ".debug_ranges", ".zdebug_ranges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, TRUE },
10983 { { ".debug_rnglists", ".zdebug_rnglists", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, TRUE },
10984 { { ".debug_static_func", ".zdebug_static_func", NO_ABBREVS }, display_debug_not_supported, NULL, FALSE },
10985 { { ".debug_static_vars", ".zdebug_static_vars", NO_ABBREVS }, display_debug_not_supported, NULL, FALSE },
10986 { { ".debug_types", ".zdebug_types", ABBREV (abbrev) }, display_debug_types, &do_debug_info, TRUE },
10987 { { ".debug_weaknames", ".zdebug_weaknames", NO_ABBREVS }, display_debug_not_supported, NULL, FALSE },
10988 { { ".gdb_index", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, FALSE },
10989 { { ".debug_names", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, FALSE },
10990 { { ".trace_info", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, TRUE },
10991 { { ".trace_abbrev", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, FALSE },
10992 { { ".trace_aranges", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, FALSE },
10993 { { ".debug_info.dwo", ".zdebug_info.dwo", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, TRUE },
10994 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, FALSE },
10995 { { ".debug_types.dwo", ".zdebug_types.dwo", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, TRUE },
10996 { { ".debug_line.dwo", ".zdebug_line.dwo", NO_ABBREVS }, display_debug_lines, &do_debug_lines, TRUE },
10997 { { ".debug_loc.dwo", ".zdebug_loc.dwo", NO_ABBREVS }, display_debug_loc, &do_debug_loc, TRUE },
10998 { { ".debug_macro.dwo", ".zdebug_macro.dwo", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, TRUE },
10999 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, FALSE },
11000 { { ".debug_str.dwo", ".zdebug_str.dwo", NO_ABBREVS }, display_debug_str, &do_debug_str, TRUE },
e4b7104b
NC
11001 { { ".debug_str_offsets", ".zdebug_str_offsets", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, TRUE },
11002 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, TRUE },
dda8d76d
NC
11003 { { ".debug_addr", ".zdebug_addr", NO_ABBREVS }, display_debug_addr, &do_debug_addr, TRUE },
11004 { { ".debug_cu_index", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, FALSE },
11005 { { ".debug_tu_index", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, FALSE },
11006 { { ".gnu_debuglink", "", NO_ABBREVS }, display_debug_links, &do_debug_links, FALSE },
11007 { { ".gnu_debugaltlink", "", NO_ABBREVS }, display_debug_links, &do_debug_links, FALSE },
11008 /* Separate debug info files can containt their own .debug_str section,
11009 and this might be in *addition* to a .debug_str section already present
11010 in the main file. Hence we need to have two entries for .debug_str. */
11011 { { ".debug_str", ".zdebug_str", NO_ABBREVS }, display_debug_str, &do_debug_str, FALSE },
19e6b90e 11012};
b451e98a
JK
11013
11014/* A static assertion. */
11015extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];