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