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