dwarf.c 188 KB
Newer Older
1
/* dwarf.c -- display DWARF contents of a BFD binary file
Nick Clifton's avatar
Nick Clifton committed
2
   Copyright 2005-2013 Free Software Foundation, Inc.
3 4 5 6 7

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9 10 11 12 13 14 15 16 17 18 19 20
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

Alan Modra's avatar
 
Alan Modra committed
21
#include "sysdep.h"
22
#include "libiberty.h"
Alan Modra's avatar
 
Alan Modra committed
23
#include "bfd.h"
24
#include "bfd_stdint.h"
Alan Modra's avatar
 
Alan Modra committed
25
#include "bucomm.h"
H.J. Lu's avatar
H.J. Lu committed
26
#include "elfcomm.h"
H.J. Lu's avatar
H.J. Lu committed
27
#include "elf/common.h"
Tom Tromey's avatar
bfd  
Tom Tromey committed
28
#include "dwarf2.h"
Alan Modra's avatar
 
Alan Modra committed
29
#include "dwarf.h"
Tom Tromey's avatar
Tom Tromey committed
30
#include "gdb/gdb-index.h"
31

Alan Modra's avatar
Alan Modra committed
32 33 34 35
#if !HAVE_DECL_STRNLEN
size_t strnlen (const char *, size_t);
#endif

Jan Kratochvil's avatar
Jan Kratochvil committed
36 37
static const char *regname (unsigned int regno, int row);

38 39 40 41 42 43 44 45
static int have_frame_base;
static int need_base_address;

static unsigned int last_pointer_size = 0;
static int warned_about_missing_comp_units = FALSE;

static unsigned int num_debug_info_entries = 0;
static debug_info *debug_information = NULL;
46 47 48
/* Special value for num_debug_info_entries to indicate
   that the .debug_info section could not be loaded/parsed.  */
#define DEBUG_INFO_UNAVAILABLE  (unsigned int) -1
49

H.J. Lu's avatar
H.J. Lu committed
50
int eh_addr_size;
51 52 53 54 55

int do_debug_info;
int do_debug_abbrevs;
int do_debug_lines;
int do_debug_pubnames;
Nick Clifton's avatar
Nick Clifton committed
56
int do_debug_pubtypes;
57 58 59 60 61 62 63
int do_debug_aranges;
int do_debug_ranges;
int do_debug_frames;
int do_debug_frames_interp;
int do_debug_macinfo;
int do_debug_str;
int do_debug_loc;
64
int do_gdb_index;
65 66 67
int do_trace_info;
int do_trace_abbrevs;
int do_trace_aranges;
Cary Coutant's avatar
Cary Coutant committed
68 69
int do_debug_addr;
int do_debug_cu_index;
70
int do_wide;
71

72 73 74
int dwarf_cutoff_level = -1;
unsigned long dwarf_start_die;

75 76
int dwarf_check = 0;

Cary Coutant's avatar
Cary Coutant committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
   sections.  For version 1 package files, each set is stored in SHNDX_POOL
   as a zero-terminated list of section indexes comprising one set of debug
   sections from a .dwo file.  */

static int cu_tu_indexes_read = 0;
static unsigned int *shndx_pool = NULL;
static unsigned int shndx_pool_size = 0;
static unsigned int shndx_pool_used = 0;

/* For version 2 package files, each set contains an array of section offsets
   and an array of section sizes, giving the offset and size of the
   contribution from a CU or TU within one of the debug sections.
   When displaying debug info from a package file, we need to use these
   tables to locate the corresponding contributions to each section.  */

struct cu_tu_set
{
  uint64_t signature;
  dwarf_vma section_offsets[DW_SECT_MAX];
  size_t section_sizes[DW_SECT_MAX];
};

static int cu_count = 0;
static int tu_count = 0;
static struct cu_tu_set *cu_sets = NULL;
static struct cu_tu_set *tu_sets = NULL;

static void load_cu_tu_indexes (void *file);

107 108 109 110
/* Values for do_debug_lines.  */
#define FLAG_DEBUG_LINES_RAW	 1
#define FLAG_DEBUG_LINES_DECODED 2

Richard Henderson's avatar
Richard Henderson committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124
static int
size_of_encoded_value (int encoding)
{
  switch (encoding & 0x7)
    {
    default:	/* ??? */
    case 0:	return eh_addr_size;
    case 2:	return 2;
    case 3:	return 4;
    case 4:	return 8;
    }
}

static dwarf_vma
125 126 127
get_encoded_value (unsigned char *data,
		   int encoding,
		   struct dwarf_section *section)
Richard Henderson's avatar
Richard Henderson committed
128 129
{
  int size = size_of_encoded_value (encoding);
130
  dwarf_vma val;
Richard Henderson's avatar
Richard Henderson committed
131 132

  if (encoding & DW_EH_PE_signed)
133
    val = byte_get_signed (data, size);
Richard Henderson's avatar
Richard Henderson committed
134
  else
135 136 137 138 139
    val = byte_get (data, size);

  if ((encoding & 0x70) == DW_EH_PE_pcrel)
    val += section->address + (data - section->start);
  return val;
Richard Henderson's avatar
Richard Henderson committed
140 141
}

142
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
143
#ifndef __MINGW32__
144 145
#define  DWARF_VMA_FMT       "ll"
#define  DWARF_VMA_FMT_LONG  "%16.16llx"
146
#else
147 148
#define  DWARF_VMA_FMT       "I64"
#define  DWARF_VMA_FMT_LONG  "%016I64x"
149
#endif
150
#else
151 152
#define  DWARF_VMA_FMT       "l"
#define  DWARF_VMA_FMT_LONG  "%16.16lx"
153 154
#endif

155 156 157 158 159
/* Convert a dwarf vma value into a string.  Returns a pointer to a static
   buffer containing the converted VALUE.  The value is converted according
   to the printf formating character FMTCH.  If NUM_BYTES is non-zero then
   it specifies the maximum number of bytes to be displayed in the converted
   value and FMTCH is ignored - hex is always used.  */
160

Kai Tietz's avatar
Kai Tietz committed
161
static const char *
162
dwarf_vmatoa_1 (const char *fmtch, dwarf_vma value, unsigned num_bytes)
Kai Tietz's avatar
Kai Tietz committed
163 164 165 166 167
{
  /* As dwarf_vmatoa is used more then once in a printf call
     for output, we are cycling through an fixed array of pointers
     for return address.  */
  static int buf_pos = 0;
168 169
  static struct dwarf_vmatoa_buf
  {
Kai Tietz's avatar
Kai Tietz committed
170 171 172 173 174
    char place[64];
  } buf[16];
  char *ret;

  ret = buf[buf_pos++].place;
175
  buf_pos %= ARRAY_SIZE (buf);
Kai Tietz's avatar
Kai Tietz committed
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  if (num_bytes)
    {
      /* Printf does not have a way of specifiying a maximum field width for an
	 integer value, so we print the full value into a buffer and then select
	 the precision we need.  */
      snprintf (ret, sizeof (buf[0].place), DWARF_VMA_FMT_LONG, value);
      if (num_bytes > 8)
	num_bytes = 8;
      return ret + (16 - 2 * num_bytes);
    }
  else
    {
      char fmt[32];

      sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
      snprintf (ret, sizeof (buf[0].place), fmt, value);
      return ret;
    }
}
Kai Tietz's avatar
Kai Tietz committed
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210
static inline const char *
dwarf_vmatoa (const char * fmtch, dwarf_vma value)
{
  return dwarf_vmatoa_1 (fmtch, value, 0);
}

/* Print a dwarf_vma value (typically an address, offset or length) in
   hexadecimal format, followed by a space.  The length of the VALUE (and
   hence the precision displayed) is determined by the NUM_BYTES parameter.  */

