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