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