static void
print_dwarf_vma (dwarf_vma value, unsigned num_bytes)
{
  printf ("%s ", dwarf_vmatoa_1 (NULL, value, num_bytes));
Kai Tietz's avatar
Kai Tietz committed
211 212
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/* Format a 64-bit value, given as two 32-bit values, in hex.
   For reentrancy, this uses a buffer provided by the caller.  */

static const char *
dwarf_vmatoa64 (dwarf_vma hvalue, dwarf_vma lvalue, char *buf,
		unsigned int buf_len)
{
  int len = 0;

  if (hvalue == 0)
    snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", lvalue);
  else
    {
      len = snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", hvalue);
      snprintf (buf + len, buf_len - len,
		"%08" DWARF_VMA_FMT "x", lvalue);
    }

  return buf;
}

Nick Clifton's avatar
Nick Clifton committed
234 235 236 237 238
/* Read in a LEB128 encoded value starting at address DATA.
   If SIGN is true, return a signed LEB128 value.
   If LENGTH_RETURN is not NULL, return in it the number of bytes read.
   No bytes will be read at address END or beyond.  */

239
dwarf_vma
Nick Clifton's avatar
Nick Clifton committed
240 241 242 243
read_leb128 (unsigned char *data,
	     unsigned int *length_return,
	     bfd_boolean sign,
	     const unsigned char * const end)
244
{
245
  dwarf_vma result = 0;
246 247
  unsigned int num_read = 0;
  unsigned int shift = 0;
Nick Clifton's avatar
Nick Clifton committed
248
  unsigned char byte = 0;
249

Nick Clifton's avatar
Nick Clifton committed
250
  while (data < end)
251 252 253 254
    {
      byte = *data++;
      num_read++;

255
      result |= ((dwarf_vma) (byte & 0x7f)) << shift;
256 257

      shift += 7;
Nick Clifton's avatar
Nick Clifton committed
258 259
      if ((byte & 0x80) == 0)
	break;
260 261 262 263 264 265
    }

  if (length_return != NULL)
    *length_return = num_read;

  if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
266
    result |= -1UL << shift;
267 268 269 270

  return result;
}

271
/* Create a signed version to avoid painful typecasts.  */
Nick Clifton's avatar
Nick Clifton committed
272 273 274 275 276 277 278 279 280 281 282 283
static inline dwarf_signed_vma
read_sleb128 (unsigned char * data,
	      unsigned int *  length_return,
	      const unsigned char * const end)
{
  return (dwarf_signed_vma) read_leb128 (data, length_return, TRUE, end);
}

static inline dwarf_vma
read_uleb128 (unsigned char * data,
	      unsigned int *  length_return,
	      const unsigned char * const end)
284
{
Nick Clifton's avatar
Nick Clifton committed
285
  return read_leb128 (data, length_return, FALSE, end);
286 287
}

Nick Clifton's avatar
Nick Clifton committed
288 289 290
#define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END)	\
  do						\
    {						\
291
      int dummy [sizeof (VAL) < (AMOUNT) ? -1 : 1] ATTRIBUTE_UNUSED ; \
Nick Clifton's avatar
Nick Clifton committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
      unsigned int amount = (AMOUNT);		\
      if (((PTR) + amount) >= (END))		\
	{					\
	  if ((PTR) < (END))			\
	    amount = (END) - (PTR);		\
	  else					\
	    amount = 0;				\
	}					\
      if (amount)				\
	VAL = byte_get ((PTR), amount);		\
      else					\
	VAL = 0;				\
    }						\
  while (0)

#define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)	\
  do							\
    {							\
      SAFE_BYTE_GET (VAL, PTR, AMOUNT, END);		\
      PTR += AMOUNT;					\
    }							\
  while (0)

#define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END)	\
  do							\
    {							\
      unsigned int amount = (AMOUNT);			\
      if (((PTR) + amount) >= (END))			\
	{						\
	  if ((PTR) < (END))				\
	    amount = (END) - (PTR);			\
	  else						\
	    amount = 0;					\
	}						\
      if (amount)					\
	VAL = byte_get_signed ((PTR), amount);		\
      else						\
	VAL = 0;					\
    }							\
  while (0)

#define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)	\
  do								\
    {								\
      SAFE_SIGNED_BYTE_GET (VAL, PTR, AMOUNT, END);		\
      PTR += AMOUNT;						\
    }								\
  while (0)

#define SAFE_BYTE_GET64(PTR, HIGH, LOW, END)		\
  do							\
    {							\
Cary Coutant's avatar
Cary Coutant committed
344
      if (((PTR) + 8) <= (END))				\
Nick Clifton's avatar
Nick Clifton committed
345 346 347 348 349 350 351 352 353 354
	{						\
	  byte_get_64 ((PTR), (HIGH), (LOW));		\
	}						\
      else						\
	{						\
	  * (LOW) = * (HIGH) = 0;			\
	}						\
    }							\
  while (0)

355 356
typedef struct State_Machine_Registers
{
357
  dwarf_vma address;
358 359 360 361 362
  unsigned int file;
  unsigned int line;
  unsigned int column;
  int is_stmt;
  int basic_block;
Jakub Jelinek's avatar
Jakub Jelinek committed
363 364
  unsigned char op_index;
  unsigned char end_sequence;
365 366 367 368 369 370 371 372 373 374 375
/* This variable hold the number of the last entry seen
   in the File Table.  */
  unsigned int last_file_entry;
} SMR;

static SMR state_machine_regs;

static void
reset_state_machine (int is_stmt)
{
  state_machine_regs.address = 0;
Jakub Jelinek's avatar
Jakub Jelinek committed
376
  state_machine_regs.op_index = 0;
377 378 379 380 381 382 383 384 385 386 387 388 389
  state_machine_regs.file = 1;
  state_machine_regs.line = 1;
  state_machine_regs.column = 0;
  state_machine_regs.is_stmt = is_stmt;
  state_machine_regs.basic_block = 0;
  state_machine_regs.end_sequence = 0;
  state_machine_regs.last_file_entry = 0;
}

/* Handled an extend line op.
   Returns the number of bytes read.  */

static int
Nick Clifton's avatar
Nick Clifton committed
390 391 392
process_extended_line_op (unsigned char * data,
			  int is_stmt,
			  unsigned char * end)
393 394 395 396 397
{
  unsigned char op_code;
  unsigned int bytes_read;
  unsigned int len;
  unsigned char *name;
398
  unsigned char *orig_data = data;
Nick Clifton's avatar
Nick Clifton committed
399
  dwarf_vma adr;
400

Nick Clifton's avatar
Nick Clifton committed
401
  len = read_uleb128 (data, & bytes_read, end);
402 403
  data += bytes_read;

Nick Clifton's avatar
Nick Clifton committed
404
  if (len == 0 || data == end)
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    {
      warn (_("badly formed extended line op encountered!\n"));
      return bytes_read;
    }

  len += bytes_read;
  op_code = *data++;

  printf (_("  Extended opcode %d: "), op_code);

  switch (op_code)
    {
    case DW_LNE_end_sequence:
      printf (_("End of Sequence\n\n"));
      reset_state_machine (is_stmt);
      break;

    case DW_LNE_set_address:
Nick Clifton's avatar
Nick Clifton committed
423
      SAFE_BYTE_GET (adr, data, len - bytes_read - 1, end);
Kai Tietz's avatar
Kai Tietz committed
424
      printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
425
      state_machine_regs.address = adr;
Jakub Jelinek's avatar
Jakub Jelinek committed
426
      state_machine_regs.op_index = 0;
427 428 429
      break;

    case DW_LNE_define_file:
430
      printf (_("define new File Table entry\n"));
431
      printf (_("  Entry\tDir\tTime\tSize\tName\n"));
432
      printf ("   %d\t", ++state_machine_regs.last_file_entry);
Nick Clifton's avatar
Nick Clifton committed
433

434
      name = data;
Nick Clifton's avatar
Nick Clifton committed
435
      data += strnlen ((char *) data, end - data) + 1;
Nick Clifton's avatar
Nick Clifton committed
436
      printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
437
      data += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
438
      printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
439
      data += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
440
      printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
441
      data += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
442 443 444 445
      printf ("%s\n\n", name);

      if (((unsigned int) (data - orig_data) != len) || data == end)
        warn (_("DW_LNE_define_file: Bad opcode length\n"));
446 447
      break;

Cary Coutant's avatar
Cary Coutant committed
448
    case DW_LNE_set_discriminator:
Kai Tietz's avatar
Kai Tietz committed
449
      printf (_("set Discriminator to %s\n"),
Nick Clifton's avatar
Nick Clifton committed
450
	      dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
Cary Coutant's avatar
Cary Coutant committed
451 452
      break;

453 454
    /* HP extensions.  */
    case DW_LNE_HP_negate_is_UV_update:
Cary Coutant's avatar
Cary Coutant committed
455
      printf ("DW_LNE_HP_negate_is_UV_update\n");
456 457
      break;
    case DW_LNE_HP_push_context:
Cary Coutant's avatar
Cary Coutant committed
458
      printf ("DW_LNE_HP_push_context\n");
459 460
      break;
    case DW_LNE_HP_pop_context:
Cary Coutant's avatar
Cary Coutant committed
461
      printf ("DW_LNE_HP_pop_context\n");
462 463
      break;
    case DW_LNE_HP_set_file_line_column:
Cary Coutant's avatar
Cary Coutant committed
464
      printf ("DW_LNE_HP_set_file_line_column\n");
465 466
      break;
    case DW_LNE_HP_set_routine_name:
Cary Coutant's avatar
Cary Coutant committed
467
      printf ("DW_LNE_HP_set_routine_name\n");
468 469
      break;
    case DW_LNE_HP_set_sequence:
Cary Coutant's avatar
Cary Coutant committed
470
      printf ("DW_LNE_HP_set_sequence\n");
471 472
      break;
    case DW_LNE_HP_negate_post_semantics:
Cary Coutant's avatar
Cary Coutant committed
473
      printf ("DW_LNE_HP_negate_post_semantics\n");
474 475
      break;
    case DW_LNE_HP_negate_function_exit:
Cary Coutant's avatar
Cary Coutant committed
476
      printf ("DW_LNE_HP_negate_function_exit\n");
477 478
      break;
    case DW_LNE_HP_negate_front_end_logical:
Cary Coutant's avatar
Cary Coutant committed
479
      printf ("DW_LNE_HP_negate_front_end_logical\n");
480 481
      break;
    case DW_LNE_HP_define_proc:
Cary Coutant's avatar
Cary Coutant committed
482
      printf ("DW_LNE_HP_define_proc\n");
483
      break;
484 485 486 487 488 489 490 491 492 493
    case DW_LNE_HP_source_file_correlation:
      {
        unsigned char *edata = data + len - bytes_read - 1;

        printf ("DW_LNE_HP_source_file_correlation\n");

        while (data < edata)
          {
            unsigned int opc;

Nick Clifton's avatar
Nick Clifton committed
494
            opc = read_uleb128 (data, & bytes_read, edata);
495 496 497 498 499 500 501 502 503 504
            data += bytes_read;

            switch (opc)
              {
              case DW_LNE_HP_SFC_formfeed:
                printf ("    DW_LNE_HP_SFC_formfeed\n");
                break;
              case DW_LNE_HP_SFC_set_listing_line:
                printf ("    DW_LNE_HP_SFC_set_listing_line (%s)\n",
                        dwarf_vmatoa ("u",
Nick Clifton's avatar
Nick Clifton committed
505
                                      read_uleb128 (data, & bytes_read, edata)));
506 507 508 509
                data += bytes_read;
                break;
              case DW_LNE_HP_SFC_associate:
                printf ("    DW_LNE_HP_SFC_associate ");
510
                printf ("(%s",
511
                        dwarf_vmatoa ("u",
Nick Clifton's avatar
Nick Clifton committed
512
                                      read_uleb128 (data, & bytes_read, edata)));
513
                data += bytes_read;
514
                printf (",%s",
515
                        dwarf_vmatoa ("u",
Nick Clifton's avatar
Nick Clifton committed
516
                                      read_uleb128 (data, & bytes_read, edata)));
517
                data += bytes_read;
518
                printf (",%s)\n",
519
                        dwarf_vmatoa ("u",
Nick Clifton's avatar
Nick Clifton committed
520
                                      read_uleb128 (data, & bytes_read, edata)));
521 522 523
                data += bytes_read;
                break;
              default:
524
                printf (_("    UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
525 526 527 528 529 530
                data = edata;
                break;
              }
          }
      }
      break;
531

532
    default:
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
      {
        unsigned int rlen = len - bytes_read - 1;

        if (op_code >= DW_LNE_lo_user
            /* The test against DW_LNW_hi_user is redundant due to
               the limited range of the unsigned char data type used
               for op_code.  */
            /*&& op_code <= DW_LNE_hi_user*/)
          printf (_("user defined: "));
        else
          printf (_("UNKNOWN: "));
        printf (_("length %d ["), rlen);
        for (; rlen; rlen--)
          printf (" %02x", *data++);
        printf ("]\n");
      }
549 550 551 552 553 554
      break;
    }

  return len;
}

Nick Clifton's avatar
Nick Clifton committed
555
static const unsigned char *
556
fetch_indirect_string (dwarf_vma offset)
557 558 559 560
{
  struct dwarf_section *section = &debug_displays [str].section;

  if (section->start == NULL)
Nick Clifton's avatar
Nick Clifton committed
561
    return (const unsigned char *) _("<no .debug_str section>");
562

563 564
  /* DWARF sections under Mach-O have non-zero addresses.  */
  offset -= section->address;
565 566
  if (offset > section->size)
    {
567 568
      warn (_("DW_FORM_strp offset too big: %s\n"),
	    dwarf_vmatoa ("x", offset));
Nick Clifton's avatar
Nick Clifton committed
569
      return (const unsigned char *) _("<offset is too big>");
570 571
    }

Nick Clifton's avatar
Nick Clifton committed
572
  return (const unsigned char *) section->start + offset;
573 574
}

575
static const char *
Cary Coutant's avatar
Cary Coutant committed
576 577
fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
		      dwarf_vma offset_size, int dwo)
578 579 580 581 582 583 584 585 586 587 588 589 590 591
{
  enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
  enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
  struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
  struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
  dwarf_vma index_offset = idx * offset_size;
  dwarf_vma str_offset;

  if (index_section->start == NULL)
    return (dwo ? _("<no .debug_str_offsets.dwo section>")
		: _("<no .debug_str_offsets section>"));

  /* DWARF sections under Mach-O have non-zero addresses.  */
  index_offset -= index_section->address;
Cary Coutant's avatar
Cary Coutant committed
592 593
  if (this_set != NULL)
    index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
  if (index_offset > index_section->size)
    {
      warn (_("DW_FORM_GNU_str_index offset too big: %s\n"),
	    dwarf_vmatoa ("x", index_offset));
      return _("<index offset is too big>");
    }

  if (str_section->start == NULL)
    return (dwo ? _("<no .debug_str.dwo section>")
		: _("<no .debug_str section>"));

  str_offset = byte_get (index_section->start + index_offset, offset_size);
  str_offset -= str_section->address;
  if (str_offset > str_section->size)
    {
      warn (_("DW_FORM_GNU_str_index indirect offset too big: %s\n"),
	    dwarf_vmatoa ("x", str_offset));
      return _("<indirect index offset is too big>");
    }

  return (const char *) str_section->start + str_offset;
}

static const char *
fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
{
  struct dwarf_section *section = &debug_displays [debug_addr].section;

  if (section->start == NULL)
    return (_("<no .debug_addr section>"));

  if (offset + bytes > section->size)
    {
      warn (_("Offset into section %s too big: %s\n"),
            section->name, dwarf_vmatoa ("x", offset));
      return "<offset too big>";
    }

  return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
}


636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/* FIXME:  There are better and more efficient ways to handle
   these structures.  For now though, I just want something that
   is simple to implement.  */
typedef struct abbrev_attr
{
  unsigned long attribute;
  unsigned long form;
  struct abbrev_attr *next;
}
abbrev_attr;

typedef struct abbrev_entry
{
  unsigned long entry;
  unsigned long tag;
  int children;
  struct abbrev_attr *first_attr;
  struct abbrev_attr *last_attr;
  struct abbrev_entry *next;
}
abbrev_entry;

static abbrev_entry *first_abbrev = NULL;
static abbrev_entry *last_abbrev = NULL;

static void
free_abbrevs (void)
{
664
  abbrev_entry *abbrv;
665

666
  for (abbrv = first_abbrev; abbrv;)
667
    {
668
      abbrev_entry *next_abbrev = abbrv->next;
669 670
      abbrev_attr *attr;

671
      for (attr = abbrv->first_attr; attr;)
672
	{
673
	  abbrev_attr *next_attr = attr->next;
674 675

	  free (attr);
676
	  attr = next_attr;
677 678
	}

679 680
      free (abbrv);
      abbrv = next_abbrev;
681 682 683 684 685 686 687 688 689 690
    }

  last_abbrev = first_abbrev = NULL;
}

static void
add_abbrev (unsigned long number, unsigned long tag, int children)
{
  abbrev_entry *entry;

691
  entry = (abbrev_entry *) malloc (sizeof (*entry));
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
  if (entry == NULL)
    /* ugg */
    return;

  entry->entry      = number;
  entry->tag        = tag;
  entry->children   = children;
  entry->first_attr = NULL;
  entry->last_attr  = NULL;
  entry->next       = NULL;

  if (first_abbrev == NULL)
    first_abbrev = entry;
  else
    last_abbrev->next = entry;

  last_abbrev = entry;
}

static void
add_abbrev_attr (unsigned long attribute, unsigned long form)
{
  abbrev_attr *attr;

716
  attr = (abbrev_attr *) malloc (sizeof (*attr));
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
  if (attr == NULL)
    /* ugg */
    return;

  attr->attribute = attribute;
  attr->form      = form;
  attr->next      = NULL;

  if (last_abbrev->first_attr == NULL)
    last_abbrev->first_attr = attr;
  else
    last_abbrev->last_attr->next = attr;

  last_abbrev->last_attr = attr;
}

/* Processes the (partial) contents of a .debug_abbrev section.
   Returns NULL if the end of the section was encountered.
   Returns the address after the last byte read if the end of
   an abbreviation set was found.  */

static unsigned char *
process_abbrev_section (unsigned char *start, unsigned char *end)
{
  if (first_abbrev != NULL)
    return NULL;

  while (start < end)
    {
      unsigned int bytes_read;
      unsigned long entry;
      unsigned long tag;
      unsigned long attribute;
      int children;

Nick Clifton's avatar
Nick Clifton committed
752
      entry = read_uleb128 (start, & bytes_read, end);
753 754 755 756 757
      start += bytes_read;

      /* A single zero is supposed to end the section according
	 to the standard.  If there's more, then signal that to
	 the caller.  */
Nick Clifton's avatar
Nick Clifton committed
758 759
      if (start == end)
	return NULL;
760
      if (entry == 0)
Nick Clifton's avatar
Nick Clifton committed
761
	return start;
762

Nick Clifton's avatar
Nick Clifton committed
763
      tag = read_uleb128 (start, & bytes_read, end);
764
      start += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
765 766
      if (start == end)
	return NULL;
767 768 769 770 771 772 773 774 775

      children = *start++;

      add_abbrev (entry, tag, children);

      do
	{
	  unsigned long form;

Nick Clifton's avatar
Nick Clifton committed
776
	  attribute = read_uleb128 (start, & bytes_read, end);
777
	  start += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
778 779
	  if (start == end)
	    break;
780

Nick Clifton's avatar
Nick Clifton committed
781
	  form = read_uleb128 (start, & bytes_read, end);
782
	  start += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
783 784
	  if (start == end)
	    break;
785

786
	  add_abbrev_attr (attribute, form);
787 788 789 790
	}
      while (attribute != 0);
    }

791 792 793
  /* Report the missing single zero which ends the section.  */
  error (_(".debug_abbrev section not zero terminated\n"));

794 795 796
  return NULL;
}

797
static const char *
798 799
get_TAG_name (unsigned long tag)
{
James Lemke's avatar
 
James Lemke committed
800
  const char *name = get_DW_TAG_name ((unsigned int)tag);
801 802

  if (name == NULL)
803
    {
804
      static char buffer[100];
805

806 807
      snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
      return buffer;
808
    }
809 810

  return name;
811 812
}

813
static const char *
814 815
get_FORM_name (unsigned long form)
{
816
  const char *name;
817

818 819
  if (form == 0)
    return "DW_FORM value: 0";
820

821
  name = get_DW_FORM_name (form);
822
  if (name == NULL)
823
    {
824
      static char buffer[100];
825

826 827
      snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
      return buffer;
828
    }
829 830

  return name;
831 832 833
}

static unsigned char *
Nick Clifton's avatar
Nick Clifton committed
834 835 836
display_block (unsigned char *data,
	       dwarf_vma length,
	       const unsigned char * const end)
837
{
Nick Clifton's avatar
Nick Clifton committed
838 839
  dwarf_vma maxlen;

840
  printf (_(" %s byte block: "), dwarf_vmatoa ("u", length));
841

Nick Clifton's avatar
Nick Clifton committed
842 843 844
  maxlen = (dwarf_vma) (end - data);
  length = length > maxlen ? maxlen : length;

845 846 847 848 849 850 851 852 853
  while (length --)
    printf ("%lx ", (unsigned long) byte_get (data++, 1));

  return data;
}

static int
decode_location_expression (unsigned char * data,
			    unsigned int pointer_size,
854 855
			    unsigned int offset_size,
			    int dwarf_version,
856 857
			    dwarf_vma length,
			    dwarf_vma cu_offset,
Richard Henderson's avatar
Richard Henderson committed
858
			    struct dwarf_section * section)
859 860 861
{
  unsigned op;
  unsigned int bytes_read;
862
  dwarf_vma uvalue;
Nick Clifton's avatar
Nick Clifton committed
863
  dwarf_signed_vma svalue;
864 865 866 867 868 869 870 871 872 873
  unsigned char *end = data + length;
  int need_frame_base = 0;

  while (data < end)
    {
      op = *data++;

      switch (op)
	{
	case DW_OP_addr:
Nick Clifton's avatar
Nick Clifton committed
874 875
	  SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
	  printf ("DW_OP_addr: %s", dwarf_vmatoa ("x", uvalue));
876 877 878 879 880
	  break;
	case DW_OP_deref:
	  printf ("DW_OP_deref");
	  break;
	case DW_OP_const1u:
Nick Clifton's avatar
Nick Clifton committed
881 882
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
	  printf ("DW_OP_const1u: %lu", (unsigned long) uvalue);
883 884
	  break;
	case DW_OP_const1s:
Nick Clifton's avatar
Nick Clifton committed
885 886
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
	  printf ("DW_OP_const1s: %ld", (long) svalue);
887 888
	  break;
	case DW_OP_const2u:
Cary Coutant's avatar
Cary Coutant committed
889
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
Nick Clifton's avatar
Nick Clifton committed
890
	  printf ("DW_OP_const2u: %lu", (unsigned long) uvalue);
891 892
	  break;
	case DW_OP_const2s:
Nick Clifton's avatar
Nick Clifton committed
893 894
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
	  printf ("DW_OP_const2s: %ld", (long) svalue);
895 896
	  break;
	case DW_OP_const4u:
Nick Clifton's avatar
Nick Clifton committed
897 898
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
	  printf ("DW_OP_const4u: %lu", (unsigned long) uvalue);
899 900
	  break;
	case DW_OP_const4s:
Nick Clifton's avatar
Nick Clifton committed
901 902
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
	  printf ("DW_OP_const4s: %ld", (long) svalue);
903 904
	  break;
	case DW_OP_const8u:
Nick Clifton's avatar
Nick Clifton committed
905 906 907 908
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
	  printf ("DW_OP_const8u: %lu ", (unsigned long) uvalue);
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
	  printf ("%lu", (unsigned long) uvalue);
909 910
	  break;
	case DW_OP_const8s:
Nick Clifton's avatar
Nick Clifton committed
911 912 913 914
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
	  printf ("DW_OP_const8s: %ld ", (long) svalue);
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
	  printf ("%ld", (long) svalue);
915 916
	  break;
	case DW_OP_constu:
917
	  printf ("DW_OP_constu: %s",
Nick Clifton's avatar
Nick Clifton committed
918
		  dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
919 920 921
	  data += bytes_read;
	  break;
	case DW_OP_consts:
922
	  printf ("DW_OP_consts: %s",
Nick Clifton's avatar
Nick Clifton committed
923
		  dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
924 925 926 927 928 929 930 931 932 933 934 935
	  data += bytes_read;
	  break;
	case DW_OP_dup:
	  printf ("DW_OP_dup");
	  break;
	case DW_OP_drop:
	  printf ("DW_OP_drop");
	  break;
	case DW_OP_over:
	  printf ("DW_OP_over");
	  break;
	case DW_OP_pick:
Nick Clifton's avatar
Nick Clifton committed
936 937
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
	  printf ("DW_OP_pick: %ld", (unsigned long) uvalue);
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
	  break;
	case DW_OP_swap:
	  printf ("DW_OP_swap");
	  break;
	case DW_OP_rot:
	  printf ("DW_OP_rot");
	  break;
	case DW_OP_xderef:
	  printf ("DW_OP_xderef");
	  break;
	case DW_OP_abs:
	  printf ("DW_OP_abs");
	  break;
	case DW_OP_and:
	  printf ("DW_OP_and");
	  break;
	case DW_OP_div:
	  printf ("DW_OP_div");
	  break;
	case DW_OP_minus:
	  printf ("DW_OP_minus");
	  break;
	case DW_OP_mod:
	  printf ("DW_OP_mod");
	  break;
	case DW_OP_mul:
	  printf ("DW_OP_mul");
	  break;
	case DW_OP_neg:
	  printf ("DW_OP_neg");
	  break;
	case DW_OP_not:
	  printf ("DW_OP_not");
	  break;
	case DW_OP_or:
	  printf ("DW_OP_or");
	  break;
	case DW_OP_plus:
	  printf ("DW_OP_plus");
	  break;
	case DW_OP_plus_uconst:
979
	  printf ("DW_OP_plus_uconst: %s",
Nick Clifton's avatar
Nick Clifton committed
980
		  dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	  data += bytes_read;
	  break;
	case DW_OP_shl:
	  printf ("DW_OP_shl");
	  break;
	case DW_OP_shr:
	  printf ("DW_OP_shr");
	  break;
	case DW_OP_shra:
	  printf ("DW_OP_shra");
	  break;
	case DW_OP_xor:
	  printf ("DW_OP_xor");
	  break;
	case DW_OP_bra:
Nick Clifton's avatar
Nick Clifton committed
996 997
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
	  printf ("DW_OP_bra: %ld", (long) svalue);
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	  break;
	case DW_OP_eq:
	  printf ("DW_OP_eq");
	  break;
	case DW_OP_ge:
	  printf ("DW_OP_ge");
	  break;
	case DW_OP_gt:
	  printf ("DW_OP_gt");
	  break;
	case DW_OP_le:
	  printf ("DW_OP_le");
	  break;
	case DW_OP_lt:
	  printf ("DW_OP_lt");
	  break;
	case DW_OP_ne:
	  printf ("DW_OP_ne");
	  break;
	case DW_OP_skip:
Nick Clifton's avatar
Nick Clifton committed
1018 1019
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
	  printf ("DW_OP_skip: %ld", (long) svalue);
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	  break;

	case DW_OP_lit0:
	case DW_OP_lit1:
	case DW_OP_lit2:
	case DW_OP_lit3:
	case DW_OP_lit4:
	case DW_OP_lit5:
	case DW_OP_lit6:
	case DW_OP_lit7:
	case DW_OP_lit8:
	case DW_OP_lit9:
	case DW_OP_lit10:
	case DW_OP_lit11:
	case DW_OP_lit12:
	case DW_OP_lit13:
	case DW_OP_lit14:
	case DW_OP_lit15:
	case DW_OP_lit16:
	case DW_OP_lit17:
	case DW_OP_lit18:
	case DW_OP_lit19:
	case DW_OP_lit20:
	case DW_OP_lit21:
	case DW_OP_lit22:
	case DW_OP_lit23:
	case DW_OP_lit24:
	case DW_OP_lit25:
	case DW_OP_lit26:
	case DW_OP_lit27:
	case DW_OP_lit28:
	case DW_OP_lit29:
	case DW_OP_lit30:
	case DW_OP_lit31:
	  printf ("DW_OP_lit%d", op - DW_OP_lit0);
	  break;

	case DW_OP_reg0:
	case DW_OP_reg1:
	case DW_OP_reg2:
	case DW_OP_reg3:
	case DW_OP_reg4:
	case DW_OP_reg5:
	case DW_OP_reg6:
	case DW_OP_reg7:
	case DW_OP_reg8:
	case DW_OP_reg9:
	case DW_OP_reg10:
	case DW_OP_reg11:
	case DW_OP_reg12:
	case DW_OP_reg13:
	case DW_OP_reg14:
	case DW_OP_reg15:
	case DW_OP_reg16:
	case DW_OP_reg17:
	case DW_OP_reg18:
	case DW_OP_reg19:
	case DW_OP_reg20:
	case DW_OP_reg21:
	case DW_OP_reg22:
	case DW_OP_reg23:
	case DW_OP_reg24:
	case DW_OP_reg25:
	case DW_OP_reg26:
	case DW_OP_reg27:
	case DW_OP_reg28:
	case DW_OP_reg29:
	case DW_OP_reg30:
	case DW_OP_reg31:
Jan Kratochvil's avatar
Jan Kratochvil committed
1089 1090
	  printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
		  regname (op - DW_OP_reg0, 1));
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	  break;

	case DW_OP_breg0:
	case DW_OP_breg1:
	case DW_OP_breg2:
	case DW_OP_breg3:
	case DW_OP_breg4:
	case DW_OP_breg5:
	case DW_OP_breg6:
	case DW_OP_breg7:
	case DW_OP_breg8:
	case DW_OP_breg9:
	case DW_OP_breg10:
	case DW_OP_breg11:
	case DW_OP_breg12:
	case DW_OP_breg13:
	case DW_OP_breg14:
	case DW_OP_breg15:
	case DW_OP_breg16:
	case DW_OP_breg17:
	case DW_OP_breg18:
	case DW_OP_breg19:
	case DW_OP_breg20:
	case DW_OP_breg21:
	case DW_OP_breg22:
	case DW_OP_breg23:
	case DW_OP_breg24:
	case DW_OP_breg25:
	case DW_OP_breg26:
	case DW_OP_breg27:
	case DW_OP_breg28:
	case DW_OP_breg29:
	case DW_OP_breg30:
	case DW_OP_breg31:
1125
	  printf ("DW_OP_breg%d (%s): %s",
Kai Tietz's avatar
Kai Tietz committed
1126
		  op - DW_OP_breg0,
Jan Kratochvil's avatar
Jan Kratochvil committed
1127
		  regname (op - DW_OP_breg0, 1),
Nick Clifton's avatar
Nick Clifton committed
1128
		  dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1129 1130 1131 1132
	  data += bytes_read;
	  break;

	case DW_OP_regx:
Nick Clifton's avatar
Nick Clifton committed
1133
	  uvalue = read_uleb128 (data, &bytes_read, end);
1134
	  data += bytes_read;
1135 1136
	  printf ("DW_OP_regx: %s (%s)",
		  dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1137 1138 1139
	  break;
	case DW_OP_fbreg:
	  need_frame_base = 1;
1140
	  printf ("DW_OP_fbreg: %s",
Nick Clifton's avatar
Nick Clifton committed
1141
		  dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1142 1143 1144
	  data += bytes_read;
	  break;
	case DW_OP_bregx:
Nick Clifton's avatar
Nick Clifton committed
1145
	  uvalue = read_uleb128 (data, &bytes_read, end);
1146
	  data += bytes_read;
1147 1148
	  printf ("DW_OP_bregx: %s (%s) %s",
		  dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
Nick Clifton's avatar
Nick Clifton committed
1149
		  dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1150 1151 1152
	  data += bytes_read;
	  break;
	case DW_OP_piece:
1153
	  printf ("DW_OP_piece: %s",
Nick Clifton's avatar
Nick Clifton committed
1154
		  dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1155 1156 1157
	  data += bytes_read;
	  break;
	case DW_OP_deref_size:
Nick Clifton's avatar
Nick Clifton committed
1158 1159
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
	  printf ("DW_OP_deref_size: %ld", (long) uvalue);
1160 1161
	  break;
	case DW_OP_xderef_size:
Nick Clifton's avatar
Nick Clifton committed
1162 1163
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
	  printf ("DW_OP_xderef_size: %ld", (long) uvalue);
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	  break;
	case DW_OP_nop:
	  printf ("DW_OP_nop");
	  break;

	  /* DWARF 3 extensions.  */
	case DW_OP_push_object_address:
	  printf ("DW_OP_push_object_address");
	  break;
	case DW_OP_call2:
	  /* XXX: Strictly speaking for 64-bit DWARF3 files
	     this ought to be an 8-byte wide computation.  */
Nick Clifton's avatar
Nick Clifton committed
1176
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1177
	  printf ("DW_OP_call2: <0x%s>",
Nick Clifton's avatar
Nick Clifton committed
1178
		  dwarf_vmatoa ("x", svalue + cu_offset));
1179 1180 1181 1182
	  break;
	case DW_OP_call4:
	  /* XXX: Strictly speaking for 64-bit DWARF3 files
	     this ought to be an 8-byte wide computation.  */
Nick Clifton's avatar
Nick Clifton committed
1183
	  SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1184
	  printf ("DW_OP_call4: <0x%s>",
Nick Clifton's avatar
Nick Clifton committed
1185
		  dwarf_vmatoa ("x", svalue + cu_offset));
1186 1187
	  break;
	case DW_OP_call_ref:
1188 1189
	  /* XXX: Strictly speaking for 64-bit DWARF3 files
	     this ought to be an 8-byte wide computation.  */
1190 1191 1192 1193 1194 1195 1196 1197
	  if (dwarf_version == -1)
	    {
	      printf (_("(DW_OP_call_ref in frame info)"));
	      /* No way to tell where the next op is, so just bail.  */
	      return need_frame_base;
	    }
	  if (dwarf_version == 2)
	    {
Nick Clifton's avatar
Nick Clifton committed
1198
	      SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1199 1200 1201
	    }
	  else
	    {
Nick Clifton's avatar
Nick Clifton committed
1202
	      SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1203
	    }
Nick Clifton's avatar
Nick Clifton committed
1204
	  printf ("DW_OP_call_ref: <0x%s>", dwarf_vmatoa ("x", uvalue));
1205
	  break;
1206 1207 1208
	case DW_OP_form_tls_address:
	  printf ("DW_OP_form_tls_address");
	  break;
1209 1210 1211 1212 1213
	case DW_OP_call_frame_cfa:
	  printf ("DW_OP_call_frame_cfa");
	  break;
	case DW_OP_bit_piece:
	  printf ("DW_OP_bit_piece: ");
1214
	  printf (_("size: %s "),
Nick Clifton's avatar
Nick Clifton committed
1215
		  dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1216
	  data += bytes_read;
1217
	  printf (_("offset: %s "),
Nick Clifton's avatar
Nick Clifton committed
1218
		  dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1219 1220
	  data += bytes_read;
	  break;
1221

1222 1223 1224 1225 1226 1227 1228
	  /* DWARF 4 extensions.  */
	case DW_OP_stack_value:
	  printf ("DW_OP_stack_value");
	  break;

	case DW_OP_implicit_value:
	  printf ("DW_OP_implicit_value");
Nick Clifton's avatar
Nick Clifton committed
1229
	  uvalue = read_uleb128 (data, &bytes_read, end);
1230
	  data += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
1231
	  display_block (data, uvalue, end);
1232 1233 1234
	  data += uvalue;
	  break;

1235 1236
	  /* GNU extensions.  */
	case DW_OP_GNU_push_tls_address:
1237
	  printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1238 1239 1240 1241 1242
	  break;
	case DW_OP_GNU_uninit:
	  printf ("DW_OP_GNU_uninit");
	  /* FIXME: Is there data associated with this OP ?  */
	  break;
Richard Henderson's avatar
Richard Henderson committed
1243 1244 1245 1246
	case DW_OP_GNU_encoded_addr:
	  {
	    int encoding;
	    dwarf_vma addr;
1247

Richard Henderson's avatar
Richard Henderson committed
1248
	    encoding = *data++;
1249
	    addr = get_encoded_value (data, encoding, section);
Richard Henderson's avatar
Richard Henderson committed
1250 1251 1252 1253 1254 1255
	    data += size_of_encoded_value (encoding);

	    printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
	    print_dwarf_vma (addr, pointer_size);
	  }
	  break;
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	case DW_OP_GNU_implicit_pointer:
	  /* XXX: Strictly speaking for 64-bit DWARF3 files
	     this ought to be an 8-byte wide computation.  */
	  if (dwarf_version == -1)
	    {
	      printf (_("(DW_OP_GNU_implicit_pointer in frame info)"));
	      /* No way to tell where the next op is, so just bail.  */
	      return need_frame_base;
	    }
	  if (dwarf_version == 2)
	    {
Nick Clifton's avatar
Nick Clifton committed
1267
	      SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1268 1269 1270
	    }
	  else
	    {
Nick Clifton's avatar
Nick Clifton committed
1271
	      SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1272
	    }
Nick Clifton's avatar
Nick Clifton committed
1273 1274 1275 1276 1277
	  printf ("DW_OP_GNU_implicit_pointer: <0x%s> %s",
		  dwarf_vmatoa ("x", uvalue),
		  dwarf_vmatoa ("d", read_sleb128 (data,
						   &bytes_read, end)));
	  data += bytes_read;
1278
	  break;
1279
	case DW_OP_GNU_entry_value:
Nick Clifton's avatar
Nick Clifton committed
1280
	  uvalue = read_uleb128 (data, &bytes_read, end);
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	  data += bytes_read;
	  printf ("DW_OP_GNU_entry_value: (");
	  if (decode_location_expression (data, pointer_size, offset_size,
					  dwarf_version, uvalue,
					  cu_offset, section))
	    need_frame_base = 1;
	  putchar (')');
	  data += uvalue;
	  break;
	case DW_OP_GNU_const_type:
Nick Clifton's avatar
Nick Clifton committed
1291
	  uvalue = read_uleb128 (data, &bytes_read, end);
1292 1293 1294
	  data += bytes_read;
	  printf ("DW_OP_GNU_const_type: <0x%s> ",
		  dwarf_vmatoa ("x", cu_offset + uvalue));
Nick Clifton's avatar
Nick Clifton committed
1295 1296
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
	  display_block (data, uvalue, end);
1297 1298 1299
	  data += uvalue;
	  break;
	case DW_OP_GNU_regval_type:
Nick Clifton's avatar
Nick Clifton committed
1300
	  uvalue = read_uleb128 (data, &bytes_read, end);
1301 1302 1303
	  data += bytes_read;
	  printf ("DW_OP_GNU_regval_type: %s (%s)",
		  dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
Nick Clifton's avatar
Nick Clifton committed
1304
	  uvalue = read_uleb128 (data, &bytes_read, end);
1305 1306 1307 1308
	  data += bytes_read;
	  printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
	  break;
	case DW_OP_GNU_deref_type:
Nick Clifton's avatar
Nick Clifton committed
1309 1310
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
	  printf ("DW_OP_GNU_deref_type: %ld", (long) uvalue);
Nick Clifton's avatar
Nick Clifton committed
1311
	  uvalue = read_uleb128 (data, &bytes_read, end);
1312 1313 1314 1315
	  data += bytes_read;
	  printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
	  break;
	case DW_OP_GNU_convert:
Nick Clifton's avatar
Nick Clifton committed
1316
	  uvalue = read_uleb128 (data, &bytes_read, end);
1317 1318
	  data += bytes_read;
	  printf ("DW_OP_GNU_convert <0x%s>",
1319
		  dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1320 1321
	  break;
	case DW_OP_GNU_reinterpret:
Nick Clifton's avatar
Nick Clifton committed
1322
	  uvalue = read_uleb128 (data, &bytes_read, end);
1323 1324
	  data += bytes_read;
	  printf ("DW_OP_GNU_reinterpret <0x%s>",
1325 1326 1327
		  dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
	  break;
	case DW_OP_GNU_parameter_ref:
Nick Clifton's avatar
Nick Clifton committed
1328
	  SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1329
	  printf ("DW_OP_GNU_parameter_ref: <0x%s>",
Nick Clifton's avatar
Nick Clifton committed
1330
		  dwarf_vmatoa ("x", cu_offset + uvalue));
1331
	  break;
1332
        case DW_OP_GNU_addr_index:
Nick Clifton's avatar
Nick Clifton committed
1333
          uvalue = read_uleb128 (data, &bytes_read, end);
1334 1335 1336
          data += bytes_read;
          printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
          break;
1337
        case DW_OP_GNU_const_index:
Nick Clifton's avatar
Nick Clifton committed
1338
          uvalue = read_uleb128 (data, &bytes_read, end);
1339 1340 1341
          data += bytes_read;
          printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
          break;
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

	  /* HP extensions.  */
	case DW_OP_HP_is_value:
	  printf ("DW_OP_HP_is_value");
	  /* FIXME: Is there data associated with this OP ?  */
	  break;
	case DW_OP_HP_fltconst4:
	  printf ("DW_OP_HP_fltconst4");
	  /* FIXME: Is there data associated with this OP ?  */
	  break;
	case DW_OP_HP_fltconst8:
	  printf ("DW_OP_HP_fltconst8");
	  /* FIXME: Is there data associated with this OP ?  */
	  break;
	case DW_OP_HP_mod_range:
	  printf ("DW_OP_HP_mod_range");
	  /* FIXME: Is there data associated with this OP ?  */
	  break;
	case DW_OP_HP_unmod_range:
	  printf ("DW_OP_HP_unmod_range");
	  /* FIXME: Is there data associated with this OP ?  */
	  break;
	case DW_OP_HP_tls:
	  printf ("DW_OP_HP_tls");
	  /* FIXME: Is there data associated with this OP ?  */
1367 1368
	  break;

1369 1370 1371 1372 1373 1374 1375 1376 1377
	  /* PGI (STMicroelectronics) extensions.  */
	case DW_OP_PGI_omp_thread_num:
	  /* Pushes the thread number for the current thread as it would be
	     returned by the standard OpenMP library function:
	     omp_get_thread_num().  The "current thread" is the thread for
	     which the expression is being evaluated.  */
	  printf ("DW_OP_PGI_omp_thread_num");
	  break;

1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
	default:
	  if (op >= DW_OP_lo_user
	      && op <= DW_OP_hi_user)
	    printf (_("(User defined location op)"));
	  else
	    printf (_("(Unknown location op)"));
	  /* No way to tell where the next op is, so just bail.  */
	  return need_frame_base;
	}

      /* Separate the ops.  */
      if (data < end)
	printf ("; ");
    }

  return need_frame_base;
}

Cary Coutant's avatar
Cary Coutant committed
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
/* Find the CU or TU set corresponding to the given CU_OFFSET.
   This is used for DWARF package files.  */

static struct cu_tu_set *
find_cu_tu_set_v2 (dwarf_vma cu_offset, int do_types)
{
  struct cu_tu_set *p;
  unsigned int nsets;
  unsigned int dw_sect;

  if (do_types)
    {
      p = tu_sets;
      nsets = tu_count;
      dw_sect = DW_SECT_TYPES;
    }
  else
    {
      p = cu_sets;
      nsets = cu_count;
      dw_sect = DW_SECT_INFO;
    }
  while (nsets > 0)
    {
      if (p->section_offsets [dw_sect] == cu_offset)
	return p;
      p++;
      nsets--;
    }
  return NULL;
}

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
/* Add INC to HIGH_BITS:LOW_BITS.  */
static void
add64 (dwarf_vma * high_bits, dwarf_vma * low_bits, dwarf_vma inc)
{
  dwarf_vma tmp = * low_bits;

  tmp += inc;

  /* FIXME: There is probably a better way of handling this:

     We need to cope with dwarf_vma being a 32-bit or 64-bit
     type.  Plus regardless of its size LOW_BITS is meant to
     only hold 32-bits, so if there is overflow or wrap around
     we must propagate into HIGH_BITS.  */
  if (tmp < * low_bits)
    {
      ++ * high_bits;
    }
  else if (sizeof (tmp) > 8
	   && (tmp >> 31) > 1)
    {
      ++ * high_bits;
      tmp &= 0xFFFFFFFF;
    }

  * low_bits = tmp;
}

1456
static unsigned char *
1457 1458
read_and_display_attr_value (unsigned long attribute,
			     unsigned long form,
1459
			     unsigned char * data,
Nick Clifton's avatar
Nick Clifton committed
1460
			     unsigned char * end,
1461 1462 1463
			     dwarf_vma cu_offset,
			     dwarf_vma pointer_size,
			     dwarf_vma offset_size,
1464 1465 1466
			     int dwarf_version,
			     debug_info * debug_info_p,
			     int do_loc,
Cary Coutant's avatar
Cary Coutant committed
1467 1468
			     struct dwarf_section * section,
			     struct cu_tu_set * this_set)
1469
{
1470
  dwarf_vma uvalue = 0;
1471
  unsigned char *block_start = NULL;
1472
  unsigned char * orig_data = data;
1473 1474
  unsigned int bytes_read;

Nick Clifton's avatar
Nick Clifton committed
1475 1476 1477 1478 1479 1480
  if (data == end)
    {
      warn (_("corrupt attribute\n"));
      return data;
    }

1481 1482 1483 1484 1485 1486 1487
  switch (form)
    {
    default:
      break;

    case DW_FORM_ref_addr:
      if (dwarf_version == 2)
Nick Clifton's avatar
Nick Clifton committed
1488
	SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1489
      else if (dwarf_version == 3 || dwarf_version == 4)
Nick Clifton's avatar
Nick Clifton committed
1490
	SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1491
      else
1492 1493
	error (_("Internal error: DWARF version is not 2, 3 or 4.\n"));

1494 1495 1496
      break;

    case DW_FORM_addr:
Nick Clifton's avatar
Nick Clifton committed
1497
      SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1498 1499 1500
      break;

    case DW_FORM_strp:
1501
    case DW_FORM_sec_offset:
1502 1503
    case DW_FORM_GNU_ref_alt:
    case DW_FORM_GNU_strp_alt:
Nick Clifton's avatar
Nick Clifton committed
1504
      SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1505 1506
      break;

1507 1508 1509 1510
    case DW_FORM_flag_present:
      uvalue = 1;
      break;

1511 1512 1513
    case DW_FORM_ref1:
    case DW_FORM_flag:
    case DW_FORM_data1:
Nick Clifton's avatar
Nick Clifton committed
1514
      SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1515 1516 1517 1518
      break;

    case DW_FORM_ref2:
    case DW_FORM_data2:
Nick Clifton's avatar
Nick Clifton committed
1519
      SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1520 1521 1522 1523
      break;

    case DW_FORM_ref4:
    case DW_FORM_data4:
Nick Clifton's avatar
Nick Clifton committed
1524
      SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1525 1526 1527
      break;

    case DW_FORM_sdata:
Nick Clifton's avatar
Nick Clifton committed
1528
      uvalue = read_sleb128 (data, & bytes_read, end);
1529 1530 1531
      data += bytes_read;
      break;

1532
    case DW_FORM_GNU_str_index:
Nick Clifton's avatar
Nick Clifton committed
1533
      uvalue = read_uleb128 (data, & bytes_read, end);
1534 1535 1536
      data += bytes_read;
      break;

1537 1538
    case DW_FORM_ref_udata:
    case DW_FORM_udata:
Nick Clifton's avatar
Nick Clifton committed
1539
      uvalue = read_uleb128 (data, & bytes_read, end);
1540 1541 1542 1543
      data += bytes_read;
      break;

    case DW_FORM_indirect:
Nick Clifton's avatar
Nick Clifton committed
1544
      form = read_uleb128 (data, & bytes_read, end);
1545 1546 1547
      data += bytes_read;
      if (!do_loc)
	printf (" %s", get_FORM_name (form));
Nick Clifton's avatar
Nick Clifton committed
1548
      return read_and_display_attr_value (attribute, form, data, end,
1549 1550
					  cu_offset, pointer_size,
					  offset_size, dwarf_version,
1551
					  debug_info_p, do_loc,
Cary Coutant's avatar
Cary Coutant committed
1552
					  section, this_set);
1553
    case DW_FORM_GNU_addr_index:
Nick Clifton's avatar
Nick Clifton committed
1554
      uvalue = read_uleb128 (data, & bytes_read, end);
1555 1556
      data += bytes_read;
      break;
1557 1558 1559 1560 1561 1562
    }

  switch (form)
    {
    case DW_FORM_ref_addr:
      if (!do_loc)
1563
	printf (" <0x%s>", dwarf_vmatoa ("x",uvalue));
1564 1565
      break;

1566 1567 1568 1569 1570
    case DW_FORM_GNU_ref_alt:
      if (!do_loc)
	printf (" <alt 0x%s>", dwarf_vmatoa ("x",uvalue));
      break;

1571 1572 1573 1574 1575
    case DW_FORM_ref1:
    case DW_FORM_ref2:
    case DW_FORM_ref4:
    case DW_FORM_ref_udata:
      if (!do_loc)
1576
	printf (" <0x%s>", dwarf_vmatoa ("x", uvalue + cu_offset));
1577 1578 1579 1580
      break;

    case DW_FORM_data4:
    case DW_FORM_addr:
1581
    case DW_FORM_sec_offset:
1582
      if (!do_loc)
1583
	printf (" 0x%s", dwarf_vmatoa ("x", uvalue));
1584 1585
      break;

1586
    case DW_FORM_flag_present:
1587 1588 1589 1590 1591 1592
    case DW_FORM_flag:
    case DW_FORM_data1:
    case DW_FORM_data2:
    case DW_FORM_sdata:
    case DW_FORM_udata:
      if (!do_loc)
1593
	printf (" %s", dwarf_vmatoa ("d", uvalue));
1594 1595 1596 1597 1598 1599
      break;

    case DW_FORM_ref8:
    case DW_FORM_data8:
      if (!do_loc)
	{
1600
	  dwarf_vma high_bits;
1601
	  dwarf_vma utmp;
1602 1603
	  char buf[64];

Nick Clifton's avatar
Nick Clifton committed
1604
	  SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1605 1606 1607
	  utmp = uvalue;
	  if (form == DW_FORM_ref8)
	    add64 (& high_bits, & utmp, cu_offset);
1608
	  printf (" 0x%s",
1609
		  dwarf_vmatoa64 (high_bits, utmp, buf, sizeof (buf)));
1610
	}
Nick Clifton's avatar
Nick Clifton committed
1611

1612 1613 1614 1615
      if ((do_loc || do_debug_loc || do_debug_ranges)
	  && num_debug_info_entries == 0)
	{
	  if (sizeof (uvalue) == 8)
Nick Clifton's avatar
Nick Clifton committed
1616
	    SAFE_BYTE_GET (uvalue, data, 8, end);
1617
	  else
1618
	    error (_("DW_FORM_data8 is unsupported when sizeof (dwarf_vma) != 8\n"));
1619
	}
Nick Clifton's avatar
Nick Clifton committed
1620

1621 1622 1623 1624 1625
      data += 8;
      break;

    case DW_FORM_string:
      if (!do_loc)
Alan Modra's avatar
Alan Modra committed
1626
	printf (" %.*s", (int) (end - data), data);
Nick Clifton's avatar
Nick Clifton committed
1627
      data += strnlen ((char *) data, end - data) + 1;
1628 1629 1630
      break;

    case DW_FORM_block:
1631
    case DW_FORM_exprloc:
Nick Clifton's avatar
Nick Clifton committed
1632
      uvalue = read_uleb128 (data, & bytes_read, end);
1633 1634 1635 1636
      block_start = data + bytes_read;
      if (do_loc)
	data = block_start + uvalue;
      else
Nick Clifton's avatar
Nick Clifton committed
1637
	data = display_block (block_start, uvalue, end);
1638 1639 1640
      break;

    case DW_FORM_block1:
Nick Clifton's avatar
Nick Clifton committed
1641
      SAFE_BYTE_GET (uvalue, data, 1, end);
1642 1643 1644 1645
      block_start = data + 1;
      if (do_loc)
	data = block_start + uvalue;
      else
Nick Clifton's avatar
Nick Clifton committed
1646
	data = display_block (block_start, uvalue, end);
1647 1648 1649
      break;

    case DW_FORM_block2:
Nick Clifton's avatar
Nick Clifton committed
1650
      SAFE_BYTE_GET (uvalue, data, 2, end);
1651 1652 1653 1654
      block_start = data + 2;
      if (do_loc)
	data = block_start + uvalue;
      else
Nick Clifton's avatar
Nick Clifton committed
1655
	data = display_block (block_start, uvalue, end);
1656 1657 1658
      break;

    case DW_FORM_block4:
Nick Clifton's avatar
Nick Clifton committed
1659
      SAFE_BYTE_GET (uvalue, data, 4, end);
1660 1661 1662 1663
      block_start = data + 4;
      if (do_loc)
	data = block_start + uvalue;
      else
Nick Clifton's avatar
Nick Clifton committed
1664
	data = display_block (block_start, uvalue, end);
1665 1666 1667 1668
      break;

    case DW_FORM_strp:
      if (!do_loc)
Kai Tietz's avatar
Kai Tietz committed
1669 1670 1671
	printf (_(" (indirect string, offset: 0x%s): %s"),
		dwarf_vmatoa ("x", uvalue),
		fetch_indirect_string (uvalue));
1672 1673
      break;

1674 1675 1676 1677 1678 1679 1680 1681
    case DW_FORM_GNU_str_index:
      if (!do_loc)
        {
          const char *suffix = strrchr (section->name, '.');
          int dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? 1 : 0;

          printf (_(" (indexed string: 0x%s): %s"),
                  dwarf_vmatoa ("x", uvalue),
Cary Coutant's avatar
Cary Coutant committed
1682
                  fetch_indexed_string (uvalue, this_set, offset_size, dwo));
1683 1684 1685
        }
      break;

1686 1687 1688 1689 1690 1691
    case DW_FORM_GNU_strp_alt:
      if (!do_loc)
	printf (_(" (alt indirect string, offset: 0x%s)"),
		dwarf_vmatoa ("x", uvalue));
      break;

1692 1693 1694 1695
    case DW_FORM_indirect:
      /* Handled above.  */
      break;

Cary Coutant's avatar
Cary Coutant committed
1696 1697 1698
    case DW_FORM_ref_sig8:
      if (!do_loc)
	{
1699 1700 1701
	  dwarf_vma high_bits;
	  char buf[64];

Nick Clifton's avatar
Nick Clifton committed
1702
	  SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1703 1704
	  printf (" signature: 0x%s",
		  dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
Cary Coutant's avatar
Cary Coutant committed
1705
	}
1706
      data += 8;
Cary Coutant's avatar
Cary Coutant committed
1707 1708
      break;

1709 1710 1711 1712 1713 1714 1715
    case DW_FORM_GNU_addr_index:
      if (!do_loc)
        printf (_(" (addr_index: 0x%s): %s"),
                dwarf_vmatoa ("x", uvalue),
                fetch_indexed_value (uvalue * pointer_size, pointer_size));
      break;

1716 1717 1718 1719 1720 1721
    default:
      warn (_("Unrecognized form: %lu\n"), form);
      break;
    }

  if ((do_loc || do_debug_loc || do_debug_ranges)
1722 1723
      && num_debug_info_entries == 0
      && debug_info_p != NULL)
1724 1725 1726 1727 1728 1729
    {
      switch (attribute)
	{
	case DW_AT_frame_base:
	  have_frame_base = 1;
	case DW_AT_location:
1730 1731
	case DW_AT_string_length:
	case DW_AT_return_addr:
1732 1733
	case DW_AT_data_member_location:
	case DW_AT_vtable_elem_location:
1734 1735 1736
	case DW_AT_segment:
	case DW_AT_static_link:
	case DW_AT_use_location:
1737 1738 1739 1740
	case DW_AT_GNU_call_site_value:
	case DW_AT_GNU_call_site_data_value:
	case DW_AT_GNU_call_site_target:
	case DW_AT_GNU_call_site_target_clobbered:
Jan Kratochvil's avatar
Jan Kratochvil committed
1741 1742
    	  if ((dwarf_version < 4
	       && (form == DW_FORM_data4 || form == DW_FORM_data8))
1743
	      || form == DW_FORM_sec_offset)
1744 1745
	    {
	      /* Process location list.  */
1746
	      unsigned int lmax = debug_info_p->max_loc_offsets;
1747 1748
	      unsigned int num = debug_info_p->num_loc_offsets;

1749
	      if (lmax == 0 || num >= lmax)
1750
		{
1751
		  lmax += 1024;
1752
		  debug_info_p->loc_offsets = (dwarf_vma *)
1753
                      xcrealloc (debug_info_p->loc_offsets,
1754
				 lmax, sizeof (*debug_info_p->loc_offsets));
1755 1756
		  debug_info_p->have_frame_base = (int *)
                      xcrealloc (debug_info_p->have_frame_base,
1757 1758
				 lmax, sizeof (*debug_info_p->have_frame_base));
		  debug_info_p->max_loc_offsets = lmax;
1759
		}
Cary Coutant's avatar
Cary Coutant committed
1760 1761
	      if (this_set != NULL)
	        uvalue += this_set->section_offsets [DW_SECT_LOC];
1762 1763 1764 1765 1766
	      debug_info_p->loc_offsets [num] = uvalue;
	      debug_info_p->have_frame_base [num] = have_frame_base;
	      debug_info_p->num_loc_offsets++;
	    }
	  break;
1767

1768 1769 1770 1771 1772
	case DW_AT_low_pc:
	  if (need_base_address)
	    debug_info_p->base_address = uvalue;
	  break;

1773 1774 1775 1776 1777 1778 1779 1780
	case DW_AT_GNU_addr_base:
          debug_info_p->addr_base = uvalue;
	  break;

	case DW_AT_GNU_ranges_base:
          debug_info_p->ranges_base = uvalue;
	  break;

1781
	case DW_AT_ranges:
Jan Kratochvil's avatar
Jan Kratochvil committed
1782 1783
    	  if ((dwarf_version < 4
	       && (form == DW_FORM_data4 || form == DW_FORM_data8))
1784
	      || form == DW_FORM_sec_offset)
1785 1786
	    {
	      /* Process range list.  */
1787
	      unsigned int lmax = debug_info_p->max_range_lists;
1788 1789
	      unsigned int num = debug_info_p->num_range_lists;

1790
	      if (lmax == 0 || num >= lmax)
1791
		{
1792
		  lmax += 1024;
1793
		  debug_info_p->range_lists = (dwarf_vma *)
1794
                      xcrealloc (debug_info_p->range_lists,
1795 1796
				 lmax, sizeof (*debug_info_p->range_lists));
		  debug_info_p->max_range_lists = lmax;
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
		}
	      debug_info_p->range_lists [num] = uvalue;
	      debug_info_p->num_range_lists++;
	    }
	  break;

	default:
	  break;
	}
    }

1808
  if (do_loc || attribute == 0)
1809 1810
    return data;

1811
  /* For some attributes we can display further information.  */
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
  printf ("\t");

  switch (attribute)
    {
    case DW_AT_inline:
      switch (uvalue)
	{
	case DW_INL_not_inlined:
	  printf (_("(not inlined)"));
	  break;
	case DW_INL_inlined:
	  printf (_("(inlined)"));
	  break;
	case DW_INL_declared_not_inlined:
	  printf (_("(declared as inline but ignored)"));
	  break;
	case DW_INL_declared_inlined:
	  printf (_("(declared as inline and inlined)"));
	  break;
	default:
Kai Tietz's avatar
Kai Tietz committed
1832 1833
	  printf (_("  (Unknown inline attribute value: %s)"),
		  dwarf_vmatoa ("x", uvalue));
1834 1835 1836 1837 1838 1839 1840
	  break;
	}
      break;

    case DW_AT_language:
      switch (uvalue)
	{
Nick Clifton's avatar
Nick Clifton committed
1841
	  /* Ordered by the numeric value of these constants.  */
1842
	case DW_LANG_C89:		printf ("(ANSI C)"); break;
Nick Clifton's avatar
Nick Clifton committed
1843 1844
	case DW_LANG_C:			printf ("(non-ANSI C)"); break;
	case DW_LANG_Ada83:		printf ("(Ada)"); break;
1845
	case DW_LANG_C_plus_plus:	printf ("(C++)"); break;
Nick Clifton's avatar
Nick Clifton committed
1846 1847
	case DW_LANG_Cobol74:		printf ("(Cobol 74)"); break;
	case DW_LANG_Cobol85:		printf ("(Cobol 85)"); break;
1848 1849 1850
	case DW_LANG_Fortran77:		printf ("(FORTRAN 77)"); break;
	case DW_LANG_Fortran90:		printf ("(Fortran 90)"); break;
	case DW_LANG_Pascal83:		printf ("(ANSI Pascal)"); break;
Nick Clifton's avatar
Nick Clifton committed
1851
	case DW_LANG_Modula2:		printf ("(Modula 2)"); break;
1852
	  /* DWARF 2.1 values.	*/
Nick Clifton's avatar
Nick Clifton committed
1853
	case DW_LANG_Java:		printf ("(Java)"); break;
1854 1855 1856
	case DW_LANG_C99:		printf ("(ANSI C99)"); break;
	case DW_LANG_Ada95:		printf ("(ADA 95)"); break;
	case DW_LANG_Fortran95:		printf ("(Fortran 95)"); break;
Nick Clifton's avatar
Nick Clifton committed
1857 1858 1859 1860 1861 1862
	  /* DWARF 3 values.  */
	case DW_LANG_PLI:		printf ("(PLI)"); break;
	case DW_LANG_ObjC:		printf ("(Objective C)"); break;
	case DW_LANG_ObjC_plus_plus:	printf ("(Objective C++)"); break;
	case DW_LANG_UPC:		printf ("(Unified Parallel C)"); break;
	case DW_LANG_D:			printf ("(D)"); break;
Cary Coutant's avatar
Cary Coutant committed
1863 1864
	  /* DWARF 4 values.  */
	case DW_LANG_Python:		printf ("(Python)"); break;
1865 1866
	  /* DWARF 5 values.  */
	case DW_LANG_Go:		printf ("(Go)"); break;
1867 1868 1869 1870 1871
	  /* MIPS extension.  */
	case DW_LANG_Mips_Assembler:	printf ("(MIPS assembler)"); break;
	  /* UPC extension.  */
	case DW_LANG_Upc:		printf ("(Unified Parallel C)"); break;
	default:
Nick Clifton's avatar
Nick Clifton committed
1872
	  if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1873
	    printf (_("(implementation defined: %s)"),
1874
		    dwarf_vmatoa ("x", uvalue));
Nick Clifton's avatar
Nick Clifton committed
1875
	  else
1876
	    printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
	  break;
	}
      break;

    case DW_AT_encoding:
      switch (uvalue)
	{
	case DW_ATE_void:		printf ("(void)"); break;
	case DW_ATE_address:		printf ("(machine address)"); break;
	case DW_ATE_boolean:		printf ("(boolean)"); break;
	case DW_ATE_complex_float:	printf ("(complex float)"); break;
	case DW_ATE_float:		printf ("(float)"); break;
	case DW_ATE_signed:		printf ("(signed)"); break;
	case DW_ATE_signed_char:	printf ("(signed char)"); break;
	case DW_ATE_unsigned:		printf ("(unsigned)"); break;
	case DW_ATE_unsigned_char:	printf ("(unsigned char)"); break;
1893
	  /* DWARF 2.1 values:  */
1894 1895
	case DW_ATE_imaginary_float:	printf ("(imaginary float)"); break;
	case DW_ATE_decimal_float:	printf ("(decimal float)"); break;
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
	  /* DWARF 3 values:  */
	case DW_ATE_packed_decimal:	printf ("(packed_decimal)"); break;
	case DW_ATE_numeric_string:	printf ("(numeric_string)"); break;
	case DW_ATE_edited:		printf ("(edited)"); break;
	case DW_ATE_signed_fixed:	printf ("(signed_fixed)"); break;
	case DW_ATE_unsigned_fixed:	printf ("(unsigned_fixed)"); break;
	  /* HP extensions:  */
	case DW_ATE_HP_float80:		printf ("(HP_float80)"); break;
	case DW_ATE_HP_complex_float80:	printf ("(HP_complex_float80)"); break;
	case DW_ATE_HP_float128:	printf ("(HP_float128)"); break;
	case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
	case DW_ATE_HP_floathpintel:	printf ("(HP_floathpintel)"); break;
	case DW_ATE_HP_imaginary_float80:	printf ("(HP_imaginary_float80)"); break;
	case DW_ATE_HP_imaginary_float128:	printf ("(HP_imaginary_float128)"); break;

1911 1912 1913
	default:
	  if (uvalue >= DW_ATE_lo_user
	      && uvalue <= DW_ATE_hi_user)
1914
	    printf (_("(user defined type)"));
1915
	  else
1916
	    printf (_("(unknown type)"));
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
	  break;
	}
      break;

    case DW_AT_accessibility:
      switch (uvalue)
	{
	case DW_ACCESS_public:		printf ("(public)"); break;
	case DW_ACCESS_protected:	printf ("(protected)"); break;
	case DW_ACCESS_private:		printf ("(private)"); break;
	default:
1928
	  printf (_("(unknown accessibility)"));
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
	  break;
	}
      break;

    case DW_AT_visibility:
      switch (uvalue)
	{
	case DW_VIS_local:		printf ("(local)"); break;
	case DW_VIS_exported:		printf ("(exported)"); break;
	case DW_VIS_qualified:		printf ("(qualified)"); break;
1939
	default:			printf (_("(unknown visibility)")); break;
1940 1941 1942 1943 1944 1945 1946 1947 1948
	}
      break;

    case DW_AT_virtuality:
      switch (uvalue)
	{
	case DW_VIRTUALITY_none:	printf ("(none)"); break;
	case DW_VIRTUALITY_virtual:	printf ("(virtual)"); break;
	case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1949
	default:			printf (_("(unknown virtuality)")); break;
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
	}
      break;

    case DW_AT_identifier_case:
      switch (uvalue)
	{
	case DW_ID_case_sensitive:	printf ("(case_sensitive)"); break;
	case DW_ID_up_case:		printf ("(up_case)"); break;
	case DW_ID_down_case:		printf ("(down_case)"); break;
	case DW_ID_case_insensitive:	printf ("(case_insensitive)"); break;
1960
	default:			printf (_("(unknown case)")); break;
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
	}
      break;

    case DW_AT_calling_convention:
      switch (uvalue)
	{
	case DW_CC_normal:	printf ("(normal)"); break;
	case DW_CC_program:	printf ("(program)"); break;
	case DW_CC_nocall:	printf ("(nocall)"); break;
	default:
	  if (uvalue >= DW_CC_lo_user
	      && uvalue <= DW_CC_hi_user)
1973
	    printf (_("(user defined)"));
1974
	  else
1975
	    printf (_("(unknown convention)"));
1976 1977 1978 1979 1980 1981
	}
      break;

    case DW_AT_ordering:
      switch (uvalue)
	{
1982
	case -1: printf (_("(undefined)")); break;
1983 1984 1985 1986 1987 1988 1989 1990
	case 0:  printf ("(row major)"); break;
	case 1:  printf ("(column major)"); break;
	}
      break;

    case DW_AT_frame_base:
      have_frame_base = 1;
    case DW_AT_location:
1991 1992
    case DW_AT_string_length:
    case DW_AT_return_addr:
1993 1994
    case DW_AT_data_member_location:
    case DW_AT_vtable_elem_location:
1995 1996 1997
    case DW_AT_segment:
    case DW_AT_static_link:
    case DW_AT_use_location:
1998 1999 2000 2001
    case DW_AT_GNU_call_site_value:
    case DW_AT_GNU_call_site_data_value:
    case DW_AT_GNU_call_site_target:
    case DW_AT_GNU_call_site_target_clobbered:
Jan Kratochvil's avatar
Jan Kratochvil committed
2002 2003
      if ((dwarf_version < 4
           && (form == DW_FORM_data4 || form == DW_FORM_data8))
2004
	  || form == DW_FORM_sec_offset)
2005 2006
	printf (_("(location list)"));
      /* Fall through.  */
2007 2008 2009 2010 2011
    case DW_AT_allocated:
    case DW_AT_associated:
    case DW_AT_data_location:
    case DW_AT_stride:
    case DW_AT_upper_bound:
2012
    case DW_AT_lower_bound:
2013 2014 2015 2016 2017 2018 2019
      if (block_start)
	{
	  int need_frame_base;

	  printf ("(");
	  need_frame_base = decode_location_expression (block_start,
							pointer_size,
2020 2021
							offset_size,
							dwarf_version,
2022
							uvalue,
Richard Henderson's avatar
Richard Henderson committed
2023
							cu_offset, section);
2024 2025 2026 2027 2028 2029
	  printf (")");
	  if (need_frame_base && !have_frame_base)
	    printf (_(" [without DW_AT_frame_base]"));
	}
      break;

2030 2031
    case DW_AT_import:
      {
2032 2033
	if (form == DW_FORM_ref_sig8
	    || form == DW_FORM_GNU_ref_alt)
Cary Coutant's avatar
Cary Coutant committed
2034 2035
          break;

2036 2037
	if (form == DW_FORM_ref1
	    || form == DW_FORM_ref2
Jan Kratochvil's avatar
Jan Kratochvil committed
2038 2039
	    || form == DW_FORM_ref4
	    || form == DW_FORM_ref_udata)
2040 2041
	  uvalue += cu_offset;

2042
	if (uvalue >= section->size)
Kai Tietz's avatar
Kai Tietz committed
2043 2044 2045
	  warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset %lx is too big.\n"),
		dwarf_vmatoa ("x", uvalue),
		(unsigned long) (orig_data - section->start));
2046 2047 2048 2049 2050
	else
	  {
	    unsigned long abbrev_number;
	    abbrev_entry * entry;

Nick Clifton's avatar
Nick Clifton committed
2051
	    abbrev_number = read_uleb128 (section->start + uvalue, NULL, end);
2052

2053
	    printf (_("[Abbrev Number: %ld"), abbrev_number);
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
	    /* Don't look up abbrev for DW_FORM_ref_addr, as it very often will
	       use different abbrev table, and we don't track .debug_info chunks
	       yet.  */
	    if (form != DW_FORM_ref_addr)
	      {
		for (entry = first_abbrev; entry != NULL; entry = entry->next)
		  if (entry->entry == abbrev_number)
		    break;
		if (entry != NULL)
		  printf (" (%s)", get_TAG_name (entry->tag));
	      }
2065 2066
	    printf ("]");
	  }
2067 2068 2069
      }
      break;

2070 2071 2072 2073 2074 2075 2076
    default:
      break;
    }

  return data;
}

2077
static const char *
2078 2079
get_AT_name (unsigned long attribute)
{
2080
  const char *name;
2081

2082 2083 2084
  if (attribute == 0)
    return "DW_AT value: 0";

2085 2086 2087
  /* One value is shared by the MIPS and HP extensions:  */
  if (attribute == DW_AT_MIPS_fde)
    return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
2088

2089 2090 2091 2092 2093 2094 2095 2096 2097
  name = get_DW_AT_name (attribute);

  if (name == NULL)
    {
      static char buffer[100];

      snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
		attribute);
      return buffer;
2098
    }
2099 2100

  return name;
2101 2102 2103
}

static unsigned char *
2104 2105
read_and_display_attr (unsigned long attribute,
		       unsigned long form,
2106
		       unsigned char * data,
Nick Clifton's avatar
Nick Clifton committed
2107
		       unsigned char * end,
2108 2109 2110
		       dwarf_vma cu_offset,
		       dwarf_vma pointer_size,
		       dwarf_vma offset_size,
2111 2112 2113
		       int dwarf_version,
		       debug_info * debug_info_p,
		       int do_loc,
Cary Coutant's avatar
Cary Coutant committed
2114 2115
		       struct dwarf_section * section,
		       struct cu_tu_set * this_set)
2116 2117
{
  if (!do_loc)
2118
    printf ("   %-18s:", get_AT_name (attribute));
Nick Clifton's avatar
Nick Clifton committed
2119 2120
  data = read_and_display_attr_value (attribute, form, data, end,
				      cu_offset, pointer_size, offset_size,
2121
				      dwarf_version, debug_info_p,
Cary Coutant's avatar
Cary Coutant committed
2122
				      do_loc, section, this_set);
2123 2124 2125 2126 2127 2128 2129
  if (!do_loc)
    printf ("\n");
  return data;
}

/* Process the contents of a .debug_info section.  If do_loc is non-zero
   then we are scanning for location lists and we do not want to display
Cary Coutant's avatar
Cary Coutant committed
2130 2131
   anything to the user.  If do_types is non-zero, we are processing
   a .debug_types section instead of a .debug_info section.  */
2132 2133

static int
2134 2135
process_debug_info (struct dwarf_section *section,
		    void *file,
2136
                    enum dwarf_section_display_enum abbrev_sec,
Cary Coutant's avatar
Cary Coutant committed
2137 2138
		    int do_loc,
		    int do_types)
2139 2140 2141 2142 2143 2144 2145 2146
{
  unsigned char *start = section->start;
  unsigned char *end = start + section->size;
  unsigned char *section_begin;
  unsigned int unit;
  unsigned int num_units = 0;

  if ((do_loc || do_debug_loc || do_debug_ranges)
Cary Coutant's avatar
Cary Coutant committed
2147 2148
      && num_debug_info_entries == 0
      && ! do_types)
2149
    {
2150
      dwarf_vma length;
2151 2152 2153 2154 2155 2156 2157 2158

      /* First scan the section to get the number of comp units.  */
      for (section_begin = start, num_units = 0; section_begin < end;
	   num_units ++)
	{
	  /* Read the first 4 bytes.  For a 32-bit DWARF section, this
	     will be the length.  For a 64-bit DWARF section, it'll be
	     the escape code 0xffffffff followed by an 8 byte length.  */
Nick Clifton's avatar
Nick Clifton committed
2159
	  SAFE_BYTE_GET (length, section_begin, 4, end);
2160 2161 2162

	  if (length == 0xffffffff)
	    {
Nick Clifton's avatar
Nick Clifton committed
2163
	      SAFE_BYTE_GET (length, section_begin + 4, 8, end);
2164 2165
	      section_begin += length + 12;
	    }
2166 2167
	  else if (length >= 0xfffffff0 && length < 0xffffffff)
	    {
2168 2169
	      warn (_("Reserved length value (0x%s) found in section %s\n"),
		    dwarf_vmatoa ("x", length), section->name);
2170 2171
	      return 0;
	    }
2172 2173
	  else
	    section_begin += length + 4;
2174 2175 2176 2177 2178 2179

	  /* Negative values are illegal, they may even cause infinite
	     looping.  This can happen if we can't accurately apply
	     relocations to an object file.  */
	  if ((signed long) length <= 0)
	    {
2180 2181
	      warn (_("Corrupt unit length (0x%s) found in section %s\n"),
		    dwarf_vmatoa ("x", length), section->name);
2182 2183
	      return 0;
	    }
2184 2185 2186 2187 2188 2189 2190 2191 2192
	}

      if (num_units == 0)
	{
	  error (_("No comp units in %s section ?"), section->name);
	  return 0;
	}

      /* Then allocate an array to hold the information.  */
2193 2194
      debug_information = (debug_info *) cmalloc (num_units,
                                                  sizeof (* debug_information));
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
      if (debug_information == NULL)
	{
	  error (_("Not enough memory for a debug info array of %u entries"),
		 num_units);
	  return 0;
	}
    }

  if (!do_loc)
    {
2205 2206
      if (dwarf_start_die == 0)
	printf (_("Contents of the %s section:\n\n"), section->name);
2207 2208

      load_debug_section (str, file);
2209 2210 2211 2212
      load_debug_section (str_dwo, file);
      load_debug_section (str_index, file);
      load_debug_section (str_index_dwo, file);
      load_debug_section (debug_addr, file);
2213 2214
    }

2215 2216
  load_debug_section (abbrev_sec, file);
  if (debug_displays [abbrev_sec].section.start == NULL)
2217 2218
    {
      warn (_("Unable to locate %s section!\n"),
2219
	    debug_displays [abbrev_sec].section.name);
2220 2221 2222 2223 2224 2225 2226 2227
      return 0;
    }

  for (section_begin = start, unit = 0; start < end; unit++)
    {
      DWARF2_Internal_CompUnit compunit;
      unsigned char *hdrptr;
      unsigned char *tags;
2228
      int level, last_level, saved_level;
2229
      dwarf_vma cu_offset;
2230
      unsigned int offset_size;
2231
      int initial_length_size;
2232 2233
      dwarf_vma signature_high = 0;
      dwarf_vma signature_low = 0;
2234
      dwarf_vma type_offset = 0;
Cary Coutant's avatar
Cary Coutant committed
2235 2236 2237
      struct cu_tu_set *this_set;
      dwarf_vma abbrev_base;
      size_t abbrev_size;
2238 2239 2240

      hdrptr = start;

Nick Clifton's avatar
Nick Clifton committed
2241
      SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
2242 2243 2244

      if (compunit.cu_length == 0xffffffff)
	{
Nick Clifton's avatar
Nick Clifton committed
2245
	  SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
2246 2247 2248 2249 2250 2251 2252 2253 2254
	  offset_size = 8;
	  initial_length_size = 12;
	}
      else
	{
	  offset_size = 4;
	  initial_length_size = 4;
	}

Nick Clifton's avatar
Nick Clifton committed
2255
      SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end);
2256 2257 2258

      cu_offset = start - section_begin;

Cary Coutant's avatar
Cary Coutant committed
2259 2260
      this_set = find_cu_tu_set_v2 (cu_offset, do_types);

Nick Clifton's avatar
Nick Clifton committed
2261
      SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end);
2262

Cary Coutant's avatar
Cary Coutant committed
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
      if (this_set == NULL)
	{
	  abbrev_base = 0;
	  abbrev_size = debug_displays [abbrev_sec].section.size;
	}
      else
	{
	  abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
	  abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
	}

Nick Clifton's avatar
Nick Clifton committed
2274
      SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
Cary Coutant's avatar
Cary Coutant committed
2275 2276 2277

      if (do_types)
        {
Nick Clifton's avatar
Nick Clifton committed
2278
	  SAFE_BYTE_GET64 (hdrptr, &signature_high, &signature_low, end);
2279
	  hdrptr += 8;
Nick Clifton's avatar
Nick Clifton committed
2280
	  SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end);
Cary Coutant's avatar
Cary Coutant committed
2281 2282
        }

2283
      if ((do_loc || do_debug_loc || do_debug_ranges)
Cary Coutant's avatar
Cary Coutant committed
2284 2285
	  && num_debug_info_entries == 0
	  && ! do_types)
2286 2287 2288 2289
	{
	  debug_information [unit].cu_offset = cu_offset;
	  debug_information [unit].pointer_size
	    = compunit.cu_pointer_size;
2290 2291
	  debug_information [unit].offset_size = offset_size;
	  debug_information [unit].dwarf_version = compunit.cu_version;
2292
	  debug_information [unit].base_address = 0;
2293 2294
	  debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
	  debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
2295 2296 2297 2298 2299 2300 2301 2302 2303
	  debug_information [unit].loc_offsets = NULL;
	  debug_information [unit].have_frame_base = NULL;
	  debug_information [unit].max_loc_offsets = 0;
	  debug_information [unit].num_loc_offsets = 0;
	  debug_information [unit].range_lists = NULL;
	  debug_information [unit].max_range_lists= 0;
	  debug_information [unit].num_range_lists = 0;
	}

2304
      if (!do_loc && dwarf_start_die == 0)
2305
	{
Kai Tietz's avatar
Kai Tietz committed
2306 2307 2308 2309
	  printf (_("  Compilation Unit @ offset 0x%s:\n"),
		  dwarf_vmatoa ("x", cu_offset));
	  printf (_("   Length:        0x%s (%s)\n"),
		  dwarf_vmatoa ("x", compunit.cu_length),
2310
		  offset_size == 8 ? "64-bit" : "32-bit");
2311
	  printf (_("   Version:       %d\n"), compunit.cu_version);
Alan Modra's avatar
Alan Modra committed
2312 2313
	  printf (_("   Abbrev Offset: 0x%s\n"),
		  dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
2314
	  printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
Cary Coutant's avatar
Cary Coutant committed
2315 2316
	  if (do_types)
	    {
2317 2318 2319 2320 2321 2322 2323
	      char buf[64];

	      printf (_("   Signature:     0x%s\n"),
		      dwarf_vmatoa64 (signature_high, signature_low,
				      buf, sizeof (buf)));
	      printf (_("   Type Offset:   0x%s\n"),
		      dwarf_vmatoa ("x", type_offset));
Cary Coutant's avatar
Cary Coutant committed
2324
	    }
Cary Coutant's avatar
Cary Coutant committed
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
	  if (this_set != NULL)
	    {
	      dwarf_vma *offsets = this_set->section_offsets;
	      size_t *sizes = this_set->section_sizes;

	      printf (_("   Section contributions:\n"));
	      printf (_("    .debug_abbrev.dwo:       0x%s  0x%s\n"),
		      dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
		      dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
	      printf (_("    .debug_line.dwo:         0x%s  0x%s\n"),
		      dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
		      dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
	      printf (_("    .debug_loc.dwo:          0x%s  0x%s\n"),
		      dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
		      dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
	      printf (_("    .debug_str_offsets.dwo:  0x%s  0x%s\n"),
		      dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
		      dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
	    }
2344 2345
	}

2346 2347 2348
      if (cu_offset + compunit.cu_length + initial_length_size
	  > section->size)
	{
Kai Tietz's avatar
Kai Tietz committed
2349 2350 2351 2352
	  warn (_("Debug info is corrupted, length of CU at %s"
	  	  " extends beyond end of section (length = %s)\n"),
		dwarf_vmatoa ("x", cu_offset),
		dwarf_vmatoa ("x", compunit.cu_length));
2353 2354 2355 2356 2357
	  break;
	}
      tags = hdrptr;
      start += compunit.cu_length + initial_length_size;

2358 2359 2360
      if (compunit.cu_version != 2
	  && compunit.cu_version != 3
	  && compunit.cu_version != 4)
2361
	{
Kai Tietz's avatar
Kai Tietz committed
2362 2363 2364
	  warn (_("CU at offset %s contains corrupt or "
		  "unsupported version number: %d.\n"),
		dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
2365 2366 2367 2368 2369
	  continue;
	}

      free_abbrevs ();

2370 2371
      /* Process the abbrevs used by this compilation unit. DWARF
	 sections under Mach-O have non-zero addresses.  */
Cary Coutant's avatar
Cary Coutant committed
2372
      if (compunit.cu_abbrev_offset >= abbrev_size)
2373 2374
	warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
	      (unsigned long) compunit.cu_abbrev_offset,
Cary Coutant's avatar
Cary Coutant committed
2375
	      (unsigned long) abbrev_size);
2376 2377
      else
	process_abbrev_section
Cary Coutant's avatar
Cary Coutant committed
2378 2379 2380 2381
	  (((unsigned char *) debug_displays [abbrev_sec].section.start
	    + abbrev_base + compunit.cu_abbrev_offset),
	   ((unsigned char *) debug_displays [abbrev_sec].section.start
	    + abbrev_base + abbrev_size));
2382 2383

      level = 0;
2384 2385
      last_level = level;
      saved_level = -1;
2386 2387 2388 2389
      while (tags < start)
	{
	  unsigned int bytes_read;
	  unsigned long abbrev_number;
2390
	  unsigned long die_offset;
2391 2392
	  abbrev_entry *entry;
	  abbrev_attr *attr;
2393
	  int do_printing = 1;
2394

2395 2396
	  die_offset = tags - section_begin;

Nick Clifton's avatar
Nick Clifton committed
2397
	  abbrev_number = read_uleb128 (tags, & bytes_read, start);
2398 2399
	  tags += bytes_read;

Jan Kratochvil's avatar
Jan Kratochvil committed
2400 2401
	  /* A null DIE marks the end of a list of siblings or it may also be
	     a section padding.  */
2402 2403
	  if (abbrev_number == 0)
	    {
Jan Kratochvil's avatar
Jan Kratochvil committed
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
	      /* Check if it can be a section padding for the last CU.  */
	      if (level == 0 && start == end)
		{
		  unsigned char *chk;

		  for (chk = tags; chk < start; chk++)
		    if (*chk != 0)
		      break;
		  if (chk == start)
		    break;
		}

2416 2417 2418
	      if (!do_loc && die_offset >= dwarf_start_die
		  && (dwarf_cutoff_level == -1
		      || level < dwarf_cutoff_level))
2419 2420 2421
		printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
			level, die_offset);

2422
	      --level;
2423 2424 2425 2426 2427 2428
	      if (level < 0)
		{
		  static unsigned num_bogus_warns = 0;

		  if (num_bogus_warns < 3)
		    {
2429 2430
		      warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
			    die_offset, section->name);
2431 2432 2433 2434 2435
		      num_bogus_warns ++;
		      if (num_bogus_warns == 3)
			warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
		    }
		}
2436 2437
	      if (dwarf_start_die != 0 && level < saved_level)
		return 1;
2438 2439 2440
	      continue;
	    }

Nick Clifton's avatar
Nick Clifton committed
2441
	  if (!do_loc)
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
	    {
	      if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
		do_printing = 0;
	      else
		{
		  if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
		    saved_level = level;
		  do_printing = (dwarf_cutoff_level == -1
				 || level < dwarf_cutoff_level);
		  if (do_printing)
		    printf (_(" <%d><%lx>: Abbrev Number: %lu"),
			    level, die_offset, abbrev_number);
		  else if (dwarf_cutoff_level == -1
			   || last_level < dwarf_cutoff_level)
		    printf (_(" <%d><%lx>: ...\n"), level, die_offset);
		  last_level = level;
		}
	    }
2460

2461 2462 2463 2464 2465 2466 2467 2468 2469
	  /* Scan through the abbreviation list until we reach the
	     correct entry.  */
	  for (entry = first_abbrev;
	       entry && entry->entry != abbrev_number;
	       entry = entry->next)
	    continue;

	  if (entry == NULL)
	    {
2470
	      if (!do_loc && do_printing)
Nick Clifton's avatar
Nick Clifton committed
2471 2472 2473 2474
		{
		  printf ("\n");
		  fflush (stdout);
		}
2475 2476
	      warn (_("DIE at offset %lx refers to abbreviation number %lu which does not exist\n"),
		    die_offset, abbrev_number);
2477 2478 2479
	      return 0;
	    }

2480
	  if (!do_loc && do_printing)
2481
	    printf (" (%s)\n", get_TAG_name (entry->tag));
2482

2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
	  switch (entry->tag)
	    {
	    default:
	      need_base_address = 0;
	      break;
	    case DW_TAG_compile_unit:
	      need_base_address = 1;
	      break;
	    case DW_TAG_entry_point:
	    case DW_TAG_subprogram:
	      need_base_address = 0;
	      /* Assuming that there is no DW_AT_frame_base.  */
	      have_frame_base = 0;
	      break;
	    }

2499 2500 2501
	  for (attr = entry->first_attr;
	       attr && attr->attribute;
	       attr = attr->next)
Nick Clifton's avatar
Nick Clifton committed
2502
	    {
2503 2504 2505
	      debug_info *arg;

	      if (! do_loc && do_printing)
Nick Clifton's avatar
Nick Clifton committed
2506
		/* Show the offset from where the tag was extracted.  */
2507 2508 2509 2510 2511
		printf ("    <%lx>", (unsigned long)(tags - section_begin));

	      arg = debug_information;
	      if (debug_information)
		arg += unit;
Nick Clifton's avatar
Nick Clifton committed
2512 2513 2514

	      tags = read_and_display_attr (attr->attribute,
					    attr->form,
Cary Coutant's avatar
Cary Coutant committed
2515
					    tags,
Nick Clifton's avatar
Nick Clifton committed
2516
					    end,
Cary Coutant's avatar
Cary Coutant committed
2517
					    cu_offset,
Nick Clifton's avatar
Nick Clifton committed
2518 2519 2520
					    compunit.cu_pointer_size,
					    offset_size,
					    compunit.cu_version,
2521
					    arg,
Cary Coutant's avatar
Cary Coutant committed
2522 2523 2524
					    do_loc || ! do_printing,
					    section,
					    this_set);
Nick Clifton's avatar
Nick Clifton committed
2525
	    }
2526

2527 2528 2529 2530
 	  if (entry->children)
 	    ++level;
 	}
    }
2531

2532 2533 2534
  /* Set num_debug_info_entries here so that it can be used to check if
     we need to process .debug_loc and .debug_ranges sections.  */
  if ((do_loc || do_debug_loc || do_debug_ranges)
Cary Coutant's avatar
Cary Coutant committed
2535 2536
      && num_debug_info_entries == 0
      && ! do_types)
2537
    num_debug_info_entries = num_units;
2538

2539
  if (!do_loc)
2540
    printf ("\n");
2541

2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
  return 1;
}

/* Locate and scan the .debug_info section in the file and record the pointer
   sizes and offsets for the compilation units in it.  Usually an executable
   will have just one pointer size, but this is not guaranteed, and so we try
   not to make any assumptions.  Returns zero upon failure, or the number of
   compilation units upon success.  */

static unsigned int
load_debug_info (void * file)
{
  /* Reset the last pointer size so that we can issue correct error
     messages if we are displaying the contents of more than one section.  */
  last_pointer_size = 0;
  warned_about_missing_comp_units = FALSE;

2559
  /* If we have already tried and failed to load the .debug_info
Cary Coutant's avatar
Cary Coutant committed
2560
     section then do not bother to repeat the task.  */
2561
  if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2562 2563
    return 0;

2564 2565 2566 2567
  /* If we already have the information there is nothing else to do.  */
  if (num_debug_info_entries > 0)
    return num_debug_info_entries;

Cary Coutant's avatar
Cary Coutant committed
2568 2569 2570
  /* If this is a DWARF package file, load the CU and TU indexes.  */
  load_cu_tu_indexes (file);

2571
  if (load_debug_section (info, file)
2572
      && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
2573
    return num_debug_info_entries;
2574 2575 2576 2577
  else if (load_debug_section (info_dwo, file)
           && process_debug_info (&debug_displays [info_dwo].section, file,
                                  abbrev_dwo, 1, 0))
    return num_debug_info_entries;
2578

2579
  num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2580
  return 0;
2581 2582
}

2583 2584 2585 2586
/* Read a DWARF .debug_line section header starting at DATA.
   Upon success returns an updated DATA pointer and the LINFO
   structure and the END_OF_SEQUENCE pointer will be filled in.
   Otherwise returns NULL.  */
2587

2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
static unsigned char *
read_debug_line_header (struct dwarf_section * section,
			unsigned char * data,
			unsigned char * end,
			DWARF2_Internal_LineInfo * linfo,
			unsigned char ** end_of_sequence)
{
  unsigned char *hdrptr;
  unsigned int offset_size;
  unsigned int initial_length_size;
2598

2599 2600
  /* Extract information from the Line Number Program Header.
     (section 6.2.4 in the Dwarf3 doc).  */
2601 2602
      hdrptr = data;

2603 2604
  /* Get and check the length of the block.  */
  SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
2605

2606
  if (linfo->li_length == 0xffffffff)
2607 2608
	{
	  /* This section is 64-bit DWARF 3.  */
2609
      SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
2610 2611 2612 2613 2614 2615 2616 2617 2618
	  offset_size = 8;
	  initial_length_size = 12;
	}
      else
	{
	  offset_size = 4;
	  initial_length_size = 4;
	}

2619
  if (linfo->li_length + initial_length_size > section->size)
2620
	{
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
      /* If the length is just a bias against the initial_length_size then
	 this means that the field has a relocation against it which has not
	 been applied.  (Ie we are dealing with an object file, not a linked
	 binary).  Do not complain but instead assume that the rest of the
	 section applies to this particular header.  */
      if (linfo->li_length == - initial_length_size)
	{
	  linfo->li_length = section->size - initial_length_size;
	}
      else
	{
	  warn (_("The line info appears to be corrupt - "
		  "the section is too small\n"));
	  return NULL;
	}
2636 2637
	}

2638 2639 2640 2641 2642 2643
  /* Get and check the version number.  */
  SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);

  if (linfo->li_version != 2
      && linfo->li_version != 3
      && linfo->li_version != 4)
2644
	{
2645
	  warn (_("Only DWARF version 2, 3 and 4 line info is currently supported.\n"));
2646
      return NULL;
2647 2648
	}

2649 2650
  SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr, offset_size, end);
  SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
Nick Clifton's avatar
Nick Clifton committed
2651

2652
  if (linfo->li_version >= 4)
Jakub Jelinek's avatar
Jakub Jelinek committed
2653
	{
2654
      SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
Nick Clifton's avatar
Nick Clifton committed
2655

2656
      if (linfo->li_max_ops_per_insn == 0)
Jakub Jelinek's avatar
Jakub Jelinek committed
2657 2658
	    {
	      warn (_("Invalid maximum operations per insn.\n"));
2659
	  return NULL;
Jakub Jelinek's avatar
Jakub Jelinek committed
2660 2661 2662
	    }
	}
      else
2663
    linfo->li_max_ops_per_insn = 1;
Nick Clifton's avatar
Nick Clifton committed
2664

2665 2666 2667 2668
  SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
  SAFE_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
  SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
  SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
2669 2670

      /* Sign extend the line base field.  */
2671 2672
  linfo->li_line_base <<= 24;
  linfo->li_line_base >>= 24;
2673

2674 2675 2676
  * end_of_sequence = data + linfo->li_length + initial_length_size;
  return hdrptr;
}
2677

2678 2679 2680 2681 2682 2683
static int
display_debug_lines_raw (struct dwarf_section *section,
			 unsigned char *data,
                         unsigned char *end)
{
  unsigned char *start = section->start;
2684

2685 2686
  printf (_("Raw dump of debug contents of section %s:\n\n"),
          section->name);
2687

2688 2689 2690 2691 2692 2693
  while (data < end)
    {
      static DWARF2_Internal_LineInfo saved_linfo;
      DWARF2_Internal_LineInfo linfo;
      unsigned char *standard_opcodes;
      unsigned char *end_of_sequence;
Cary Coutant's avatar
Cary Coutant committed
2694 2695
      unsigned int last_dir_entry = 0;
      int i;
2696

2697 2698 2699 2700
      if (const_strneq (section->name, ".debug_line.")
	  /* Note: the following does not apply to .debug_line.dwo sections.
	     These are full debug_line sections.  */
	  && strcmp (section->name, ".debug_line.dwo") != 0)
2701
	{
2702 2703 2704 2705 2706 2707 2708
	  /* Sections named .debug_line.<foo> are fragments of a .debug_line
	     section containing just the Line Number Statements.  They are
	     created by the assembler and intended to be used alongside gcc's
	     -ffunction-sections command line option.  When the linker's
	     garbage collection decides to discard a .text.<foo> section it
	     can then also discard the line number information in .debug_line.<foo>.

2709
	     Since the section is a fragment it does not have the details
2710
	     needed to fill out a LineInfo structure, so instead we use the
2711
	     details from the last full debug_line section that we processed.  */
2712 2713 2714 2715
	  end_of_sequence = end;
	  standard_opcodes = NULL;
	  linfo = saved_linfo;
	  reset_state_machine (linfo.li_default_is_stmt);
2716 2717 2718
	}
      else
	{
2719
	  unsigned char * hdrptr;
2720

2721 2722 2723
	  if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
						& end_of_sequence)) == NULL)
	    return 0;
2724

2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
	  printf (_("  Offset:                      0x%lx\n"), (long)(data - start));
	  printf (_("  Length:                      %ld\n"), (long) linfo.li_length);
	  printf (_("  DWARF Version:               %d\n"), linfo.li_version);
	  printf (_("  Prologue Length:             %d\n"), linfo.li_prologue_length);
	  printf (_("  Minimum Instruction Length:  %d\n"), linfo.li_min_insn_length);
	  if (linfo.li_version >= 4)
	    printf (_("  Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
	  printf (_("  Initial value of 'is_stmt':  %d\n"), linfo.li_default_is_stmt);
	  printf (_("  Line Base:                   %d\n"), linfo.li_line_base);
	  printf (_("  Line Range:                  %d\n"), linfo.li_line_range);
	  printf (_("  Opcode Base:                 %d\n"), linfo.li_opcode_base);
2736

2737
	  reset_state_machine (linfo.li_default_is_stmt);
2738

2739 2740
	  /* Display the contents of the Opcodes table.  */
	  standard_opcodes = hdrptr;
2741

2742
	  printf (_("\n Opcodes:\n"));
2743

2744 2745
	  for (i = 1; i < linfo.li_opcode_base; i++)
	    printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
2746

2747 2748
	  /* Display the contents of the Directory table.  */
	  data = standard_opcodes + linfo.li_opcode_base - 1;
2749

2750 2751 2752 2753
	  if (*data == 0)
	    printf (_("\n The Directory Table is empty.\n"));
	  else
	    {
Cary Coutant's avatar
Cary Coutant committed
2754 2755
	      printf (_("\n The Directory Table (offset 0x%lx):\n"),
		      (long)(data - start));
2756

2757
	      while (*data != 0)
Jakub Jelinek's avatar
Jakub Jelinek committed
2758
		{
Cary Coutant's avatar
Cary Coutant committed
2759
		  printf ("  %d\t%s\n", ++last_dir_entry, data);
2760 2761

		  data += strnlen ((char *) data, end - data) + 1;
Jakub Jelinek's avatar
Jakub Jelinek committed
2762
		}
2763
	    }
2764

2765 2766
	  /* Skip the NUL at the end of the table.  */
	  data++;
2767

2768 2769 2770 2771 2772
	  /* Display the contents of the File Name table.  */
	  if (*data == 0)
	    printf (_("\n The File Name Table is empty.\n"));
	  else
	    {
Cary Coutant's avatar
Cary Coutant committed
2773 2774
	      printf (_("\n The File Name Table (offset 0x%lx):\n"),
		      (long)(data - start));
2775
	      printf (_("  Entry\tDir\tTime\tSize\tName\n"));
2776

2777 2778 2779 2780
	      while (*data != 0)
		{
		  unsigned char *name;
		  unsigned int bytes_read;
2781

2782 2783 2784
		  printf ("  %d\t", ++state_machine_regs.last_file_entry);
		  name = data;
		  data += strnlen ((char *) data, end - data) + 1;
2785

2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
		  printf ("%s\t",
			  dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
		  data += bytes_read;
		  printf ("%s\t",
			  dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
		  data += bytes_read;
		  printf ("%s\t",
			  dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
		  data += bytes_read;
		  printf ("%s\n", name);
2796

2797 2798 2799 2800 2801
		  if (data == end)
		    {
		      warn (_("Corrupt file name table entry\n"));
		      break;
		    }
Jakub Jelinek's avatar
Jakub Jelinek committed
2802
		}
2803
	    }
2804

2805 2806 2807 2808 2809
	  /* Skip the NUL at the end of the table.  */
	  data++;
	  putchar ('\n');
	  saved_linfo = linfo;
	}
2810

2811 2812 2813 2814 2815 2816
      /* Now display the statements.  */
      if (data >= end_of_sequence)
	printf (_(" No Line Number Statements.\n"));
      else
	{
	  printf (_(" Line Number Statements:\n"));
2817

2818 2819 2820 2821 2822 2823
	  while (data < end_of_sequence)
	    {
	      unsigned char op_code;
	      dwarf_signed_vma adv;
	      dwarf_vma uladv;
	      unsigned int bytes_read;
2824

Cary Coutant's avatar
Cary Coutant committed
2825 2826
	      printf ("  [0x%08lx]", (long)(data - start));

2827
	      op_code = *data++;
2828

2829
	      if (op_code >= linfo.li_opcode_base)
2830
		{
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
		  op_code -= linfo.li_opcode_base;
		  uladv = (op_code / linfo.li_line_range);
		  if (linfo.li_max_ops_per_insn == 1)
		    {
		      uladv *= linfo.li_min_insn_length;
		      state_machine_regs.address += uladv;
		      printf (_("  Special opcode %d: "
				"advance Address by %s to 0x%s"),
			      op_code, dwarf_vmatoa ("u", uladv),
			      dwarf_vmatoa ("x", state_machine_regs.address));
		    }
		  else
		    {
		      state_machine_regs.address
			+= ((state_machine_regs.op_index + uladv)
			    / linfo.li_max_ops_per_insn)
			* linfo.li_min_insn_length;
		      state_machine_regs.op_index
			= (state_machine_regs.op_index + uladv)
			% linfo.li_max_ops_per_insn;
		      printf (_("  Special opcode %d: "
				"advance Address by %s to 0x%s[%d]"),
			      op_code, dwarf_vmatoa ("u", uladv),
			      dwarf_vmatoa ("x", state_machine_regs.address),
			      state_machine_regs.op_index);
		    }
		  adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
		  state_machine_regs.line += adv;
		  printf (_(" and Line by %s to %d\n"),
			  dwarf_vmatoa ("d", adv), state_machine_regs.line);
2861
		}
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
	      else switch (op_code)
		     {
		     case DW_LNS_extended_op:
		       data += process_extended_line_op (data, linfo.li_default_is_stmt, end);
		       break;

		     case DW_LNS_copy:
		       printf (_("  Copy\n"));
		       break;

		     case DW_LNS_advance_pc:
		       uladv = read_uleb128 (data, & bytes_read, end);
		       data += bytes_read;
		       if (linfo.li_max_ops_per_insn == 1)
			 {
			   uladv *= linfo.li_min_insn_length;
			   state_machine_regs.address += uladv;
			   printf (_("  Advance PC by %s to 0x%s\n"),
				   dwarf_vmatoa ("u", uladv),
				   dwarf_vmatoa ("x", state_machine_regs.address));
			 }
		       else
			 {
			   state_machine_regs.address
			     += ((state_machine_regs.op_index + uladv)
				 / linfo.li_max_ops_per_insn)
			     * linfo.li_min_insn_length;
			   state_machine_regs.op_index
			     = (state_machine_regs.op_index + uladv)
			     % linfo.li_max_ops_per_insn;
			   printf (_("  Advance PC by %s to 0x%s[%d]\n"),
				   dwarf_vmatoa ("u", uladv),
				   dwarf_vmatoa ("x", state_machine_regs.address),
				   state_machine_regs.op_index);
			 }
		       break;

		     case DW_LNS_advance_line:
		       adv = read_sleb128 (data, & bytes_read, end);
		       data += bytes_read;
		       state_machine_regs.line += adv;
		       printf (_("  Advance Line by %s to %d\n"),
			       dwarf_vmatoa ("d", adv),
			       state_machine_regs.line);
		       break;

		     case DW_LNS_set_file:
		       adv = read_uleb128 (data, & bytes_read, end);
		       data += bytes_read;
		       printf (_("  Set File Name to entry %s in the File Name Table\n"),
			       dwarf_vmatoa ("d", adv));
		       state_machine_regs.file = adv;
		       break;

		     case DW_LNS_set_column:
		       uladv = read_uleb128 (data, & bytes_read, end);
		       data += bytes_read;
		       printf (_("  Set column to %s\n"),
			       dwarf_vmatoa ("u", uladv));
		       state_machine_regs.column = uladv;
		       break;

		     case DW_LNS_negate_stmt:
		       adv = state_machine_regs.is_stmt;
		       adv = ! adv;
		       printf (_("  Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
		       state_machine_regs.is_stmt = adv;
		       break;

		     case DW_LNS_set_basic_block:
		       printf (_("  Set basic block\n"));
		       state_machine_regs.basic_block = 1;
		       break;

		     case DW_LNS_const_add_pc:
		       uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
		       if (linfo.li_max_ops_per_insn)
			 {
			   uladv *= linfo.li_min_insn_length;
			   state_machine_regs.address += uladv;
			   printf (_("  Advance PC by constant %s to 0x%s\n"),
				   dwarf_vmatoa ("u", uladv),
				   dwarf_vmatoa ("x", state_machine_regs.address));
			 }
		       else
			 {
			   state_machine_regs.address
			     += ((state_machine_regs.op_index + uladv)
				 / linfo.li_max_ops_per_insn)
			     * linfo.li_min_insn_length;
			   state_machine_regs.op_index
			     = (state_machine_regs.op_index + uladv)
			     % linfo.li_max_ops_per_insn;
			   printf (_("  Advance PC by constant %s to 0x%s[%d]\n"),
				   dwarf_vmatoa ("u", uladv),
				   dwarf_vmatoa ("x", state_machine_regs.address),
				   state_machine_regs.op_index);
			 }
		       break;

		     case DW_LNS_fixed_advance_pc:
		       SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
		       state_machine_regs.address += uladv;
		       state_machine_regs.op_index = 0;
		       printf (_("  Advance PC by fixed size amount %s to 0x%s\n"),
			       dwarf_vmatoa ("u", uladv),
			       dwarf_vmatoa ("x", state_machine_regs.address));
		       break;

		     case DW_LNS_set_prologue_end:
		       printf (_("  Set prologue_end to true\n"));
		       break;

		     case DW_LNS_set_epilogue_begin:
		       printf (_("  Set epilogue_begin to true\n"));
		       break;

		     case DW_LNS_set_isa:
		       uladv = read_uleb128 (data, & bytes_read, end);
		       data += bytes_read;
		       printf (_("  Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
		       break;

		     default:
		       printf (_("  Unknown opcode %d with operands: "), op_code);

		       if (standard_opcodes != NULL)
			 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
			   {
			     printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
										&bytes_read, end)),
				     i == 1 ? "" : ", ");
			     data += bytes_read;
			   }
		       putchar ('\n');
		       break;
		     }
2999
	    }
3000
	  putchar ('\n');
3001 3002 3003 3004 3005 3006
	}
    }

  return 1;
}

3007 3008
typedef struct
{
3009 3010 3011 3012
  unsigned char *name;
  unsigned int directory_index;
  unsigned int modification_date;
  unsigned int length;
3013 3014 3015 3016 3017 3018 3019 3020 3021
} File_Entry;

/* Output a decoded representation of the .debug_line section.  */

static int
display_debug_lines_decoded (struct dwarf_section *section,
			     unsigned char *data,
			     unsigned char *end)
{
3022 3023
  static DWARF2_Internal_LineInfo saved_linfo;

3024 3025 3026 3027 3028 3029
  printf (_("Decoded dump of debug contents of section %s:\n\n"),
          section->name);

  while (data < end)
    {
      /* This loop amounts to one iteration per compilation unit.  */
3030
      DWARF2_Internal_LineInfo linfo;
3031 3032 3033 3034
      unsigned char *standard_opcodes;
      unsigned char *end_of_sequence;
      int i;
      File_Entry *file_table = NULL;
3035
      unsigned int n_files = 0;
3036
      unsigned char **directory_table = NULL;
3037
      unsigned int n_directories = 0;
3038

3039 3040 3041 3042
      if (const_strneq (section->name, ".debug_line.")
	  /* Note: the following does not apply to .debug_line.dwo sections.
	     These are full debug_line sections.  */
	  && strcmp (section->name, ".debug_line.dwo") != 0)
3043
        {
3044
	  /* See comment in display_debug_lines_raw().  */
3045 3046 3047 3048
	  end_of_sequence = end;
	  standard_opcodes = NULL;
	  linfo = saved_linfo;
	  reset_state_machine (linfo.li_default_is_stmt);
3049 3050 3051
        }
      else
        {
3052
	  unsigned char *hdrptr;
3053

3054 3055
	  if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
						& end_of_sequence)) == NULL)
Jakub Jelinek's avatar
Jakub Jelinek committed
3056
	      return 0;
Nick Clifton's avatar
Nick Clifton committed
3057

3058
	  reset_state_machine (linfo.li_default_is_stmt);
3059

3060 3061
	  /* Save a pointer to the contents of the Opcodes table.  */
	  standard_opcodes = hdrptr;
3062

3063 3064 3065 3066 3067
	  /* Traverse the Directory table just to count entries.  */
	  data = standard_opcodes + linfo.li_opcode_base - 1;
	  if (*data != 0)
	    {
	      unsigned char *ptr_directory_table = data;
3068

3069 3070 3071 3072 3073
	      while (*data != 0)
		{
		  data += strnlen ((char *) data, end - data) + 1;
		  n_directories++;
		}
3074

3075 3076 3077
	      /* Go through the directory table again to save the directories.  */
	      directory_table = (unsigned char **)
		xmalloc (n_directories * sizeof (unsigned char *));
3078

3079 3080 3081 3082 3083 3084 3085 3086
	      i = 0;
	      while (*ptr_directory_table != 0)
		{
		  directory_table[i] = ptr_directory_table;
		  ptr_directory_table += strnlen ((char *) ptr_directory_table,
						  ptr_directory_table - end) + 1;
		  i++;
		}
3087
	    }
3088 3089
	  /* Skip the NUL at the end of the table.  */
	  data++;
3090

3091 3092 3093 3094
	  /* Traverse the File Name table just to count the entries.  */
	  if (*data != 0)
	    {
	      unsigned char *ptr_file_name_table = data;
3095

3096 3097 3098
	      while (*data != 0)
		{
		  unsigned int bytes_read;
3099

3100 3101 3102 3103 3104 3105 3106 3107 3108
		  /* Skip Name, directory index, last modification time and length
		     of file.  */
		  data += strnlen ((char *) data, end - data) + 1;
		  read_uleb128 (data, & bytes_read, end);
		  data += bytes_read;
		  read_uleb128 (data, & bytes_read, end);
		  data += bytes_read;
		  read_uleb128 (data, & bytes_read, end);
		  data += bytes_read;
3109

3110 3111
		  n_files++;
		}
3112

3113 3114
	      /* Go through the file table again to save the strings.  */
	      file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
3115

3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136
	      i = 0;
	      while (*ptr_file_name_table != 0)
		{
		  unsigned int bytes_read;

		  file_table[i].name = ptr_file_name_table;
		  ptr_file_name_table += strnlen ((char *) ptr_file_name_table,
						  end - ptr_file_name_table) + 1;

		  /* We are not interested in directory, time or size.  */
		  file_table[i].directory_index = read_uleb128 (ptr_file_name_table,
								& bytes_read, end);
		  ptr_file_name_table += bytes_read;
		  file_table[i].modification_date = read_uleb128 (ptr_file_name_table,
								  & bytes_read, end);
		  ptr_file_name_table += bytes_read;
		  file_table[i].length = read_uleb128 (ptr_file_name_table, & bytes_read, end);
		  ptr_file_name_table += bytes_read;
		  i++;
		}
	      i = 0;
3137

3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
	      /* Print the Compilation Unit's name and a header.  */
	      if (directory_table == NULL)
		{
		  printf (_("CU: %s:\n"), file_table[0].name);
		  printf (_("File name                            Line number    Starting address\n"));
		}
	      else
		{
		  unsigned int ix = file_table[0].directory_index;
		  const char *directory = ix ? (char *)directory_table[ix - 1] : ".";
3148

3149 3150 3151 3152
		  if (do_wide || strlen (directory) < 76)
		    printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
		  else
		    printf ("%s:\n", file_table[0].name);
Nick Clifton's avatar
Nick Clifton committed
3153

3154 3155 3156
		  printf (_("File name                            Line number    Starting address\n"));
		}
	    }
3157

3158 3159
	  /* Skip the NUL at the end of the table.  */
	  data++;
3160

3161 3162
	  saved_linfo = linfo;
	}
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174

      /* This loop iterates through the Dwarf Line Number Program.  */
      while (data < end_of_sequence)
        {
	  unsigned char op_code;
          int adv;
          unsigned long int uladv;
          unsigned int bytes_read;
          int is_special_opcode = 0;

          op_code = *data++;

3175
          if (op_code >= linfo.li_opcode_base)
3176
	    {
3177
	      op_code -= linfo.li_opcode_base;
Jakub Jelinek's avatar
Jakub Jelinek committed
3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
	      uladv = (op_code / linfo.li_line_range);
	      if (linfo.li_max_ops_per_insn == 1)
		{
		  uladv *= linfo.li_min_insn_length;
		  state_machine_regs.address += uladv;
		}
	      else
		{
		  state_machine_regs.address
		    += ((state_machine_regs.op_index + uladv)
			/ linfo.li_max_ops_per_insn)
3189
		    * linfo.li_min_insn_length;
Jakub Jelinek's avatar
Jakub Jelinek committed
3190 3191
		  state_machine_regs.op_index
		    = (state_machine_regs.op_index + uladv)
3192
		    % linfo.li_max_ops_per_insn;
Jakub Jelinek's avatar
Jakub Jelinek committed
3193
		}
3194

3195
              adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
3196 3197 3198 3199
              state_machine_regs.line += adv;
              is_special_opcode = 1;
            }
          else switch (op_code)
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225
		 {
		 case DW_LNS_extended_op:
		   {
		     unsigned int ext_op_code_len;
		     unsigned char ext_op_code;
		     unsigned char *op_code_data = data;

		     ext_op_code_len = read_uleb128 (op_code_data, &bytes_read,
						     end_of_sequence);
		     op_code_data += bytes_read;

		     if (ext_op_code_len == 0)
		       {
			 warn (_("badly formed extended line op encountered!\n"));
			 break;
		       }
		     ext_op_code_len += bytes_read;
		     ext_op_code = *op_code_data++;

		     switch (ext_op_code)
		       {
		       case DW_LNE_end_sequence:
			 reset_state_machine (linfo.li_default_is_stmt);
			 break;
		       case DW_LNE_set_address:
			 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
Cary Coutant's avatar
Cary Coutant committed
3226 3227
						op_code_data,
						ext_op_code_len - bytes_read - 1,
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
						end);
			 state_machine_regs.op_index = 0;
			 break;
		       case DW_LNE_define_file:
			 {
			   file_table = (File_Entry *) xrealloc
			     (file_table, (n_files + 1) * sizeof (File_Entry));

			   ++state_machine_regs.last_file_entry;
			   /* Source file name.  */
			   file_table[n_files].name = op_code_data;
			   op_code_data += strlen ((char *) op_code_data) + 1;
			   /* Directory index.  */
			   file_table[n_files].directory_index =
			     read_uleb128 (op_code_data, & bytes_read,
					   end_of_sequence);
			   op_code_data += bytes_read;
			   /* Last modification time.  */
			   file_table[n_files].modification_date =
			     read_uleb128 (op_code_data, & bytes_read,
					   end_of_sequence);
			   op_code_data += bytes_read;
			   /* File length.  */
			   file_table[n_files].length =
			     read_uleb128 (op_code_data, & bytes_read,
					   end_of_sequence);

			   n_files++;
			   break;
			 }
		       case DW_LNE_set_discriminator:
		       case DW_LNE_HP_set_sequence:
			 /* Simply ignored.  */
			 break;

		       default:
			 printf (_("UNKNOWN (%u): length %d\n"),
				 ext_op_code, ext_op_code_len - bytes_read);
			 break;
		       }
		     data += ext_op_code_len;
		     break;
		   }
		 case DW_LNS_copy:
		   break;

		 case DW_LNS_advance_pc:
		   uladv = read_uleb128 (data, & bytes_read, end);
		   data += bytes_read;
		   if (linfo.li_max_ops_per_insn == 1)
		     {
		       uladv *= linfo.li_min_insn_length;
		       state_machine_regs.address += uladv;
		     }
		   else
		     {
		       state_machine_regs.address
			 += ((state_machine_regs.op_index + uladv)
			     / linfo.li_max_ops_per_insn)
			 * linfo.li_min_insn_length;
		       state_machine_regs.op_index
			 = (state_machine_regs.op_index + uladv)
			 % linfo.li_max_ops_per_insn;
		     }
		   break;

		 case DW_LNS_advance_line:
		   adv = read_sleb128 (data, & bytes_read, end);
		   data += bytes_read;
		   state_machine_regs.line += adv;
		   break;

		 case DW_LNS_set_file:
		   adv = read_uleb128 (data, & bytes_read, end);
		   data += bytes_read;
		   state_machine_regs.file = adv;

		   if (file_table == NULL)
		     printf (_("\n [Use file table entry %d]\n"), state_machine_regs.file - 1);
		   else if (file_table[state_machine_regs.file - 1].directory_index == 0)
		     /* If directory index is 0, that means current directory.  */
		     printf ("\n./%s:[++]\n",
			     file_table[state_machine_regs.file - 1].name);
		   else if (directory_table == NULL)
		     printf (_("\n [Use directory table entry %d]\n"),
			     file_table[state_machine_regs.file - 1].directory_index - 1);
		   else
		     /* The directory index starts counting at 1.  */
		     printf ("\n%s/%s:\n",
			     directory_table[file_table[state_machine_regs.file - 1].directory_index - 1],
			     file_table[state_machine_regs.file - 1].name);
		   break;

		 case DW_LNS_set_column:
		   uladv = read_uleb128 (data, & bytes_read, end);
		   data += bytes_read;
		   state_machine_regs.column = uladv;
		   break;

		 case DW_LNS_negate_stmt:
		   adv = state_machine_regs.is_stmt;
		   adv = ! adv;
		   state_machine_regs.is_stmt = adv;
		   break;

		 case DW_LNS_set_basic_block:
		   state_machine_regs.basic_block = 1;
		   break;

		 case DW_LNS_const_add_pc:
		   uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
		   if (linfo.li_max_ops_per_insn == 1)
		     {
		       uladv *= linfo.li_min_insn_length;
		       state_machine_regs.address += uladv;
		     }
		   else
		     {
		       state_machine_regs.address
			 += ((state_machine_regs.op_index + uladv)
			     / linfo.li_max_ops_per_insn)
			 * linfo.li_min_insn_length;
		       state_machine_regs.op_index
			 = (state_machine_regs.op_index + uladv)
			 % linfo.li_max_ops_per_insn;
		     }
		   break;

		 case DW_LNS_fixed_advance_pc:
		   SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
		   state_machine_regs.address += uladv;
		   state_machine_regs.op_index = 0;
		   break;

		 case DW_LNS_set_prologue_end:
		   break;

		 case DW_LNS_set_epilogue_begin:
		   break;

		 case DW_LNS_set_isa:
		   uladv = read_uleb128 (data, & bytes_read, end);
		   data += bytes_read;
		   printf (_("  Set ISA to %lu\n"), uladv);
		   break;

		 default:
		   printf (_("  Unknown opcode %d with operands: "), op_code);

		   if (standard_opcodes != NULL)
		     for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
		       {
			 printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
									    &bytes_read, end)),
				 i == 1 ? "" : ", ");
			 data += bytes_read;
		       }
		   putchar ('\n');
		   break;
		 }
3388 3389 3390 3391 3392 3393 3394

          /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
             to the DWARF address/line matrix.  */
          if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
	      || (op_code == DW_LNS_copy))
            {
              const unsigned int MAX_FILENAME_LENGTH = 35;
3395
              char *fileName;
3396
              char *newFileName = NULL;
3397 3398 3399 3400 3401 3402 3403 3404
              size_t fileNameLength;

	      if (file_table)
		fileName = (char *) file_table[state_machine_regs.file - 1].name;
	      else
		fileName = "<unknown>";

	      fileNameLength = strlen (fileName);
3405 3406 3407

              if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
                {
3408
                  newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
3409 3410 3411 3412 3413 3414 3415
                  /* Truncate file name */
                  strncpy (newFileName,
                           fileName + fileNameLength - MAX_FILENAME_LENGTH,
                           MAX_FILENAME_LENGTH + 1);
                }
              else
                {
3416
                  newFileName = (char *) xmalloc (fileNameLength + 1);
3417 3418 3419 3420 3421
                  strncpy (newFileName, fileName, fileNameLength + 1);
                }

              if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
                {
Jakub Jelinek's avatar
Jakub Jelinek committed
3422
		  if (linfo.li_max_ops_per_insn == 1)
3423 3424
		    printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x\n",
			    newFileName, state_machine_regs.line,
Jakub Jelinek's avatar
Jakub Jelinek committed
3425 3426
			    state_machine_regs.address);
		  else
3427 3428
		    printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
			    newFileName, state_machine_regs.line,
Jakub Jelinek's avatar
Jakub Jelinek committed
3429 3430
			    state_machine_regs.address,
			    state_machine_regs.op_index);
3431 3432 3433
                }
              else
                {
Jakub Jelinek's avatar
Jakub Jelinek committed
3434
		  if (linfo.li_max_ops_per_insn == 1)
3435 3436
		    printf ("%s  %11d  %#18" DWARF_VMA_FMT "x\n",
			    newFileName, state_machine_regs.line,
Jakub Jelinek's avatar
Jakub Jelinek committed
3437 3438
			    state_machine_regs.address);
		  else
3439 3440
		    printf ("%s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
			    newFileName, state_machine_regs.line,
Jakub Jelinek's avatar
Jakub Jelinek committed
3441 3442
			    state_machine_regs.address,
			    state_machine_regs.op_index);
3443 3444 3445 3446 3447 3448 3449 3450
                }

              if (op_code == DW_LNE_end_sequence)
		printf ("\n");

              free (newFileName);
            }
        }
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465

      if (file_table)
	{
	  free (file_table);
	  file_table = NULL;
	  n_files = 0;
	}

      if (directory_table)
	{
	  free (directory_table);
	  directory_table = NULL;
	  n_directories = 0;
	}

3466 3467 3468 3469 3470 3471 3472
      putchar ('\n');
    }

  return 1;
}

static int
3473
display_debug_lines (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
3474 3475 3476
{
  unsigned char *data = section->start;
  unsigned char *end = data + section->size;
3477 3478
  int retValRaw = 1;
  int retValDecoded = 1;
3479

3480 3481 3482
  if (do_debug_lines == 0)
    do_debug_lines |= FLAG_DEBUG_LINES_RAW;

3483
  if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
3484 3485
    retValRaw = display_debug_lines_raw (section, data, end);

3486
  if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
3487 3488
    retValDecoded = display_debug_lines_decoded (section, data, end);

3489
  if (!retValRaw || !retValDecoded)
3490 3491 3492 3493 3494
    return 0;

  return 1;
}

3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509
static debug_info *
find_debug_info_for_offset (unsigned long offset)
{
  unsigned int i;

  if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
    return NULL;

  for (i = 0; i < num_debug_info_entries; i++)
    if (debug_information[i].cu_offset == offset)
      return debug_information + i;

  return NULL;
}

3510 3511 3512 3513
static int
display_debug_pubnames (struct dwarf_section *section,
			void *file ATTRIBUTE_UNUSED)
{
3514
  DWARF2_Internal_PubNames names;
3515 3516 3517
  unsigned char *start = section->start;
  unsigned char *end = start + section->size;

3518 3519 3520 3521
  /* It does not matter if this load fails,
     we test for that later on.  */
  load_debug_info (file);

3522 3523 3524 3525 3526 3527
  printf (_("Contents of the %s section:\n\n"), section->name);

  while (start < end)
    {
      unsigned char *data;
      unsigned long offset;
3528
      unsigned int offset_size, initial_length_size;
3529 3530 3531

      data = start;

Nick Clifton's avatar
Nick Clifton committed
3532
      SAFE_BYTE_GET_AND_INC (names.pn_length, data, 4, end);
3533
      if (names.pn_length == 0xffffffff)
3534
	{
Nick Clifton's avatar
Nick Clifton committed
3535
	  SAFE_BYTE_GET_AND_INC (names.pn_length, data, 8, end);
3536 3537 3538 3539 3540 3541 3542 3543 3544
	  offset_size = 8;
	  initial_length_size = 12;
	}
      else
	{
	  offset_size = 4;
	  initial_length_size = 4;
	}

Nick Clifton's avatar
Nick Clifton committed
3545 3546
      SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, end);
      SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, end);
3547 3548 3549

      if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
	  && num_debug_info_entries > 0
3550
	  && find_debug_info_for_offset (names.pn_offset) == NULL)
3551
	warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
Kai Tietz's avatar
Kai Tietz committed
3552
	      (unsigned long) names.pn_offset, section->name);
3553

Nick Clifton's avatar
Nick Clifton committed
3554
      SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, end);
3555

3556
      start += names.pn_length + initial_length_size;
3557

3558
      if (names.pn_version != 2 && names.pn_version != 3)
3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
	{
	  static int warned = 0;

	  if (! warned)
	    {
	      warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
	      warned = 1;
	    }

	  continue;
	}

      printf (_("  Length:                              %ld\n"),
Kai Tietz's avatar
Kai Tietz committed
3572
	      (long) names.pn_length);
3573
      printf (_("  Version:                             %d\n"),
3574
	      names.pn_version);
3575
      printf (_("  Offset into .debug_info section:     0x%lx\n"),
Kai Tietz's avatar
Kai Tietz committed
3576
	      (unsigned long) names.pn_offset);
3577
      printf (_("  Size of area in .debug_info section: %ld\n"),
Kai Tietz's avatar
Kai Tietz committed
3578
	      (long) names.pn_size);
3579 3580 3581 3582 3583

      printf (_("\n    Offset\tName\n"));

      do
	{
Nick Clifton's avatar
Nick Clifton committed
3584
	  SAFE_BYTE_GET (offset, data, offset_size, end);
3585 3586 3587 3588

	  if (offset != 0)
	    {
	      data += offset_size;
3589
	      printf ("    %-6lx\t%s\n", offset, data);
Nick Clifton's avatar
Nick Clifton committed
3590
	      data += strnlen ((char *) data, end - data) + 1;
3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614
	    }
	}
      while (offset != 0);
    }

  printf ("\n");
  return 1;
}

static int
display_debug_macinfo (struct dwarf_section *section,
		       void *file ATTRIBUTE_UNUSED)
{
  unsigned char *start = section->start;
  unsigned char *end = start + section->size;
  unsigned char *curr = start;
  unsigned int bytes_read;
  enum dwarf_macinfo_record_type op;

  printf (_("Contents of the %s section:\n\n"), section->name);

  while (curr < end)
    {
      unsigned int lineno;
Nick Clifton's avatar
Nick Clifton committed
3615
      const unsigned char *string;
3616

3617
      op = (enum dwarf_macinfo_record_type) *curr;
3618 3619 3620 3621 3622 3623 3624 3625
      curr++;

      switch (op)
	{
	case DW_MACINFO_start_file:
	  {
	    unsigned int filenum;

Nick Clifton's avatar
Nick Clifton committed
3626
	    lineno = read_uleb128 (curr, & bytes_read, end);
3627
	    curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3628
	    filenum = read_uleb128 (curr, & bytes_read, end);
3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640
	    curr += bytes_read;

	    printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
		    lineno, filenum);
	  }
	  break;

	case DW_MACINFO_end_file:
	  printf (_(" DW_MACINFO_end_file\n"));
	  break;

	case DW_MACINFO_define:
Nick Clifton's avatar
Nick Clifton committed
3641
	  lineno = read_uleb128 (curr, & bytes_read, end);
3642
	  curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3643 3644
	  string = curr;
	  curr += strnlen ((char *) string, end - string) + 1;
3645 3646 3647 3648 3649
	  printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
		  lineno, string);
	  break;

	case DW_MACINFO_undef:
Nick Clifton's avatar
Nick Clifton committed
3650
	  lineno = read_uleb128 (curr, & bytes_read, end);
3651
	  curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3652 3653
	  string = curr;
	  curr += strnlen ((char *) string, end - string) + 1;
3654 3655 3656 3657 3658 3659 3660 3661
	  printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
		  lineno, string);
	  break;

	case DW_MACINFO_vendor_ext:
	  {
	    unsigned int constant;

Nick Clifton's avatar
Nick Clifton committed
3662
	    constant = read_uleb128 (curr, & bytes_read, end);
3663
	    curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3664 3665
	    string = curr;
	    curr += strnlen ((char *) string, end - string) + 1;
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
	    printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
		    constant, string);
	  }
	  break;
	}
    }

  return 1;
}

3676 3677 3678 3679 3680
/* Given LINE_OFFSET into the .debug_line section, attempt to return
   filename and dirname corresponding to file name table entry with index
   FILEIDX.  Return NULL on failure.  */

static unsigned char *
Nick Clifton's avatar
Nick Clifton committed
3681 3682
get_line_filename_and_dirname (dwarf_vma line_offset,
			       dwarf_vma fileidx,
3683 3684 3685 3686 3687 3688 3689
			       unsigned char **dir_name)
{
  struct dwarf_section *section = &debug_displays [line].section;
  unsigned char *hdrptr, *dirtable, *file_name;
  unsigned int offset_size, initial_length_size;
  unsigned int version, opcode_base, bytes_read;
  dwarf_vma length, diridx;
Nick Clifton's avatar
Nick Clifton committed
3690
  const unsigned char * end;
3691 3692 3693 3694 3695 3696 3697 3698

  *dir_name = NULL;
  if (section->start == NULL
      || line_offset >= section->size
      || fileidx == 0)
    return NULL;

  hdrptr = section->start + line_offset;
Nick Clifton's avatar
Nick Clifton committed
3699
  end = section->start + section->size;
Nick Clifton's avatar
Nick Clifton committed
3700 3701

  SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
3702 3703 3704
  if (length == 0xffffffff)
    {
      /* This section is 64-bit DWARF 3.  */
Nick Clifton's avatar
Nick Clifton committed
3705
      SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
      offset_size = 8;
      initial_length_size = 12;
    }
  else
    {
      offset_size = 4;
      initial_length_size = 4;
    }
  if (length + initial_length_size > section->size)
    return NULL;
Nick Clifton's avatar
Nick Clifton committed
3716 3717

  SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
3718 3719 3720 3721 3722 3723
  if (version != 2 && version != 3 && version != 4)
    return NULL;
  hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length.  */
  if (version >= 4)
    hdrptr++;		    /* Skip max_ops_per_insn.  */
  hdrptr += 3;		    /* Skip default_is_stmt, line_base, line_range.  */
Nick Clifton's avatar
Nick Clifton committed
3724 3725

  SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
3726 3727
  if (opcode_base == 0)
    return NULL;
Nick Clifton's avatar
Nick Clifton committed
3728

3729 3730 3731 3732
  hdrptr += opcode_base - 1;
  dirtable = hdrptr;
  /* Skip over dirname table.  */
  while (*hdrptr != '\0')
Nick Clifton's avatar
Nick Clifton committed
3733
    hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
3734 3735 3736 3737
  hdrptr++;		    /* Skip the NUL at the end of the table.  */
  /* Now skip over preceding filename table entries.  */
  for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
    {
Nick Clifton's avatar
Nick Clifton committed
3738
      hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
Nick Clifton's avatar
Nick Clifton committed
3739
      read_uleb128 (hdrptr, &bytes_read, end);
3740
      hdrptr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3741
      read_uleb128 (hdrptr, &bytes_read, end);
3742
      hdrptr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3743
      read_uleb128 (hdrptr, &bytes_read, end);
3744 3745
      hdrptr += bytes_read;
    }
Nick Clifton's avatar
Nick Clifton committed
3746
  if (hdrptr == end || *hdrptr == '\0')
3747 3748
    return NULL;
  file_name = hdrptr;
Nick Clifton's avatar
Nick Clifton committed
3749
  hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
Nick Clifton's avatar
Nick Clifton committed
3750
  diridx = read_uleb128 (hdrptr, &bytes_read, end);
3751 3752 3753
  if (diridx == 0)
    return file_name;
  for (; *dirtable != '\0' && diridx > 1; diridx--)
Nick Clifton's avatar
Nick Clifton committed
3754
    dirtable += strnlen ((char *) dirtable, end - dirtable) + 1;
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
  if (*dirtable == '\0')
    return NULL;
  *dir_name = dirtable;
  return file_name;
}

static int
display_debug_macro (struct dwarf_section *section,
		     void *file)
{
  unsigned char *start = section->start;
  unsigned char *end = start + section->size;
  unsigned char *curr = start;
  unsigned char *extended_op_buf[256];
  unsigned int bytes_read;

  load_debug_section (str, file);
  load_debug_section (line, file);

  printf (_("Contents of the %s section:\n\n"), section->name);

  while (curr < end)
    {
      unsigned int lineno, version, flags;
      unsigned int offset_size = 4;
Nick Clifton's avatar
Nick Clifton committed
3780
      const unsigned char *string;
3781 3782 3783
      dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
      unsigned char **extended_ops = NULL;

Nick Clifton's avatar
Nick Clifton committed
3784
      SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
3785 3786 3787 3788 3789 3790 3791
      if (version != 4)
	{
	  error (_("Only GNU extension to DWARF 4 of %s is currently supported.\n"),
		 section->name);
	  return 0;
	}

Nick Clifton's avatar
Nick Clifton committed
3792
      SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
3793 3794 3795 3796 3797 3798 3799 3800
      if (flags & 1)
	offset_size = 8;
      printf (_("  Offset:                      0x%lx\n"),
	      (unsigned long) sec_offset);
      printf (_("  Version:                     %d\n"), version);
      printf (_("  Offset size:                 %d\n"), offset_size);
      if (flags & 2)
	{
Nick Clifton's avatar
Nick Clifton committed
3801
	  SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
3802 3803 3804 3805 3806
	  printf (_("  Offset into .debug_line:     0x%lx\n"),
		  (unsigned long) line_offset);
	}
      if (flags & 4)
	{
Nick Clifton's avatar
Nick Clifton committed
3807
	  unsigned int i, count, op;
3808
	  dwarf_vma nargs, n;
Nick Clifton's avatar
Nick Clifton committed
3809 3810

	  SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
3811

3812 3813 3814 3815 3816 3817 3818
	  memset (extended_op_buf, 0, sizeof (extended_op_buf));
	  extended_ops = extended_op_buf;
	  if (count)
	    {
	      printf (_("  Extension opcode arguments:\n"));
	      for (i = 0; i < count; i++)
		{
Nick Clifton's avatar
Nick Clifton committed
3819
		  SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
3820
		  extended_ops[op] = curr;
Nick Clifton's avatar
Nick Clifton committed
3821
		  nargs = read_uleb128 (curr, &bytes_read, end);
3822 3823 3824 3825 3826 3827 3828 3829
		  curr += bytes_read;
		  if (nargs == 0)
		    printf (_("    DW_MACRO_GNU_%02x has no arguments\n"), op);
		  else
		    {
		      printf (_("    DW_MACRO_GNU_%02x arguments: "), op);
		      for (n = 0; n < nargs; n++)
			{
Nick Clifton's avatar
Nick Clifton committed
3830 3831 3832
			  unsigned int form;

			  SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873
			  printf ("%s%s", get_FORM_name (form),
				  n == nargs - 1 ? "\n" : ", ");
			  switch (form)
			    {
			    case DW_FORM_data1:
			    case DW_FORM_data2:
			    case DW_FORM_data4:
			    case DW_FORM_data8:
			    case DW_FORM_sdata:
			    case DW_FORM_udata:
			    case DW_FORM_block:
			    case DW_FORM_block1:
			    case DW_FORM_block2:
			    case DW_FORM_block4:
			    case DW_FORM_flag:
			    case DW_FORM_string:
			    case DW_FORM_strp:
			    case DW_FORM_sec_offset:
			      break;
			    default:
			      error (_("Invalid extension opcode form %s\n"),
				     get_FORM_name (form));
			      return 0;
			    }
			}
		    }
		}
	    }
	}
      printf ("\n");

      while (1)
	{
	  unsigned int op;

	  if (curr >= end)
	    {
	      error (_(".debug_macro section not zero terminated\n"));
	      return 0;
	    }

Nick Clifton's avatar
Nick Clifton committed
3874
	  SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
3875 3876 3877 3878 3879 3880 3881 3882 3883 3884
	  if (op == 0)
	    break;

	  switch (op)
	    {
	    case DW_MACRO_GNU_start_file:
	      {
		unsigned int filenum;
		unsigned char *file_name = NULL, *dir_name = NULL;

Nick Clifton's avatar
Nick Clifton committed
3885
		lineno = read_uleb128 (curr, &bytes_read, end);
3886
		curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3887
		filenum = read_uleb128 (curr, &bytes_read, end);
3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911
		curr += bytes_read;

		if ((flags & 2) == 0)
		  error (_("DW_MACRO_GNU_start_file used, but no .debug_line offset provided.\n"));
		else
		  file_name
		    = get_line_filename_and_dirname (line_offset, filenum,
						     &dir_name);
		if (file_name == NULL)
		  printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d\n"),
			  lineno, filenum);
		else
		  printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
			  lineno, filenum,
			  dir_name != NULL ? (const char *) dir_name : "",
			  dir_name != NULL ? "/" : "", file_name);
	      }
	      break;

	    case DW_MACRO_GNU_end_file:
	      printf (_(" DW_MACRO_GNU_end_file\n"));
	      break;

	    case DW_MACRO_GNU_define:
Nick Clifton's avatar
Nick Clifton committed
3912
	      lineno = read_uleb128 (curr, &bytes_read, end);
3913
	      curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3914 3915
	      string = curr;
	      curr += strnlen ((char *) string, end - string) + 1;
3916 3917 3918 3919 3920
	      printf (_(" DW_MACRO_GNU_define - lineno : %d macro : %s\n"),
		      lineno, string);
	      break;

	    case DW_MACRO_GNU_undef:
Nick Clifton's avatar
Nick Clifton committed
3921
	      lineno = read_uleb128 (curr, &bytes_read, end);
3922
	      curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3923 3924
	      string = curr;
	      curr += strnlen ((char *) string, end - string) + 1;
3925 3926 3927 3928 3929
	      printf (_(" DW_MACRO_GNU_undef - lineno : %d macro : %s\n"),
		      lineno, string);
	      break;

	    case DW_MACRO_GNU_define_indirect:
Nick Clifton's avatar
Nick Clifton committed
3930
	      lineno = read_uleb128 (curr, &bytes_read, end);
3931
	      curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3932
	      SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
3933 3934 3935 3936 3937 3938
	      string = fetch_indirect_string (offset);
	      printf (_(" DW_MACRO_GNU_define_indirect - lineno : %d macro : %s\n"),
		      lineno, string);
	      break;

	    case DW_MACRO_GNU_undef_indirect:
Nick Clifton's avatar
Nick Clifton committed
3939
	      lineno = read_uleb128 (curr, &bytes_read, end);
3940
	      curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3941
	      SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
3942 3943 3944 3945 3946 3947
	      string = fetch_indirect_string (offset);
	      printf (_(" DW_MACRO_GNU_undef_indirect - lineno : %d macro : %s\n"),
		      lineno, string);
	      break;

	    case DW_MACRO_GNU_transparent_include:
Nick Clifton's avatar
Nick Clifton committed
3948
	      SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
3949 3950 3951 3952
	      printf (_(" DW_MACRO_GNU_transparent_include - offset : 0x%lx\n"),
		      (unsigned long) offset);
	      break;

3953
	    case DW_MACRO_GNU_define_indirect_alt:
Nick Clifton's avatar
Nick Clifton committed
3954
	      lineno = read_uleb128 (curr, &bytes_read, end);
3955
	      curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3956
	      SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
3957 3958 3959 3960 3961
	      printf (_(" DW_MACRO_GNU_define_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
		      lineno, (unsigned long) offset);
	      break;

	    case DW_MACRO_GNU_undef_indirect_alt:
Nick Clifton's avatar
Nick Clifton committed
3962
	      lineno = read_uleb128 (curr, &bytes_read, end);
3963
	      curr += bytes_read;
Nick Clifton's avatar
Nick Clifton committed
3964
	      SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
3965 3966 3967 3968 3969
	      printf (_(" DW_MACRO_GNU_undef_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
		      lineno, (unsigned long) offset);
	      break;

	    case DW_MACRO_GNU_transparent_include_alt:
Nick Clifton's avatar
Nick Clifton committed
3970
	      SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
3971 3972 3973 3974
	      printf (_(" DW_MACRO_GNU_transparent_include_alt - offset : 0x%lx\n"),
		      (unsigned long) offset);
	      break;

3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985
	    default:
	      if (extended_ops == NULL || extended_ops[op] == NULL)
		{
		  error (_(" Unknown macro opcode %02x seen\n"), op);
		  return 0;
		}
	      else
		{
		  /* Skip over unhandled opcodes.  */
		  dwarf_vma nargs, n;
		  unsigned char *desc = extended_ops[op];
Nick Clifton's avatar
Nick Clifton committed
3986
		  nargs = read_uleb128 (desc, &bytes_read, end);
3987 3988 3989 3990 3991 3992 3993 3994 3995
		  desc += bytes_read;
		  if (nargs == 0)
		    {
		      printf (_(" DW_MACRO_GNU_%02x\n"), op);
		      break;
		    }
		  printf (_(" DW_MACRO_GNU_%02x -"), op);
		  for (n = 0; n < nargs; n++)
		    {
Nick Clifton's avatar
Nick Clifton committed
3996 3997 3998
		      int val;

		      SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
3999
		      curr
Nick Clifton's avatar
Nick Clifton committed
4000
			= read_and_display_attr_value (0, val,
Nick Clifton's avatar
Nick Clifton committed
4001
						       curr, end, 0, 0, offset_size,
Cary Coutant's avatar
Cary Coutant committed
4002 4003
						       version, NULL, 0, NULL,
						       NULL);
4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018
		      if (n != nargs - 1)
			printf (",");
		    }
		  printf ("\n");
		}
	      break;
	    }
	}

      printf ("\n");
    }	

  return 1;
}

4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030
static int
display_debug_abbrev (struct dwarf_section *section,
		      void *file ATTRIBUTE_UNUSED)
{
  abbrev_entry *entry;
  unsigned char *start = section->start;
  unsigned char *end = start + section->size;

  printf (_("Contents of the %s section:\n\n"), section->name);

  do
    {
Alan Modra's avatar
Alan Modra committed
4031 4032
      unsigned char *last;

4033 4034
      free_abbrevs ();

Alan Modra's avatar
Alan Modra committed
4035
      last = start;
4036 4037 4038 4039 4040
      start = process_abbrev_section (start, end);

      if (first_abbrev == NULL)
	continue;

Alan Modra's avatar
Alan Modra committed
4041
      printf (_("  Number TAG (0x%lx)\n"), (long) (last - section->start));
4042 4043 4044 4045 4046

      for (entry = first_abbrev; entry; entry = entry->next)
	{
	  abbrev_attr *attr;

4047
	  printf ("   %ld      %s    [%s]\n",
4048 4049 4050 4051 4052
		  entry->entry,
		  get_TAG_name (entry->tag),
		  entry->children ? _("has children") : _("no children"));

	  for (attr = entry->first_attr; attr; attr = attr->next)
4053
	    printf ("    %-18s %s\n",
4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064
		    get_AT_name (attr->attribute),
		    get_FORM_name (attr->form));
	}
    }
  while (start);

  printf ("\n");

  return 1;
}

4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
/* Display a location list from a normal (ie, non-dwo) .debug_loc section.  */

static void
display_loc_list (struct dwarf_section *section,
                  unsigned char **start_ptr,
                  int debug_info_entry,
                  unsigned long offset,
                  unsigned long base_address,
                  int has_frame_base)
{
  unsigned char *start = *start_ptr;
  unsigned char *section_end = section->start + section->size;
  unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
  unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
  unsigned int offset_size = debug_information [debug_info_entry].offset_size;
  int dwarf_version = debug_information [debug_info_entry].dwarf_version;

  dwarf_vma begin;
  dwarf_vma end;
  unsigned short length;
  int need_frame_base;

  while (1)
    {
      if (start + 2 * pointer_size > section_end)
        {
          warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
                offset);
          break;
        }

Cary Coutant's avatar
Cary Coutant committed
4096 4097
      printf ("    %8.8lx ", offset + (start - *start_ptr));

4098 4099 4100 4101
      /* Note: we use sign extension here in order to be sure that we can detect
         the -1 escape value.  Sign extension into the top 32 bits of a 32-bit
         address will not affect the values that we display since we always show
         hex values, and always the bottom 32-bits.  */
Nick Clifton's avatar
Nick Clifton committed
4102 4103
      SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
      SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127

      if (begin == 0 && end == 0)
        {
          printf (_("<End of list>\n"));
          break;
        }

      /* Check base address specifiers.  */
      if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
        {
          base_address = end;
          print_dwarf_vma (begin, pointer_size);
          print_dwarf_vma (end, pointer_size);
          printf (_("(base address)\n"));
          continue;
        }

      if (start + 2 > section_end)
        {
          warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
                offset);
          break;
        }

Nick Clifton's avatar
Nick Clifton committed
4128
      SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164

      if (start + length > section_end)
        {
          warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
                offset);
          break;
        }

      print_dwarf_vma (begin + base_address, pointer_size);
      print_dwarf_vma (end + base_address, pointer_size);

      putchar ('(');
      need_frame_base = decode_location_expression (start,
                                                    pointer_size,
                                                    offset_size,
                                                    dwarf_version,
                                                    length,
                                                    cu_offset, section);
      putchar (')');

      if (need_frame_base && !has_frame_base)
        printf (_(" [without DW_AT_frame_base]"));

      if (begin == end)
        fputs (_(" (start == end)"), stdout);
      else if (begin > end)
        fputs (_(" (start > end)"), stdout);

      putchar ('\n');

      start += length;
    }

  *start_ptr = start;
}

Cary Coutant's avatar
Cary Coutant committed
4165 4166 4167 4168 4169 4170 4171 4172
/* Print a .debug_addr table index in decimal, surrounded by square brackets,
   right-adjusted in a field of length LEN, and followed by a space.  */

static void
print_addr_index (unsigned int idx, unsigned int len)
{
  static char buf[15];
  snprintf (buf, sizeof (buf), "[%d]", idx);
Cary Coutant's avatar
Cary Coutant committed
4173
  printf ("%*s ", len, buf);
Cary Coutant's avatar
Cary Coutant committed
4174 4175
}

4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195
/* Display a location list from a .dwo section. It uses address indexes rather
   than embedded addresses.  This code closely follows display_loc_list, but the
   two are sufficiently different that combining things is very ugly.  */

static void
display_loc_list_dwo (struct dwarf_section *section,
                      unsigned char **start_ptr,
                      int debug_info_entry,
                      unsigned long offset,
                      int has_frame_base)
{
  unsigned char *start = *start_ptr;
  unsigned char *section_end = section->start + section->size;
  unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
  unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
  unsigned int offset_size = debug_information [debug_info_entry].offset_size;
  int dwarf_version = debug_information [debug_info_entry].dwarf_version;
  int entry_type;
  unsigned short length;
  int need_frame_base;
Cary Coutant's avatar
Cary Coutant committed
4196
  unsigned int idx;
4197 4198 4199 4200
  unsigned int bytes_read;

  while (1)
    {
Cary Coutant's avatar
Cary Coutant committed
4201
      printf ("    %8.8lx ", offset + (start - *start_ptr));
4202

Cary Coutant's avatar
Cary Coutant committed
4203
      if (start >= section_end)
4204 4205 4206 4207 4208 4209
        {
          warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
                offset);
          break;
        }

Nick Clifton's avatar
Nick Clifton committed
4210
      SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
4211 4212 4213 4214
      switch (entry_type)
        {
          case 0: /* A terminating entry.  */
            *start_ptr = start;
Cary Coutant's avatar
Cary Coutant committed
4215
	    printf (_("<End of list>\n"));
4216 4217
            return;
          case 1: /* A base-address entry.  */
Nick Clifton's avatar
Nick Clifton committed
4218
            idx = read_uleb128 (start, &bytes_read, section_end);
4219
            start += bytes_read;
Cary Coutant's avatar
Cary Coutant committed
4220 4221 4222
            print_addr_index (idx, 8);
            printf ("         ");
            printf (_("(base address selection entry)\n"));
4223
            continue;
Cary Coutant's avatar
Cary Coutant committed
4224
          case 2: /* A start/end entry.  */
Nick Clifton's avatar
Nick Clifton committed
4225
            idx = read_uleb128 (start, &bytes_read, section_end);
Cary Coutant's avatar
Cary Coutant committed
4226 4227
            start += bytes_read;
            print_addr_index (idx, 8);
Nick Clifton's avatar
Nick Clifton committed
4228
            idx = read_uleb128 (start, &bytes_read, section_end);
4229
            start += bytes_read;
Cary Coutant's avatar
Cary Coutant committed
4230 4231 4232
            print_addr_index (idx, 8);
            break;
          case 3: /* A start/length entry.  */
Nick Clifton's avatar
Nick Clifton committed
4233
            idx = read_uleb128 (start, &bytes_read, section_end);
4234
            start += bytes_read;
Cary Coutant's avatar
Cary Coutant committed
4235
            print_addr_index (idx, 8);
Nick Clifton's avatar
Nick Clifton committed
4236
	    SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
Cary Coutant's avatar
Cary Coutant committed
4237 4238 4239
            printf ("%08x ", idx);
            break;
          case 4: /* An offset pair entry.  */
Nick Clifton's avatar
Nick Clifton committed
4240
	    SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
Cary Coutant's avatar
Cary Coutant committed
4241
            printf ("%08x ", idx);
Nick Clifton's avatar
Nick Clifton committed
4242
	    SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
Cary Coutant's avatar
Cary Coutant committed
4243
            printf ("%08x ", idx);
4244 4245
            break;
          default:
Cary Coutant's avatar
Cary Coutant committed
4246
            warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257
            *start_ptr = start;
            return;
        }

      if (start + 2 > section_end)
        {
          warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
                offset);
          break;
        }

Nick Clifton's avatar
Nick Clifton committed
4258
      SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285
      if (start + length > section_end)
        {
          warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
                offset);
          break;
        }

      putchar ('(');
      need_frame_base = decode_location_expression (start,
                                                    pointer_size,
                                                    offset_size,
                                                    dwarf_version,
                                                    length,
                                                    cu_offset, section);
      putchar (')');

      if (need_frame_base && !has_frame_base)
        printf (_(" [without DW_AT_frame_base]"));

      putchar ('\n');

      start += length;
    }

  *start_ptr = start;
}

4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298
/* Sort array of indexes in ascending order of loc_offsets[idx].  */

static dwarf_vma *loc_offsets;

static int
loc_offsets_compar (const void *ap, const void *bp)
{
  dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
  dwarf_vma b = loc_offsets[*(const unsigned int *) bp];

  return (a > b) - (b > a);
}

4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309
static int
display_debug_loc (struct dwarf_section *section, void *file)
{
  unsigned char *start = section->start;
  unsigned long bytes;
  unsigned char *section_begin = start;
  unsigned int num_loc_list = 0;
  unsigned long last_offset = 0;
  unsigned int first = 0;
  unsigned int i;
  unsigned int j;
4310
  unsigned int k;
4311
  int seen_first_offset = 0;
4312
  int locs_sorted = 1;
4313
  unsigned char *next;
4314
  unsigned int *array = NULL;
4315 4316 4317 4318 4319
  const char *suffix = strrchr (section->name, '.');
  int is_dwo = 0;

  if (suffix && strcmp (suffix, ".dwo") == 0)
    is_dwo = 1;
4320 4321 4322 4323 4324 4325 4326 4327 4328

  bytes = section->size;

  if (bytes == 0)
    {
      printf (_("\nThe %s section is empty.\n"), section->name);
      return 0;
    }

4329 4330 4331 4332 4333 4334
  if (load_debug_info (file) == 0)
    {
      warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
	    section->name);
      return 0;
    }
4335 4336 4337 4338 4339 4340 4341 4342 4343

  /* Check the order of location list in .debug_info section. If
     offsets of location lists are in the ascending order, we can
     use `debug_information' directly.  */
  for (i = 0; i < num_debug_info_entries; i++)
    {
      unsigned int num;

      num = debug_information [i].num_loc_offsets;
4344 4345
      if (num > num_loc_list)
	num_loc_list = num;
4346 4347

      /* Check if we can use `debug_information' directly.  */
4348
      if (locs_sorted && num != 0)
4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365
	{
	  if (!seen_first_offset)
	    {
	      /* This is the first location list.  */
	      last_offset = debug_information [i].loc_offsets [0];
	      first = i;
	      seen_first_offset = 1;
	      j = 1;
	    }
	  else
	    j = 0;

	  for (; j < num; j++)
	    {
	      if (last_offset >
		  debug_information [i].loc_offsets [j])
		{
4366
		  locs_sorted = 0;
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376
		  break;
		}
	      last_offset = debug_information [i].loc_offsets [j];
	    }
	}
    }

  if (!seen_first_offset)
    error (_("No location lists in .debug_info section!\n"));

4377
  /* DWARF sections under Mach-O have non-zero addresses.  */
4378 4379
  if (debug_information [first].num_loc_offsets > 0
      && debug_information [first].loc_offsets [0] != section->address)
Kai Tietz's avatar
Kai Tietz committed
4380 4381 4382
    warn (_("Location lists in %s section start at 0x%s\n"),
	  section->name,
	  dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
4383

4384 4385
  if (!locs_sorted)
    array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
4386
  printf (_("Contents of the %s section:\n\n"), section->name);
Cary Coutant's avatar
Cary Coutant committed
4387
  printf (_("    Offset   Begin    End      Expression\n"));
4388 4389 4390 4391 4392 4393 4394 4395

  seen_first_offset = 0;
  for (i = first; i < num_debug_info_entries; i++)
    {
      unsigned long offset;
      unsigned long base_address;
      int has_frame_base;

4396 4397 4398 4399 4400 4401 4402 4403
      if (!locs_sorted)
	{
	  for (k = 0; k < debug_information [i].num_loc_offsets; k++)
	    array[k] = k;
	  loc_offsets = debug_information [i].loc_offsets;
	  qsort (array, debug_information [i].num_loc_offsets,
		 sizeof (*array), loc_offsets_compar);
	}
4404

4405
      for (k = 0; k < debug_information [i].num_loc_offsets; k++)
4406
	{
4407 4408 4409 4410 4411 4412
	  j = locs_sorted ? k : array[k];
	  if (k
	      && debug_information [i].loc_offsets [locs_sorted
						    ? k - 1 : array [k - 1]]
		 == debug_information [i].loc_offsets [j])
	    continue;
4413
	  has_frame_base = debug_information [i].have_frame_base [j];
4414
	  /* DWARF sections under Mach-O have non-zero addresses.  */
4415
	  offset = debug_information [i].loc_offsets [j] - section->address;
4416 4417 4418 4419 4420 4421 4422 4423 4424
	  next = section_begin + offset;
	  base_address = debug_information [i].base_address;

	  if (!seen_first_offset)
	    seen_first_offset = 1;
	  else
	    {
	      if (start < next)
		warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
Alan Modra's avatar
Alan Modra committed
4425 4426
		      (unsigned long) (start - section_begin),
		      (unsigned long) (next - section_begin));
4427 4428
	      else if (start > next)
		warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
Alan Modra's avatar
Alan Modra committed
4429 4430
		      (unsigned long) (start - section_begin),
		      (unsigned long) (next - section_begin));
4431 4432 4433 4434 4435 4436 4437 4438 4439 4440
	    }
	  start = next;

	  if (offset >= bytes)
	    {
	      warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
		    offset);
	      continue;
	    }

4441 4442 4443 4444 4445
          if (is_dwo)
            display_loc_list_dwo (section, &start, i, offset, has_frame_base);
          else
            display_loc_list (section, &start, i, offset, base_address,
                              has_frame_base);
4446 4447
	}
    }
4448

4449
  if (start < section->start + section->size)
4450
    warn (_("There are %ld unused bytes at the end of section %s\n"),
4451
	  (long) (section->start + section->size - start), section->name);
4452
  putchar ('\n');
4453
  free (array);
4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517
  return 1;
}

static int
display_debug_str (struct dwarf_section *section,
		   void *file ATTRIBUTE_UNUSED)
{
  unsigned char *start = section->start;
  unsigned long bytes = section->size;
  dwarf_vma addr = section->address;

  if (bytes == 0)
    {
      printf (_("\nThe %s section is empty.\n"), section->name);
      return 0;
    }

  printf (_("Contents of the %s section:\n\n"), section->name);

  while (bytes)
    {
      int j;
      int k;
      int lbytes;

      lbytes = (bytes > 16 ? 16 : bytes);

      printf ("  0x%8.8lx ", (unsigned long) addr);

      for (j = 0; j < 16; j++)
	{
	  if (j < lbytes)
	    printf ("%2.2x", start[j]);
	  else
	    printf ("  ");

	  if ((j & 3) == 3)
	    printf (" ");
	}

      for (j = 0; j < lbytes; j++)
	{
	  k = start[j];
	  if (k >= ' ' && k < 0x80)
	    printf ("%c", k);
	  else
	    printf (".");
	}

      putchar ('\n');

      start += lbytes;
      addr  += lbytes;
      bytes -= lbytes;
    }

  putchar ('\n');

  return 1;
}

static int
display_debug_info (struct dwarf_section *section, void *file)
{
4518
  return process_debug_info (section, file, section->abbrev_sec, 0, 0);
4519 4520
}

Cary Coutant's avatar
Cary Coutant committed
4521 4522 4523
static int
display_debug_types (struct dwarf_section *section, void *file)
{
4524
  return process_debug_info (section, file, section->abbrev_sec, 0, 1);
4525 4526 4527 4528 4529
}

static int
display_trace_info (struct dwarf_section *section, void *file)
{
4530
  return process_debug_info (section, file, section->abbrev_sec, 0, 0);
Cary Coutant's avatar
Cary Coutant committed
4531
}
4532 4533 4534 4535 4536 4537 4538 4539

static int
display_debug_aranges (struct dwarf_section *section,
		       void *file ATTRIBUTE_UNUSED)
{
  unsigned char *start = section->start;
  unsigned char *end = start + section->size;

4540
  printf (_("Contents of the %s section:\n\n"), section->name);
4541

4542 4543 4544 4545
  /* It does not matter if this load fails,
     we test for that later on.  */
  load_debug_info (file);

4546 4547 4548 4549
  while (start < end)
    {
      unsigned char *hdrptr;
      DWARF2_Internal_ARange arange;
4550
      unsigned char *addr_ranges;
4551 4552
      dwarf_vma length;
      dwarf_vma address;
4553
      unsigned char address_size;
4554
      int excess;
4555 4556
      unsigned int offset_size;
      unsigned int initial_length_size;
4557 4558 4559

      hdrptr = start;

Nick Clifton's avatar
Nick Clifton committed
4560
      SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
4561 4562
      if (arange.ar_length == 0xffffffff)
	{
Nick Clifton's avatar
Nick Clifton committed
4563
	  SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
4564 4565 4566 4567 4568 4569 4570 4571 4572
	  offset_size = 8;
	  initial_length_size = 12;
	}
      else
	{
	  offset_size = 4;
	  initial_length_size = 4;
	}

Nick Clifton's avatar
Nick Clifton committed
4573 4574
      SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end);
      SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size, end);
4575

4576 4577 4578 4579
      if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
	  && num_debug_info_entries > 0
	  && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
	warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
Kai Tietz's avatar
Kai Tietz committed
4580
	      (unsigned long) arange.ar_info_offset, section->name);
4581

Nick Clifton's avatar
Nick Clifton committed
4582 4583
      SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end);
      SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end);
4584 4585 4586 4587 4588 4589 4590

      if (arange.ar_version != 2 && arange.ar_version != 3)
	{
	  warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
	  break;
	}

Kai Tietz's avatar
Kai Tietz committed
4591 4592
      printf (_("  Length:                   %ld\n"),
	      (long) arange.ar_length);
4593
      printf (_("  Version:                  %d\n"), arange.ar_version);
Kai Tietz's avatar
Kai Tietz committed
4594 4595
      printf (_("  Offset into .debug_info:  0x%lx\n"),
	      (unsigned long) arange.ar_info_offset);
4596 4597 4598
      printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
      printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);

4599 4600
      address_size = arange.ar_pointer_size + arange.ar_segment_size;

H.J. Lu's avatar
H.J. Lu committed
4601 4602 4603 4604 4605 4606 4607
      if (address_size == 0)
	{
	  error (_("Invalid address size in %s section!\n"),
		 section->name);
	  break;
	}

4608 4609 4610 4611 4612 4613 4614 4615
      /* The DWARF spec does not require that the address size be a power
	 of two, but we do.  This will have to change if we ever encounter
	 an uneven architecture.  */
      if ((address_size & (address_size - 1)) != 0)
	{
	  warn (_("Pointer size + Segment size is not a power of two.\n"));
	  break;
	}
4616

Nick Clifton's avatar
Nick Clifton committed
4617 4618 4619 4620
      if (address_size > 4)
	printf (_("\n    Address            Length\n"));
      else
	printf (_("\n    Address    Length\n"));
4621

4622
      addr_ranges = hdrptr;
4623

4624 4625
      /* Must pad to an alignment boundary that is twice the address size.  */
      excess = (hdrptr - start) % (2 * address_size);
4626
      if (excess)
4627
	addr_ranges += (2 * address_size) - excess;
4628

4629 4630
      start += arange.ar_length + initial_length_size;

4631
      while (addr_ranges + 2 * address_size <= start)
4632
	{
Nick Clifton's avatar
Nick Clifton committed
4633 4634
	  SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size, end);
	  SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size, end);
4635

4636
	  printf ("    ");
4637 4638 4639
	  print_dwarf_vma (address, address_size);
	  print_dwarf_vma (length, address_size);
	  putchar ('\n');
4640 4641 4642 4643 4644 4645 4646 4647
	}
    }

  printf ("\n");

  return 1;
}

4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682
/* Comparison function for qsort.  */
static int
comp_addr_base (const void * v0, const void * v1)
{
  debug_info * info0 = (debug_info *) v0;
  debug_info * info1 = (debug_info *) v1;
  return info0->addr_base - info1->addr_base;
}

/* Display the debug_addr section.  */
static int
display_debug_addr (struct dwarf_section *section,
                    void *file)
{
  debug_info **debug_addr_info;
  unsigned char *entry;
  unsigned char *end;
  unsigned int i;
  unsigned int count;

  if (section->size == 0)
    {
      printf (_("\nThe %s section is empty.\n"), section->name);
      return 0;
    }

  if (load_debug_info (file) == 0)
    {
      warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
	    section->name);
      return 0;
    }

  printf (_("Contents of the %s section:\n\n"), section->name);

4683
  debug_addr_info = (debug_info **) xmalloc ((num_debug_info_entries + 1)
4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700
                                             * sizeof (debug_info *));

  count = 0;
  for (i = 0; i < num_debug_info_entries; i++)
    {
      if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
        debug_addr_info [count++] = &debug_information [i];
    }

  /* Add a sentinel to make iteration convenient.  */
  debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
  debug_addr_info [count]->addr_base = section->size;

  qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
  for (i = 0; i < count; i++)
    {
      unsigned int idx;
Cary Coutant's avatar
Cary Coutant committed
4701
      unsigned int address_size = debug_addr_info [i]->pointer_size;
4702 4703 4704 4705

      printf (_("  For compilation unit at offset 0x%s:\n"),
              dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));

Cary Coutant's avatar
Cary Coutant committed
4706
      printf (_("\tIndex\tAddress\n"));
4707 4708 4709 4710 4711
      entry = section->start + debug_addr_info [i]->addr_base;
      end = section->start + debug_addr_info [i + 1]->addr_base;
      idx = 0;
      while (entry < end)
        {
Cary Coutant's avatar
Cary Coutant committed
4712 4713 4714 4715 4716
          dwarf_vma base = byte_get (entry, address_size);
          printf (_("\t%d:\t"), idx);
          print_dwarf_vma (base, address_size);
          printf ("\n");
          entry += address_size;
4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740
          idx++;
        }
    }
  printf ("\n");

  free (debug_addr_info);
  return 1;
}

/* Display the .debug_str_offsets and .debug_str_offsets.dwo sections.  */
static int
display_debug_str_offsets (struct dwarf_section *section,
                           void *file ATTRIBUTE_UNUSED)
{
  if (section->size == 0)
    {
      printf (_("\nThe %s section is empty.\n"), section->name);
      return 0;
    }
  /* TODO: Dump the contents.  This is made somewhat difficult by not knowing
     what the offset size is for this section.  */
  return 1;
}

Jan Kratochvil's avatar
Jan Kratochvil committed
4741 4742 4743 4744
/* Each debug_information[x].range_lists[y] gets this representation for
   sorting purposes.  */

struct range_entry
4745 4746 4747
{
  /* The debug_information[x].range_lists[y] value.  */
  unsigned long ranges_offset;
Jan Kratochvil's avatar
Jan Kratochvil committed
4748

4749 4750 4751
  /* Original debug_information to find parameters of the data.  */
  debug_info *debug_info_p;
};
Jan Kratochvil's avatar
Jan Kratochvil committed
4752 4753 4754 4755 4756 4757

/* Sort struct range_entry in ascending order of its RANGES_OFFSET.  */

static int
range_entry_compar (const void *ap, const void *bp)
{
4758 4759
  const struct range_entry *a_re = (const struct range_entry *) ap;
  const struct range_entry *b_re = (const struct range_entry *) bp;
Jan Kratochvil's avatar
Jan Kratochvil committed
4760 4761 4762 4763 4764 4765
  const unsigned long a = a_re->ranges_offset;
  const unsigned long b = b_re->ranges_offset;

  return (a > b) - (b > a);
}

4766 4767 4768 4769 4770
static int
display_debug_ranges (struct dwarf_section *section,
		      void *file ATTRIBUTE_UNUSED)
{
  unsigned char *start = section->start;
4771
  unsigned char *last_start = start;
Nick Clifton's avatar
Nick Clifton committed
4772
  unsigned long bytes = section->size;
4773
  unsigned char *section_begin = start;
Nick Clifton's avatar
Nick Clifton committed
4774
  unsigned char *finish = start + bytes;
Jan Kratochvil's avatar
Jan Kratochvil committed
4775 4776
  unsigned int num_range_list, i;
  struct range_entry *range_entries, *range_entry_fill;
4777 4778 4779 4780 4781 4782 4783

  if (bytes == 0)
    {
      printf (_("\nThe %s section is empty.\n"), section->name);
      return 0;
    }

4784 4785 4786 4787 4788 4789
  if (load_debug_info (file) == 0)
    {
      warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
	    section->name);
      return 0;
    }
4790

Jan Kratochvil's avatar
Jan Kratochvil committed
4791
  num_range_list = 0;
4792
  for (i = 0; i < num_debug_info_entries; i++)
Jan Kratochvil's avatar
Jan Kratochvil committed
4793
    num_range_list += debug_information [i].num_range_lists;
4794

Jan Kratochvil's avatar
Jan Kratochvil committed
4795
  if (num_range_list == 0)
4796 4797 4798 4799 4800 4801
    {
      /* This can happen when the file was compiled with -gsplit-debug
         which removes references to range lists from the primary .o file.  */
      printf (_("No range lists in .debug_info section.\n"));
      return 1;
    }
4802

4803 4804
  range_entries = (struct range_entry *)
      xmalloc (sizeof (*range_entries) * num_range_list);
Jan Kratochvil's avatar
Jan Kratochvil committed
4805
  range_entry_fill = range_entries;
4806

Jan Kratochvil's avatar
Jan Kratochvil committed
4807 4808 4809 4810 4811 4812 4813 4814 4815 4816
  for (i = 0; i < num_debug_info_entries; i++)
    {
      debug_info *debug_info_p = &debug_information[i];
      unsigned int j;

      for (j = 0; j < debug_info_p->num_range_lists; j++)
	{
	  range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
	  range_entry_fill->debug_info_p = debug_info_p;
	  range_entry_fill++;
4817 4818 4819
	}
    }

Jan Kratochvil's avatar
Jan Kratochvil committed
4820 4821
  qsort (range_entries, num_range_list, sizeof (*range_entries),
	 range_entry_compar);
4822

4823
  /* DWARF sections under Mach-O have non-zero addresses.  */
4824
  if (dwarf_check != 0 && range_entries[0].ranges_offset != section->address)
4825
    warn (_("Range lists in %s section start at 0x%lx\n"),
Jan Kratochvil's avatar
Jan Kratochvil committed
4826
	  section->name, range_entries[0].ranges_offset);
4827 4828 4829 4830

  printf (_("Contents of the %s section:\n\n"), section->name);
  printf (_("    Offset   Begin    End\n"));

Jan Kratochvil's avatar
Jan Kratochvil committed
4831
  for (i = 0; i < num_range_list; i++)
4832
    {
Jan Kratochvil's avatar
Jan Kratochvil committed
4833 4834
      struct range_entry *range_entry = &range_entries[i];
      debug_info *debug_info_p = range_entry->debug_info_p;
4835
      unsigned int pointer_size;
Jan Kratochvil's avatar
Jan Kratochvil committed
4836 4837
      unsigned long offset;
      unsigned char *next;
4838 4839
      unsigned long base_address;

Jan Kratochvil's avatar
Jan Kratochvil committed
4840 4841 4842 4843 4844 4845
      pointer_size = debug_info_p->pointer_size;

      /* DWARF sections under Mach-O have non-zero addresses.  */
      offset = range_entry->ranges_offset - section->address;
      next = section_begin + offset;
      base_address = debug_info_p->base_address;
4846

4847
      if (dwarf_check != 0 && i > 0)
4848
	{
Jan Kratochvil's avatar
Jan Kratochvil committed
4849 4850 4851 4852 4853
	  if (start < next)
	    warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
		  (unsigned long) (start - section_begin),
		  (unsigned long) (next - section_begin), section->name);
	  else if (start > next)
4854 4855 4856 4857 4858 4859 4860
	    {
	      if (next == last_start)
		continue;
	      warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
		    (unsigned long) (start - section_begin),
		    (unsigned long) (next - section_begin), section->name);
	    }
Jan Kratochvil's avatar
Jan Kratochvil committed
4861 4862
	}
      start = next;
4863
      last_start = next;
4864

Nick Clifton's avatar
Nick Clifton committed
4865
      while (start < finish)
Jan Kratochvil's avatar
Jan Kratochvil committed
4866 4867 4868 4869 4870 4871 4872 4873 4874
	{
	  dwarf_vma begin;
	  dwarf_vma end;

	  /* Note: we use sign extension here in order to be sure that
	     we can detect the -1 escape value.  Sign extension into the
	     top 32 bits of a 32-bit address will not affect the values
	     that we display since we always show hex values, and always
	     the bottom 32-bits.  */
Nick Clifton's avatar
Nick Clifton committed
4875
	  SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
Nick Clifton's avatar
Nick Clifton committed
4876 4877
	  if (start >= finish)
	    break;
Nick Clifton's avatar
Nick Clifton committed
4878
	  SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
Jan Kratochvil's avatar
Jan Kratochvil committed
4879 4880 4881 4882

	  printf ("    %8.8lx ", offset);

	  if (begin == 0 && end == 0)
4883
	    {
Jan Kratochvil's avatar
Jan Kratochvil committed
4884 4885
	      printf (_("<End of list>\n"));
	      break;
4886 4887
	    }

Jan Kratochvil's avatar
Jan Kratochvil committed
4888 4889
	  /* Check base address specifiers.  */
	  if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
4890
	    {
Jan Kratochvil's avatar
Jan Kratochvil committed
4891 4892 4893 4894 4895 4896
	      base_address = end;
	      print_dwarf_vma (begin, pointer_size);
	      print_dwarf_vma (end, pointer_size);
	      printf ("(base address)\n");
	      continue;
	    }
4897

Jan Kratochvil's avatar
Jan Kratochvil committed
4898 4899
	  print_dwarf_vma (begin + base_address, pointer_size);
	  print_dwarf_vma (end + base_address, pointer_size);
4900

Jan Kratochvil's avatar
Jan Kratochvil committed
4901 4902 4903 4904
	  if (begin == end)
	    fputs (_("(start == end)"), stdout);
	  else if (begin > end)
	    fputs (_("(start > end)"), stdout);
4905

Jan Kratochvil's avatar
Jan Kratochvil committed
4906
	  putchar ('\n');
4907 4908 4909
	}
    }
  putchar ('\n');
Jan Kratochvil's avatar
Jan Kratochvil committed
4910 4911 4912

  free (range_entries);

4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926
  return 1;
}

typedef struct Frame_Chunk
{
  struct Frame_Chunk *next;
  unsigned char *chunk_start;
  int ncols;
  /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
  short int *col_type;
  int *col_offset;
  char *augmentation;
  unsigned int code_factor;
  int data_factor;
4927 4928
  dwarf_vma pc_begin;
  dwarf_vma pc_range;
4929 4930 4931 4932 4933
  int cfa_reg;
  int cfa_offset;
  int ra;
  unsigned char fde_encoding;
  unsigned char cfa_exp;
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
4934 4935
  unsigned char ptr_size;
  unsigned char segment_size;
4936 4937 4938
}
Frame_Chunk;

4939 4940 4941
static const char *const *dwarf_regnames;
static unsigned int dwarf_regnames_count;

4942 4943 4944 4945
/* A marker for a col_type that means this column was never referenced
   in the frame info.  */
#define DW_CFA_unreferenced (-1)

4946 4947 4948 4949 4950
/* Return 0 if not more space is needed, 1 if more space is needed,
   -1 for invalid reg.  */

static int
frame_need_space (Frame_Chunk *fc, unsigned int reg)
4951 4952 4953
{
  int prev = fc->ncols;

4954 4955 4956 4957 4958 4959
  if (reg < (unsigned int) fc->ncols)
    return 0;

  if (dwarf_regnames_count
      && reg > dwarf_regnames_count)
    return -1;
4960 4961

  fc->ncols = reg + 1;
4962 4963 4964
  fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
                                          sizeof (short int));
  fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
4965 4966 4967 4968 4969 4970 4971

  while (prev < fc->ncols)
    {
      fc->col_type[prev] = DW_CFA_unreferenced;
      fc->col_offset[prev] = 0;
      prev++;
    }
4972
  return 1;
4973 4974
}

H.J. Lu's avatar
H.J. Lu committed
4975 4976
static const char *const dwarf_regnames_i386[] =
{
H.J. Lu's avatar
H.J. Lu committed
4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996
  "eax", "ecx", "edx", "ebx",			  /* 0 - 3  */
  "esp", "ebp", "esi", "edi",			  /* 4 - 7  */
  "eip", "eflags", NULL,			  /* 8 - 10  */
  "st0", "st1", "st2", "st3",			  /* 11 - 14  */
  "st4", "st5", "st6", "st7",			  /* 15 - 18  */
  NULL, NULL,					  /* 19 - 20  */
  "xmm0", "xmm1", "xmm2", "xmm3",		  /* 21 - 24  */
  "xmm4", "xmm5", "xmm6", "xmm7",		  /* 25 - 28  */
  "mm0", "mm1", "mm2", "mm3",			  /* 29 - 32  */
  "mm4", "mm5", "mm6", "mm7",			  /* 33 - 36  */
  "fcw", "fsw", "mxcsr",			  /* 37 - 39  */
  "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47  */
  "tr", "ldtr",					  /* 48 - 49  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89  */
  NULL, NULL, NULL,				  /* 90 - 92  */
  "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"  /* 93 - 100  */
H.J. Lu's avatar
H.J. Lu committed
4997 4998
};

4999 5000 5001 5002 5003 5004 5005
void
init_dwarf_regnames_i386 (void)
{
  dwarf_regnames = dwarf_regnames_i386;
  dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
}

H.J. Lu's avatar
H.J. Lu committed
5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024
static const char *const dwarf_regnames_x86_64[] =
{
  "rax", "rdx", "rcx", "rbx",
  "rsi", "rdi", "rbp", "rsp",
  "r8",  "r9",  "r10", "r11",
  "r12", "r13", "r14", "r15",
  "rip",
  "xmm0",  "xmm1",  "xmm2",  "xmm3",
  "xmm4",  "xmm5",  "xmm6",  "xmm7",
  "xmm8",  "xmm9",  "xmm10", "xmm11",
  "xmm12", "xmm13", "xmm14", "xmm15",
  "st0", "st1", "st2", "st3",
  "st4", "st5", "st6", "st7",
  "mm0", "mm1", "mm2", "mm3",
  "mm4", "mm5", "mm6", "mm7",
  "rflags",
  "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
  "fs.base", "gs.base", NULL, NULL,
  "tr", "ldtr",
H.J. Lu's avatar
H.J. Lu committed
5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035
  "mxcsr", "fcw", "fsw",
  "xmm16",  "xmm17",  "xmm18",  "xmm19",
  "xmm20",  "xmm21",  "xmm22",  "xmm23",
  "xmm24",  "xmm25",  "xmm26",  "xmm27",
  "xmm28",  "xmm29",  "xmm30",  "xmm31",
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106  */
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114  */
  NULL, NULL, NULL,				  /* 115 - 117  */
  "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
H.J. Lu's avatar
H.J. Lu committed
5036 5037
};

5038 5039 5040 5041 5042 5043 5044
void
init_dwarf_regnames_x86_64 (void)
{
  dwarf_regnames = dwarf_regnames_x86_64;
  dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
}

H.J. Lu's avatar
H.J. Lu committed
5045 5046 5047 5048 5049 5050 5051
void
init_dwarf_regnames (unsigned int e_machine)
{
  switch (e_machine)
    {
    case EM_386:
    case EM_486:
5052
      init_dwarf_regnames_i386 ();
H.J. Lu's avatar
H.J. Lu committed
5053 5054 5055
      break;

    case EM_X86_64:
H.J. Lu's avatar
H.J. Lu committed
5056
    case EM_L1OM:
H.J. Lu's avatar
H.J. Lu committed
5057
    case EM_K1OM:
5058
      init_dwarf_regnames_x86_64 ();
H.J. Lu's avatar
H.J. Lu committed
5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083
      break;

    default:
      break;
    }
}

static const char *
regname (unsigned int regno, int row)
{
  static char reg[64];
  if (dwarf_regnames
      && regno < dwarf_regnames_count
      && dwarf_regnames [regno] != NULL)
    {
      if (row)
	return dwarf_regnames [regno];
      snprintf (reg, sizeof (reg), "r%d (%s)", regno,
		dwarf_regnames [regno]);
    }
  else
    snprintf (reg, sizeof (reg), "r%d", regno);
  return reg;
}

5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094
static void
frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
{
  int r;
  char tmp[100];

  if (*max_regs < fc->ncols)
    *max_regs = fc->ncols;

  if (*need_col_headers)
    {
5095
      static const char *sloc = "   LOC";
H.J. Lu's avatar
H.J. Lu committed
5096

5097 5098
      *need_col_headers = 0;

5099
      printf ("%-*s CFA      ", eh_addr_size * 2, sloc);
5100 5101 5102 5103 5104

      for (r = 0; r < *max_regs; r++)
	if (fc->col_type[r] != DW_CFA_unreferenced)
	  {
	    if (r == fc->ra)
H.J. Lu's avatar
H.J. Lu committed
5105
	      printf ("ra      ");
5106
	    else
H.J. Lu's avatar
H.J. Lu committed
5107
	      printf ("%-5s ", regname (r, 1));
5108 5109 5110 5111 5112
	  }

      printf ("\n");
    }

5113
  print_dwarf_vma (fc->pc_begin, eh_addr_size);
5114 5115 5116
  if (fc->cfa_exp)
    strcpy (tmp, "exp");
  else
H.J. Lu's avatar
H.J. Lu committed
5117
    sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), fc->cfa_offset);
5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134
  printf ("%-8s ", tmp);

  for (r = 0; r < fc->ncols; r++)
    {
      if (fc->col_type[r] != DW_CFA_unreferenced)
	{
	  switch (fc->col_type[r])
	    {
	    case DW_CFA_undefined:
	      strcpy (tmp, "u");
	      break;
	    case DW_CFA_same_value:
	      strcpy (tmp, "s");
	      break;
	    case DW_CFA_offset:
	      sprintf (tmp, "c%+d", fc->col_offset[r]);
	      break;
5135 5136 5137
	    case DW_CFA_val_offset:
	      sprintf (tmp, "v%+d", fc->col_offset[r]);
	      break;
5138
	    case DW_CFA_register:
H.J. Lu's avatar
H.J. Lu committed
5139
	      sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
5140 5141 5142 5143
	      break;
	    case DW_CFA_expression:
	      strcpy (tmp, "exp");
	      break;
5144 5145 5146
	    case DW_CFA_val_expression:
	      strcpy (tmp, "vexp");
	      break;
5147 5148 5149 5150
	    default:
	      strcpy (tmp, "n/a");
	      break;
	    }
H.J. Lu's avatar
H.J. Lu committed
5151
	  printf ("%-5s ", tmp);
5152 5153 5154 5155 5156
	}
    }
  printf ("\n");
}

Nick Clifton's avatar
Nick Clifton committed
5157
#define GET(VAR, N)	SAFE_BYTE_GET_AND_INC (VAR, start, N, end);
Nick Clifton's avatar
Nick Clifton committed
5158 5159
#define LEB()	read_uleb128 (start, & length_return, end); start += length_return
#define SLEB()	read_sleb128 (start, & length_return, end); start += length_return
5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173

static int
display_debug_frames (struct dwarf_section *section,
		      void *file ATTRIBUTE_UNUSED)
{
  unsigned char *start = section->start;
  unsigned char *end = start + section->size;
  unsigned char *section_start = start;
  Frame_Chunk *chunks = 0;
  Frame_Chunk *remembered_state = 0;
  Frame_Chunk *rs;
  int is_eh = strcmp (section->name, ".eh_frame") == 0;
  unsigned int length_return;
  int max_regs = 0;
5174
  const char *bad_reg = _("bad register: ");
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5175
  int saved_eh_addr_size = eh_addr_size;
5176

5177
  printf (_("Contents of the %s section:\n"), section->name);
5178 5179 5180 5181 5182

  while (start < end)
    {
      unsigned char *saved_start;
      unsigned char *block_end;
5183 5184
      dwarf_vma length;
      dwarf_vma cie_id;
5185 5186 5187 5188 5189
      Frame_Chunk *fc;
      Frame_Chunk *cie;
      int need_col_headers = 1;
      unsigned char *augmentation_data = NULL;
      unsigned long augmentation_data_len = 0;
5190 5191 5192
      unsigned int encoded_ptr_size = saved_eh_addr_size;
      unsigned int offset_size;
      unsigned int initial_length_size;
5193 5194 5195

      saved_start = start;

Nick Clifton's avatar
Nick Clifton committed
5196
      SAFE_BYTE_GET_AND_INC (length, start, 4, end);
5197 5198 5199 5200
      if (length == 0)
	{
	  printf ("\n%08lx ZERO terminator\n\n",
		    (unsigned long)(saved_start - section_start));
5201
	  continue;
5202 5203 5204 5205
	}

      if (length == 0xffffffff)
	{
Nick Clifton's avatar
Nick Clifton committed
5206
	  SAFE_BYTE_GET_AND_INC (length, start, 8, end);
5207 5208 5209 5210 5211 5212 5213 5214 5215 5216
	  offset_size = 8;
	  initial_length_size = 12;
	}
      else
	{
	  offset_size = 4;
	  initial_length_size = 4;
	}

      block_end = saved_start + length + initial_length_size;
5217 5218
      if (block_end > end)
	{
5219 5220 5221
	  warn ("Invalid length 0x%s in FDE at %#08lx\n",
		dwarf_vmatoa_1 (NULL, length, offset_size),
		(unsigned long) (saved_start - section_start));
5222 5223
	  block_end = end;
	}
Nick Clifton's avatar
Nick Clifton committed
5224 5225

      SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, end);
5226

5227 5228
      if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
				   || (offset_size == 8 && cie_id == DW64_CIE_ID)))
5229 5230 5231
	{
	  int version;

5232
	  fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
5233 5234 5235 5236 5237 5238
	  memset (fc, 0, sizeof (Frame_Chunk));

	  fc->next = chunks;
	  chunks = fc;
	  fc->chunk_start = saved_start;
	  fc->ncols = 0;
5239 5240
	  fc->col_type = (short int *) xmalloc (sizeof (short int));
	  fc->col_offset = (int *) xmalloc (sizeof (int));
5241
	  frame_need_space (fc, max_regs - 1);
5242 5243 5244 5245 5246 5247

	  version = *start++;

	  fc->augmentation = (char *) start;
	  start = (unsigned char *) strchr ((char *) start, '\0') + 1;

Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5248 5249 5250 5251
	  if (strcmp (fc->augmentation, "eh") == 0)
	    start += eh_addr_size;

	  if (version >= 4)
5252
	    {
Nick Clifton's avatar
Nick Clifton committed
5253 5254
	      GET (fc->ptr_size, 1);
	      GET (fc->segment_size, 1);
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5255
	      eh_addr_size = fc->ptr_size;
5256
	    }
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5257
	  else
5258
	    {
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5259 5260 5261 5262 5263 5264 5265
	      fc->ptr_size = eh_addr_size;
	      fc->segment_size = 0;
	    }
	  fc->code_factor = LEB ();
	  fc->data_factor = SLEB ();
	  if (version == 1)
	    {
Nick Clifton's avatar
Nick Clifton committed
5266
	      GET (fc->ra, 1);
5267 5268 5269
	    }
	  else
	    {
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5270 5271 5272 5273 5274 5275 5276 5277
	      fc->ra = LEB ();
	    }

	  if (fc->augmentation[0] == 'z')
	    {
	      augmentation_data_len = LEB ();
	      augmentation_data = start;
	      start += augmentation_data_len;
5278 5279 5280
	    }
	  cie = fc;

5281 5282
	  printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
	  print_dwarf_vma (length, fc->ptr_size);
5283
	  print_dwarf_vma (cie_id, offset_size);
5284

5285
	  if (do_debug_frames_interp)
5286 5287 5288 5289
	    {
	      printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
		      fc->code_factor, fc->data_factor, fc->ra);
	    }
5290 5291
	  else
	    {
5292
	      printf ("CIE\n");
5293 5294
	      printf ("  Version:               %d\n", version);
	      printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5295 5296 5297 5298 5299
	      if (version >= 4)
		{
		  printf ("  Pointer Size:          %u\n", fc->ptr_size);
		  printf ("  Segment Size:          %u\n", fc->segment_size);
		}
5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328
	      printf ("  Code alignment factor: %u\n", fc->code_factor);
	      printf ("  Data alignment factor: %d\n", fc->data_factor);
	      printf ("  Return address column: %d\n", fc->ra);

	      if (augmentation_data_len)
		{
		  unsigned long i;
		  printf ("  Augmentation data:    ");
		  for (i = 0; i < augmentation_data_len; ++i)
		    printf (" %02x", augmentation_data[i]);
		  putchar ('\n');
		}
	      putchar ('\n');
	    }

	  if (augmentation_data_len)
	    {
	      unsigned char *p, *q;
	      p = (unsigned char *) fc->augmentation + 1;
	      q = augmentation_data;

	      while (1)
		{
		  if (*p == 'L')
		    q++;
		  else if (*p == 'P')
		    q += 1 + size_of_encoded_value (*q);
		  else if (*p == 'R')
		    fc->fde_encoding = *q++;
5329 5330
		  else if (*p == 'S')
		    ;
5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
		  else
		    break;
		  p++;
		}

	      if (fc->fde_encoding)
		encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
	    }

	  frame_need_space (fc, fc->ra);
	}
      else
	{
	  unsigned char *look_for;
	  static Frame_Chunk fde_fc;
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5346
	  unsigned long segment_selector;
5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358

	  fc = & fde_fc;
	  memset (fc, 0, sizeof (Frame_Chunk));

	  look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;

	  for (cie = chunks; cie ; cie = cie->next)
	    if (cie->chunk_start == look_for)
	      break;

	  if (!cie)
	    {
5359 5360 5361
	      warn ("Invalid CIE pointer 0x%s in FDE at %#08lx\n",
		    dwarf_vmatoa_1 (NULL, cie_id, offset_size),
		    (unsigned long) (saved_start - section_start));
5362
	      fc->ncols = 0;
5363 5364
	      fc->col_type = (short int *) xmalloc (sizeof (short int));
	      fc->col_offset = (int *) xmalloc (sizeof (int));
5365 5366 5367 5368
	      frame_need_space (fc, max_regs - 1);
	      cie = fc;
	      fc->augmentation = "";
	      fc->fde_encoding = 0;
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5369 5370
	      fc->ptr_size = eh_addr_size;
	      fc->segment_size = 0;
5371 5372 5373 5374
	    }
	  else
	    {
	      fc->ncols = cie->ncols;
5375 5376
	      fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
	      fc->col_offset =  (int *) xcmalloc (fc->ncols, sizeof (int));
5377 5378 5379
	      memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
	      memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
	      fc->augmentation = cie->augmentation;
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5380 5381 5382
	      fc->ptr_size = cie->ptr_size;
	      eh_addr_size = cie->ptr_size;
	      fc->segment_size = cie->segment_size;
5383 5384 5385 5386 5387
	      fc->code_factor = cie->code_factor;
	      fc->data_factor = cie->data_factor;
	      fc->cfa_reg = cie->cfa_reg;
	      fc->cfa_offset = cie->cfa_offset;
	      fc->ra = cie->ra;
5388
	      frame_need_space (fc, max_regs - 1);
5389 5390 5391 5392 5393 5394
	      fc->fde_encoding = cie->fde_encoding;
	    }

	  if (fc->fde_encoding)
	    encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);

Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5395 5396 5397
	  segment_selector = 0;
	  if (fc->segment_size)
	    {
Nick Clifton's avatar
Nick Clifton committed
5398
	      SAFE_BYTE_GET_AND_INC (segment_selector, start, fc->segment_size, end);
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5399
	    }
5400
	  fc->pc_begin = get_encoded_value (start, fc->fde_encoding, section);
5401 5402
	  start += encoded_ptr_size;

Nick Clifton's avatar
Nick Clifton committed
5403 5404 5405 5406 5407
	  /* FIXME: It appears that sometimes the final pc_range value is
	     encoded in less than encoded_ptr_size bytes.  See the x86_64
	     run of the "objcopy on compressed debug sections" test for an
	     example of this.  */
	  SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size, end);
5408

5409 5410 5411 5412 5413 5414 5415
	  if (cie->augmentation[0] == 'z')
	    {
	      augmentation_data_len = LEB ();
	      augmentation_data = start;
	      start += augmentation_data_len;
	    }

5416 5417 5418
	  printf ("\n%08lx %s %s FDE cie=%08lx pc=",
		  (unsigned long)(saved_start - section_start),
		  dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
5419
		  dwarf_vmatoa_1 (NULL, cie_id, offset_size),
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5420
		  (unsigned long)(cie->chunk_start - section_start));
5421

Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5422 5423
	  if (fc->segment_size)
	    printf ("%04lx:", segment_selector);
5424 5425 5426 5427 5428

	  printf ("%s..%s\n",
		  dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
		  dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));

5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456
	  if (! do_debug_frames_interp && augmentation_data_len)
	    {
	      unsigned long i;

	      printf ("  Augmentation data:    ");
	      for (i = 0; i < augmentation_data_len; ++i)
		printf (" %02x", augmentation_data[i]);
	      putchar ('\n');
	      putchar ('\n');
	    }
	}

      /* At this point, fc is the current chunk, cie (if any) is set, and
	 we're about to interpret instructions for the chunk.  */
      /* ??? At present we need to do this always, since this sizes the
	 fc->col_type and fc->col_offset arrays, which we write into always.
	 We should probably split the interpreted and non-interpreted bits
	 into two different routines, since there's so much that doesn't
	 really overlap between them.  */
      if (1 || do_debug_frames_interp)
	{
	  /* Start by making a pass over the chunk, allocating storage
	     and taking note of what registers are used.  */
	  unsigned char *tmp = start;

	  while (start < block_end)
	    {
	      unsigned op, opa;
5457
	      unsigned long reg, temp;
5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471

	      op = *start++;
	      opa = op & 0x3f;
	      if (op & 0xc0)
		op &= 0xc0;

	      /* Warning: if you add any more cases to this switch, be
		 sure to add them to the corresponding switch below.  */
	      switch (op)
		{
		case DW_CFA_advance_loc:
		  break;
		case DW_CFA_offset:
		  LEB ();
5472 5473
		  if (frame_need_space (fc, opa) >= 0)
		    fc->col_type[opa] = DW_CFA_undefined;
5474 5475
		  break;
		case DW_CFA_restore:
5476 5477
		  if (frame_need_space (fc, opa) >= 0)
		    fc->col_type[opa] = DW_CFA_undefined;
5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491
		  break;
		case DW_CFA_set_loc:
		  start += encoded_ptr_size;
		  break;
		case DW_CFA_advance_loc1:
		  start += 1;
		  break;
		case DW_CFA_advance_loc2:
		  start += 2;
		  break;
		case DW_CFA_advance_loc4:
		  start += 4;
		  break;
		case DW_CFA_offset_extended:
5492
		case DW_CFA_val_offset:
5493
		  reg = LEB (); LEB ();
5494 5495
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
5496 5497 5498 5499
		  break;
		case DW_CFA_restore_extended:
		  reg = LEB ();
		  frame_need_space (fc, reg);
5500 5501
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
5502 5503 5504
		  break;
		case DW_CFA_undefined:
		  reg = LEB ();
5505 5506
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
5507 5508 5509
		  break;
		case DW_CFA_same_value:
		  reg = LEB ();
5510 5511
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
5512 5513 5514
		  break;
		case DW_CFA_register:
		  reg = LEB (); LEB ();
5515 5516
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527
		  break;
		case DW_CFA_def_cfa:
		  LEB (); LEB ();
		  break;
		case DW_CFA_def_cfa_register:
		  LEB ();
		  break;
		case DW_CFA_def_cfa_offset:
		  LEB ();
		  break;
		case DW_CFA_def_cfa_expression:
5528 5529
		  temp = LEB ();
		  start += temp;
5530 5531
		  break;
		case DW_CFA_expression:
5532
		case DW_CFA_val_expression:
5533
		  reg = LEB ();
5534 5535
		  temp = LEB ();
		  start += temp;
5536 5537
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
5538 5539
		  break;
		case DW_CFA_offset_extended_sf:
5540
		case DW_CFA_val_offset_sf:
5541
		  reg = LEB (); SLEB ();
5542 5543
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558
		  break;
		case DW_CFA_def_cfa_sf:
		  LEB (); SLEB ();
		  break;
		case DW_CFA_def_cfa_offset_sf:
		  SLEB ();
		  break;
		case DW_CFA_MIPS_advance_loc8:
		  start += 8;
		  break;
		case DW_CFA_GNU_args_size:
		  LEB ();
		  break;
		case DW_CFA_GNU_negative_offset_extended:
		  reg = LEB (); LEB ();
5559 5560 5561
		  if (frame_need_space (fc, reg) >= 0)
		    fc->col_type[reg] = DW_CFA_undefined;
		  break;
5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575
		default:
		  break;
		}
	    }
	  start = tmp;
	}

      /* Now we know what registers are used, make a second pass over
	 the chunk, this time actually printing out the info.  */

      while (start < block_end)
	{
	  unsigned op, opa;
	  unsigned long ul, reg, roffs;
5576 5577
	  long l;
	  dwarf_vma ofs;
5578
	  dwarf_vma vma;
5579
	  const char *reg_prefix = "";
5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593

	  op = *start++;
	  opa = op & 0x3f;
	  if (op & 0xc0)
	    op &= 0xc0;

	  /* Warning: if you add any more cases to this switch, be
	     sure to add them to the corresponding switch above.  */
	  switch (op)
	    {
	    case DW_CFA_advance_loc:
	      if (do_debug_frames_interp)
		frame_display_row (fc, &need_col_headers, &max_regs);
	      else
5594
		printf ("  DW_CFA_advance_loc: %d to %s\n",
5595
			opa * fc->code_factor,
5596 5597 5598
			dwarf_vmatoa_1 (NULL, 
					fc->pc_begin + opa * fc->code_factor,
					fc->ptr_size));
5599 5600 5601 5602 5603
	      fc->pc_begin += opa * fc->code_factor;
	      break;

	    case DW_CFA_offset:
	      roffs = LEB ();
5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614
	      if (opa >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_offset: %s%s at cfa%+ld\n",
			reg_prefix, regname (opa, 0),
			roffs * fc->data_factor);
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[opa] = DW_CFA_offset;
		  fc->col_offset[opa] = roffs * fc->data_factor;
		}
5615 5616 5617
	      break;

	    case DW_CFA_restore:
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627
	      if (opa >= (unsigned int) cie->ncols
		  || opa >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_restore: %s%s\n",
			reg_prefix, regname (opa, 0));
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[opa] = cie->col_type[opa];
		  fc->col_offset[opa] = cie->col_offset[opa];
5628 5629 5630
		  if (do_debug_frames_interp
		      && fc->col_type[opa] == DW_CFA_unreferenced)
		    fc->col_type[opa] = DW_CFA_undefined;
5631
		}
5632 5633 5634
	      break;

	    case DW_CFA_set_loc:
5635
	      vma = get_encoded_value (start, fc->fde_encoding, section);
5636 5637 5638 5639
	      start += encoded_ptr_size;
	      if (do_debug_frames_interp)
		frame_display_row (fc, &need_col_headers, &max_regs);
	      else
5640 5641
		printf ("  DW_CFA_set_loc: %s\n",
			dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
5642 5643 5644 5645
	      fc->pc_begin = vma;
	      break;

	    case DW_CFA_advance_loc1:
Nick Clifton's avatar
Nick Clifton committed
5646
	      SAFE_BYTE_GET_AND_INC (ofs, start, 1, end);
5647 5648 5649
	      if (do_debug_frames_interp)
		frame_display_row (fc, &need_col_headers, &max_regs);
	      else
5650 5651 5652 5653 5654
		printf ("  DW_CFA_advance_loc1: %ld to %s\n",
			(unsigned long) (ofs * fc->code_factor),
			dwarf_vmatoa_1 (NULL,
					fc->pc_begin + ofs * fc->code_factor,
					fc->ptr_size));
5655 5656 5657 5658
	      fc->pc_begin += ofs * fc->code_factor;
	      break;

	    case DW_CFA_advance_loc2:
Nick Clifton's avatar
Nick Clifton committed
5659
	      SAFE_BYTE_GET_AND_INC (ofs, start, 2, end);
5660 5661 5662
	      if (do_debug_frames_interp)
		frame_display_row (fc, &need_col_headers, &max_regs);
	      else
5663 5664 5665 5666 5667
		printf ("  DW_CFA_advance_loc2: %ld to %s\n",
			(unsigned long) (ofs * fc->code_factor),
			dwarf_vmatoa_1 (NULL,
					fc->pc_begin + ofs * fc->code_factor,
					fc->ptr_size));
5668 5669 5670 5671
	      fc->pc_begin += ofs * fc->code_factor;
	      break;

	    case DW_CFA_advance_loc4:
Nick Clifton's avatar
Nick Clifton committed
5672
	      SAFE_BYTE_GET_AND_INC (ofs, start, 4, end);
5673 5674 5675
	      if (do_debug_frames_interp)
		frame_display_row (fc, &need_col_headers, &max_regs);
	      else
5676 5677 5678 5679 5680
		printf ("  DW_CFA_advance_loc4: %ld to %s\n",
			(unsigned long) (ofs * fc->code_factor),
			dwarf_vmatoa_1 (NULL,
					fc->pc_begin + ofs * fc->code_factor,
					fc->ptr_size));
5681 5682 5683 5684 5685 5686
	      fc->pc_begin += ofs * fc->code_factor;
	      break;

	    case DW_CFA_offset_extended:
	      reg = LEB ();
	      roffs = LEB ();
5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697
	      if (reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_offset_extended: %s%s at cfa%+ld\n",
			reg_prefix, regname (reg, 0),
			roffs * fc->data_factor);
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_offset;
		  fc->col_offset[reg] = roffs * fc->data_factor;
		}
5698 5699
	      break;

5700 5701 5702
	    case DW_CFA_val_offset:
	      reg = LEB ();
	      roffs = LEB ();
5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713
	      if (reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_val_offset: %s%s at cfa%+ld\n",
			reg_prefix, regname (reg, 0),
			roffs * fc->data_factor);
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_val_offset;
		  fc->col_offset[reg] = roffs * fc->data_factor;
		}
5714 5715
	      break;

5716 5717
	    case DW_CFA_restore_extended:
	      reg = LEB ();
5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728
	      if (reg >= (unsigned int) cie->ncols
		  || reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_restore_extended: %s%s\n",
			reg_prefix, regname (reg, 0));
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = cie->col_type[reg];
		  fc->col_offset[reg] = cie->col_offset[reg];
		}
5729 5730 5731 5732
	      break;

	    case DW_CFA_undefined:
	      reg = LEB ();
5733 5734 5735 5736 5737 5738 5739 5740 5741 5742
	      if (reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_undefined: %s%s\n",
			reg_prefix, regname (reg, 0));
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_undefined;
		  fc->col_offset[reg] = 0;
		}
5743 5744 5745 5746
	      break;

	    case DW_CFA_same_value:
	      reg = LEB ();
5747 5748 5749 5750 5751 5752 5753 5754 5755 5756
	      if (reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_same_value: %s%s\n",
			reg_prefix, regname (reg, 0));
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_same_value;
		  fc->col_offset[reg] = 0;
		}
5757 5758 5759 5760 5761
	      break;

	    case DW_CFA_register:
	      reg = LEB ();
	      roffs = LEB ();
5762 5763 5764
	      if (reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
H.J. Lu's avatar
H.J. Lu committed
5765
		{
5766 5767
		  printf ("  DW_CFA_register: %s%s in ",
			  reg_prefix, regname (reg, 0));
H.J. Lu's avatar
H.J. Lu committed
5768 5769
		  puts (regname (roffs, 0));
		}
5770 5771 5772 5773 5774
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_register;
		  fc->col_offset[reg] = roffs;
		}
5775 5776 5777 5778 5779
	      break;

	    case DW_CFA_remember_state:
	      if (! do_debug_frames_interp)
		printf ("  DW_CFA_remember_state\n");
5780
	      rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
5781
	      rs->ncols = fc->ncols;
5782 5783 5784
	      rs->col_type = (short int *) xcmalloc (rs->ncols,
                                                     sizeof (short int));
	      rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (int));
5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797
	      memcpy (rs->col_type, fc->col_type, rs->ncols);
	      memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
	      rs->next = remembered_state;
	      remembered_state = rs;
	      break;

	    case DW_CFA_restore_state:
	      if (! do_debug_frames_interp)
		printf ("  DW_CFA_restore_state\n");
	      rs = remembered_state;
	      if (rs)
		{
		  remembered_state = rs->next;
5798
		  frame_need_space (fc, rs->ncols - 1);
5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814
		  memcpy (fc->col_type, rs->col_type, rs->ncols);
		  memcpy (fc->col_offset, rs->col_offset,
			  rs->ncols * sizeof (int));
		  free (rs->col_type);
		  free (rs->col_offset);
		  free (rs);
		}
	      else if (do_debug_frames_interp)
		printf ("Mismatched DW_CFA_restore_state\n");
	      break;

	    case DW_CFA_def_cfa:
	      fc->cfa_reg = LEB ();
	      fc->cfa_offset = LEB ();
	      fc->cfa_exp = 0;
	      if (! do_debug_frames_interp)
H.J. Lu's avatar
H.J. Lu committed
5815 5816
		printf ("  DW_CFA_def_cfa: %s ofs %d\n",
			regname (fc->cfa_reg, 0), fc->cfa_offset);
5817 5818 5819 5820 5821 5822
	      break;

	    case DW_CFA_def_cfa_register:
	      fc->cfa_reg = LEB ();
	      fc->cfa_exp = 0;
	      if (! do_debug_frames_interp)
H.J. Lu's avatar
H.J. Lu committed
5823 5824
		printf ("  DW_CFA_def_cfa_register: %s\n",
			regname (fc->cfa_reg, 0));
5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842
	      break;

	    case DW_CFA_def_cfa_offset:
	      fc->cfa_offset = LEB ();
	      if (! do_debug_frames_interp)
		printf ("  DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
	      break;

	    case DW_CFA_nop:
	      if (! do_debug_frames_interp)
		printf ("  DW_CFA_nop\n");
	      break;

	    case DW_CFA_def_cfa_expression:
	      ul = LEB ();
	      if (! do_debug_frames_interp)
		{
		  printf ("  DW_CFA_def_cfa_expression (");
5843 5844
		  decode_location_expression (start, eh_addr_size, 0, -1,
					      ul, 0, section);
5845 5846 5847 5848 5849 5850 5851 5852 5853
		  printf (")\n");
		}
	      fc->cfa_exp = 1;
	      start += ul;
	      break;

	    case DW_CFA_expression:
	      reg = LEB ();
	      ul = LEB ();
5854 5855 5856
	      if (reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5857
		{
5858 5859
		  printf ("  DW_CFA_expression: %s%s (",
			  reg_prefix, regname (reg, 0));
5860
		  decode_location_expression (start, eh_addr_size, 0, -1,
Richard Henderson's avatar
Richard Henderson committed
5861
					      ul, 0, section);
5862 5863
		  printf (")\n");
		}
5864 5865
	      if (*reg_prefix == '\0')
		fc->col_type[reg] = DW_CFA_expression;
5866 5867 5868
	      start += ul;
	      break;

5869 5870 5871
	    case DW_CFA_val_expression:
	      reg = LEB ();
	      ul = LEB ();
5872 5873 5874
	      if (reg >= (unsigned int) fc->ncols)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
5875
		{
5876 5877
		  printf ("  DW_CFA_val_expression: %s%s (",
			  reg_prefix, regname (reg, 0));
5878 5879
		  decode_location_expression (start, eh_addr_size, 0, -1,
					      ul, 0, section);
5880 5881
		  printf (")\n");
		}
5882 5883
	      if (*reg_prefix == '\0')
		fc->col_type[reg] = DW_CFA_val_expression;
5884 5885 5886
	      start += ul;
	      break;

5887 5888 5889
	    case DW_CFA_offset_extended_sf:
	      reg = LEB ();
	      l = SLEB ();
5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900
	      if (frame_need_space (fc, reg) < 0)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
			reg_prefix, regname (reg, 0),
			l * fc->data_factor);
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_offset;
		  fc->col_offset[reg] = l * fc->data_factor;
		}
5901 5902
	      break;

5903 5904 5905
	    case DW_CFA_val_offset_sf:
	      reg = LEB ();
	      l = SLEB ();
5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916
	      if (frame_need_space (fc, reg) < 0)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_val_offset_sf: %s%s at cfa%+ld\n",
			reg_prefix, regname (reg, 0),
			l * fc->data_factor);
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_val_offset;
		  fc->col_offset[reg] = l * fc->data_factor;
		}
5917 5918
	      break;

5919 5920 5921 5922 5923 5924
	    case DW_CFA_def_cfa_sf:
	      fc->cfa_reg = LEB ();
	      fc->cfa_offset = SLEB ();
	      fc->cfa_offset = fc->cfa_offset * fc->data_factor;
	      fc->cfa_exp = 0;
	      if (! do_debug_frames_interp)
H.J. Lu's avatar
H.J. Lu committed
5925 5926
		printf ("  DW_CFA_def_cfa_sf: %s ofs %d\n",
			regname (fc->cfa_reg, 0), fc->cfa_offset);
5927 5928 5929 5930 5931 5932 5933 5934 5935 5936
	      break;

	    case DW_CFA_def_cfa_offset_sf:
	      fc->cfa_offset = SLEB ();
	      fc->cfa_offset = fc->cfa_offset * fc->data_factor;
	      if (! do_debug_frames_interp)
		printf ("  DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
	      break;

	    case DW_CFA_MIPS_advance_loc8:
Nick Clifton's avatar
Nick Clifton committed
5937
	      SAFE_BYTE_GET_AND_INC (ofs, start, 8, end);
5938 5939 5940
	      if (do_debug_frames_interp)
		frame_display_row (fc, &need_col_headers, &max_regs);
	      else
5941 5942 5943 5944 5945
		printf ("  DW_CFA_MIPS_advance_loc8: %ld to %s\n",
			(unsigned long) (ofs * fc->code_factor),
			dwarf_vmatoa_1 (NULL,
					fc->pc_begin + ofs * fc->code_factor,
					fc->ptr_size));
5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962
	      fc->pc_begin += ofs * fc->code_factor;
	      break;

	    case DW_CFA_GNU_window_save:
	      if (! do_debug_frames_interp)
		printf ("  DW_CFA_GNU_window_save\n");
	      break;

	    case DW_CFA_GNU_args_size:
	      ul = LEB ();
	      if (! do_debug_frames_interp)
		printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
	      break;

	    case DW_CFA_GNU_negative_offset_extended:
	      reg = LEB ();
	      l = - LEB ();
5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973
	      if (frame_need_space (fc, reg) < 0)
		reg_prefix = bad_reg;
	      if (! do_debug_frames_interp || *reg_prefix != '\0')
		printf ("  DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
			reg_prefix, regname (reg, 0),
			l * fc->data_factor);
	      if (*reg_prefix == '\0')
		{
		  fc->col_type[reg] = DW_CFA_offset;
		  fc->col_offset[reg] = l * fc->data_factor;
		}
5974 5975 5976
	      break;

	    default:
5977 5978 5979
	      if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
		printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
	      else
5980
		warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
5981 5982 5983 5984 5985 5986 5987 5988
	      start = block_end;
	    }
	}

      if (do_debug_frames_interp)
	frame_display_row (fc, &need_col_headers, &max_regs);

      start = block_end;
Jakub Jelinek's avatar
bfd/  
Jakub Jelinek committed
5989
      eh_addr_size = saved_eh_addr_size;
5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000
    }

  printf ("\n");

  return 1;
}

#undef GET
#undef LEB
#undef SLEB

6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025
static int
display_gdb_index (struct dwarf_section *section,
		   void *file ATTRIBUTE_UNUSED)
{
  unsigned char *start = section->start;
  uint32_t version;
  uint32_t cu_list_offset, tu_list_offset;
  uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
  unsigned int cu_list_elements, tu_list_elements;
  unsigned int address_table_size, symbol_table_slots;
  unsigned char *cu_list, *tu_list;
  unsigned char *address_table, *symbol_table, *constant_pool;
  unsigned int i;

  /* The documentation for the format of this file is in gdb/dwarf2read.c.  */

  printf (_("Contents of the %s section:\n"), section->name);

  if (section->size < 6 * sizeof (uint32_t))
    {
      warn (_("Truncated header in the %s section.\n"), section->name);
      return 0;
    }

  version = byte_get_little_endian (start, 4);
6026
  printf (_("Version %ld\n"), (long) version);
6027 6028 6029

  /* Prior versions are obsolete, and future versions may not be
     backwards compatible.  */
6030
  if (version < 3 || version > 8)
6031
    {
6032
      warn (_("Unsupported version %lu.\n"), (unsigned long) version);
6033 6034
      return 0;
    }
Tom Tromey's avatar
Tom Tromey committed
6035 6036 6037 6038 6039 6040 6041 6042
  if (version < 4)
    warn (_("The address table data in version 3 may be wrong.\n"));
  if (version < 5)
    warn (_("Version 4 does not support case insensitive lookups.\n"));
  if (version < 6)
    warn (_("Version 5 does not include inlined functions.\n"));
  if (version < 7)
      warn (_("Version 6 does not include symbol attributes.\n"));
6043 6044 6045 6046
  /* Version 7 indices generated by Gold have bad type unit references,
     PR binutils/15021.  But we don't know if the index was generated by
     Gold or not, so to avoid worrying users with gdb-generated indices
     we say nothing for version 7 here.  */
6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108

  cu_list_offset = byte_get_little_endian (start + 4, 4);
  tu_list_offset = byte_get_little_endian (start + 8, 4);
  address_table_offset = byte_get_little_endian (start + 12, 4);
  symbol_table_offset = byte_get_little_endian (start + 16, 4);
  constant_pool_offset = byte_get_little_endian (start + 20, 4);

  if (cu_list_offset > section->size
      || tu_list_offset > section->size
      || address_table_offset > section->size
      || symbol_table_offset > section->size
      || constant_pool_offset > section->size)
    {
      warn (_("Corrupt header in the %s section.\n"), section->name);
      return 0;
    }

  cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
  tu_list_elements = (address_table_offset - tu_list_offset) / 8;
  address_table_size = symbol_table_offset - address_table_offset;
  symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;

  cu_list = start + cu_list_offset;
  tu_list = start + tu_list_offset;
  address_table = start + address_table_offset;
  symbol_table = start + symbol_table_offset;
  constant_pool = start + constant_pool_offset;

  printf (_("\nCU table:\n"));
  for (i = 0; i < cu_list_elements; i += 2)
    {
      uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
      uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);

      printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
	      (unsigned long) cu_offset,
	      (unsigned long) (cu_offset + cu_length - 1));
    }

  printf (_("\nTU table:\n"));
  for (i = 0; i < tu_list_elements; i += 3)
    {
      uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
      uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
      uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);

      printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
	      (unsigned long) tu_offset,
	      (unsigned long) type_offset);
      print_dwarf_vma (signature, 8);
      printf ("\n");
    }

  printf (_("\nAddress table:\n"));
  for (i = 0; i < address_table_size; i += 2 * 8 + 4)
    {
      uint64_t low = byte_get_little_endian (address_table + i, 8);
      uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
      uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);

      print_dwarf_vma (low, 8);
      print_dwarf_vma (high, 8);
6109
      printf (_("%lu\n"), (unsigned long) cu_index);
6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125
    }

  printf (_("\nSymbol table:\n"));
  for (i = 0; i < symbol_table_slots; ++i)
    {
      uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
      uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
      uint32_t num_cus, cu;

      if (name_offset != 0
	  || cu_vector_offset != 0)
	{
	  unsigned int j;

	  printf ("[%3u] %s:", i, constant_pool + name_offset);
	  num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
Tom Tromey's avatar
Tom Tromey committed
6126 6127
	  if (num_cus > 1)
	    printf ("\n");
6128 6129
	  for (j = 0; j < num_cus; ++j)
	    {
6130
	      int is_static;
Tom Tromey's avatar
Tom Tromey committed
6131 6132
	      gdb_index_symbol_kind kind;

6133
	      cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
6134
	      is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
Tom Tromey's avatar
Tom Tromey committed
6135 6136
	      kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
	      cu = GDB_INDEX_CU_VALUE (cu);
6137
	      /* Convert to TU number if it's for a type unit.  */
6138
	      if (cu >= cu_list_elements / 2)
Tom Tromey's avatar
Tom Tromey committed
6139 6140
		printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
			(unsigned long) (cu - cu_list_elements / 2));
6141
	      else
Tom Tromey's avatar
Tom Tromey committed
6142 6143 6144 6145 6146 6147 6148 6149
		printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);

	      switch (kind)
		{
		case GDB_INDEX_SYMBOL_KIND_NONE:
		  printf (_(" [no symbol information]"));
		  break;
		case GDB_INDEX_SYMBOL_KIND_TYPE:
6150 6151 6152
		  printf (is_static
			  ? _(" [static type]")
			  : _(" [global type]"));
Tom Tromey's avatar
Tom Tromey committed
6153 6154
		  break;
		case GDB_INDEX_SYMBOL_KIND_VARIABLE:
6155 6156 6157
		  printf (is_static
			  ? _(" [static variable]")
			  : _(" [global variable]"));
Tom Tromey's avatar
Tom Tromey committed
6158 6159
		  break;
		case GDB_INDEX_SYMBOL_KIND_FUNCTION:
6160 6161 6162
		  printf (is_static
			  ? _(" [static function]")
			  : _(" [global function]"));
Tom Tromey's avatar
Tom Tromey committed
6163 6164
		  break;
		case GDB_INDEX_SYMBOL_KIND_OTHER:
6165 6166 6167
		  printf (is_static
			  ? _(" [static other]")
			  : _(" [global other]"));
Tom Tromey's avatar
Tom Tromey committed
6168 6169
		  break;
		default:
6170 6171 6172 6173
		  printf (is_static
			  ? _(" [static unknown: %d]")
			  : _(" [global unknown: %d]"),
			  kind);
Tom Tromey's avatar
Tom Tromey committed
6174 6175 6176 6177
		  break;
		}
	      if (num_cus > 1)
		printf ("\n");
6178
	    }
Tom Tromey's avatar
Tom Tromey committed
6179 6180
	  if (num_cus <= 1)
	    printf ("\n");
6181 6182 6183 6184 6185 6186
	}
    }

  return 1;
}

Cary Coutant's avatar
Cary Coutant committed
6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228
/* Pre-allocate enough space for the CU/TU sets needed.  */

static void
prealloc_cu_tu_list (unsigned int nshndx)
{
  if (shndx_pool == NULL)
    {
      shndx_pool_size = nshndx;
      shndx_pool_used = 0;
      shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
					      sizeof (unsigned int));
    }
  else
    {
      shndx_pool_size = shndx_pool_used + nshndx;
      shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
					       sizeof (unsigned int));
    }
}

static void
add_shndx_to_cu_tu_entry (unsigned int shndx)
{
  if (shndx_pool_used >= shndx_pool_size)
    {
      error (_("Internal error: out of space in the shndx pool.\n"));
      return;
    }
  shndx_pool [shndx_pool_used++] = shndx;
}

static void
end_cu_tu_entry (void)
{
  if (shndx_pool_used >= shndx_pool_size)
    {
      error (_("Internal error: out of space in the shndx pool.\n"));
      return;
    }
  shndx_pool [shndx_pool_used++] = 0;
}

Cary Coutant's avatar
Cary Coutant committed
6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264
/* Return the short name of a DWARF section given by a DW_SECT enumerator.  */

static const char *
get_DW_SECT_short_name (unsigned int dw_sect)
{
  static char buf[16];

  switch (dw_sect)
    {
      case DW_SECT_INFO:
	return "info";
      case DW_SECT_TYPES:
	return "types";
      case DW_SECT_ABBREV:
	return "abbrev";
      case DW_SECT_LINE:
	return "line";
      case DW_SECT_LOC:
	return "loc";
      case DW_SECT_STR_OFFSETS:
	return "str_off";
      case DW_SECT_MACINFO:
	return "macinfo";
      case DW_SECT_MACRO:
	return "macro";
      default:
        break;
    }

  snprintf (buf, sizeof (buf), "%d", dw_sect);
  return buf;
}

/* Process a CU or TU index.  If DO_DISPLAY is true, print the contents.
   These sections are extensions for Fission.
   See http://gcc.gnu.org/wiki/DebugFissionDWP.  */
Cary Coutant's avatar
Cary Coutant committed
6265 6266 6267 6268 6269 6270 6271 6272 6273 6274

static int
process_cu_tu_index (struct dwarf_section *section, int do_display)
{
  unsigned char *phdr = section->start;
  unsigned char *limit = phdr + section->size;
  unsigned char *phash;
  unsigned char *pindex;
  unsigned char *ppool;
  unsigned int version;
Cary Coutant's avatar
Cary Coutant committed
6275
  unsigned int ncols = 0;
Cary Coutant's avatar
Cary Coutant committed
6276 6277 6278
  unsigned int nused;
  unsigned int nslots;
  unsigned int i;
Cary Coutant's avatar
Cary Coutant committed
6279 6280 6281 6282
  unsigned int j;
  dwarf_vma signature_high;
  dwarf_vma signature_low;
  char buf[64];
Cary Coutant's avatar
Cary Coutant committed
6283 6284

  version = byte_get (phdr, 4);
Cary Coutant's avatar
Cary Coutant committed
6285 6286
  if (version >= 2)
    ncols = byte_get (phdr + 4, 4);
Cary Coutant's avatar
Cary Coutant committed
6287 6288 6289 6290 6291 6292 6293 6294 6295 6296
  nused = byte_get (phdr + 8, 4);
  nslots = byte_get (phdr + 12, 4);
  phash = phdr + 16;
  pindex = phash + nslots * 8;
  ppool = pindex + nslots * 4;

  if (do_display)
    {
      printf (_("Contents of the %s section:\n\n"), section->name);
      printf (_("  Version:                 %d\n"), version);
Cary Coutant's avatar
Cary Coutant committed
6297 6298
      if (version >= 2)
	printf (_("  Number of columns:       %d\n"), ncols);
Cary Coutant's avatar
Cary Coutant committed
6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309
      printf (_("  Number of used entries:  %d\n"), nused);
      printf (_("  Number of slots:         %d\n\n"), nslots);
    }

  if (ppool > limit)
    {
      warn (_("Section %s too small for %d hash table entries\n"),
	    section->name, nslots);
      return 0;
    }

Cary Coutant's avatar
Cary Coutant committed
6310
  if (version == 1)
Cary Coutant's avatar
Cary Coutant committed
6311
    {
Cary Coutant's avatar
Cary Coutant committed
6312 6313 6314
      if (!do_display)
	prealloc_cu_tu_list ((limit - ppool) / 4);
      for (i = 0; i < nslots; i++)
Cary Coutant's avatar
Cary Coutant committed
6315
	{
Cary Coutant's avatar
Cary Coutant committed
6316 6317 6318 6319 6320
	  unsigned char *shndx_list;
	  unsigned int shndx;

	  byte_get_64 (phash, &signature_high, &signature_low);
	  if (signature_high != 0 || signature_low != 0)
Cary Coutant's avatar
Cary Coutant committed
6321
	    {
Cary Coutant's avatar
Cary Coutant committed
6322 6323 6324 6325 6326 6327 6328
	      j = byte_get (pindex, 4);
	      shndx_list = ppool + j * 4;
	      if (do_display)
		printf (_("  [%3d] Signature:  0x%s  Sections: "),
			i, dwarf_vmatoa64 (signature_high, signature_low,
					   buf, sizeof (buf)));
	      for (;;)
Cary Coutant's avatar
Cary Coutant committed
6329
		{
Cary Coutant's avatar
Cary Coutant committed
6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343
		  if (shndx_list >= limit)
		    {
		      warn (_("Section %s too small for shndx pool\n"),
			    section->name);
		      return 0;
		    }
		  shndx = byte_get (shndx_list, 4);
		  if (shndx == 0)
		    break;
		  if (do_display)
		    printf (" %d", shndx);
		  else
		    add_shndx_to_cu_tu_entry (shndx);
		  shndx_list += 4;
Cary Coutant's avatar
Cary Coutant committed
6344 6345
		}
	      if (do_display)
Cary Coutant's avatar
Cary Coutant committed
6346
		printf ("\n");
Cary Coutant's avatar
Cary Coutant committed
6347
	      else
Cary Coutant's avatar
Cary Coutant committed
6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389
		end_cu_tu_entry ();
	    }
	  phash += 8;
	  pindex += 4;
	}
    }
  else if (version == 2)
    {
      unsigned int val;
      unsigned int dw_sect;
      unsigned char *ph = phash;
      unsigned char *pi = pindex;
      unsigned char *poffsets = ppool + ncols * 4;
      unsigned char *psizes = poffsets + nused * ncols * 4;
      unsigned char *pend = psizes + nused * ncols * 4;
      bfd_boolean is_tu_index;
      struct cu_tu_set *this_set = NULL;
      unsigned int row;
      unsigned char *prow;

      is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;

      if (pend > limit)
	{
	  warn (_("Section %s too small for offset and size tables\n"),
		section->name);
	  return 0;
	}

      if (do_display)
	{
	  printf (_("  Offset table\n"));
	  printf ("  slot  %-16s  ",
		 is_tu_index ? _("signature") : _("dwo_id"));
	}
      else
	{
	  if (is_tu_index)
	    {
	      tu_count = nused;
	      tu_sets = xcmalloc (nused, sizeof (struct cu_tu_set));
	      this_set = tu_sets;
Cary Coutant's avatar
Cary Coutant committed
6390 6391
	    }
	  else
Cary Coutant's avatar
Cary Coutant committed
6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481
	    {
	      cu_count = nused;
	      cu_sets = xcmalloc (nused, sizeof (struct cu_tu_set));
	      this_set = cu_sets;
	    }
	}
      if (do_display)
	{
	  for (j = 0; j < ncols; j++)
	    {
	      dw_sect = byte_get (ppool + j * 4, 4);
	      printf (" %8s", get_DW_SECT_short_name (dw_sect));
	    }
	  printf ("\n");
	}
      for (i = 0; i < nslots; i++)
	{
	  byte_get_64 (ph, &signature_high, &signature_low);
	  row = byte_get (pi, 4);
	  if (row != 0)
	    {
	      if (!do_display)
		memcpy (&this_set[row - 1].signature, ph, sizeof (uint64_t));
	      prow = poffsets + (row - 1) * ncols * 4;
	      if (do_display)
		printf (_("  [%3d] 0x%s"),
			i, dwarf_vmatoa64 (signature_high, signature_low,
					   buf, sizeof (buf)));
	      for (j = 0; j < ncols; j++)
		{
		  val = byte_get (prow + j * 4, 4);
		  if (do_display)
		    printf (" %8d", val);
		  else
		    {
		      dw_sect = byte_get (ppool + j * 4, 4);
		      this_set [row - 1].section_offsets [dw_sect] = val;
		    }
		}
	      if (do_display)
		printf ("\n");
	    }
	  ph += 8;
	  pi += 4;
	}

      ph = phash;
      pi = pindex;
      if (do_display)
        {
	  printf ("\n");
	  printf (_("  Size table\n"));
	  printf ("  slot  %-16s  ",
		 is_tu_index ? _("signature") : _("dwo_id"));
        }
      for (j = 0; j < ncols; j++)
	{
	  val = byte_get (ppool + j * 4, 4);
	  if (do_display)
	    printf (" %8s", get_DW_SECT_short_name (val));
	}
      if (do_display)
	printf ("\n");
      for (i = 0; i < nslots; i++)
	{
	  byte_get_64 (ph, &signature_high, &signature_low);
	  row = byte_get (pi, 4);
	  if (row != 0)
	    {
	      prow = psizes + (row - 1) * ncols * 4;
	      if (do_display)
		printf (_("  [%3d] 0x%s"),
			i, dwarf_vmatoa64 (signature_high, signature_low,
					   buf, sizeof (buf)));
	      for (j = 0; j < ncols; j++)
		{
		  val = byte_get (prow + j * 4, 4);
		  if (do_display)
		    printf (" %8d", val);
		  else
		    {
		      dw_sect = byte_get (ppool + j * 4, 4);
		      this_set [row - 1].section_sizes [dw_sect] = val;
		    }
		}
	      if (do_display)
		printf ("\n");
	    }
	  ph += 8;
	  pi += 4;
Cary Coutant's avatar
Cary Coutant committed
6482 6483
	}
    }
Cary Coutant's avatar
Cary Coutant committed
6484 6485
  else if (do_display)
    printf (_("  Unsupported version\n"));
Cary Coutant's avatar
Cary Coutant committed
6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545

  if (do_display)
      printf ("\n");

  return 1;
}

/* Load the CU and TU indexes if present.  This will build a list of
   section sets that we can use to associate a .debug_info.dwo section
   with its associated .debug_abbrev.dwo section in a .dwp file.  */

static void
load_cu_tu_indexes (void *file)
{
  /* If we have already loaded (or tried to load) the CU and TU indexes
     then do not bother to repeat the task.  */
  if (cu_tu_indexes_read)
    return;

  if (load_debug_section (dwp_cu_index, file))
    process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0);

  if (load_debug_section (dwp_tu_index, file))
    process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0);

  cu_tu_indexes_read = 1;
}

/* Find the set of sections that includes section SHNDX.  */

unsigned int *
find_cu_tu_set (void *file, unsigned int shndx)
{
  unsigned int i;

  load_cu_tu_indexes (file);

  /* Find SHNDX in the shndx pool.  */
  for (i = 0; i < shndx_pool_used; i++)
    if (shndx_pool [i] == shndx)
      break;

  if (i >= shndx_pool_used)
    return NULL;

  /* Now backup to find the first entry in the set.  */
  while (i > 0 && shndx_pool [i - 1] != 0)
    i--;

  return shndx_pool + i;
}

/* Display a .debug_cu_index or .debug_tu_index section.  */

static int
display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
{
  return process_cu_tu_index (section, 1);
}

6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588
static int
display_debug_not_supported (struct dwarf_section *section,
			     void *file ATTRIBUTE_UNUSED)
{
  printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
	    section->name);

  return 1;
}

void *
cmalloc (size_t nmemb, size_t size)
{
  /* Check for overflow.  */
  if (nmemb >= ~(size_t) 0 / size)
    return NULL;
  else
    return malloc (nmemb * size);
}

void *
xcmalloc (size_t nmemb, size_t size)
{
  /* Check for overflow.  */
  if (nmemb >= ~(size_t) 0 / size)
    return NULL;
  else
    return xmalloc (nmemb * size);
}

void *
xcrealloc (void *ptr, size_t nmemb, size_t size)
{
  /* Check for overflow.  */
  if (nmemb >= ~(size_t) 0 / size)
    return NULL;
  else
    return xrealloc (ptr, nmemb * size);
}

void
free_debug_memory (void)
{
6589
  unsigned int i;
6590 6591 6592 6593

  free_abbrevs ();

  for (i = 0; i < max; i++)
6594
    free_debug_section ((enum dwarf_section_display_enum) i);
6595

6596
  if (debug_information != NULL)
6597
    {
6598
      if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
6599
	{
6600
	  for (i = 0; i < num_debug_info_entries; i++)
6601
	    {
6602 6603 6604 6605 6606 6607 6608
	      if (!debug_information [i].max_loc_offsets)
		{
		  free (debug_information [i].loc_offsets);
		  free (debug_information [i].have_frame_base);
		}
	      if (!debug_information [i].max_range_lists)
		free (debug_information [i].range_lists);
6609 6610
	    }
	}
6611

6612 6613 6614 6615 6616 6617
      free (debug_information);
      debug_information = NULL;
      num_debug_info_entries = 0;
    }
}

6618 6619 6620 6621 6622 6623 6624
void
dwarf_select_sections_by_names (const char *names)
{
  typedef struct
  {
    const char * option;
    int *        variable;
Nick Clifton's avatar
Nick Clifton committed
6625
    int          val;
6626 6627 6628 6629 6630 6631 6632 6633
  }
  debug_dump_long_opts;

  static const debug_dump_long_opts opts_table [] =
    {
      /* Please keep this table alpha- sorted.  */
      { "Ranges", & do_debug_ranges, 1 },
      { "abbrev", & do_debug_abbrevs, 1 },
Cary Coutant's avatar
Cary Coutant committed
6634
      { "addr", & do_debug_addr, 1 },
6635
      { "aranges", & do_debug_aranges, 1 },
Cary Coutant's avatar
Cary Coutant committed
6636 6637
      { "cu_index", & do_debug_cu_index, 1 },
      { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
6638 6639
      { "frames", & do_debug_frames, 1 },
      { "frames-interp", & do_debug_frames_interp, 1 },
Cary Coutant's avatar
Cary Coutant committed
6640 6641
      /* The special .gdb_index section.  */
      { "gdb_index", & do_gdb_index, 1 },
6642 6643 6644 6645 6646
      { "info", & do_debug_info, 1 },
      { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility.  */
      { "loc",  & do_debug_loc, 1 },
      { "macro", & do_debug_macinfo, 1 },
      { "pubnames", & do_debug_pubnames, 1 },
6647
      { "pubtypes", & do_debug_pubtypes, 1 },
6648 6649 6650
      /* This entry is for compatability
	 with earlier versions of readelf.  */
      { "ranges", & do_debug_aranges, 1 },
Cary Coutant's avatar
Cary Coutant committed
6651
      { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
6652
      { "str", & do_debug_str, 1 },
6653 6654 6655 6656
      /* These trace_* sections are used by Itanium VMS.  */
      { "trace_abbrev", & do_trace_abbrevs, 1 },
      { "trace_aranges", & do_trace_aranges, 1 },
      { "trace_info", & do_trace_info, 1 },
6657 6658 6659 6660
      { NULL, NULL, 0 }
    };

  const char *p;
6661

6662 6663 6664 6665
  p = names;
  while (*p)
    {
      const debug_dump_long_opts * entry;
6666

6667 6668 6669
      for (entry = opts_table; entry->option; entry++)
	{
	  size_t len = strlen (entry->option);
6670

6671 6672 6673 6674
	  if (strncmp (p, entry->option, len) == 0
	      && (p[len] == ',' || p[len] == '\0'))
	    {
	      * entry->variable |= entry->val;
6675

6676 6677 6678 6679 6680 6681 6682 6683 6684
	      /* The --debug-dump=frames-interp option also
		 enables the --debug-dump=frames option.  */
	      if (do_debug_frames_interp)
		do_debug_frames = 1;

	      p += len;
	      break;
	    }
	}
6685

6686 6687 6688 6689 6690 6691 6692
      if (entry->option == NULL)
	{
	  warn (_("Unrecognized debug option '%s'\n"), p);
	  p = strchr (p, ',');
	  if (p == NULL)
	    break;
	}
6693

6694 6695 6696 6697 6698 6699 6700 6701
      if (*p == ',')
	p++;
    }
}

void
dwarf_select_sections_by_letters (const char *letters)
{
6702
  unsigned int lindex = 0;
6703

6704 6705
  while (letters[lindex])
    switch (letters[lindex++])
6706 6707 6708 6709
      {
      case 'i':
	do_debug_info = 1;
	break;
6710

6711 6712 6713
      case 'a':
	do_debug_abbrevs = 1;
	break;
6714

6715 6716 6717
      case 'l':
	do_debug_lines |= FLAG_DEBUG_LINES_RAW;
	break;
6718

6719 6720 6721
      case 'L':
	do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
	break;
6722

6723 6724 6725
      case 'p':
	do_debug_pubnames = 1;
	break;
6726

Nick Clifton's avatar
Nick Clifton committed
6727 6728 6729
      case 't':
	do_debug_pubtypes = 1;
	break;
6730

6731 6732 6733
      case 'r':
	do_debug_aranges = 1;
	break;
6734

6735 6736 6737
      case 'R':
	do_debug_ranges = 1;
	break;
6738

6739 6740 6741 6742 6743
      case 'F':
	do_debug_frames_interp = 1;
      case 'f':
	do_debug_frames = 1;
	break;
6744

6745 6746 6747
      case 'm':
	do_debug_macinfo = 1;
	break;
6748

6749 6750 6751
      case 's':
	do_debug_str = 1;
	break;
6752

6753 6754 6755
      case 'o':
	do_debug_loc = 1;
	break;
6756

6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769
      default:
	warn (_("Unrecognized debug option '%s'\n"), optarg);
	break;
      }
}

void
dwarf_select_sections_all (void)
{
  do_debug_info = 1;
  do_debug_abbrevs = 1;
  do_debug_lines = FLAG_DEBUG_LINES_RAW;
  do_debug_pubnames = 1;
Nick Clifton's avatar
Nick Clifton committed
6770
  do_debug_pubtypes = 1;
6771 6772 6773 6774 6775 6776
  do_debug_aranges = 1;
  do_debug_ranges = 1;
  do_debug_frames = 1;
  do_debug_macinfo = 1;
  do_debug_str = 1;
  do_debug_loc = 1;
6777
  do_gdb_index = 1;
6778 6779 6780
  do_trace_info = 1;
  do_trace_abbrevs = 1;
  do_trace_aranges = 1;
Cary Coutant's avatar
Cary Coutant committed
6781 6782
  do_debug_addr = 1;
  do_debug_cu_index = 1;
6783 6784
}

6785 6786
struct dwarf_section_display debug_displays[] =
{
Cary Coutant's avatar
Cary Coutant committed
6787
  { { ".debug_abbrev",	    ".zdebug_abbrev",	NULL, NULL, 0, 0, 0 },
6788
    display_debug_abbrev,   &do_debug_abbrevs,	0 },
Cary Coutant's avatar
Cary Coutant committed
6789
  { { ".debug_aranges",	    ".zdebug_aranges",	NULL, NULL, 0, 0, 0 },
6790
    display_debug_aranges,  &do_debug_aranges,	1 },
Cary Coutant's avatar
Cary Coutant committed
6791
  { { ".debug_frame",       ".zdebug_frame",	NULL, NULL, 0, 0, 0 },
6792 6793 6794
    display_debug_frames,   &do_debug_frames,	1 },
  { { ".debug_info",	    ".zdebug_info",	NULL, NULL, 0, 0, abbrev },
    display_debug_info,	    &do_debug_info,	1 },
Cary Coutant's avatar
Cary Coutant committed
6795
  { { ".debug_line",	    ".zdebug_line",	NULL, NULL, 0, 0, 0 },
6796
    display_debug_lines,    &do_debug_lines,	1 },
Cary Coutant's avatar
Cary Coutant committed
6797
  { { ".debug_pubnames",    ".zdebug_pubnames",	NULL, NULL, 0, 0, 0 },
6798
    display_debug_pubnames, &do_debug_pubnames,	0 },
Cary Coutant's avatar
Cary Coutant committed
6799
  { { ".eh_frame",	    "",			NULL, NULL, 0, 0, 0 },
6800
    display_debug_frames,   &do_debug_frames,	1 },
Cary Coutant's avatar
Cary Coutant committed
6801
  { { ".debug_macinfo",	    ".zdebug_macinfo",	NULL, NULL, 0, 0, 0 },
6802
    display_debug_macinfo,  &do_debug_macinfo,	0 },
Cary Coutant's avatar
Cary Coutant committed
6803
  { { ".debug_macro",	    ".zdebug_macro",	NULL, NULL, 0, 0, 0 },
6804
    display_debug_macro,    &do_debug_macinfo,	1 },
Cary Coutant's avatar
Cary Coutant committed
6805
  { { ".debug_str",	    ".zdebug_str",	NULL, NULL, 0, 0, 0 },
6806
    display_debug_str,	    &do_debug_str,	0 },
Cary Coutant's avatar
Cary Coutant committed
6807
  { { ".debug_loc",	    ".zdebug_loc",	NULL, NULL, 0, 0, 0 },
6808
    display_debug_loc,	    &do_debug_loc,	1 },
Cary Coutant's avatar
Cary Coutant committed
6809
  { { ".debug_pubtypes",    ".zdebug_pubtypes",	NULL, NULL, 0, 0, 0 },
6810
    display_debug_pubnames, &do_debug_pubtypes,	0 },
Cary Coutant's avatar
Cary Coutant committed
6811
  { { ".debug_ranges",	    ".zdebug_ranges",	NULL, NULL, 0, 0, 0 },
6812
    display_debug_ranges,   &do_debug_ranges,	1 },
Cary Coutant's avatar
Cary Coutant committed
6813
  { { ".debug_static_func", ".zdebug_static_func", NULL, NULL, 0, 0, 0 },
6814
    display_debug_not_supported, NULL,		0 },
Cary Coutant's avatar
Cary Coutant committed
6815
  { { ".debug_static_vars", ".zdebug_static_vars", NULL, NULL, 0, 0, 0 },
6816 6817 6818
    display_debug_not_supported, NULL,		0 },
  { { ".debug_types",	    ".zdebug_types",	NULL, NULL, 0, 0, abbrev },
    display_debug_types,    &do_debug_info,	1 },
Cary Coutant's avatar
Cary Coutant committed
6819
  { { ".debug_weaknames",   ".zdebug_weaknames", NULL, NULL, 0, 0, 0 },
6820
    display_debug_not_supported, NULL,		0 },
Cary Coutant's avatar
Cary Coutant committed
6821 6822
  { { ".gdb_index",	    "",	                NULL, NULL, 0, 0, 0 },
    display_gdb_index,      &do_gdb_index,	0 },
6823
  { { ".trace_info",	    "",			NULL, NULL, 0, 0, trace_abbrev },
Cary Coutant's avatar
Cary Coutant committed
6824 6825 6826 6827 6828
    display_trace_info,	    &do_trace_info,	1 },
  { { ".trace_abbrev",	    "",			NULL, NULL, 0, 0, 0 },
    display_debug_abbrev,   &do_trace_abbrevs,	0 },
  { { ".trace_aranges",	    "",			NULL, NULL, 0, 0, 0 },
    display_debug_aranges,  &do_trace_aranges,	0 },
6829
  { { ".debug_info.dwo",    ".zdebug_info.dwo",	NULL, NULL, 0, 0, abbrev_dwo },
Cary Coutant's avatar
Cary Coutant committed
6830 6831 6832
    display_debug_info,	    &do_debug_info,	1 },
  { { ".debug_abbrev.dwo",  ".zdebug_abbrev.dwo", NULL, NULL, 0, 0, 0 },
    display_debug_abbrev,   &do_debug_abbrevs,	0 },
6833
  { { ".debug_types.dwo",   ".zdebug_types.dwo", NULL, NULL, 0, 0, abbrev_dwo },
Cary Coutant's avatar
Cary Coutant committed
6834 6835 6836 6837
    display_debug_types,    &do_debug_info,	1 },
  { { ".debug_line.dwo",    ".zdebug_line.dwo", NULL, NULL, 0, 0, 0 },
    display_debug_lines,    &do_debug_lines,	1 },
  { { ".debug_loc.dwo",	    ".zdebug_loc.dwo",	NULL, NULL, 0, 0, 0 },
6838
    display_debug_loc,	    &do_debug_loc,	1 },
Cary Coutant's avatar
Cary Coutant committed
6839
  { { ".debug_macro.dwo",   ".zdebug_macro.dwo", NULL, NULL, 0, 0, 0 },
6840
    display_debug_macro,    &do_debug_macinfo,	1 },
Cary Coutant's avatar
Cary Coutant committed
6841
  { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", NULL, NULL, 0, 0, 0 },
6842
    display_debug_macinfo,  &do_debug_macinfo,	0 },
Cary Coutant's avatar
Cary Coutant committed
6843 6844 6845
  { { ".debug_str.dwo",     ".zdebug_str.dwo",  NULL, NULL, 0, 0, 0 },
    display_debug_str,      &do_debug_str,	1 },
  { { ".debug_str_offsets", ".zdebug_str_offsets", NULL, NULL, 0, 0, 0 },
6846
    display_debug_str_offsets, NULL,		0 },
Cary Coutant's avatar
Cary Coutant committed
6847
  { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", NULL, NULL, 0, 0, 0 },
6848
    display_debug_str_offsets, NULL,		0 },
Cary Coutant's avatar
Cary Coutant committed
6849 6850 6851 6852 6853 6854
  { { ".debug_addr",	    ".zdebug_addr",     NULL, NULL, 0, 0, 0 },
    display_debug_addr,     &do_debug_addr,	1 },
  { { ".debug_cu_index",    "",			NULL, NULL, 0, 0, 0 },
    display_cu_index,       &do_debug_cu_index,	0 },
  { { ".debug_tu_index",    "",			NULL, NULL, 0, 0, 0 },
    display_cu_index,       &do_debug_cu_index,	0 },
6855
};