OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [binutils/] [dwarf.c] - Blame information for rev 861

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 205 julius
/* dwarf.c -- display DWARF contents of a BFD binary file
2
   Copyright 2005, 2006, 2007, 2008, 2009
3
   Free Software Foundation, Inc.
4
 
5
   This file is part of GNU Binutils.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
21
 
22
#include "sysdep.h"
23
#include "libiberty.h"
24
#include "bfd.h"
25
#include "bucomm.h"
26
#include "elf/common.h"
27
#include "dwarf2.h"
28
#include "dwarf.h"
29
 
30
static int have_frame_base;
31
static int need_base_address;
32
 
33
static unsigned int last_pointer_size = 0;
34
static int warned_about_missing_comp_units = FALSE;
35
 
36
static unsigned int num_debug_info_entries = 0;
37
static debug_info *debug_information = NULL;
38
/* Special value for num_debug_info_entries to indicate
39
   that the .debug_info section could not be loaded/parsed.  */
40
#define DEBUG_INFO_UNAVAILABLE  (unsigned int) -1
41
 
42
int eh_addr_size;
43
 
44
int do_debug_info;
45
int do_debug_abbrevs;
46
int do_debug_lines;
47
int do_debug_pubnames;
48
int do_debug_aranges;
49
int do_debug_ranges;
50
int do_debug_frames;
51
int do_debug_frames_interp;
52
int do_debug_macinfo;
53
int do_debug_str;
54
int do_debug_loc;
55
int do_wide;
56
 
57
/* Values for do_debug_lines.  */
58
#define FLAG_DEBUG_LINES_RAW     1
59
#define FLAG_DEBUG_LINES_DECODED 2
60
 
61
dwarf_vma (*byte_get) (unsigned char *, int);
62
 
63
dwarf_vma
64
byte_get_little_endian (unsigned char *field, int size)
65
{
66
  switch (size)
67
    {
68
    case 1:
69
      return *field;
70
 
71
    case 2:
72
      return  ((unsigned int) (field[0]))
73
        |    (((unsigned int) (field[1])) << 8);
74
 
75
    case 3:
76
      return  ((unsigned long) (field[0]))
77
        |    (((unsigned long) (field[1])) << 8)
78
        |    (((unsigned long) (field[2])) << 16);
79
 
80
    case 4:
81
      return  ((unsigned long) (field[0]))
82
        |    (((unsigned long) (field[1])) << 8)
83
        |    (((unsigned long) (field[2])) << 16)
84
        |    (((unsigned long) (field[3])) << 24);
85
 
86
    case 8:
87
      if (sizeof (dwarf_vma) == 8)
88
        return  ((dwarf_vma) (field[0]))
89
          |    (((dwarf_vma) (field[1])) << 8)
90
          |    (((dwarf_vma) (field[2])) << 16)
91
          |    (((dwarf_vma) (field[3])) << 24)
92
          |    (((dwarf_vma) (field[4])) << 32)
93
          |    (((dwarf_vma) (field[5])) << 40)
94
          |    (((dwarf_vma) (field[6])) << 48)
95
          |    (((dwarf_vma) (field[7])) << 56);
96
      else if (sizeof (dwarf_vma) == 4)
97
        /* We want to extract data from an 8 byte wide field and
98
           place it into a 4 byte wide field.  Since this is a little
99
           endian source we can just use the 4 byte extraction code.  */
100
        return  ((unsigned long) (field[0]))
101
          |    (((unsigned long) (field[1])) << 8)
102
          |    (((unsigned long) (field[2])) << 16)
103
          |    (((unsigned long) (field[3])) << 24);
104
 
105
    default:
106
      error (_("Unhandled data length: %d\n"), size);
107
      abort ();
108
    }
109
}
110
 
111
dwarf_vma
112
byte_get_big_endian (unsigned char *field, int size)
113
{
114
  switch (size)
115
    {
116
    case 1:
117
      return *field;
118
 
119
    case 2:
120
      return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
121
 
122
    case 3:
123
      return ((unsigned long) (field[2]))
124
        |   (((unsigned long) (field[1])) << 8)
125
        |   (((unsigned long) (field[0])) << 16);
126
 
127
    case 4:
128
      return ((unsigned long) (field[3]))
129
        |   (((unsigned long) (field[2])) << 8)
130
        |   (((unsigned long) (field[1])) << 16)
131
        |   (((unsigned long) (field[0])) << 24);
132
 
133
    case 8:
134
      if (sizeof (dwarf_vma) == 8)
135
        return ((dwarf_vma) (field[7]))
136
          |   (((dwarf_vma) (field[6])) << 8)
137
          |   (((dwarf_vma) (field[5])) << 16)
138
          |   (((dwarf_vma) (field[4])) << 24)
139
          |   (((dwarf_vma) (field[3])) << 32)
140
          |   (((dwarf_vma) (field[2])) << 40)
141
          |   (((dwarf_vma) (field[1])) << 48)
142
          |   (((dwarf_vma) (field[0])) << 56);
143
      else if (sizeof (dwarf_vma) == 4)
144
        {
145
          /* Although we are extracing data from an 8 byte wide field,
146
             we are returning only 4 bytes of data.  */
147
          field += 4;
148
          return ((unsigned long) (field[3]))
149
            |   (((unsigned long) (field[2])) << 8)
150
            |   (((unsigned long) (field[1])) << 16)
151
            |   (((unsigned long) (field[0])) << 24);
152
        }
153
 
154
    default:
155
      error (_("Unhandled data length: %d\n"), size);
156
      abort ();
157
    }
158
}
159
 
160
static dwarf_vma
161
byte_get_signed (unsigned char *field, int size)
162
{
163
  dwarf_vma x = byte_get (field, size);
164
 
165
  switch (size)
166
    {
167
    case 1:
168
      return (x ^ 0x80) - 0x80;
169
    case 2:
170
      return (x ^ 0x8000) - 0x8000;
171
    case 4:
172
      return (x ^ 0x80000000) - 0x80000000;
173
    case 8:
174
      return x;
175
    default:
176
      abort ();
177
    }
178
}
179
 
180
static int
181
size_of_encoded_value (int encoding)
182
{
183
  switch (encoding & 0x7)
184
    {
185
    default:    /* ??? */
186
    case 0:      return eh_addr_size;
187
    case 2:     return 2;
188
    case 3:     return 4;
189
    case 4:     return 8;
190
    }
191
}
192
 
193
static dwarf_vma
194
get_encoded_value (unsigned char *data, int encoding)
195
{
196
  int size = size_of_encoded_value (encoding);
197
 
198
  if (encoding & DW_EH_PE_signed)
199
    return byte_get_signed (data, size);
200
  else
201
    return byte_get (data, size);
202
}
203
 
204
/* Print a dwarf_vma value (typically an address, offset or length) in
205
   hexadecimal format, followed by a space.  The length of the value (and
206
   hence the precision displayed) is determined by the byte_size parameter.  */
207
 
208
static void
209
print_dwarf_vma (dwarf_vma val, unsigned byte_size)
210
{
211
  static char buff[18];
212
 
213
  /* Printf does not have a way of specifiying a maximum field width for an
214
     integer value, so we print the full value into a buffer and then select
215
     the precision we need.  */
216
#if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
217
#ifndef __MSVCRT__
218
  snprintf (buff, sizeof (buff), "%16.16llx ", val);
219
#else
220
  snprintf (buff, sizeof (buff), "%016I64x ", val);
221
#endif
222
#else
223
  snprintf (buff, sizeof (buff), "%16.16lx ", val);
224
#endif
225
 
226
  fputs (buff + (byte_size == 4 ? 8 : 0), stdout);
227
}
228
 
229
static unsigned long int
230
read_leb128 (unsigned char *data, unsigned int *length_return, int sign)
231
{
232
  unsigned long int result = 0;
233
  unsigned int num_read = 0;
234
  unsigned int shift = 0;
235
  unsigned char byte;
236
 
237
  do
238
    {
239
      byte = *data++;
240
      num_read++;
241
 
242
      result |= ((unsigned long int) (byte & 0x7f)) << shift;
243
 
244
      shift += 7;
245
 
246
    }
247
  while (byte & 0x80);
248
 
249
  if (length_return != NULL)
250
    *length_return = num_read;
251
 
252
  if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
253
    result |= -1L << shift;
254
 
255
  return result;
256
}
257
 
258
typedef struct State_Machine_Registers
259
{
260
  unsigned long address;
261
  unsigned int file;
262
  unsigned int line;
263
  unsigned int column;
264
  int is_stmt;
265
  int basic_block;
266
  int end_sequence;
267
/* This variable hold the number of the last entry seen
268
   in the File Table.  */
269
  unsigned int last_file_entry;
270
} SMR;
271
 
272
static SMR state_machine_regs;
273
 
274
static void
275
reset_state_machine (int is_stmt)
276
{
277
  state_machine_regs.address = 0;
278
  state_machine_regs.file = 1;
279
  state_machine_regs.line = 1;
280
  state_machine_regs.column = 0;
281
  state_machine_regs.is_stmt = is_stmt;
282
  state_machine_regs.basic_block = 0;
283
  state_machine_regs.end_sequence = 0;
284
  state_machine_regs.last_file_entry = 0;
285
}
286
 
287
/* Handled an extend line op.
288
   Returns the number of bytes read.  */
289
 
290
static int
291
process_extended_line_op (unsigned char *data, int is_stmt)
292
{
293
  unsigned char op_code;
294
  unsigned int bytes_read;
295
  unsigned int len;
296
  unsigned char *name;
297
  unsigned long adr;
298
 
299
  len = read_leb128 (data, & bytes_read, 0);
300
  data += bytes_read;
301
 
302
  if (len == 0)
303
    {
304
      warn (_("badly formed extended line op encountered!\n"));
305
      return bytes_read;
306
    }
307
 
308
  len += bytes_read;
309
  op_code = *data++;
310
 
311
  printf (_("  Extended opcode %d: "), op_code);
312
 
313
  switch (op_code)
314
    {
315
    case DW_LNE_end_sequence:
316
      printf (_("End of Sequence\n\n"));
317
      reset_state_machine (is_stmt);
318
      break;
319
 
320
    case DW_LNE_set_address:
321
      adr = byte_get (data, len - bytes_read - 1);
322
      printf (_("set Address to 0x%lx\n"), adr);
323
      state_machine_regs.address = adr;
324
      break;
325
 
326
    case DW_LNE_define_file:
327
      printf (_("  define new File Table entry\n"));
328
      printf (_("  Entry\tDir\tTime\tSize\tName\n"));
329
 
330
      printf (_("   %d\t"), ++state_machine_regs.last_file_entry);
331
      name = data;
332
      data += strlen ((char *) data) + 1;
333
      printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
334
      data += bytes_read;
335
      printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
336
      data += bytes_read;
337
      printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
338
      printf (_("%s\n\n"), name);
339
      break;
340
 
341
    case DW_LNE_set_discriminator:
342
      printf (_("set Discriminator to %lu\n"),
343
              read_leb128 (data, & bytes_read, 0));
344
      break;
345
 
346
    /* HP extensions.  */
347
    case DW_LNE_HP_negate_is_UV_update:
348
      printf ("DW_LNE_HP_negate_is_UV_update\n");
349
      break;
350
    case DW_LNE_HP_push_context:
351
      printf ("DW_LNE_HP_push_context\n");
352
      break;
353
    case DW_LNE_HP_pop_context:
354
      printf ("DW_LNE_HP_pop_context\n");
355
      break;
356
    case DW_LNE_HP_set_file_line_column:
357
      printf ("DW_LNE_HP_set_file_line_column\n");
358
      break;
359
    case DW_LNE_HP_set_routine_name:
360
      printf ("DW_LNE_HP_set_routine_name\n");
361
      break;
362
    case DW_LNE_HP_set_sequence:
363
      printf ("DW_LNE_HP_set_sequence\n");
364
      break;
365
    case DW_LNE_HP_negate_post_semantics:
366
      printf ("DW_LNE_HP_negate_post_semantics\n");
367
      break;
368
    case DW_LNE_HP_negate_function_exit:
369
      printf ("DW_LNE_HP_negate_function_exit\n");
370
      break;
371
    case DW_LNE_HP_negate_front_end_logical:
372
      printf ("DW_LNE_HP_negate_front_end_logical\n");
373
      break;
374
    case DW_LNE_HP_define_proc:
375
      printf ("DW_LNE_HP_define_proc\n");
376
      break;
377
 
378
    default:
379
      if (op_code >= DW_LNE_lo_user
380
          /* The test against DW_LNW_hi_user is redundant due to
381
             the limited range of the unsigned char data type used
382
             for op_code.  */
383
          /*&& op_code <= DW_LNE_hi_user*/)
384
        printf (_("user defined: length %d\n"), len - bytes_read);
385
      else
386
        printf (_("UNKNOWN: length %d\n"), len - bytes_read);
387
      break;
388
    }
389
 
390
  return len;
391
}
392
 
393
static const char *
394
fetch_indirect_string (unsigned long offset)
395
{
396
  struct dwarf_section *section = &debug_displays [str].section;
397
 
398
  if (section->start == NULL)
399
    return _("<no .debug_str section>");
400
 
401
  /* DWARF sections under Mach-O have non-zero addresses.  */
402
  offset -= section->address;
403
  if (offset > section->size)
404
    {
405
      warn (_("DW_FORM_strp offset too big: %lx\n"), offset);
406
      return _("<offset is too big>");
407
    }
408
 
409
  return (const char *) section->start + offset;
410
}
411
 
412
/* FIXME:  There are better and more efficient ways to handle
413
   these structures.  For now though, I just want something that
414
   is simple to implement.  */
415
typedef struct abbrev_attr
416
{
417
  unsigned long attribute;
418
  unsigned long form;
419
  struct abbrev_attr *next;
420
}
421
abbrev_attr;
422
 
423
typedef struct abbrev_entry
424
{
425
  unsigned long entry;
426
  unsigned long tag;
427
  int children;
428
  struct abbrev_attr *first_attr;
429
  struct abbrev_attr *last_attr;
430
  struct abbrev_entry *next;
431
}
432
abbrev_entry;
433
 
434
static abbrev_entry *first_abbrev = NULL;
435
static abbrev_entry *last_abbrev = NULL;
436
 
437
static void
438
free_abbrevs (void)
439
{
440
  abbrev_entry *abbrev;
441
 
442
  for (abbrev = first_abbrev; abbrev;)
443
    {
444
      abbrev_entry *next = abbrev->next;
445
      abbrev_attr *attr;
446
 
447
      for (attr = abbrev->first_attr; attr;)
448
        {
449
          abbrev_attr *next = attr->next;
450
 
451
          free (attr);
452
          attr = next;
453
        }
454
 
455
      free (abbrev);
456
      abbrev = next;
457
    }
458
 
459
  last_abbrev = first_abbrev = NULL;
460
}
461
 
462
static void
463
add_abbrev (unsigned long number, unsigned long tag, int children)
464
{
465
  abbrev_entry *entry;
466
 
467
  entry = (abbrev_entry *) malloc (sizeof (*entry));
468
 
469
  if (entry == NULL)
470
    /* ugg */
471
    return;
472
 
473
  entry->entry      = number;
474
  entry->tag        = tag;
475
  entry->children   = children;
476
  entry->first_attr = NULL;
477
  entry->last_attr  = NULL;
478
  entry->next       = NULL;
479
 
480
  if (first_abbrev == NULL)
481
    first_abbrev = entry;
482
  else
483
    last_abbrev->next = entry;
484
 
485
  last_abbrev = entry;
486
}
487
 
488
static void
489
add_abbrev_attr (unsigned long attribute, unsigned long form)
490
{
491
  abbrev_attr *attr;
492
 
493
  attr = (abbrev_attr *) malloc (sizeof (*attr));
494
 
495
  if (attr == NULL)
496
    /* ugg */
497
    return;
498
 
499
  attr->attribute = attribute;
500
  attr->form      = form;
501
  attr->next      = NULL;
502
 
503
  if (last_abbrev->first_attr == NULL)
504
    last_abbrev->first_attr = attr;
505
  else
506
    last_abbrev->last_attr->next = attr;
507
 
508
  last_abbrev->last_attr = attr;
509
}
510
 
511
/* Processes the (partial) contents of a .debug_abbrev section.
512
   Returns NULL if the end of the section was encountered.
513
   Returns the address after the last byte read if the end of
514
   an abbreviation set was found.  */
515
 
516
static unsigned char *
517
process_abbrev_section (unsigned char *start, unsigned char *end)
518
{
519
  if (first_abbrev != NULL)
520
    return NULL;
521
 
522
  while (start < end)
523
    {
524
      unsigned int bytes_read;
525
      unsigned long entry;
526
      unsigned long tag;
527
      unsigned long attribute;
528
      int children;
529
 
530
      entry = read_leb128 (start, & bytes_read, 0);
531
      start += bytes_read;
532
 
533
      /* A single zero is supposed to end the section according
534
         to the standard.  If there's more, then signal that to
535
         the caller.  */
536
      if (entry == 0)
537
        return start == end ? NULL : start;
538
 
539
      tag = read_leb128 (start, & bytes_read, 0);
540
      start += bytes_read;
541
 
542
      children = *start++;
543
 
544
      add_abbrev (entry, tag, children);
545
 
546
      do
547
        {
548
          unsigned long form;
549
 
550
          attribute = read_leb128 (start, & bytes_read, 0);
551
          start += bytes_read;
552
 
553
          form = read_leb128 (start, & bytes_read, 0);
554
          start += bytes_read;
555
 
556
          if (attribute != 0)
557
            add_abbrev_attr (attribute, form);
558
        }
559
      while (attribute != 0);
560
    }
561
 
562
  return NULL;
563
}
564
 
565
static char *
566
get_TAG_name (unsigned long tag)
567
{
568
  switch (tag)
569
    {
570
    case DW_TAG_padding:                return "DW_TAG_padding";
571
    case DW_TAG_array_type:             return "DW_TAG_array_type";
572
    case DW_TAG_class_type:             return "DW_TAG_class_type";
573
    case DW_TAG_entry_point:            return "DW_TAG_entry_point";
574
    case DW_TAG_enumeration_type:       return "DW_TAG_enumeration_type";
575
    case DW_TAG_formal_parameter:       return "DW_TAG_formal_parameter";
576
    case DW_TAG_imported_declaration:   return "DW_TAG_imported_declaration";
577
    case DW_TAG_label:                  return "DW_TAG_label";
578
    case DW_TAG_lexical_block:          return "DW_TAG_lexical_block";
579
    case DW_TAG_member:                 return "DW_TAG_member";
580
    case DW_TAG_pointer_type:           return "DW_TAG_pointer_type";
581
    case DW_TAG_reference_type:         return "DW_TAG_reference_type";
582
    case DW_TAG_compile_unit:           return "DW_TAG_compile_unit";
583
    case DW_TAG_string_type:            return "DW_TAG_string_type";
584
    case DW_TAG_structure_type:         return "DW_TAG_structure_type";
585
    case DW_TAG_subroutine_type:        return "DW_TAG_subroutine_type";
586
    case DW_TAG_typedef:                return "DW_TAG_typedef";
587
    case DW_TAG_union_type:             return "DW_TAG_union_type";
588
    case DW_TAG_unspecified_parameters: return "DW_TAG_unspecified_parameters";
589
    case DW_TAG_variant:                return "DW_TAG_variant";
590
    case DW_TAG_common_block:           return "DW_TAG_common_block";
591
    case DW_TAG_common_inclusion:       return "DW_TAG_common_inclusion";
592
    case DW_TAG_inheritance:            return "DW_TAG_inheritance";
593
    case DW_TAG_inlined_subroutine:     return "DW_TAG_inlined_subroutine";
594
    case DW_TAG_module:                 return "DW_TAG_module";
595
    case DW_TAG_ptr_to_member_type:     return "DW_TAG_ptr_to_member_type";
596
    case DW_TAG_set_type:               return "DW_TAG_set_type";
597
    case DW_TAG_subrange_type:          return "DW_TAG_subrange_type";
598
    case DW_TAG_with_stmt:              return "DW_TAG_with_stmt";
599
    case DW_TAG_access_declaration:     return "DW_TAG_access_declaration";
600
    case DW_TAG_base_type:              return "DW_TAG_base_type";
601
    case DW_TAG_catch_block:            return "DW_TAG_catch_block";
602
    case DW_TAG_const_type:             return "DW_TAG_const_type";
603
    case DW_TAG_constant:               return "DW_TAG_constant";
604
    case DW_TAG_enumerator:             return "DW_TAG_enumerator";
605
    case DW_TAG_file_type:              return "DW_TAG_file_type";
606
    case DW_TAG_friend:                 return "DW_TAG_friend";
607
    case DW_TAG_namelist:               return "DW_TAG_namelist";
608
    case DW_TAG_namelist_item:          return "DW_TAG_namelist_item";
609
    case DW_TAG_packed_type:            return "DW_TAG_packed_type";
610
    case DW_TAG_subprogram:             return "DW_TAG_subprogram";
611
    case DW_TAG_template_type_param:    return "DW_TAG_template_type_param";
612
    case DW_TAG_template_value_param:   return "DW_TAG_template_value_param";
613
    case DW_TAG_thrown_type:            return "DW_TAG_thrown_type";
614
    case DW_TAG_try_block:              return "DW_TAG_try_block";
615
    case DW_TAG_variant_part:           return "DW_TAG_variant_part";
616
    case DW_TAG_variable:               return "DW_TAG_variable";
617
    case DW_TAG_volatile_type:          return "DW_TAG_volatile_type";
618
    case DW_TAG_MIPS_loop:              return "DW_TAG_MIPS_loop";
619
    case DW_TAG_format_label:           return "DW_TAG_format_label";
620
    case DW_TAG_function_template:      return "DW_TAG_function_template";
621
    case DW_TAG_class_template:         return "DW_TAG_class_template";
622
      /* DWARF 2.1 values.  */
623
    case DW_TAG_dwarf_procedure:        return "DW_TAG_dwarf_procedure";
624
    case DW_TAG_restrict_type:          return "DW_TAG_restrict_type";
625
    case DW_TAG_interface_type:         return "DW_TAG_interface_type";
626
    case DW_TAG_namespace:              return "DW_TAG_namespace";
627
    case DW_TAG_imported_module:        return "DW_TAG_imported_module";
628
    case DW_TAG_unspecified_type:       return "DW_TAG_unspecified_type";
629
    case DW_TAG_partial_unit:           return "DW_TAG_partial_unit";
630
    case DW_TAG_imported_unit:          return "DW_TAG_imported_unit";
631
      /* UPC values.  */
632
    case DW_TAG_upc_shared_type:        return "DW_TAG_upc_shared_type";
633
    case DW_TAG_upc_strict_type:        return "DW_TAG_upc_strict_type";
634
    case DW_TAG_upc_relaxed_type:       return "DW_TAG_upc_relaxed_type";
635
    default:
636
      {
637
        static char buffer[100];
638
 
639
        snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
640
        return buffer;
641
      }
642
    }
643
}
644
 
645
static char *
646
get_FORM_name (unsigned long form)
647
{
648
  switch (form)
649
    {
650
    case DW_FORM_addr:          return "DW_FORM_addr";
651
    case DW_FORM_block2:        return "DW_FORM_block2";
652
    case DW_FORM_block4:        return "DW_FORM_block4";
653
    case DW_FORM_data2:         return "DW_FORM_data2";
654
    case DW_FORM_data4:         return "DW_FORM_data4";
655
    case DW_FORM_data8:         return "DW_FORM_data8";
656
    case DW_FORM_string:        return "DW_FORM_string";
657
    case DW_FORM_block:         return "DW_FORM_block";
658
    case DW_FORM_block1:        return "DW_FORM_block1";
659
    case DW_FORM_data1:         return "DW_FORM_data1";
660
    case DW_FORM_flag:          return "DW_FORM_flag";
661
    case DW_FORM_sdata:         return "DW_FORM_sdata";
662
    case DW_FORM_strp:          return "DW_FORM_strp";
663
    case DW_FORM_udata:         return "DW_FORM_udata";
664
    case DW_FORM_ref_addr:      return "DW_FORM_ref_addr";
665
    case DW_FORM_ref1:          return "DW_FORM_ref1";
666
    case DW_FORM_ref2:          return "DW_FORM_ref2";
667
    case DW_FORM_ref4:          return "DW_FORM_ref4";
668
    case DW_FORM_ref8:          return "DW_FORM_ref8";
669
    case DW_FORM_ref_udata:     return "DW_FORM_ref_udata";
670
    case DW_FORM_indirect:      return "DW_FORM_indirect";
671
    default:
672
      {
673
        static char buffer[100];
674
 
675
        snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
676
        return buffer;
677
      }
678
    }
679
}
680
 
681
static unsigned char *
682
display_block (unsigned char *data, unsigned long length)
683
{
684
  printf (_(" %lu byte block: "), length);
685
 
686
  while (length --)
687
    printf ("%lx ", (unsigned long) byte_get (data++, 1));
688
 
689
  return data;
690
}
691
 
692
static int
693
decode_location_expression (unsigned char * data,
694
                            unsigned int pointer_size,
695
                            unsigned long length,
696
                            unsigned long cu_offset,
697
                            struct dwarf_section * section)
698
{
699
  unsigned op;
700
  unsigned int bytes_read;
701
  unsigned long uvalue;
702
  unsigned char *end = data + length;
703
  int need_frame_base = 0;
704
 
705
  while (data < end)
706
    {
707
      op = *data++;
708
 
709
      switch (op)
710
        {
711
        case DW_OP_addr:
712
          printf ("DW_OP_addr: %lx",
713
                  (unsigned long) byte_get (data, pointer_size));
714
          data += pointer_size;
715
          break;
716
        case DW_OP_deref:
717
          printf ("DW_OP_deref");
718
          break;
719
        case DW_OP_const1u:
720
          printf ("DW_OP_const1u: %lu", (unsigned long) byte_get (data++, 1));
721
          break;
722
        case DW_OP_const1s:
723
          printf ("DW_OP_const1s: %ld", (long) byte_get_signed (data++, 1));
724
          break;
725
        case DW_OP_const2u:
726
          printf ("DW_OP_const2u: %lu", (unsigned long) byte_get (data, 2));
727
          data += 2;
728
          break;
729
        case DW_OP_const2s:
730
          printf ("DW_OP_const2s: %ld", (long) byte_get_signed (data, 2));
731
          data += 2;
732
          break;
733
        case DW_OP_const4u:
734
          printf ("DW_OP_const4u: %lu", (unsigned long) byte_get (data, 4));
735
          data += 4;
736
          break;
737
        case DW_OP_const4s:
738
          printf ("DW_OP_const4s: %ld", (long) byte_get_signed (data, 4));
739
          data += 4;
740
          break;
741
        case DW_OP_const8u:
742
          printf ("DW_OP_const8u: %lu %lu", (unsigned long) byte_get (data, 4),
743
                  (unsigned long) byte_get (data + 4, 4));
744
          data += 8;
745
          break;
746
        case DW_OP_const8s:
747
          printf ("DW_OP_const8s: %ld %ld", (long) byte_get (data, 4),
748
                  (long) byte_get (data + 4, 4));
749
          data += 8;
750
          break;
751
        case DW_OP_constu:
752
          printf ("DW_OP_constu: %lu", read_leb128 (data, &bytes_read, 0));
753
          data += bytes_read;
754
          break;
755
        case DW_OP_consts:
756
          printf ("DW_OP_consts: %ld", read_leb128 (data, &bytes_read, 1));
757
          data += bytes_read;
758
          break;
759
        case DW_OP_dup:
760
          printf ("DW_OP_dup");
761
          break;
762
        case DW_OP_drop:
763
          printf ("DW_OP_drop");
764
          break;
765
        case DW_OP_over:
766
          printf ("DW_OP_over");
767
          break;
768
        case DW_OP_pick:
769
          printf ("DW_OP_pick: %ld", (unsigned long) byte_get (data++, 1));
770
          break;
771
        case DW_OP_swap:
772
          printf ("DW_OP_swap");
773
          break;
774
        case DW_OP_rot:
775
          printf ("DW_OP_rot");
776
          break;
777
        case DW_OP_xderef:
778
          printf ("DW_OP_xderef");
779
          break;
780
        case DW_OP_abs:
781
          printf ("DW_OP_abs");
782
          break;
783
        case DW_OP_and:
784
          printf ("DW_OP_and");
785
          break;
786
        case DW_OP_div:
787
          printf ("DW_OP_div");
788
          break;
789
        case DW_OP_minus:
790
          printf ("DW_OP_minus");
791
          break;
792
        case DW_OP_mod:
793
          printf ("DW_OP_mod");
794
          break;
795
        case DW_OP_mul:
796
          printf ("DW_OP_mul");
797
          break;
798
        case DW_OP_neg:
799
          printf ("DW_OP_neg");
800
          break;
801
        case DW_OP_not:
802
          printf ("DW_OP_not");
803
          break;
804
        case DW_OP_or:
805
          printf ("DW_OP_or");
806
          break;
807
        case DW_OP_plus:
808
          printf ("DW_OP_plus");
809
          break;
810
        case DW_OP_plus_uconst:
811
          printf ("DW_OP_plus_uconst: %lu",
812
                  read_leb128 (data, &bytes_read, 0));
813
          data += bytes_read;
814
          break;
815
        case DW_OP_shl:
816
          printf ("DW_OP_shl");
817
          break;
818
        case DW_OP_shr:
819
          printf ("DW_OP_shr");
820
          break;
821
        case DW_OP_shra:
822
          printf ("DW_OP_shra");
823
          break;
824
        case DW_OP_xor:
825
          printf ("DW_OP_xor");
826
          break;
827
        case DW_OP_bra:
828
          printf ("DW_OP_bra: %ld", (long) byte_get_signed (data, 2));
829
          data += 2;
830
          break;
831
        case DW_OP_eq:
832
          printf ("DW_OP_eq");
833
          break;
834
        case DW_OP_ge:
835
          printf ("DW_OP_ge");
836
          break;
837
        case DW_OP_gt:
838
          printf ("DW_OP_gt");
839
          break;
840
        case DW_OP_le:
841
          printf ("DW_OP_le");
842
          break;
843
        case DW_OP_lt:
844
          printf ("DW_OP_lt");
845
          break;
846
        case DW_OP_ne:
847
          printf ("DW_OP_ne");
848
          break;
849
        case DW_OP_skip:
850
          printf ("DW_OP_skip: %ld", (long) byte_get_signed (data, 2));
851
          data += 2;
852
          break;
853
 
854
        case DW_OP_lit0:
855
        case DW_OP_lit1:
856
        case DW_OP_lit2:
857
        case DW_OP_lit3:
858
        case DW_OP_lit4:
859
        case DW_OP_lit5:
860
        case DW_OP_lit6:
861
        case DW_OP_lit7:
862
        case DW_OP_lit8:
863
        case DW_OP_lit9:
864
        case DW_OP_lit10:
865
        case DW_OP_lit11:
866
        case DW_OP_lit12:
867
        case DW_OP_lit13:
868
        case DW_OP_lit14:
869
        case DW_OP_lit15:
870
        case DW_OP_lit16:
871
        case DW_OP_lit17:
872
        case DW_OP_lit18:
873
        case DW_OP_lit19:
874
        case DW_OP_lit20:
875
        case DW_OP_lit21:
876
        case DW_OP_lit22:
877
        case DW_OP_lit23:
878
        case DW_OP_lit24:
879
        case DW_OP_lit25:
880
        case DW_OP_lit26:
881
        case DW_OP_lit27:
882
        case DW_OP_lit28:
883
        case DW_OP_lit29:
884
        case DW_OP_lit30:
885
        case DW_OP_lit31:
886
          printf ("DW_OP_lit%d", op - DW_OP_lit0);
887
          break;
888
 
889
        case DW_OP_reg0:
890
        case DW_OP_reg1:
891
        case DW_OP_reg2:
892
        case DW_OP_reg3:
893
        case DW_OP_reg4:
894
        case DW_OP_reg5:
895
        case DW_OP_reg6:
896
        case DW_OP_reg7:
897
        case DW_OP_reg8:
898
        case DW_OP_reg9:
899
        case DW_OP_reg10:
900
        case DW_OP_reg11:
901
        case DW_OP_reg12:
902
        case DW_OP_reg13:
903
        case DW_OP_reg14:
904
        case DW_OP_reg15:
905
        case DW_OP_reg16:
906
        case DW_OP_reg17:
907
        case DW_OP_reg18:
908
        case DW_OP_reg19:
909
        case DW_OP_reg20:
910
        case DW_OP_reg21:
911
        case DW_OP_reg22:
912
        case DW_OP_reg23:
913
        case DW_OP_reg24:
914
        case DW_OP_reg25:
915
        case DW_OP_reg26:
916
        case DW_OP_reg27:
917
        case DW_OP_reg28:
918
        case DW_OP_reg29:
919
        case DW_OP_reg30:
920
        case DW_OP_reg31:
921
          printf ("DW_OP_reg%d", op - DW_OP_reg0);
922
          break;
923
 
924
        case DW_OP_breg0:
925
        case DW_OP_breg1:
926
        case DW_OP_breg2:
927
        case DW_OP_breg3:
928
        case DW_OP_breg4:
929
        case DW_OP_breg5:
930
        case DW_OP_breg6:
931
        case DW_OP_breg7:
932
        case DW_OP_breg8:
933
        case DW_OP_breg9:
934
        case DW_OP_breg10:
935
        case DW_OP_breg11:
936
        case DW_OP_breg12:
937
        case DW_OP_breg13:
938
        case DW_OP_breg14:
939
        case DW_OP_breg15:
940
        case DW_OP_breg16:
941
        case DW_OP_breg17:
942
        case DW_OP_breg18:
943
        case DW_OP_breg19:
944
        case DW_OP_breg20:
945
        case DW_OP_breg21:
946
        case DW_OP_breg22:
947
        case DW_OP_breg23:
948
        case DW_OP_breg24:
949
        case DW_OP_breg25:
950
        case DW_OP_breg26:
951
        case DW_OP_breg27:
952
        case DW_OP_breg28:
953
        case DW_OP_breg29:
954
        case DW_OP_breg30:
955
        case DW_OP_breg31:
956
          printf ("DW_OP_breg%d: %ld", op - DW_OP_breg0,
957
                  read_leb128 (data, &bytes_read, 1));
958
          data += bytes_read;
959
          break;
960
 
961
        case DW_OP_regx:
962
          printf ("DW_OP_regx: %lu", read_leb128 (data, &bytes_read, 0));
963
          data += bytes_read;
964
          break;
965
        case DW_OP_fbreg:
966
          need_frame_base = 1;
967
          printf ("DW_OP_fbreg: %ld", read_leb128 (data, &bytes_read, 1));
968
          data += bytes_read;
969
          break;
970
        case DW_OP_bregx:
971
          uvalue = read_leb128 (data, &bytes_read, 0);
972
          data += bytes_read;
973
          printf ("DW_OP_bregx: %lu %ld", uvalue,
974
                  read_leb128 (data, &bytes_read, 1));
975
          data += bytes_read;
976
          break;
977
        case DW_OP_piece:
978
          printf ("DW_OP_piece: %lu", read_leb128 (data, &bytes_read, 0));
979
          data += bytes_read;
980
          break;
981
        case DW_OP_deref_size:
982
          printf ("DW_OP_deref_size: %ld", (long) byte_get (data++, 1));
983
          break;
984
        case DW_OP_xderef_size:
985
          printf ("DW_OP_xderef_size: %ld", (long) byte_get (data++, 1));
986
          break;
987
        case DW_OP_nop:
988
          printf ("DW_OP_nop");
989
          break;
990
 
991
          /* DWARF 3 extensions.  */
992
        case DW_OP_push_object_address:
993
          printf ("DW_OP_push_object_address");
994
          break;
995
        case DW_OP_call2:
996
          /* XXX: Strictly speaking for 64-bit DWARF3 files
997
             this ought to be an 8-byte wide computation.  */
998
          printf ("DW_OP_call2: <%lx>", (long) byte_get (data, 2) + cu_offset);
999
          data += 2;
1000
          break;
1001
        case DW_OP_call4:
1002
          /* XXX: Strictly speaking for 64-bit DWARF3 files
1003
             this ought to be an 8-byte wide computation.  */
1004
          printf ("DW_OP_call4: <%lx>", (long) byte_get (data, 4) + cu_offset);
1005
          data += 4;
1006
          break;
1007
        case DW_OP_call_ref:
1008
          /* XXX: Strictly speaking for 64-bit DWARF3 files
1009
             this ought to be an 8-byte wide computation.  */
1010
          printf ("DW_OP_call_ref: <%lx>", (long) byte_get (data, 4) + cu_offset);
1011
          data += 4;
1012
          break;
1013
        case DW_OP_form_tls_address:
1014
          printf ("DW_OP_form_tls_address");
1015
          break;
1016
        case DW_OP_call_frame_cfa:
1017
          printf ("DW_OP_call_frame_cfa");
1018
          break;
1019
        case DW_OP_bit_piece:
1020
          printf ("DW_OP_bit_piece: ");
1021
          printf ("size: %lu ", read_leb128 (data, &bytes_read, 0));
1022
          data += bytes_read;
1023
          printf ("offset: %lu ", read_leb128 (data, &bytes_read, 0));
1024
          data += bytes_read;
1025
          break;
1026
 
1027
          /* DWARF 4 extensions.  */
1028
        case DW_OP_stack_value:
1029
          printf ("DW_OP_stack_value");
1030
          break;
1031
 
1032
        case DW_OP_implicit_value:
1033
          printf ("DW_OP_implicit_value");
1034
          uvalue = read_leb128 (data, &bytes_read, 0);
1035
          data += bytes_read;
1036
          display_block (data, uvalue);
1037
          data += uvalue;
1038
          break;
1039
 
1040
          /* GNU extensions.  */
1041
        case DW_OP_GNU_push_tls_address:
1042
          printf ("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown");
1043
          break;
1044
        case DW_OP_GNU_uninit:
1045
          printf ("DW_OP_GNU_uninit");
1046
          /* FIXME: Is there data associated with this OP ?  */
1047
          break;
1048
        case DW_OP_GNU_encoded_addr:
1049
          {
1050
            int encoding;
1051
            dwarf_vma addr;
1052
 
1053
            encoding = *data++;
1054
            addr = get_encoded_value (data, encoding);
1055
            if ((encoding & 0x70) == DW_EH_PE_pcrel)
1056
              addr += section->address + (data - section->start);
1057
            data += size_of_encoded_value (encoding);
1058
 
1059
            printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1060
            print_dwarf_vma (addr, pointer_size);
1061
          }
1062
          break;
1063
 
1064
          /* HP extensions.  */
1065
        case DW_OP_HP_is_value:
1066
          printf ("DW_OP_HP_is_value");
1067
          /* FIXME: Is there data associated with this OP ?  */
1068
          break;
1069
        case DW_OP_HP_fltconst4:
1070
          printf ("DW_OP_HP_fltconst4");
1071
          /* FIXME: Is there data associated with this OP ?  */
1072
          break;
1073
        case DW_OP_HP_fltconst8:
1074
          printf ("DW_OP_HP_fltconst8");
1075
          /* FIXME: Is there data associated with this OP ?  */
1076
          break;
1077
        case DW_OP_HP_mod_range:
1078
          printf ("DW_OP_HP_mod_range");
1079
          /* FIXME: Is there data associated with this OP ?  */
1080
          break;
1081
        case DW_OP_HP_unmod_range:
1082
          printf ("DW_OP_HP_unmod_range");
1083
          /* FIXME: Is there data associated with this OP ?  */
1084
          break;
1085
        case DW_OP_HP_tls:
1086
          printf ("DW_OP_HP_tls");
1087
          /* FIXME: Is there data associated with this OP ?  */
1088
          break;
1089
 
1090
          /* PGI (STMicroelectronics) extensions.  */
1091
        case DW_OP_PGI_omp_thread_num:
1092
          /* Pushes the thread number for the current thread as it would be
1093
             returned by the standard OpenMP library function:
1094
             omp_get_thread_num().  The "current thread" is the thread for
1095
             which the expression is being evaluated.  */
1096
          printf ("DW_OP_PGI_omp_thread_num");
1097
          break;
1098
 
1099
        default:
1100
          if (op >= DW_OP_lo_user
1101
              && op <= DW_OP_hi_user)
1102
            printf (_("(User defined location op)"));
1103
          else
1104
            printf (_("(Unknown location op)"));
1105
          /* No way to tell where the next op is, so just bail.  */
1106
          return need_frame_base;
1107
        }
1108
 
1109
      /* Separate the ops.  */
1110
      if (data < end)
1111
        printf ("; ");
1112
    }
1113
 
1114
  return need_frame_base;
1115
}
1116
 
1117
static unsigned char *
1118
read_and_display_attr_value (unsigned long attribute,
1119
                             unsigned long form,
1120
                             unsigned char * data,
1121
                             unsigned long cu_offset,
1122
                             unsigned long pointer_size,
1123
                             unsigned long offset_size,
1124
                             int dwarf_version,
1125
                             debug_info * debug_info_p,
1126
                             int do_loc,
1127
                             struct dwarf_section * section)
1128
{
1129
  unsigned long uvalue = 0;
1130
  unsigned char *block_start = NULL;
1131
  unsigned char * orig_data = data;
1132
  unsigned int bytes_read;
1133
 
1134
  switch (form)
1135
    {
1136
    default:
1137
      break;
1138
 
1139
    case DW_FORM_ref_addr:
1140
      if (dwarf_version == 2)
1141
        {
1142
          uvalue = byte_get (data, pointer_size);
1143
          data += pointer_size;
1144
        }
1145
      else if (dwarf_version == 3)
1146
        {
1147
          uvalue = byte_get (data, offset_size);
1148
          data += offset_size;
1149
        }
1150
      else
1151
        {
1152
          error (_("Internal error: DWARF version is not 2 or 3.\n"));
1153
        }
1154
      break;
1155
 
1156
    case DW_FORM_addr:
1157
      uvalue = byte_get (data, pointer_size);
1158
      data += pointer_size;
1159
      break;
1160
 
1161
    case DW_FORM_strp:
1162
      uvalue = byte_get (data, offset_size);
1163
      data += offset_size;
1164
      break;
1165
 
1166
    case DW_FORM_ref1:
1167
    case DW_FORM_flag:
1168
    case DW_FORM_data1:
1169
      uvalue = byte_get (data++, 1);
1170
      break;
1171
 
1172
    case DW_FORM_ref2:
1173
    case DW_FORM_data2:
1174
      uvalue = byte_get (data, 2);
1175
      data += 2;
1176
      break;
1177
 
1178
    case DW_FORM_ref4:
1179
    case DW_FORM_data4:
1180
      uvalue = byte_get (data, 4);
1181
      data += 4;
1182
      break;
1183
 
1184
    case DW_FORM_sdata:
1185
      uvalue = read_leb128 (data, & bytes_read, 1);
1186
      data += bytes_read;
1187
      break;
1188
 
1189
    case DW_FORM_ref_udata:
1190
    case DW_FORM_udata:
1191
      uvalue = read_leb128 (data, & bytes_read, 0);
1192
      data += bytes_read;
1193
      break;
1194
 
1195
    case DW_FORM_indirect:
1196
      form = read_leb128 (data, & bytes_read, 0);
1197
      data += bytes_read;
1198
      if (!do_loc)
1199
        printf (" %s", get_FORM_name (form));
1200
      return read_and_display_attr_value (attribute, form, data,
1201
                                          cu_offset, pointer_size,
1202
                                          offset_size, dwarf_version,
1203
                                          debug_info_p, do_loc,
1204
                                          section);
1205
    }
1206
 
1207
  switch (form)
1208
    {
1209
    case DW_FORM_ref_addr:
1210
      if (!do_loc)
1211
        printf (" <0x%lx>", uvalue);
1212
      break;
1213
 
1214
    case DW_FORM_ref1:
1215
    case DW_FORM_ref2:
1216
    case DW_FORM_ref4:
1217
    case DW_FORM_ref_udata:
1218
      if (!do_loc)
1219
        printf (" <0x%lx>", uvalue + cu_offset);
1220
      break;
1221
 
1222
    case DW_FORM_data4:
1223
    case DW_FORM_addr:
1224
      if (!do_loc)
1225
        printf (" 0x%lx", uvalue);
1226
      break;
1227
 
1228
    case DW_FORM_flag:
1229
    case DW_FORM_data1:
1230
    case DW_FORM_data2:
1231
    case DW_FORM_sdata:
1232
    case DW_FORM_udata:
1233
      if (!do_loc)
1234
        printf (" %ld", uvalue);
1235
      break;
1236
 
1237
    case DW_FORM_ref8:
1238
    case DW_FORM_data8:
1239
      if (!do_loc)
1240
        {
1241
          uvalue = byte_get (data, 4);
1242
          printf (" 0x%lx", uvalue);
1243
          printf (" 0x%lx", (unsigned long) byte_get (data + 4, 4));
1244
        }
1245
      if ((do_loc || do_debug_loc || do_debug_ranges)
1246
          && num_debug_info_entries == 0)
1247
        {
1248
          if (sizeof (uvalue) == 8)
1249
            uvalue = byte_get (data, 8);
1250
          else
1251
            error (_("DW_FORM_data8 is unsupported when sizeof (unsigned long) != 8\n"));
1252
        }
1253
      data += 8;
1254
      break;
1255
 
1256
    case DW_FORM_string:
1257
      if (!do_loc)
1258
        printf (" %s", data);
1259
      data += strlen ((char *) data) + 1;
1260
      break;
1261
 
1262
    case DW_FORM_block:
1263
      uvalue = read_leb128 (data, & bytes_read, 0);
1264
      block_start = data + bytes_read;
1265
      if (do_loc)
1266
        data = block_start + uvalue;
1267
      else
1268
        data = display_block (block_start, uvalue);
1269
      break;
1270
 
1271
    case DW_FORM_block1:
1272
      uvalue = byte_get (data, 1);
1273
      block_start = data + 1;
1274
      if (do_loc)
1275
        data = block_start + uvalue;
1276
      else
1277
        data = display_block (block_start, uvalue);
1278
      break;
1279
 
1280
    case DW_FORM_block2:
1281
      uvalue = byte_get (data, 2);
1282
      block_start = data + 2;
1283
      if (do_loc)
1284
        data = block_start + uvalue;
1285
      else
1286
        data = display_block (block_start, uvalue);
1287
      break;
1288
 
1289
    case DW_FORM_block4:
1290
      uvalue = byte_get (data, 4);
1291
      block_start = data + 4;
1292
      if (do_loc)
1293
        data = block_start + uvalue;
1294
      else
1295
        data = display_block (block_start, uvalue);
1296
      break;
1297
 
1298
    case DW_FORM_strp:
1299
      if (!do_loc)
1300
        printf (_(" (indirect string, offset: 0x%lx): %s"),
1301
                uvalue, fetch_indirect_string (uvalue));
1302
      break;
1303
 
1304
    case DW_FORM_indirect:
1305
      /* Handled above.  */
1306
      break;
1307
 
1308
    default:
1309
      warn (_("Unrecognized form: %lu\n"), form);
1310
      break;
1311
    }
1312
 
1313
  if ((do_loc || do_debug_loc || do_debug_ranges)
1314
      && num_debug_info_entries == 0)
1315
    {
1316
      switch (attribute)
1317
        {
1318
        case DW_AT_frame_base:
1319
          have_frame_base = 1;
1320
        case DW_AT_location:
1321
        case DW_AT_string_length:
1322
        case DW_AT_return_addr:
1323
        case DW_AT_data_member_location:
1324
        case DW_AT_vtable_elem_location:
1325
        case DW_AT_segment:
1326
        case DW_AT_static_link:
1327
        case DW_AT_use_location:
1328
          if (form == DW_FORM_data4 || form == DW_FORM_data8)
1329
            {
1330
              /* Process location list.  */
1331
              unsigned int max = debug_info_p->max_loc_offsets;
1332
              unsigned int num = debug_info_p->num_loc_offsets;
1333
 
1334
              if (max == 0 || num >= max)
1335
                {
1336
                  max += 1024;
1337
                  debug_info_p->loc_offsets = (long unsigned int *)
1338
                      xcrealloc (debug_info_p->loc_offsets,
1339
                                 max, sizeof (*debug_info_p->loc_offsets));
1340
                  debug_info_p->have_frame_base = (int *)
1341
                      xcrealloc (debug_info_p->have_frame_base,
1342
                                 max, sizeof (*debug_info_p->have_frame_base));
1343
                  debug_info_p->max_loc_offsets = max;
1344
                }
1345
              debug_info_p->loc_offsets [num] = uvalue;
1346
              debug_info_p->have_frame_base [num] = have_frame_base;
1347
              debug_info_p->num_loc_offsets++;
1348
            }
1349
          break;
1350
 
1351
        case DW_AT_low_pc:
1352
          if (need_base_address)
1353
            debug_info_p->base_address = uvalue;
1354
          break;
1355
 
1356
        case DW_AT_ranges:
1357
          if (form == DW_FORM_data4 || form == DW_FORM_data8)
1358
            {
1359
              /* Process range list.  */
1360
              unsigned int max = debug_info_p->max_range_lists;
1361
              unsigned int num = debug_info_p->num_range_lists;
1362
 
1363
              if (max == 0 || num >= max)
1364
                {
1365
                  max += 1024;
1366
                  debug_info_p->range_lists = (long unsigned int *)
1367
                      xcrealloc (debug_info_p->range_lists,
1368
                                 max, sizeof (*debug_info_p->range_lists));
1369
                  debug_info_p->max_range_lists = max;
1370
                }
1371
              debug_info_p->range_lists [num] = uvalue;
1372
              debug_info_p->num_range_lists++;
1373
            }
1374
          break;
1375
 
1376
        default:
1377
          break;
1378
        }
1379
    }
1380
 
1381
  if (do_loc)
1382
    return data;
1383
 
1384
  /* For some attributes we can display further information.  */
1385
  printf ("\t");
1386
 
1387
  switch (attribute)
1388
    {
1389
    case DW_AT_inline:
1390
      switch (uvalue)
1391
        {
1392
        case DW_INL_not_inlined:
1393
          printf (_("(not inlined)"));
1394
          break;
1395
        case DW_INL_inlined:
1396
          printf (_("(inlined)"));
1397
          break;
1398
        case DW_INL_declared_not_inlined:
1399
          printf (_("(declared as inline but ignored)"));
1400
          break;
1401
        case DW_INL_declared_inlined:
1402
          printf (_("(declared as inline and inlined)"));
1403
          break;
1404
        default:
1405
          printf (_("  (Unknown inline attribute value: %lx)"), uvalue);
1406
          break;
1407
        }
1408
      break;
1409
 
1410
    case DW_AT_language:
1411
      switch (uvalue)
1412
        {
1413
          /* Ordered by the numeric value of these constants.  */
1414
        case DW_LANG_C89:               printf ("(ANSI C)"); break;
1415
        case DW_LANG_C:                 printf ("(non-ANSI C)"); break;
1416
        case DW_LANG_Ada83:             printf ("(Ada)"); break;
1417
        case DW_LANG_C_plus_plus:       printf ("(C++)"); break;
1418
        case DW_LANG_Cobol74:           printf ("(Cobol 74)"); break;
1419
        case DW_LANG_Cobol85:           printf ("(Cobol 85)"); break;
1420
        case DW_LANG_Fortran77:         printf ("(FORTRAN 77)"); break;
1421
        case DW_LANG_Fortran90:         printf ("(Fortran 90)"); break;
1422
        case DW_LANG_Pascal83:          printf ("(ANSI Pascal)"); break;
1423
        case DW_LANG_Modula2:           printf ("(Modula 2)"); break;
1424
          /* DWARF 2.1 values.  */
1425
        case DW_LANG_Java:              printf ("(Java)"); break;
1426
        case DW_LANG_C99:               printf ("(ANSI C99)"); break;
1427
        case DW_LANG_Ada95:             printf ("(ADA 95)"); break;
1428
        case DW_LANG_Fortran95:         printf ("(Fortran 95)"); break;
1429
          /* DWARF 3 values.  */
1430
        case DW_LANG_PLI:               printf ("(PLI)"); break;
1431
        case DW_LANG_ObjC:              printf ("(Objective C)"); break;
1432
        case DW_LANG_ObjC_plus_plus:    printf ("(Objective C++)"); break;
1433
        case DW_LANG_UPC:               printf ("(Unified Parallel C)"); break;
1434
        case DW_LANG_D:                 printf ("(D)"); break;
1435
          /* MIPS extension.  */
1436
        case DW_LANG_Mips_Assembler:    printf ("(MIPS assembler)"); break;
1437
          /* UPC extension.  */
1438
        case DW_LANG_Upc:               printf ("(Unified Parallel C)"); break;
1439
        default:
1440
          if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1441
            printf ("(implementation defined: %lx)", uvalue);
1442
          else
1443
            printf ("(Unknown: %lx)", uvalue);
1444
          break;
1445
        }
1446
      break;
1447
 
1448
    case DW_AT_encoding:
1449
      switch (uvalue)
1450
        {
1451
        case DW_ATE_void:               printf ("(void)"); break;
1452
        case DW_ATE_address:            printf ("(machine address)"); break;
1453
        case DW_ATE_boolean:            printf ("(boolean)"); break;
1454
        case DW_ATE_complex_float:      printf ("(complex float)"); break;
1455
        case DW_ATE_float:              printf ("(float)"); break;
1456
        case DW_ATE_signed:             printf ("(signed)"); break;
1457
        case DW_ATE_signed_char:        printf ("(signed char)"); break;
1458
        case DW_ATE_unsigned:           printf ("(unsigned)"); break;
1459
        case DW_ATE_unsigned_char:      printf ("(unsigned char)"); break;
1460
          /* DWARF 2.1 values:  */
1461
        case DW_ATE_imaginary_float:    printf ("(imaginary float)"); break;
1462
        case DW_ATE_decimal_float:      printf ("(decimal float)"); break;
1463
          /* DWARF 3 values:  */
1464
        case DW_ATE_packed_decimal:     printf ("(packed_decimal)"); break;
1465
        case DW_ATE_numeric_string:     printf ("(numeric_string)"); break;
1466
        case DW_ATE_edited:             printf ("(edited)"); break;
1467
        case DW_ATE_signed_fixed:       printf ("(signed_fixed)"); break;
1468
        case DW_ATE_unsigned_fixed:     printf ("(unsigned_fixed)"); break;
1469
          /* HP extensions:  */
1470
        case DW_ATE_HP_float80:         printf ("(HP_float80)"); break;
1471
        case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
1472
        case DW_ATE_HP_float128:        printf ("(HP_float128)"); break;
1473
        case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
1474
        case DW_ATE_HP_floathpintel:    printf ("(HP_floathpintel)"); break;
1475
        case DW_ATE_HP_imaginary_float80:       printf ("(HP_imaginary_float80)"); break;
1476
        case DW_ATE_HP_imaginary_float128:      printf ("(HP_imaginary_float128)"); break;
1477
 
1478
        default:
1479
          if (uvalue >= DW_ATE_lo_user
1480
              && uvalue <= DW_ATE_hi_user)
1481
            printf ("(user defined type)");
1482
          else
1483
            printf ("(unknown type)");
1484
          break;
1485
        }
1486
      break;
1487
 
1488
    case DW_AT_accessibility:
1489
      switch (uvalue)
1490
        {
1491
        case DW_ACCESS_public:          printf ("(public)"); break;
1492
        case DW_ACCESS_protected:       printf ("(protected)"); break;
1493
        case DW_ACCESS_private:         printf ("(private)"); break;
1494
        default:
1495
          printf ("(unknown accessibility)");
1496
          break;
1497
        }
1498
      break;
1499
 
1500
    case DW_AT_visibility:
1501
      switch (uvalue)
1502
        {
1503
        case DW_VIS_local:              printf ("(local)"); break;
1504
        case DW_VIS_exported:           printf ("(exported)"); break;
1505
        case DW_VIS_qualified:          printf ("(qualified)"); break;
1506
        default:                        printf ("(unknown visibility)"); break;
1507
        }
1508
      break;
1509
 
1510
    case DW_AT_virtuality:
1511
      switch (uvalue)
1512
        {
1513
        case DW_VIRTUALITY_none:        printf ("(none)"); break;
1514
        case DW_VIRTUALITY_virtual:     printf ("(virtual)"); break;
1515
        case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1516
        default:                        printf ("(unknown virtuality)"); break;
1517
        }
1518
      break;
1519
 
1520
    case DW_AT_identifier_case:
1521
      switch (uvalue)
1522
        {
1523
        case DW_ID_case_sensitive:      printf ("(case_sensitive)"); break;
1524
        case DW_ID_up_case:             printf ("(up_case)"); break;
1525
        case DW_ID_down_case:           printf ("(down_case)"); break;
1526
        case DW_ID_case_insensitive:    printf ("(case_insensitive)"); break;
1527
        default:                        printf ("(unknown case)"); break;
1528
        }
1529
      break;
1530
 
1531
    case DW_AT_calling_convention:
1532
      switch (uvalue)
1533
        {
1534
        case DW_CC_normal:      printf ("(normal)"); break;
1535
        case DW_CC_program:     printf ("(program)"); break;
1536
        case DW_CC_nocall:      printf ("(nocall)"); break;
1537
        default:
1538
          if (uvalue >= DW_CC_lo_user
1539
              && uvalue <= DW_CC_hi_user)
1540
            printf ("(user defined)");
1541
          else
1542
            printf ("(unknown convention)");
1543
        }
1544
      break;
1545
 
1546
    case DW_AT_ordering:
1547
      switch (uvalue)
1548
        {
1549
        case -1: printf ("(undefined)"); break;
1550
        case 0:  printf ("(row major)"); break;
1551
        case 1:  printf ("(column major)"); break;
1552
        }
1553
      break;
1554
 
1555
    case DW_AT_frame_base:
1556
      have_frame_base = 1;
1557
    case DW_AT_location:
1558
    case DW_AT_string_length:
1559
    case DW_AT_return_addr:
1560
    case DW_AT_data_member_location:
1561
    case DW_AT_vtable_elem_location:
1562
    case DW_AT_segment:
1563
    case DW_AT_static_link:
1564
    case DW_AT_use_location:
1565
      if (form == DW_FORM_data4 || form == DW_FORM_data8)
1566
        printf (_("(location list)"));
1567
      /* Fall through.  */
1568
    case DW_AT_allocated:
1569
    case DW_AT_associated:
1570
    case DW_AT_data_location:
1571
    case DW_AT_stride:
1572
    case DW_AT_upper_bound:
1573
    case DW_AT_lower_bound:
1574
      if (block_start)
1575
        {
1576
          int need_frame_base;
1577
 
1578
          printf ("(");
1579
          need_frame_base = decode_location_expression (block_start,
1580
                                                        pointer_size,
1581
                                                        uvalue,
1582
                                                        cu_offset, section);
1583
          printf (")");
1584
          if (need_frame_base && !have_frame_base)
1585
            printf (_(" [without DW_AT_frame_base]"));
1586
        }
1587
      break;
1588
 
1589
    case DW_AT_import:
1590
      {
1591
        if (form == DW_FORM_ref1
1592
            || form == DW_FORM_ref2
1593
            || form == DW_FORM_ref4)
1594
          uvalue += cu_offset;
1595
 
1596
        if (uvalue >= section->size)
1597
          warn (_("Offset %lx used as value for DW_AT_import attribute of DIE at offset %lx is too big.\n"),
1598
                uvalue, (unsigned long) (orig_data - section->start));
1599
        else
1600
          {
1601
            unsigned long abbrev_number;
1602
            abbrev_entry * entry;
1603
 
1604
            abbrev_number = read_leb128 (section->start + uvalue, NULL, 0);
1605
 
1606
            printf ("[Abbrev Number: %ld", abbrev_number);
1607
            for (entry = first_abbrev; entry != NULL; entry = entry->next)
1608
              if (entry->entry == abbrev_number)
1609
                break;
1610
            if (entry != NULL)
1611
              printf (" (%s)", get_TAG_name (entry->tag));
1612
            printf ("]");
1613
          }
1614
      }
1615
      break;
1616
 
1617
    default:
1618
      break;
1619
    }
1620
 
1621
  return data;
1622
}
1623
 
1624
static char *
1625
get_AT_name (unsigned long attribute)
1626
{
1627
  switch (attribute)
1628
    {
1629
    case DW_AT_sibling:                 return "DW_AT_sibling";
1630
    case DW_AT_location:                return "DW_AT_location";
1631
    case DW_AT_name:                    return "DW_AT_name";
1632
    case DW_AT_ordering:                return "DW_AT_ordering";
1633
    case DW_AT_subscr_data:             return "DW_AT_subscr_data";
1634
    case DW_AT_byte_size:               return "DW_AT_byte_size";
1635
    case DW_AT_bit_offset:              return "DW_AT_bit_offset";
1636
    case DW_AT_bit_size:                return "DW_AT_bit_size";
1637
    case DW_AT_element_list:            return "DW_AT_element_list";
1638
    case DW_AT_stmt_list:               return "DW_AT_stmt_list";
1639
    case DW_AT_low_pc:                  return "DW_AT_low_pc";
1640
    case DW_AT_high_pc:                 return "DW_AT_high_pc";
1641
    case DW_AT_language:                return "DW_AT_language";
1642
    case DW_AT_member:                  return "DW_AT_member";
1643
    case DW_AT_discr:                   return "DW_AT_discr";
1644
    case DW_AT_discr_value:             return "DW_AT_discr_value";
1645
    case DW_AT_visibility:              return "DW_AT_visibility";
1646
    case DW_AT_import:                  return "DW_AT_import";
1647
    case DW_AT_string_length:           return "DW_AT_string_length";
1648
    case DW_AT_common_reference:        return "DW_AT_common_reference";
1649
    case DW_AT_comp_dir:                return "DW_AT_comp_dir";
1650
    case DW_AT_const_value:             return "DW_AT_const_value";
1651
    case DW_AT_containing_type:         return "DW_AT_containing_type";
1652
    case DW_AT_default_value:           return "DW_AT_default_value";
1653
    case DW_AT_inline:                  return "DW_AT_inline";
1654
    case DW_AT_is_optional:             return "DW_AT_is_optional";
1655
    case DW_AT_lower_bound:             return "DW_AT_lower_bound";
1656
    case DW_AT_producer:                return "DW_AT_producer";
1657
    case DW_AT_prototyped:              return "DW_AT_prototyped";
1658
    case DW_AT_return_addr:             return "DW_AT_return_addr";
1659
    case DW_AT_start_scope:             return "DW_AT_start_scope";
1660
    case DW_AT_stride_size:             return "DW_AT_stride_size";
1661
    case DW_AT_upper_bound:             return "DW_AT_upper_bound";
1662
    case DW_AT_abstract_origin:         return "DW_AT_abstract_origin";
1663
    case DW_AT_accessibility:           return "DW_AT_accessibility";
1664
    case DW_AT_address_class:           return "DW_AT_address_class";
1665
    case DW_AT_artificial:              return "DW_AT_artificial";
1666
    case DW_AT_base_types:              return "DW_AT_base_types";
1667
    case DW_AT_calling_convention:      return "DW_AT_calling_convention";
1668
    case DW_AT_count:                   return "DW_AT_count";
1669
    case DW_AT_data_member_location:    return "DW_AT_data_member_location";
1670
    case DW_AT_decl_column:             return "DW_AT_decl_column";
1671
    case DW_AT_decl_file:               return "DW_AT_decl_file";
1672
    case DW_AT_decl_line:               return "DW_AT_decl_line";
1673
    case DW_AT_declaration:             return "DW_AT_declaration";
1674
    case DW_AT_discr_list:              return "DW_AT_discr_list";
1675
    case DW_AT_encoding:                return "DW_AT_encoding";
1676
    case DW_AT_external:                return "DW_AT_external";
1677
    case DW_AT_frame_base:              return "DW_AT_frame_base";
1678
    case DW_AT_friend:                  return "DW_AT_friend";
1679
    case DW_AT_identifier_case:         return "DW_AT_identifier_case";
1680
    case DW_AT_macro_info:              return "DW_AT_macro_info";
1681
    case DW_AT_namelist_items:          return "DW_AT_namelist_items";
1682
    case DW_AT_priority:                return "DW_AT_priority";
1683
    case DW_AT_segment:                 return "DW_AT_segment";
1684
    case DW_AT_specification:           return "DW_AT_specification";
1685
    case DW_AT_static_link:             return "DW_AT_static_link";
1686
    case DW_AT_type:                    return "DW_AT_type";
1687
    case DW_AT_use_location:            return "DW_AT_use_location";
1688
    case DW_AT_variable_parameter:      return "DW_AT_variable_parameter";
1689
    case DW_AT_virtuality:              return "DW_AT_virtuality";
1690
    case DW_AT_vtable_elem_location:    return "DW_AT_vtable_elem_location";
1691
      /* DWARF 2.1 values.  */
1692
    case DW_AT_allocated:               return "DW_AT_allocated";
1693
    case DW_AT_associated:              return "DW_AT_associated";
1694
    case DW_AT_data_location:           return "DW_AT_data_location";
1695
    case DW_AT_stride:                  return "DW_AT_stride";
1696
    case DW_AT_entry_pc:                return "DW_AT_entry_pc";
1697
    case DW_AT_use_UTF8:                return "DW_AT_use_UTF8";
1698
    case DW_AT_extension:               return "DW_AT_extension";
1699
    case DW_AT_ranges:                  return "DW_AT_ranges";
1700
    case DW_AT_trampoline:              return "DW_AT_trampoline";
1701
    case DW_AT_call_column:             return "DW_AT_call_column";
1702
    case DW_AT_call_file:               return "DW_AT_call_file";
1703
    case DW_AT_call_line:               return "DW_AT_call_line";
1704
    case DW_AT_description:             return "DW_AT_description";
1705
    case DW_AT_binary_scale:            return "DW_AT_binary_scale";
1706
    case DW_AT_decimal_scale:           return "DW_AT_decimal_scale";
1707
    case DW_AT_small:                   return "DW_AT_small";
1708
    case DW_AT_decimal_sign:            return "DW_AT_decimal_sign";
1709
    case DW_AT_digit_count:             return "DW_AT_digit_count";
1710
    case DW_AT_picture_string:          return "DW_AT_picture_string";
1711
    case DW_AT_mutable:                 return "DW_AT_mutable";
1712
    case DW_AT_threads_scaled:          return "DW_AT_threads_scaled";
1713
    case DW_AT_explicit:                return "DW_AT_explicit";
1714
    case DW_AT_object_pointer:          return "DW_AT_object_pointer";
1715
    case DW_AT_endianity:               return "DW_AT_endianity";
1716
    case DW_AT_elemental:               return "DW_AT_elemental";
1717
    case DW_AT_pure:                    return "DW_AT_pure";
1718
    case DW_AT_recursive:               return "DW_AT_recursive";
1719
 
1720
      /* HP and SGI/MIPS extensions.  */
1721
    case DW_AT_MIPS_loop_begin:                 return "DW_AT_MIPS_loop_begin";
1722
    case DW_AT_MIPS_tail_loop_begin:            return "DW_AT_MIPS_tail_loop_begin";
1723
    case DW_AT_MIPS_epilog_begin:               return "DW_AT_MIPS_epilog_begin";
1724
    case DW_AT_MIPS_loop_unroll_factor:         return "DW_AT_MIPS_loop_unroll_factor";
1725
    case DW_AT_MIPS_software_pipeline_depth:    return "DW_AT_MIPS_software_pipeline_depth";
1726
    case DW_AT_MIPS_linkage_name:               return "DW_AT_MIPS_linkage_name";
1727
    case DW_AT_MIPS_stride:                     return "DW_AT_MIPS_stride";
1728
    case DW_AT_MIPS_abstract_name:              return "DW_AT_MIPS_abstract_name";
1729
    case DW_AT_MIPS_clone_origin:               return "DW_AT_MIPS_clone_origin";
1730
    case DW_AT_MIPS_has_inlines:                return "DW_AT_MIPS_has_inlines";
1731
 
1732
      /* HP Extensions.  */
1733
    case DW_AT_HP_block_index:                  return "DW_AT_HP_block_index";
1734
    case DW_AT_HP_actuals_stmt_list:            return "DW_AT_HP_actuals_stmt_list";
1735
    case DW_AT_HP_proc_per_section:             return "DW_AT_HP_proc_per_section";
1736
    case DW_AT_HP_raw_data_ptr:                 return "DW_AT_HP_raw_data_ptr";
1737
    case DW_AT_HP_pass_by_reference:            return "DW_AT_HP_pass_by_reference";
1738
    case DW_AT_HP_opt_level:                    return "DW_AT_HP_opt_level";
1739
    case DW_AT_HP_prof_version_id:              return "DW_AT_HP_prof_version_id";
1740
    case DW_AT_HP_opt_flags:                    return "DW_AT_HP_opt_flags";
1741
    case DW_AT_HP_cold_region_low_pc:           return "DW_AT_HP_cold_region_low_pc";
1742
    case DW_AT_HP_cold_region_high_pc:          return "DW_AT_HP_cold_region_high_pc";
1743
    case DW_AT_HP_all_variables_modifiable:     return "DW_AT_HP_all_variables_modifiable";
1744
    case DW_AT_HP_linkage_name:                 return "DW_AT_HP_linkage_name";
1745
    case DW_AT_HP_prof_flags:                   return "DW_AT_HP_prof_flags";
1746
 
1747
      /* One value is shared by the MIPS and HP extensions:  */
1748
    case DW_AT_MIPS_fde:                        return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1749
 
1750
      /* GNU extensions.  */
1751
    case DW_AT_sf_names:                return "DW_AT_sf_names";
1752
    case DW_AT_src_info:                return "DW_AT_src_info";
1753
    case DW_AT_mac_info:                return "DW_AT_mac_info";
1754
    case DW_AT_src_coords:              return "DW_AT_src_coords";
1755
    case DW_AT_body_begin:              return "DW_AT_body_begin";
1756
    case DW_AT_body_end:                return "DW_AT_body_end";
1757
    case DW_AT_GNU_vector:              return "DW_AT_GNU_vector";
1758
 
1759
      /* UPC extension.  */
1760
    case DW_AT_upc_threads_scaled:      return "DW_AT_upc_threads_scaled";
1761
 
1762
    /* PGI (STMicroelectronics) extensions.  */
1763
    case DW_AT_PGI_lbase:               return "DW_AT_PGI_lbase";
1764
    case DW_AT_PGI_soffset:             return "DW_AT_PGI_soffset";
1765
    case DW_AT_PGI_lstride:             return "DW_AT_PGI_lstride";
1766
 
1767
    default:
1768
      {
1769
        static char buffer[100];
1770
 
1771
        snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1772
                  attribute);
1773
        return buffer;
1774
      }
1775
    }
1776
}
1777
 
1778
static unsigned char *
1779
read_and_display_attr (unsigned long attribute,
1780
                       unsigned long form,
1781
                       unsigned char * data,
1782
                       unsigned long cu_offset,
1783
                       unsigned long pointer_size,
1784
                       unsigned long offset_size,
1785
                       int dwarf_version,
1786
                       debug_info * debug_info_p,
1787
                       int do_loc,
1788
                       struct dwarf_section * section)
1789
{
1790
  if (!do_loc)
1791
    printf ("   %-18s:", get_AT_name (attribute));
1792
  data = read_and_display_attr_value (attribute, form, data, cu_offset,
1793
                                      pointer_size, offset_size,
1794
                                      dwarf_version, debug_info_p,
1795
                                      do_loc, section);
1796
  if (!do_loc)
1797
    printf ("\n");
1798
  return data;
1799
}
1800
 
1801
 
1802
/* Process the contents of a .debug_info section.  If do_loc is non-zero
1803
   then we are scanning for location lists and we do not want to display
1804
   anything to the user.  */
1805
 
1806
static int
1807
process_debug_info (struct dwarf_section *section,
1808
                    void *file,
1809
                    int do_loc)
1810
{
1811
  unsigned char *start = section->start;
1812
  unsigned char *end = start + section->size;
1813
  unsigned char *section_begin;
1814
  unsigned int unit;
1815
  unsigned int num_units = 0;
1816
 
1817
  if ((do_loc || do_debug_loc || do_debug_ranges)
1818
      && num_debug_info_entries == 0)
1819
    {
1820
      unsigned long length;
1821
 
1822
      /* First scan the section to get the number of comp units.  */
1823
      for (section_begin = start, num_units = 0; section_begin < end;
1824
           num_units ++)
1825
        {
1826
          /* Read the first 4 bytes.  For a 32-bit DWARF section, this
1827
             will be the length.  For a 64-bit DWARF section, it'll be
1828
             the escape code 0xffffffff followed by an 8 byte length.  */
1829
          length = byte_get (section_begin, 4);
1830
 
1831
          if (length == 0xffffffff)
1832
            {
1833
              length = byte_get (section_begin + 4, 8);
1834
              section_begin += length + 12;
1835
            }
1836
          else if (length >= 0xfffffff0 && length < 0xffffffff)
1837
            {
1838
              warn (_("Reserved length value (%lx) found in section %s\n"), length, section->name);
1839
              return 0;
1840
            }
1841
          else
1842
            section_begin += length + 4;
1843
 
1844
          /* Negative values are illegal, they may even cause infinite
1845
             looping.  This can happen if we can't accurately apply
1846
             relocations to an object file.  */
1847
          if ((signed long) length <= 0)
1848
            {
1849
              warn (_("Corrupt unit length (%lx) found in section %s\n"), length, section->name);
1850
              return 0;
1851
            }
1852
        }
1853
 
1854
      if (num_units == 0)
1855
        {
1856
          error (_("No comp units in %s section ?"), section->name);
1857
          return 0;
1858
        }
1859
 
1860
      /* Then allocate an array to hold the information.  */
1861
      debug_information = (debug_info *) cmalloc (num_units,
1862
                                                  sizeof (* debug_information));
1863
      if (debug_information == NULL)
1864
        {
1865
          error (_("Not enough memory for a debug info array of %u entries"),
1866
                 num_units);
1867
          return 0;
1868
        }
1869
    }
1870
 
1871
  if (!do_loc)
1872
    {
1873
      printf (_("Contents of the %s section:\n\n"), section->name);
1874
 
1875
      load_debug_section (str, file);
1876
    }
1877
 
1878
  load_debug_section (abbrev, file);
1879
  if (debug_displays [abbrev].section.start == NULL)
1880
    {
1881
      warn (_("Unable to locate %s section!\n"),
1882
            debug_displays [abbrev].section.name);
1883
      return 0;
1884
    }
1885
 
1886
  for (section_begin = start, unit = 0; start < end; unit++)
1887
    {
1888
      DWARF2_Internal_CompUnit compunit;
1889
      unsigned char *hdrptr;
1890
      unsigned char *tags;
1891
      int level;
1892
      unsigned long cu_offset;
1893
      int offset_size;
1894
      int initial_length_size;
1895
 
1896
      hdrptr = start;
1897
 
1898
      compunit.cu_length = byte_get (hdrptr, 4);
1899
      hdrptr += 4;
1900
 
1901
      if (compunit.cu_length == 0xffffffff)
1902
        {
1903
          compunit.cu_length = byte_get (hdrptr, 8);
1904
          hdrptr += 8;
1905
          offset_size = 8;
1906
          initial_length_size = 12;
1907
        }
1908
      else
1909
        {
1910
          offset_size = 4;
1911
          initial_length_size = 4;
1912
        }
1913
 
1914
      compunit.cu_version = byte_get (hdrptr, 2);
1915
      hdrptr += 2;
1916
 
1917
      cu_offset = start - section_begin;
1918
 
1919
      compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
1920
      hdrptr += offset_size;
1921
 
1922
      compunit.cu_pointer_size = byte_get (hdrptr, 1);
1923
      hdrptr += 1;
1924
      if ((do_loc || do_debug_loc || do_debug_ranges)
1925
          && num_debug_info_entries == 0)
1926
        {
1927
          debug_information [unit].cu_offset = cu_offset;
1928
          debug_information [unit].pointer_size
1929
            = compunit.cu_pointer_size;
1930
          debug_information [unit].base_address = 0;
1931
          debug_information [unit].loc_offsets = NULL;
1932
          debug_information [unit].have_frame_base = NULL;
1933
          debug_information [unit].max_loc_offsets = 0;
1934
          debug_information [unit].num_loc_offsets = 0;
1935
          debug_information [unit].range_lists = NULL;
1936
          debug_information [unit].max_range_lists= 0;
1937
          debug_information [unit].num_range_lists = 0;
1938
        }
1939
 
1940
      if (!do_loc)
1941
        {
1942
          printf (_("  Compilation Unit @ offset 0x%lx:\n"), cu_offset);
1943
          printf (_("   Length:        0x%lx (%s)\n"), compunit.cu_length,
1944
                  initial_length_size == 8 ? "64-bit" : "32-bit");
1945
          printf (_("   Version:       %d\n"), compunit.cu_version);
1946
          printf (_("   Abbrev Offset: %ld\n"), compunit.cu_abbrev_offset);
1947
          printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
1948
        }
1949
 
1950
      if (cu_offset + compunit.cu_length + initial_length_size
1951
          > section->size)
1952
        {
1953
          warn (_("Debug info is corrupted, length of CU at %lx extends beyond end of section (length = %lx)\n"),
1954
                cu_offset, compunit.cu_length);
1955
          break;
1956
        }
1957
      tags = hdrptr;
1958
      start += compunit.cu_length + initial_length_size;
1959
 
1960
      if (compunit.cu_version != 2 && compunit.cu_version != 3)
1961
        {
1962
          warn (_("CU at offset %lx contains corrupt or unsupported version number: %d.\n"),
1963
                cu_offset, compunit.cu_version);
1964
          continue;
1965
        }
1966
 
1967
      free_abbrevs ();
1968
 
1969
      /* Process the abbrevs used by this compilation unit. DWARF
1970
         sections under Mach-O have non-zero addresses.  */
1971
      if (compunit.cu_abbrev_offset >= debug_displays [abbrev].section.size)
1972
        warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
1973
              (unsigned long) compunit.cu_abbrev_offset,
1974
              (unsigned long) debug_displays [abbrev].section.size);
1975
      else
1976
        process_abbrev_section
1977
          ((unsigned char *) debug_displays [abbrev].section.start
1978
           + compunit.cu_abbrev_offset - debug_displays [abbrev].section.address,
1979
           (unsigned char *) debug_displays [abbrev].section.start
1980
           + debug_displays [abbrev].section.size);
1981
 
1982
      level = 0;
1983
      while (tags < start)
1984
        {
1985
          unsigned int bytes_read;
1986
          unsigned long abbrev_number;
1987
          unsigned long die_offset;
1988
          abbrev_entry *entry;
1989
          abbrev_attr *attr;
1990
 
1991
          die_offset = tags - section_begin;
1992
 
1993
          abbrev_number = read_leb128 (tags, & bytes_read, 0);
1994
          tags += bytes_read;
1995
 
1996
          /* A null DIE marks the end of a list of siblings or it may also be
1997
             a section padding.  */
1998
          if (abbrev_number == 0)
1999
            {
2000
              /* Check if it can be a section padding for the last CU.  */
2001
              if (level == 0 && start == end)
2002
                {
2003
                  unsigned char *chk;
2004
 
2005
                  for (chk = tags; chk < start; chk++)
2006
                    if (*chk != 0)
2007
                      break;
2008
                  if (chk == start)
2009
                    break;
2010
                }
2011
 
2012
              --level;
2013
              if (level < 0)
2014
                {
2015
                  static unsigned num_bogus_warns = 0;
2016
 
2017
                  if (num_bogus_warns < 3)
2018
                    {
2019
                      warn (_("Bogus end-of-siblings marker detected at offset %lx in .debug_info section\n"),
2020
                            die_offset);
2021
                      num_bogus_warns ++;
2022
                      if (num_bogus_warns == 3)
2023
                        warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
2024
                    }
2025
                }
2026
              continue;
2027
            }
2028
 
2029
          if (!do_loc)
2030
            printf (_(" <%d><%lx>: Abbrev Number: %lu"),
2031
                    level, die_offset, abbrev_number);
2032
 
2033
          /* Scan through the abbreviation list until we reach the
2034
             correct entry.  */
2035
          for (entry = first_abbrev;
2036
               entry && entry->entry != abbrev_number;
2037
               entry = entry->next)
2038
            continue;
2039
 
2040
          if (entry == NULL)
2041
            {
2042
              if (!do_loc)
2043
                {
2044
                  printf ("\n");
2045
                  fflush (stdout);
2046
                }
2047
              warn (_("DIE at offset %lx refers to abbreviation number %lu which does not exist\n"),
2048
                    die_offset, abbrev_number);
2049
              return 0;
2050
            }
2051
 
2052
          if (!do_loc)
2053
            printf (_(" (%s)\n"), get_TAG_name (entry->tag));
2054
 
2055
          switch (entry->tag)
2056
            {
2057
            default:
2058
              need_base_address = 0;
2059
              break;
2060
            case DW_TAG_compile_unit:
2061
              need_base_address = 1;
2062
              break;
2063
            case DW_TAG_entry_point:
2064
            case DW_TAG_subprogram:
2065
              need_base_address = 0;
2066
              /* Assuming that there is no DW_AT_frame_base.  */
2067
              have_frame_base = 0;
2068
              break;
2069
            }
2070
 
2071
          for (attr = entry->first_attr; attr; attr = attr->next)
2072
            {
2073
              if (! do_loc)
2074
                /* Show the offset from where the tag was extracted.  */
2075
                printf ("    <%2lx>", (unsigned long)(tags - section_begin));
2076
 
2077
              tags = read_and_display_attr (attr->attribute,
2078
                                            attr->form,
2079
                                            tags, cu_offset,
2080
                                            compunit.cu_pointer_size,
2081
                                            offset_size,
2082
                                            compunit.cu_version,
2083
                                            debug_information + unit,
2084
                                            do_loc, section);
2085
            }
2086
 
2087
          if (entry->children)
2088
            ++level;
2089
        }
2090
    }
2091
 
2092
  /* Set num_debug_info_entries here so that it can be used to check if
2093
     we need to process .debug_loc and .debug_ranges sections.  */
2094
  if ((do_loc || do_debug_loc || do_debug_ranges)
2095
      && num_debug_info_entries == 0)
2096
    num_debug_info_entries = num_units;
2097
 
2098
  if (!do_loc)
2099
    {
2100
      printf ("\n");
2101
    }
2102
 
2103
  return 1;
2104
}
2105
 
2106
/* Locate and scan the .debug_info section in the file and record the pointer
2107
   sizes and offsets for the compilation units in it.  Usually an executable
2108
   will have just one pointer size, but this is not guaranteed, and so we try
2109
   not to make any assumptions.  Returns zero upon failure, or the number of
2110
   compilation units upon success.  */
2111
 
2112
static unsigned int
2113
load_debug_info (void * file)
2114
{
2115
  /* Reset the last pointer size so that we can issue correct error
2116
     messages if we are displaying the contents of more than one section.  */
2117
  last_pointer_size = 0;
2118
  warned_about_missing_comp_units = FALSE;
2119
 
2120
  /* If we have already tried and failed to load the .debug_info
2121
     section then do not bother to repear the task.  */
2122
  if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2123
    return 0;
2124
 
2125
  /* If we already have the information there is nothing else to do.  */
2126
  if (num_debug_info_entries > 0)
2127
    return num_debug_info_entries;
2128
 
2129
  if (load_debug_section (info, file)
2130
      && process_debug_info (&debug_displays [info].section, file, 1))
2131
    return num_debug_info_entries;
2132
 
2133
  num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2134
  return 0;
2135
}
2136
 
2137
static int
2138
display_debug_lines_raw (struct dwarf_section *section,
2139
                         unsigned char *data,
2140
                         unsigned char *end)
2141
{
2142
  unsigned char *start = section->start;
2143
 
2144
  printf (_("Raw dump of debug contents of section %s:\n\n"),
2145
          section->name);
2146
 
2147
  while (data < end)
2148
    {
2149
      DWARF2_Internal_LineInfo info;
2150
      unsigned char *standard_opcodes;
2151
      unsigned char *end_of_sequence;
2152
      unsigned char *hdrptr;
2153
      unsigned long hdroff;
2154
      int initial_length_size;
2155
      int offset_size;
2156
      int i;
2157
 
2158
      hdrptr = data;
2159
      hdroff = hdrptr - start;
2160
 
2161
      /* Check the length of the block.  */
2162
      info.li_length = byte_get (hdrptr, 4);
2163
      hdrptr += 4;
2164
 
2165
      if (info.li_length == 0xffffffff)
2166
        {
2167
          /* This section is 64-bit DWARF 3.  */
2168
          info.li_length = byte_get (hdrptr, 8);
2169
          hdrptr += 8;
2170
          offset_size = 8;
2171
          initial_length_size = 12;
2172
        }
2173
      else
2174
        {
2175
          offset_size = 4;
2176
          initial_length_size = 4;
2177
        }
2178
 
2179
      if (info.li_length + initial_length_size > section->size)
2180
        {
2181
          warn
2182
            (_("The information in section %s appears to be corrupt - the section is too small\n"),
2183
             section->name);
2184
          return 0;
2185
        }
2186
 
2187
      /* Check its version number.  */
2188
      info.li_version = byte_get (hdrptr, 2);
2189
      hdrptr += 2;
2190
      if (info.li_version != 2 && info.li_version != 3)
2191
        {
2192
          warn (_("Only DWARF version 2 and 3 line info is currently supported.\n"));
2193
          return 0;
2194
        }
2195
 
2196
      info.li_prologue_length = byte_get (hdrptr, offset_size);
2197
      hdrptr += offset_size;
2198
      info.li_min_insn_length = byte_get (hdrptr, 1);
2199
      hdrptr++;
2200
      info.li_default_is_stmt = byte_get (hdrptr, 1);
2201
      hdrptr++;
2202
      info.li_line_base = byte_get (hdrptr, 1);
2203
      hdrptr++;
2204
      info.li_line_range = byte_get (hdrptr, 1);
2205
      hdrptr++;
2206
      info.li_opcode_base = byte_get (hdrptr, 1);
2207
      hdrptr++;
2208
 
2209
      /* Sign extend the line base field.  */
2210
      info.li_line_base <<= 24;
2211
      info.li_line_base >>= 24;
2212
 
2213
      printf (_("  Offset:                      0x%lx\n"), hdroff);
2214
      printf (_("  Length:                      %ld\n"), info.li_length);
2215
      printf (_("  DWARF Version:               %d\n"), info.li_version);
2216
      printf (_("  Prologue Length:             %d\n"), info.li_prologue_length);
2217
      printf (_("  Minimum Instruction Length:  %d\n"), info.li_min_insn_length);
2218
      printf (_("  Initial value of 'is_stmt':  %d\n"), info.li_default_is_stmt);
2219
      printf (_("  Line Base:                   %d\n"), info.li_line_base);
2220
      printf (_("  Line Range:                  %d\n"), info.li_line_range);
2221
      printf (_("  Opcode Base:                 %d\n"), info.li_opcode_base);
2222
 
2223
      end_of_sequence = data + info.li_length + initial_length_size;
2224
 
2225
      reset_state_machine (info.li_default_is_stmt);
2226
 
2227
      /* Display the contents of the Opcodes table.  */
2228
      standard_opcodes = hdrptr;
2229
 
2230
      printf (_("\n Opcodes:\n"));
2231
 
2232
      for (i = 1; i < info.li_opcode_base; i++)
2233
        printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
2234
 
2235
      /* Display the contents of the Directory table.  */
2236
      data = standard_opcodes + info.li_opcode_base - 1;
2237
 
2238
      if (*data == 0)
2239
        printf (_("\n The Directory Table is empty.\n"));
2240
      else
2241
        {
2242
          printf (_("\n The Directory Table:\n"));
2243
 
2244
          while (*data != 0)
2245
            {
2246
              printf (_("  %s\n"), data);
2247
 
2248
              data += strlen ((char *) data) + 1;
2249
            }
2250
        }
2251
 
2252
      /* Skip the NUL at the end of the table.  */
2253
      data++;
2254
 
2255
      /* Display the contents of the File Name table.  */
2256
      if (*data == 0)
2257
        printf (_("\n The File Name Table is empty.\n"));
2258
      else
2259
        {
2260
          printf (_("\n The File Name Table:\n"));
2261
          printf (_("  Entry\tDir\tTime\tSize\tName\n"));
2262
 
2263
          while (*data != 0)
2264
            {
2265
              unsigned char *name;
2266
              unsigned int bytes_read;
2267
 
2268
              printf (_("  %d\t"), ++state_machine_regs.last_file_entry);
2269
              name = data;
2270
 
2271
              data += strlen ((char *) data) + 1;
2272
 
2273
              printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
2274
              data += bytes_read;
2275
              printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
2276
              data += bytes_read;
2277
              printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
2278
              data += bytes_read;
2279
              printf (_("%s\n"), name);
2280
            }
2281
        }
2282
 
2283
      /* Skip the NUL at the end of the table.  */
2284
      data++;
2285
 
2286
      /* Now display the statements.  */
2287
      printf (_("\n Line Number Statements:\n"));
2288
 
2289
      while (data < end_of_sequence)
2290
        {
2291
          unsigned char op_code;
2292
          int adv;
2293
          unsigned long int uladv;
2294
          unsigned int bytes_read;
2295
 
2296
          op_code = *data++;
2297
 
2298
          if (op_code >= info.li_opcode_base)
2299
            {
2300
              op_code -= info.li_opcode_base;
2301
              uladv = (op_code / info.li_line_range) * info.li_min_insn_length;
2302
              state_machine_regs.address += uladv;
2303
              printf (_("  Special opcode %d: advance Address by %lu to 0x%lx"),
2304
                      op_code, uladv, state_machine_regs.address);
2305
              adv = (op_code % info.li_line_range) + info.li_line_base;
2306
              state_machine_regs.line += adv;
2307
              printf (_(" and Line by %d to %d\n"),
2308
                      adv, state_machine_regs.line);
2309
            }
2310
          else switch (op_code)
2311
            {
2312
            case DW_LNS_extended_op:
2313
              data += process_extended_line_op (data, info.li_default_is_stmt);
2314
              break;
2315
 
2316
            case DW_LNS_copy:
2317
              printf (_("  Copy\n"));
2318
              break;
2319
 
2320
            case DW_LNS_advance_pc:
2321
              uladv = read_leb128 (data, & bytes_read, 0);
2322
              uladv *= info.li_min_insn_length;
2323
              data += bytes_read;
2324
              state_machine_regs.address += uladv;
2325
              printf (_("  Advance PC by %lu to 0x%lx\n"), uladv,
2326
                      state_machine_regs.address);
2327
              break;
2328
 
2329
            case DW_LNS_advance_line:
2330
              adv = read_leb128 (data, & bytes_read, 1);
2331
              data += bytes_read;
2332
              state_machine_regs.line += adv;
2333
              printf (_("  Advance Line by %d to %d\n"), adv,
2334
                      state_machine_regs.line);
2335
              break;
2336
 
2337
            case DW_LNS_set_file:
2338
              adv = read_leb128 (data, & bytes_read, 0);
2339
              data += bytes_read;
2340
              printf (_("  Set File Name to entry %d in the File Name Table\n"),
2341
                      adv);
2342
              state_machine_regs.file = adv;
2343
              break;
2344
 
2345
            case DW_LNS_set_column:
2346
              uladv = read_leb128 (data, & bytes_read, 0);
2347
              data += bytes_read;
2348
              printf (_("  Set column to %lu\n"), uladv);
2349
              state_machine_regs.column = uladv;
2350
              break;
2351
 
2352
            case DW_LNS_negate_stmt:
2353
              adv = state_machine_regs.is_stmt;
2354
              adv = ! adv;
2355
              printf (_("  Set is_stmt to %d\n"), adv);
2356
              state_machine_regs.is_stmt = adv;
2357
              break;
2358
 
2359
            case DW_LNS_set_basic_block:
2360
              printf (_("  Set basic block\n"));
2361
              state_machine_regs.basic_block = 1;
2362
              break;
2363
 
2364
            case DW_LNS_const_add_pc:
2365
              uladv = (((255 - info.li_opcode_base) / info.li_line_range)
2366
                      * info.li_min_insn_length);
2367
              state_machine_regs.address += uladv;
2368
              printf (_("  Advance PC by constant %lu to 0x%lx\n"), uladv,
2369
                      state_machine_regs.address);
2370
              break;
2371
 
2372
            case DW_LNS_fixed_advance_pc:
2373
              uladv = byte_get (data, 2);
2374
              data += 2;
2375
              state_machine_regs.address += uladv;
2376
              printf (_("  Advance PC by fixed size amount %lu to 0x%lx\n"),
2377
                      uladv, state_machine_regs.address);
2378
              break;
2379
 
2380
            case DW_LNS_set_prologue_end:
2381
              printf (_("  Set prologue_end to true\n"));
2382
              break;
2383
 
2384
            case DW_LNS_set_epilogue_begin:
2385
              printf (_("  Set epilogue_begin to true\n"));
2386
              break;
2387
 
2388
            case DW_LNS_set_isa:
2389
              uladv = read_leb128 (data, & bytes_read, 0);
2390
              data += bytes_read;
2391
              printf (_("  Set ISA to %lu\n"), uladv);
2392
              break;
2393
 
2394
            default:
2395
              printf (_("  Unknown opcode %d with operands: "), op_code);
2396
 
2397
              for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2398
                {
2399
                  printf ("0x%lx%s", read_leb128 (data, &bytes_read, 0),
2400
                          i == 1 ? "" : ", ");
2401
                  data += bytes_read;
2402
                }
2403
              putchar ('\n');
2404
              break;
2405
            }
2406
        }
2407
      putchar ('\n');
2408
    }
2409
 
2410
  return 1;
2411
}
2412
 
2413
typedef struct
2414
{
2415
    unsigned char *name;
2416
    unsigned int directory_index;
2417
    unsigned int modification_date;
2418
    unsigned int length;
2419
} File_Entry;
2420
 
2421
/* Output a decoded representation of the .debug_line section.  */
2422
 
2423
static int
2424
display_debug_lines_decoded (struct dwarf_section *section,
2425
                             unsigned char *data,
2426
                             unsigned char *end)
2427
{
2428
  printf (_("Decoded dump of debug contents of section %s:\n\n"),
2429
          section->name);
2430
 
2431
  while (data < end)
2432
    {
2433
      /* This loop amounts to one iteration per compilation unit.  */
2434
      DWARF2_Internal_LineInfo info;
2435
      unsigned char *standard_opcodes;
2436
      unsigned char *end_of_sequence;
2437
      unsigned char *hdrptr;
2438
      int initial_length_size;
2439
      int offset_size;
2440
      int i;
2441
      File_Entry *file_table = NULL;
2442
      unsigned char **directory_table = NULL;
2443
 
2444
      hdrptr = data;
2445
 
2446
      /* Extract information from the Line Number Program Header.
2447
        (section 6.2.4 in the Dwarf3 doc).  */
2448
 
2449
      /* Get the length of this CU's line number information block.  */
2450
      info.li_length = byte_get (hdrptr, 4);
2451
      hdrptr += 4;
2452
 
2453
      if (info.li_length == 0xffffffff)
2454
        {
2455
          /* This section is 64-bit DWARF 3.  */
2456
          info.li_length = byte_get (hdrptr, 8);
2457
          hdrptr += 8;
2458
          offset_size = 8;
2459
          initial_length_size = 12;
2460
        }
2461
      else
2462
        {
2463
          offset_size = 4;
2464
          initial_length_size = 4;
2465
        }
2466
 
2467
      if (info.li_length + initial_length_size > section->size)
2468
        {
2469
          warn (_("The line info appears to be corrupt - "
2470
                  "the section is too small\n"));
2471
          return 0;
2472
        }
2473
 
2474
      /* Get this CU's Line Number Block version number.  */
2475
      info.li_version = byte_get (hdrptr, 2);
2476
      hdrptr += 2;
2477
      if (info.li_version != 2 && info.li_version != 3)
2478
        {
2479
          warn (_("Only DWARF version 2 and 3 line info is currently "
2480
                "supported.\n"));
2481
          return 0;
2482
        }
2483
 
2484
      info.li_prologue_length = byte_get (hdrptr, offset_size);
2485
      hdrptr += offset_size;
2486
      info.li_min_insn_length = byte_get (hdrptr, 1);
2487
      hdrptr++;
2488
      info.li_default_is_stmt = byte_get (hdrptr, 1);
2489
      hdrptr++;
2490
      info.li_line_base = byte_get (hdrptr, 1);
2491
      hdrptr++;
2492
      info.li_line_range = byte_get (hdrptr, 1);
2493
      hdrptr++;
2494
      info.li_opcode_base = byte_get (hdrptr, 1);
2495
      hdrptr++;
2496
 
2497
      /* Sign extend the line base field.  */
2498
      info.li_line_base <<= 24;
2499
      info.li_line_base >>= 24;
2500
 
2501
      /* Find the end of this CU's Line Number Information Block.  */
2502
      end_of_sequence = data + info.li_length + initial_length_size;
2503
 
2504
      reset_state_machine (info.li_default_is_stmt);
2505
 
2506
      /* Save a pointer to the contents of the Opcodes table.  */
2507
      standard_opcodes = hdrptr;
2508
 
2509
      /* Traverse the Directory table just to count entries.  */
2510
      data = standard_opcodes + info.li_opcode_base - 1;
2511
      if (*data != 0)
2512
        {
2513
          unsigned int n_directories = 0;
2514
          unsigned char *ptr_directory_table = data;
2515
          int i;
2516
 
2517
          while (*data != 0)
2518
            {
2519
              data += strlen ((char *) data) + 1;
2520
              n_directories++;
2521
            }
2522
 
2523
          /* Go through the directory table again to save the directories.  */
2524
          directory_table = (unsigned char **)
2525
              xmalloc (n_directories * sizeof (unsigned char *));
2526
 
2527
          i = 0;
2528
          while (*ptr_directory_table != 0)
2529
            {
2530
              directory_table[i] = ptr_directory_table;
2531
              ptr_directory_table += strlen ((char *) ptr_directory_table) + 1;
2532
              i++;
2533
            }
2534
        }
2535
      /* Skip the NUL at the end of the table.  */
2536
      data++;
2537
 
2538
      /* Traverse the File Name table just to count the entries.  */
2539
      if (*data != 0)
2540
        {
2541
          unsigned int n_files = 0;
2542
          unsigned char *ptr_file_name_table = data;
2543
          int i;
2544
 
2545
          while (*data != 0)
2546
            {
2547
              unsigned int bytes_read;
2548
 
2549
              /* Skip Name, directory index, last modification time and length
2550
                 of file.  */
2551
              data += strlen ((char *) data) + 1;
2552
              read_leb128 (data, & bytes_read, 0);
2553
              data += bytes_read;
2554
              read_leb128 (data, & bytes_read, 0);
2555
              data += bytes_read;
2556
              read_leb128 (data, & bytes_read, 0);
2557
              data += bytes_read;
2558
 
2559
              n_files++;
2560
            }
2561
 
2562
          /* Go through the file table again to save the strings.  */
2563
          file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
2564
 
2565
          i = 0;
2566
          while (*ptr_file_name_table != 0)
2567
            {
2568
              unsigned int bytes_read;
2569
 
2570
              file_table[i].name = ptr_file_name_table;
2571
              ptr_file_name_table += strlen ((char *) ptr_file_name_table) + 1;
2572
 
2573
              /* We are not interested in directory, time or size.  */
2574
              file_table[i].directory_index = read_leb128 (ptr_file_name_table,
2575
                                                           & bytes_read, 0);
2576
              ptr_file_name_table += bytes_read;
2577
              file_table[i].modification_date = read_leb128 (ptr_file_name_table,
2578
                                                             & bytes_read, 0);
2579
              ptr_file_name_table += bytes_read;
2580
              file_table[i].length = read_leb128 (ptr_file_name_table, & bytes_read, 0);
2581
              ptr_file_name_table += bytes_read;
2582
              i++;
2583
            }
2584
          i = 0;
2585
 
2586
          /* Print the Compilation Unit's name and a header.  */
2587
          if (directory_table == NULL)
2588
            {
2589
              printf (_("CU: %s:\n"), file_table[0].name);
2590
              printf (_("File name                            Line number    Starting address\n"));
2591
            }
2592
          else
2593
            {
2594
              if (do_wide || strlen ((char *) directory_table[0]) < 76)
2595
                {
2596
                  printf (_("CU: %s/%s:\n"), directory_table[0],
2597
                          file_table[0].name);
2598
                }
2599
              else
2600
                {
2601
                  printf (_("%s:\n"), file_table[0].name);
2602
                }
2603
              printf (_("File name                            Line number    Starting address\n"));
2604
            }
2605
        }
2606
 
2607
      /* Skip the NUL at the end of the table.  */
2608
      data++;
2609
 
2610
      /* This loop iterates through the Dwarf Line Number Program.  */
2611
      while (data < end_of_sequence)
2612
        {
2613
          unsigned char op_code;
2614
          int adv;
2615
          unsigned long int uladv;
2616
          unsigned int bytes_read;
2617
          int is_special_opcode = 0;
2618
 
2619
          op_code = *data++;
2620
 
2621
          if (op_code >= info.li_opcode_base)
2622
            {
2623
              op_code -= info.li_opcode_base;
2624
              uladv = (op_code / info.li_line_range) * info.li_min_insn_length;
2625
              state_machine_regs.address += uladv;
2626
 
2627
              adv = (op_code % info.li_line_range) + info.li_line_base;
2628
              state_machine_regs.line += adv;
2629
              is_special_opcode = 1;
2630
            }
2631
          else switch (op_code)
2632
            {
2633
            case DW_LNS_extended_op:
2634
              {
2635
                unsigned int ext_op_code_len;
2636
                unsigned int bytes_read;
2637
                unsigned char ext_op_code;
2638
                unsigned char *op_code_data = data;
2639
 
2640
                ext_op_code_len = read_leb128 (op_code_data, &bytes_read, 0);
2641
                op_code_data += bytes_read;
2642
 
2643
                if (ext_op_code_len == 0)
2644
                  {
2645
                    warn (_("badly formed extended line op encountered!\n"));
2646
                    break;
2647
                  }
2648
                ext_op_code_len += bytes_read;
2649
                ext_op_code = *op_code_data++;
2650
 
2651
                switch (ext_op_code)
2652
                  {
2653
                  case DW_LNE_end_sequence:
2654
                    reset_state_machine (info.li_default_is_stmt);
2655
                    break;
2656
                  case DW_LNE_set_address:
2657
                    state_machine_regs.address =
2658
                    byte_get (op_code_data, ext_op_code_len - bytes_read - 1);
2659
                    break;
2660
                  case DW_LNE_define_file:
2661
                    {
2662
                      unsigned int dir_index = 0;
2663
 
2664
                      ++state_machine_regs.last_file_entry;
2665
                      op_code_data += strlen ((char *) op_code_data) + 1;
2666
                      dir_index = read_leb128 (op_code_data, & bytes_read, 0);
2667
                      op_code_data += bytes_read;
2668
                      read_leb128 (op_code_data, & bytes_read, 0);
2669
                      op_code_data += bytes_read;
2670
                      read_leb128 (op_code_data, & bytes_read, 0);
2671
 
2672
                      printf (_("%s:\n"), directory_table[dir_index]);
2673
                      break;
2674
                    }
2675
                  default:
2676
                    printf (_("UNKNOWN: length %d\n"), ext_op_code_len - bytes_read);
2677
                    break;
2678
                  }
2679
                data += ext_op_code_len;
2680
                break;
2681
              }
2682
            case DW_LNS_copy:
2683
              break;
2684
 
2685
            case DW_LNS_advance_pc:
2686
              uladv = read_leb128 (data, & bytes_read, 0);
2687
              uladv *= info.li_min_insn_length;
2688
              data += bytes_read;
2689
              state_machine_regs.address += uladv;
2690
              break;
2691
 
2692
            case DW_LNS_advance_line:
2693
              adv = read_leb128 (data, & bytes_read, 1);
2694
              data += bytes_read;
2695
              state_machine_regs.line += adv;
2696
              break;
2697
 
2698
            case DW_LNS_set_file:
2699
              adv = read_leb128 (data, & bytes_read, 0);
2700
              data += bytes_read;
2701
              state_machine_regs.file = adv;
2702
              if (file_table[state_machine_regs.file - 1].directory_index == 0)
2703
                {
2704
                  /* If directory index is 0, that means current directory.  */
2705
                  printf (_("\n./%s:[++]\n"),
2706
                          file_table[state_machine_regs.file - 1].name);
2707
                }
2708
              else
2709
                {
2710
                  /* The directory index starts counting at 1.  */
2711
                  printf (_("\n%s/%s:\n"),
2712
                          directory_table[file_table[state_machine_regs.file - 1].directory_index - 1],
2713
                          file_table[state_machine_regs.file - 1].name);
2714
                }
2715
              break;
2716
 
2717
            case DW_LNS_set_column:
2718
              uladv = read_leb128 (data, & bytes_read, 0);
2719
              data += bytes_read;
2720
              state_machine_regs.column = uladv;
2721
              break;
2722
 
2723
            case DW_LNS_negate_stmt:
2724
              adv = state_machine_regs.is_stmt;
2725
              adv = ! adv;
2726
              state_machine_regs.is_stmt = adv;
2727
              break;
2728
 
2729
            case DW_LNS_set_basic_block:
2730
              state_machine_regs.basic_block = 1;
2731
              break;
2732
 
2733
            case DW_LNS_const_add_pc:
2734
              uladv = (((255 - info.li_opcode_base) / info.li_line_range)
2735
                       * info.li_min_insn_length);
2736
              state_machine_regs.address += uladv;
2737
              break;
2738
 
2739
            case DW_LNS_fixed_advance_pc:
2740
              uladv = byte_get (data, 2);
2741
              data += 2;
2742
              state_machine_regs.address += uladv;
2743
              break;
2744
 
2745
            case DW_LNS_set_prologue_end:
2746
              break;
2747
 
2748
            case DW_LNS_set_epilogue_begin:
2749
              break;
2750
 
2751
            case DW_LNS_set_isa:
2752
              uladv = read_leb128 (data, & bytes_read, 0);
2753
              data += bytes_read;
2754
              printf (_("  Set ISA to %lu\n"), uladv);
2755
              break;
2756
 
2757
            default:
2758
              printf (_("  Unknown opcode %d with operands: "), op_code);
2759
 
2760
              for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2761
                {
2762
                  printf ("0x%lx%s", read_leb128 (data, &bytes_read, 0),
2763
                          i == 1 ? "" : ", ");
2764
                  data += bytes_read;
2765
                }
2766
              putchar ('\n');
2767
              break;
2768
            }
2769
 
2770
          /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
2771
             to the DWARF address/line matrix.  */
2772
          if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
2773
              || (op_code == DW_LNS_copy))
2774
            {
2775
              const unsigned int MAX_FILENAME_LENGTH = 35;
2776
              char *fileName = (char *)file_table[state_machine_regs.file - 1].name;
2777
              char *newFileName = NULL;
2778
              size_t fileNameLength = strlen (fileName);
2779
 
2780
              if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
2781
                {
2782
                  newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
2783
                  /* Truncate file name */
2784
                  strncpy (newFileName,
2785
                           fileName + fileNameLength - MAX_FILENAME_LENGTH,
2786
                           MAX_FILENAME_LENGTH + 1);
2787
                }
2788
              else
2789
                {
2790
                  newFileName = (char *) xmalloc (fileNameLength + 1);
2791
                  strncpy (newFileName, fileName, fileNameLength + 1);
2792
                }
2793
 
2794
              if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
2795
                {
2796
                  printf (_("%-35s  %11d  %#18lx\n"), newFileName,
2797
                          state_machine_regs.line, state_machine_regs.address);
2798
                }
2799
              else
2800
                {
2801
                  printf (_("%s  %11d  %#18lx\n"), newFileName,
2802
                          state_machine_regs.line, state_machine_regs.address);
2803
                }
2804
 
2805
              if (op_code == DW_LNE_end_sequence)
2806
                printf ("\n");
2807
 
2808
              free (newFileName);
2809
            }
2810
        }
2811
      free (file_table);
2812
      file_table = NULL;
2813
      free (directory_table);
2814
      directory_table = NULL;
2815
      putchar ('\n');
2816
    }
2817
 
2818
  return 1;
2819
}
2820
 
2821
static int
2822
display_debug_lines (struct dwarf_section *section, void *file)
2823
{
2824
  unsigned char *data = section->start;
2825
  unsigned char *end = data + section->size;
2826
  int retValRaw = 1;
2827
  int retValDecoded = 1;
2828
 
2829
  if (load_debug_info (file) == 0)
2830
    {
2831
      warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
2832
            section->name);
2833
      return 0;
2834
    }
2835
 
2836
  if (do_debug_lines == 0)
2837
    do_debug_lines |= FLAG_DEBUG_LINES_RAW;
2838
 
2839
  if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
2840
    retValRaw = display_debug_lines_raw (section, data, end);
2841
 
2842
  if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
2843
    retValDecoded = display_debug_lines_decoded (section, data, end);
2844
 
2845
  if (!retValRaw || !retValDecoded)
2846
    return 0;
2847
 
2848
  return 1;
2849
}
2850
 
2851
static debug_info *
2852
find_debug_info_for_offset (unsigned long offset)
2853
{
2854
  unsigned int i;
2855
 
2856
  if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2857
    return NULL;
2858
 
2859
  for (i = 0; i < num_debug_info_entries; i++)
2860
    if (debug_information[i].cu_offset == offset)
2861
      return debug_information + i;
2862
 
2863
  return NULL;
2864
}
2865
 
2866
static int
2867
display_debug_pubnames (struct dwarf_section *section,
2868
                        void *file ATTRIBUTE_UNUSED)
2869
{
2870
  DWARF2_Internal_PubNames pubnames;
2871
  unsigned char *start = section->start;
2872
  unsigned char *end = start + section->size;
2873
 
2874
  /* It does not matter if this load fails,
2875
     we test for that later on.  */
2876
  load_debug_info (file);
2877
 
2878
  printf (_("Contents of the %s section:\n\n"), section->name);
2879
 
2880
  while (start < end)
2881
    {
2882
      unsigned char *data;
2883
      unsigned long offset;
2884
      int offset_size, initial_length_size;
2885
 
2886
      data = start;
2887
 
2888
      pubnames.pn_length = byte_get (data, 4);
2889
      data += 4;
2890
      if (pubnames.pn_length == 0xffffffff)
2891
        {
2892
          pubnames.pn_length = byte_get (data, 8);
2893
          data += 8;
2894
          offset_size = 8;
2895
          initial_length_size = 12;
2896
        }
2897
      else
2898
        {
2899
          offset_size = 4;
2900
          initial_length_size = 4;
2901
        }
2902
 
2903
      pubnames.pn_version = byte_get (data, 2);
2904
      data += 2;
2905
 
2906
      pubnames.pn_offset = byte_get (data, offset_size);
2907
      data += offset_size;
2908
 
2909
      if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
2910
          && num_debug_info_entries > 0
2911
          && find_debug_info_for_offset (pubnames.pn_offset) == NULL)
2912
        warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
2913
              pubnames.pn_offset, section->name);
2914
 
2915
      pubnames.pn_size = byte_get (data, offset_size);
2916
      data += offset_size;
2917
 
2918
      start += pubnames.pn_length + initial_length_size;
2919
 
2920
      if (pubnames.pn_version != 2 && pubnames.pn_version != 3)
2921
        {
2922
          static int warned = 0;
2923
 
2924
          if (! warned)
2925
            {
2926
              warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
2927
              warned = 1;
2928
            }
2929
 
2930
          continue;
2931
        }
2932
 
2933
      printf (_("  Length:                              %ld\n"),
2934
              pubnames.pn_length);
2935
      printf (_("  Version:                             %d\n"),
2936
              pubnames.pn_version);
2937
      printf (_("  Offset into .debug_info section:     0x%lx\n"),
2938
              pubnames.pn_offset);
2939
      printf (_("  Size of area in .debug_info section: %ld\n"),
2940
              pubnames.pn_size);
2941
 
2942
      printf (_("\n    Offset\tName\n"));
2943
 
2944
      do
2945
        {
2946
          offset = byte_get (data, offset_size);
2947
 
2948
          if (offset != 0)
2949
            {
2950
              data += offset_size;
2951
              printf ("    %-6lx\t%s\n", offset, data);
2952
              data += strlen ((char *) data) + 1;
2953
            }
2954
        }
2955
      while (offset != 0);
2956
    }
2957
 
2958
  printf ("\n");
2959
  return 1;
2960
}
2961
 
2962
static int
2963
display_debug_macinfo (struct dwarf_section *section,
2964
                       void *file ATTRIBUTE_UNUSED)
2965
{
2966
  unsigned char *start = section->start;
2967
  unsigned char *end = start + section->size;
2968
  unsigned char *curr = start;
2969
  unsigned int bytes_read;
2970
  enum dwarf_macinfo_record_type op;
2971
 
2972
  printf (_("Contents of the %s section:\n\n"), section->name);
2973
 
2974
  while (curr < end)
2975
    {
2976
      unsigned int lineno;
2977
      const char *string;
2978
 
2979
      op = (enum dwarf_macinfo_record_type) *curr;
2980
      curr++;
2981
 
2982
      switch (op)
2983
        {
2984
        case DW_MACINFO_start_file:
2985
          {
2986
            unsigned int filenum;
2987
 
2988
            lineno = read_leb128 (curr, & bytes_read, 0);
2989
            curr += bytes_read;
2990
            filenum = read_leb128 (curr, & bytes_read, 0);
2991
            curr += bytes_read;
2992
 
2993
            printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
2994
                    lineno, filenum);
2995
          }
2996
          break;
2997
 
2998
        case DW_MACINFO_end_file:
2999
          printf (_(" DW_MACINFO_end_file\n"));
3000
          break;
3001
 
3002
        case DW_MACINFO_define:
3003
          lineno = read_leb128 (curr, & bytes_read, 0);
3004
          curr += bytes_read;
3005
          string = (char *) curr;
3006
          curr += strlen (string) + 1;
3007
          printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
3008
                  lineno, string);
3009
          break;
3010
 
3011
        case DW_MACINFO_undef:
3012
          lineno = read_leb128 (curr, & bytes_read, 0);
3013
          curr += bytes_read;
3014
          string = (char *) curr;
3015
          curr += strlen (string) + 1;
3016
          printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
3017
                  lineno, string);
3018
          break;
3019
 
3020
        case DW_MACINFO_vendor_ext:
3021
          {
3022
            unsigned int constant;
3023
 
3024
            constant = read_leb128 (curr, & bytes_read, 0);
3025
            curr += bytes_read;
3026
            string = (char *) curr;
3027
            curr += strlen (string) + 1;
3028
            printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
3029
                    constant, string);
3030
          }
3031
          break;
3032
        }
3033
    }
3034
 
3035
  return 1;
3036
}
3037
 
3038
static int
3039
display_debug_abbrev (struct dwarf_section *section,
3040
                      void *file ATTRIBUTE_UNUSED)
3041
{
3042
  abbrev_entry *entry;
3043
  unsigned char *start = section->start;
3044
  unsigned char *end = start + section->size;
3045
 
3046
  printf (_("Contents of the %s section:\n\n"), section->name);
3047
 
3048
  do
3049
    {
3050
      free_abbrevs ();
3051
 
3052
      start = process_abbrev_section (start, end);
3053
 
3054
      if (first_abbrev == NULL)
3055
        continue;
3056
 
3057
      printf (_("  Number TAG\n"));
3058
 
3059
      for (entry = first_abbrev; entry; entry = entry->next)
3060
        {
3061
          abbrev_attr *attr;
3062
 
3063
          printf (_("   %ld      %s    [%s]\n"),
3064
                  entry->entry,
3065
                  get_TAG_name (entry->tag),
3066
                  entry->children ? _("has children") : _("no children"));
3067
 
3068
          for (attr = entry->first_attr; attr; attr = attr->next)
3069
            printf (_("    %-18s %s\n"),
3070
                    get_AT_name (attr->attribute),
3071
                    get_FORM_name (attr->form));
3072
        }
3073
    }
3074
  while (start);
3075
 
3076
  printf ("\n");
3077
 
3078
  return 1;
3079
}
3080
 
3081
static int
3082
display_debug_loc (struct dwarf_section *section, void *file)
3083
{
3084
  unsigned char *start = section->start;
3085
  unsigned char *section_end;
3086
  unsigned long bytes;
3087
  unsigned char *section_begin = start;
3088
  unsigned int num_loc_list = 0;
3089
  unsigned long last_offset = 0;
3090
  unsigned int first = 0;
3091
  unsigned int i;
3092
  unsigned int j;
3093
  int seen_first_offset = 0;
3094
  int use_debug_info = 1;
3095
  unsigned char *next;
3096
 
3097
  bytes = section->size;
3098
  section_end = start + bytes;
3099
 
3100
  if (bytes == 0)
3101
    {
3102
      printf (_("\nThe %s section is empty.\n"), section->name);
3103
      return 0;
3104
    }
3105
 
3106
  if (load_debug_info (file) == 0)
3107
    {
3108
      warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
3109
            section->name);
3110
      return 0;
3111
    }
3112
 
3113
  /* Check the order of location list in .debug_info section. If
3114
     offsets of location lists are in the ascending order, we can
3115
     use `debug_information' directly.  */
3116
  for (i = 0; i < num_debug_info_entries; i++)
3117
    {
3118
      unsigned int num;
3119
 
3120
      num = debug_information [i].num_loc_offsets;
3121
      num_loc_list += num;
3122
 
3123
      /* Check if we can use `debug_information' directly.  */
3124
      if (use_debug_info && num != 0)
3125
        {
3126
          if (!seen_first_offset)
3127
            {
3128
              /* This is the first location list.  */
3129
              last_offset = debug_information [i].loc_offsets [0];
3130
              first = i;
3131
              seen_first_offset = 1;
3132
              j = 1;
3133
            }
3134
          else
3135
            j = 0;
3136
 
3137
          for (; j < num; j++)
3138
            {
3139
              if (last_offset >
3140
                  debug_information [i].loc_offsets [j])
3141
                {
3142
                  use_debug_info = 0;
3143
                  break;
3144
                }
3145
              last_offset = debug_information [i].loc_offsets [j];
3146
            }
3147
        }
3148
    }
3149
 
3150
  if (!use_debug_info)
3151
    /* FIXME: Should we handle this case?  */
3152
    error (_("Location lists in .debug_info section aren't in ascending order!\n"));
3153
 
3154
  if (!seen_first_offset)
3155
    error (_("No location lists in .debug_info section!\n"));
3156
 
3157
  /* DWARF sections under Mach-O have non-zero addresses.  */
3158
  if (debug_information [first].num_loc_offsets > 0
3159
      && debug_information [first].loc_offsets [0] != section->address)
3160
    warn (_("Location lists in %s section start at 0x%lx\n"),
3161
          section->name, debug_information [first].loc_offsets [0]);
3162
 
3163
  printf (_("Contents of the %s section:\n\n"), section->name);
3164
  printf (_("    Offset   Begin    End      Expression\n"));
3165
 
3166
  seen_first_offset = 0;
3167
  for (i = first; i < num_debug_info_entries; i++)
3168
    {
3169
      dwarf_vma begin;
3170
      dwarf_vma end;
3171
      unsigned short length;
3172
      unsigned long offset;
3173
      unsigned int pointer_size;
3174
      unsigned long cu_offset;
3175
      unsigned long base_address;
3176
      int need_frame_base;
3177
      int has_frame_base;
3178
 
3179
      pointer_size = debug_information [i].pointer_size;
3180
      cu_offset = debug_information [i].cu_offset;
3181
 
3182
      for (j = 0; j < debug_information [i].num_loc_offsets; j++)
3183
        {
3184
          has_frame_base = debug_information [i].have_frame_base [j];
3185
          /* DWARF sections under Mach-O have non-zero addresses.  */
3186
          offset = debug_information [i].loc_offsets [j] - section->address;
3187
          next = section_begin + offset;
3188
          base_address = debug_information [i].base_address;
3189
 
3190
          if (!seen_first_offset)
3191
            seen_first_offset = 1;
3192
          else
3193
            {
3194
              if (start < next)
3195
                warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
3196
                      (unsigned long) (start - section_begin),
3197
                      (unsigned long) (next - section_begin));
3198
              else if (start > next)
3199
                warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
3200
                      (unsigned long) (start - section_begin),
3201
                      (unsigned long) (next - section_begin));
3202
            }
3203
          start = next;
3204
 
3205
          if (offset >= bytes)
3206
            {
3207
              warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
3208
                    offset);
3209
              continue;
3210
            }
3211
 
3212
          while (1)
3213
            {
3214
              if (start + 2 * pointer_size > section_end)
3215
                {
3216
                  warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3217
                        offset);
3218
                  break;
3219
                }
3220
 
3221
              /* Note: we use sign extension here in order to be sure that
3222
                 we can detect the -1 escape value.  Sign extension into the
3223
                 top 32 bits of a 32-bit address will not affect the values
3224
                 that we display since we always show hex values, and always
3225
                 the bottom 32-bits.  */
3226
              begin = byte_get_signed (start, pointer_size);
3227
              start += pointer_size;
3228
              end = byte_get_signed (start, pointer_size);
3229
              start += pointer_size;
3230
 
3231
              printf ("    %8.8lx ", offset);
3232
 
3233
              if (begin == 0 && end == 0)
3234
                {
3235
                  printf (_("<End of list>\n"));
3236
                  break;
3237
                }
3238
 
3239
              /* Check base address specifiers.  */
3240
              if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
3241
                {
3242
                  base_address = end;
3243
                  print_dwarf_vma (begin, pointer_size);
3244
                  print_dwarf_vma (end, pointer_size);
3245
                  printf (_("(base address)\n"));
3246
                  continue;
3247
                }
3248
 
3249
              if (start + 2 > section_end)
3250
                {
3251
                  warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3252
                        offset);
3253
                  break;
3254
                }
3255
 
3256
              length = byte_get (start, 2);
3257
              start += 2;
3258
 
3259
              if (start + length > section_end)
3260
                {
3261
                  warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3262
                        offset);
3263
                  break;
3264
                }
3265
 
3266
              print_dwarf_vma (begin + base_address, pointer_size);
3267
              print_dwarf_vma (end + base_address, pointer_size);
3268
 
3269
              putchar ('(');
3270
              need_frame_base = decode_location_expression (start,
3271
                                                            pointer_size,
3272
                                                            length,
3273
                                                            cu_offset, section);
3274
              putchar (')');
3275
 
3276
              if (need_frame_base && !has_frame_base)
3277
                printf (_(" [without DW_AT_frame_base]"));
3278
 
3279
              if (begin == end)
3280
                fputs (_(" (start == end)"), stdout);
3281
              else if (begin > end)
3282
                fputs (_(" (start > end)"), stdout);
3283
 
3284
              putchar ('\n');
3285
 
3286
              start += length;
3287
            }
3288
        }
3289
    }
3290
 
3291
  if (start < section_end)
3292
    warn (_("There are %ld unused bytes at the end of section %s\n"),
3293
          (long) (section_end - start), section->name);
3294
  putchar ('\n');
3295
  return 1;
3296
}
3297
 
3298
static int
3299
display_debug_str (struct dwarf_section *section,
3300
                   void *file ATTRIBUTE_UNUSED)
3301
{
3302
  unsigned char *start = section->start;
3303
  unsigned long bytes = section->size;
3304
  dwarf_vma addr = section->address;
3305
 
3306
  if (bytes == 0)
3307
    {
3308
      printf (_("\nThe %s section is empty.\n"), section->name);
3309
      return 0;
3310
    }
3311
 
3312
  printf (_("Contents of the %s section:\n\n"), section->name);
3313
 
3314
  while (bytes)
3315
    {
3316
      int j;
3317
      int k;
3318
      int lbytes;
3319
 
3320
      lbytes = (bytes > 16 ? 16 : bytes);
3321
 
3322
      printf ("  0x%8.8lx ", (unsigned long) addr);
3323
 
3324
      for (j = 0; j < 16; j++)
3325
        {
3326
          if (j < lbytes)
3327
            printf ("%2.2x", start[j]);
3328
          else
3329
            printf ("  ");
3330
 
3331
          if ((j & 3) == 3)
3332
            printf (" ");
3333
        }
3334
 
3335
      for (j = 0; j < lbytes; j++)
3336
        {
3337
          k = start[j];
3338
          if (k >= ' ' && k < 0x80)
3339
            printf ("%c", k);
3340
          else
3341
            printf (".");
3342
        }
3343
 
3344
      putchar ('\n');
3345
 
3346
      start += lbytes;
3347
      addr  += lbytes;
3348
      bytes -= lbytes;
3349
    }
3350
 
3351
  putchar ('\n');
3352
 
3353
  return 1;
3354
}
3355
 
3356
static int
3357
display_debug_info (struct dwarf_section *section, void *file)
3358
{
3359
  return process_debug_info (section, file, 0);
3360
}
3361
 
3362
 
3363
static int
3364
display_debug_aranges (struct dwarf_section *section,
3365
                       void *file ATTRIBUTE_UNUSED)
3366
{
3367
  unsigned char *start = section->start;
3368
  unsigned char *end = start + section->size;
3369
 
3370
  printf (_("Contents of the %s section:\n\n"), section->name);
3371
 
3372
  /* It does not matter if this load fails,
3373
     we test for that later on.  */
3374
  load_debug_info (file);
3375
 
3376
  while (start < end)
3377
    {
3378
      unsigned char *hdrptr;
3379
      DWARF2_Internal_ARange arange;
3380
      unsigned char *ranges;
3381
      dwarf_vma length;
3382
      dwarf_vma address;
3383
      unsigned char address_size;
3384
      int excess;
3385
      int offset_size;
3386
      int initial_length_size;
3387
 
3388
      hdrptr = start;
3389
 
3390
      arange.ar_length = byte_get (hdrptr, 4);
3391
      hdrptr += 4;
3392
 
3393
      if (arange.ar_length == 0xffffffff)
3394
        {
3395
          arange.ar_length = byte_get (hdrptr, 8);
3396
          hdrptr += 8;
3397
          offset_size = 8;
3398
          initial_length_size = 12;
3399
        }
3400
      else
3401
        {
3402
          offset_size = 4;
3403
          initial_length_size = 4;
3404
        }
3405
 
3406
      arange.ar_version = byte_get (hdrptr, 2);
3407
      hdrptr += 2;
3408
 
3409
      arange.ar_info_offset = byte_get (hdrptr, offset_size);
3410
      hdrptr += offset_size;
3411
 
3412
      if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
3413
          && num_debug_info_entries > 0
3414
          && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
3415
        warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
3416
              arange.ar_info_offset, section->name);
3417
 
3418
      arange.ar_pointer_size = byte_get (hdrptr, 1);
3419
      hdrptr += 1;
3420
 
3421
      arange.ar_segment_size = byte_get (hdrptr, 1);
3422
      hdrptr += 1;
3423
 
3424
      if (arange.ar_version != 2 && arange.ar_version != 3)
3425
        {
3426
          warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
3427
          break;
3428
        }
3429
 
3430
      printf (_("  Length:                   %ld\n"), arange.ar_length);
3431
      printf (_("  Version:                  %d\n"), arange.ar_version);
3432
      printf (_("  Offset into .debug_info:  0x%lx\n"), arange.ar_info_offset);
3433
      printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
3434
      printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
3435
 
3436
      address_size = arange.ar_pointer_size + arange.ar_segment_size;
3437
 
3438
      /* The DWARF spec does not require that the address size be a power
3439
         of two, but we do.  This will have to change if we ever encounter
3440
         an uneven architecture.  */
3441
      if ((address_size & (address_size - 1)) != 0)
3442
        {
3443
          warn (_("Pointer size + Segment size is not a power of two.\n"));
3444
          break;
3445
        }
3446
 
3447
      if (address_size > 4)
3448
        printf (_("\n    Address            Length\n"));
3449
      else
3450
        printf (_("\n    Address    Length\n"));
3451
 
3452
      ranges = hdrptr;
3453
 
3454
      /* Must pad to an alignment boundary that is twice the address size.  */
3455
      excess = (hdrptr - start) % (2 * address_size);
3456
      if (excess)
3457
        ranges += (2 * address_size) - excess;
3458
 
3459
      start += arange.ar_length + initial_length_size;
3460
 
3461
      while (ranges + 2 * address_size <= start)
3462
        {
3463
          address = byte_get (ranges, address_size);
3464
 
3465
          ranges += address_size;
3466
 
3467
          length  = byte_get (ranges, address_size);
3468
 
3469
          ranges += address_size;
3470
 
3471
          printf ("    ");
3472
          print_dwarf_vma (address, address_size);
3473
          print_dwarf_vma (length, address_size);
3474
          putchar ('\n');
3475
        }
3476
    }
3477
 
3478
  printf ("\n");
3479
 
3480
  return 1;
3481
}
3482
 
3483
/* Each debug_information[x].range_lists[y] gets this representation for
3484
   sorting purposes.  */
3485
 
3486
struct range_entry
3487
  {
3488
    /* The debug_information[x].range_lists[y] value.  */
3489
    unsigned long ranges_offset;
3490
 
3491
    /* Original debug_information to find parameters of the data.  */
3492
    debug_info *debug_info_p;
3493
  };
3494
 
3495
/* Sort struct range_entry in ascending order of its RANGES_OFFSET.  */
3496
 
3497
static int
3498
range_entry_compar (const void *ap, const void *bp)
3499
{
3500
  const struct range_entry *a_re = (const struct range_entry *) ap;
3501
  const struct range_entry *b_re = (const struct range_entry *) bp;
3502
  const unsigned long a = a_re->ranges_offset;
3503
  const unsigned long b = b_re->ranges_offset;
3504
 
3505
  return (a > b) - (b > a);
3506
}
3507
 
3508
static int
3509
display_debug_ranges (struct dwarf_section *section,
3510
                      void *file ATTRIBUTE_UNUSED)
3511
{
3512
  unsigned char *start = section->start;
3513
  unsigned long bytes;
3514
  unsigned char *section_begin = start;
3515
  unsigned int num_range_list, i;
3516
  struct range_entry *range_entries, *range_entry_fill;
3517
 
3518
  bytes = section->size;
3519
 
3520
  if (bytes == 0)
3521
    {
3522
      printf (_("\nThe %s section is empty.\n"), section->name);
3523
      return 0;
3524
    }
3525
 
3526
  if (load_debug_info (file) == 0)
3527
    {
3528
      warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
3529
            section->name);
3530
      return 0;
3531
    }
3532
 
3533
  num_range_list = 0;
3534
  for (i = 0; i < num_debug_info_entries; i++)
3535
    num_range_list += debug_information [i].num_range_lists;
3536
 
3537
  if (num_range_list == 0)
3538
    error (_("No range lists in .debug_info section!\n"));
3539
 
3540
  range_entries = (struct range_entry *)
3541
      xmalloc (sizeof (*range_entries) * num_range_list);
3542
  range_entry_fill = range_entries;
3543
 
3544
  for (i = 0; i < num_debug_info_entries; i++)
3545
    {
3546
      debug_info *debug_info_p = &debug_information[i];
3547
      unsigned int j;
3548
 
3549
      for (j = 0; j < debug_info_p->num_range_lists; j++)
3550
        {
3551
          range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
3552
          range_entry_fill->debug_info_p = debug_info_p;
3553
          range_entry_fill++;
3554
        }
3555
    }
3556
 
3557
  qsort (range_entries, num_range_list, sizeof (*range_entries),
3558
         range_entry_compar);
3559
 
3560
  /* DWARF sections under Mach-O have non-zero addresses.  */
3561
  if (range_entries[0].ranges_offset != section->address)
3562
    warn (_("Range lists in %s section start at 0x%lx\n"),
3563
          section->name, range_entries[0].ranges_offset);
3564
 
3565
  printf (_("Contents of the %s section:\n\n"), section->name);
3566
  printf (_("    Offset   Begin    End\n"));
3567
 
3568
  for (i = 0; i < num_range_list; i++)
3569
    {
3570
      struct range_entry *range_entry = &range_entries[i];
3571
      debug_info *debug_info_p = range_entry->debug_info_p;
3572
      unsigned int pointer_size;
3573
      unsigned long offset;
3574
      unsigned char *next;
3575
      unsigned long base_address;
3576
 
3577
      pointer_size = debug_info_p->pointer_size;
3578
 
3579
      /* DWARF sections under Mach-O have non-zero addresses.  */
3580
      offset = range_entry->ranges_offset - section->address;
3581
      next = section_begin + offset;
3582
      base_address = debug_info_p->base_address;
3583
 
3584
      if (i > 0)
3585
        {
3586
          if (start < next)
3587
            warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
3588
                  (unsigned long) (start - section_begin),
3589
                  (unsigned long) (next - section_begin), section->name);
3590
          else if (start > next)
3591
            warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
3592
                  (unsigned long) (start - section_begin),
3593
                  (unsigned long) (next - section_begin), section->name);
3594
        }
3595
      start = next;
3596
 
3597
      while (1)
3598
        {
3599
          dwarf_vma begin;
3600
          dwarf_vma end;
3601
 
3602
          /* Note: we use sign extension here in order to be sure that
3603
             we can detect the -1 escape value.  Sign extension into the
3604
             top 32 bits of a 32-bit address will not affect the values
3605
             that we display since we always show hex values, and always
3606
             the bottom 32-bits.  */
3607
          begin = byte_get_signed (start, pointer_size);
3608
          start += pointer_size;
3609
          end = byte_get_signed (start, pointer_size);
3610
          start += pointer_size;
3611
 
3612
          printf ("    %8.8lx ", offset);
3613
 
3614
          if (begin == 0 && end == 0)
3615
            {
3616
              printf (_("<End of list>\n"));
3617
              break;
3618
            }
3619
 
3620
          /* Check base address specifiers.  */
3621
          if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
3622
            {
3623
              base_address = end;
3624
              print_dwarf_vma (begin, pointer_size);
3625
              print_dwarf_vma (end, pointer_size);
3626
              printf ("(base address)\n");
3627
              continue;
3628
            }
3629
 
3630
          print_dwarf_vma (begin + base_address, pointer_size);
3631
          print_dwarf_vma (end + base_address, pointer_size);
3632
 
3633
          if (begin == end)
3634
            fputs (_("(start == end)"), stdout);
3635
          else if (begin > end)
3636
            fputs (_("(start > end)"), stdout);
3637
 
3638
          putchar ('\n');
3639
        }
3640
    }
3641
  putchar ('\n');
3642
 
3643
  free (range_entries);
3644
 
3645
  return 1;
3646
}
3647
 
3648
typedef struct Frame_Chunk
3649
{
3650
  struct Frame_Chunk *next;
3651
  unsigned char *chunk_start;
3652
  int ncols;
3653
  /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
3654
  short int *col_type;
3655
  int *col_offset;
3656
  char *augmentation;
3657
  unsigned int code_factor;
3658
  int data_factor;
3659
  unsigned long pc_begin;
3660
  unsigned long pc_range;
3661
  int cfa_reg;
3662
  int cfa_offset;
3663
  int ra;
3664
  unsigned char fde_encoding;
3665
  unsigned char cfa_exp;
3666
}
3667
Frame_Chunk;
3668
 
3669
static const char *const *dwarf_regnames;
3670
static unsigned int dwarf_regnames_count;
3671
 
3672
/* A marker for a col_type that means this column was never referenced
3673
   in the frame info.  */
3674
#define DW_CFA_unreferenced (-1)
3675
 
3676
/* Return 0 if not more space is needed, 1 if more space is needed,
3677
   -1 for invalid reg.  */
3678
 
3679
static int
3680
frame_need_space (Frame_Chunk *fc, unsigned int reg)
3681
{
3682
  int prev = fc->ncols;
3683
 
3684
  if (reg < (unsigned int) fc->ncols)
3685
    return 0;
3686
 
3687
  if (dwarf_regnames_count
3688
      && reg > dwarf_regnames_count)
3689
    return -1;
3690
 
3691
  fc->ncols = reg + 1;
3692
  fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
3693
                                          sizeof (short int));
3694
  fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
3695
 
3696
  while (prev < fc->ncols)
3697
    {
3698
      fc->col_type[prev] = DW_CFA_unreferenced;
3699
      fc->col_offset[prev] = 0;
3700
      prev++;
3701
    }
3702
  return 1;
3703
}
3704
 
3705
static const char *const dwarf_regnames_i386[] =
3706
{
3707
  "eax", "ecx", "edx", "ebx",
3708
  "esp", "ebp", "esi", "edi",
3709
  "eip", "eflags", NULL,
3710
  "st0", "st1", "st2", "st3",
3711
  "st4", "st5", "st6", "st7",
3712
  NULL, NULL,
3713
  "xmm0", "xmm1", "xmm2", "xmm3",
3714
  "xmm4", "xmm5", "xmm6", "xmm7",
3715
  "mm0", "mm1", "mm2", "mm3",
3716
  "mm4", "mm5", "mm6", "mm7",
3717
  "fcw", "fsw", "mxcsr",
3718
  "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
3719
  "tr", "ldtr"
3720
};
3721
 
3722
static const char *const dwarf_regnames_x86_64[] =
3723
{
3724
  "rax", "rdx", "rcx", "rbx",
3725
  "rsi", "rdi", "rbp", "rsp",
3726
  "r8",  "r9",  "r10", "r11",
3727
  "r12", "r13", "r14", "r15",
3728
  "rip",
3729
  "xmm0",  "xmm1",  "xmm2",  "xmm3",
3730
  "xmm4",  "xmm5",  "xmm6",  "xmm7",
3731
  "xmm8",  "xmm9",  "xmm10", "xmm11",
3732
  "xmm12", "xmm13", "xmm14", "xmm15",
3733
  "st0", "st1", "st2", "st3",
3734
  "st4", "st5", "st6", "st7",
3735
  "mm0", "mm1", "mm2", "mm3",
3736
  "mm4", "mm5", "mm6", "mm7",
3737
  "rflags",
3738
  "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
3739
  "fs.base", "gs.base", NULL, NULL,
3740
  "tr", "ldtr",
3741
  "mxcsr", "fcw", "fsw"
3742
};
3743
 
3744
void
3745
init_dwarf_regnames (unsigned int e_machine)
3746
{
3747
  switch (e_machine)
3748
    {
3749
    case EM_386:
3750
    case EM_486:
3751
      dwarf_regnames = dwarf_regnames_i386;
3752
      dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
3753
      break;
3754
 
3755
    case EM_X86_64:
3756
      dwarf_regnames = dwarf_regnames_x86_64;
3757
      dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
3758
      break;
3759
 
3760
    default:
3761
      break;
3762
    }
3763
}
3764
 
3765
static const char *
3766
regname (unsigned int regno, int row)
3767
{
3768
  static char reg[64];
3769
  if (dwarf_regnames
3770
      && regno < dwarf_regnames_count
3771
      && dwarf_regnames [regno] != NULL)
3772
    {
3773
      if (row)
3774
        return dwarf_regnames [regno];
3775
      snprintf (reg, sizeof (reg), "r%d (%s)", regno,
3776
                dwarf_regnames [regno]);
3777
    }
3778
  else
3779
    snprintf (reg, sizeof (reg), "r%d", regno);
3780
  return reg;
3781
}
3782
 
3783
static void
3784
frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
3785
{
3786
  int r;
3787
  char tmp[100];
3788
 
3789
  if (*max_regs < fc->ncols)
3790
    *max_regs = fc->ncols;
3791
 
3792
  if (*need_col_headers)
3793
    {
3794
      static const char *loc = "   LOC";
3795
 
3796
      *need_col_headers = 0;
3797
 
3798
      printf ("%-*s CFA      ", eh_addr_size * 2, loc);
3799
 
3800
      for (r = 0; r < *max_regs; r++)
3801
        if (fc->col_type[r] != DW_CFA_unreferenced)
3802
          {
3803
            if (r == fc->ra)
3804
              printf ("ra      ");
3805
            else
3806
              printf ("%-5s ", regname (r, 1));
3807
          }
3808
 
3809
      printf ("\n");
3810
    }
3811
 
3812
  printf ("%0*lx ", eh_addr_size * 2, fc->pc_begin);
3813
  if (fc->cfa_exp)
3814
    strcpy (tmp, "exp");
3815
  else
3816
    sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), fc->cfa_offset);
3817
  printf ("%-8s ", tmp);
3818
 
3819
  for (r = 0; r < fc->ncols; r++)
3820
    {
3821
      if (fc->col_type[r] != DW_CFA_unreferenced)
3822
        {
3823
          switch (fc->col_type[r])
3824
            {
3825
            case DW_CFA_undefined:
3826
              strcpy (tmp, "u");
3827
              break;
3828
            case DW_CFA_same_value:
3829
              strcpy (tmp, "s");
3830
              break;
3831
            case DW_CFA_offset:
3832
              sprintf (tmp, "c%+d", fc->col_offset[r]);
3833
              break;
3834
            case DW_CFA_val_offset:
3835
              sprintf (tmp, "v%+d", fc->col_offset[r]);
3836
              break;
3837
            case DW_CFA_register:
3838
              sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
3839
              break;
3840
            case DW_CFA_expression:
3841
              strcpy (tmp, "exp");
3842
              break;
3843
            case DW_CFA_val_expression:
3844
              strcpy (tmp, "vexp");
3845
              break;
3846
            default:
3847
              strcpy (tmp, "n/a");
3848
              break;
3849
            }
3850
          printf ("%-5s ", tmp);
3851
        }
3852
    }
3853
  printf ("\n");
3854
}
3855
 
3856
#define GET(N)  byte_get (start, N); start += N
3857
#define LEB()   read_leb128 (start, & length_return, 0); start += length_return
3858
#define SLEB()  read_leb128 (start, & length_return, 1); start += length_return
3859
 
3860
static int
3861
display_debug_frames (struct dwarf_section *section,
3862
                      void *file ATTRIBUTE_UNUSED)
3863
{
3864
  unsigned char *start = section->start;
3865
  unsigned char *end = start + section->size;
3866
  unsigned char *section_start = start;
3867
  Frame_Chunk *chunks = 0;
3868
  Frame_Chunk *remembered_state = 0;
3869
  Frame_Chunk *rs;
3870
  int is_eh = strcmp (section->name, ".eh_frame") == 0;
3871
  unsigned int length_return;
3872
  int max_regs = 0;
3873
  const char *bad_reg = _("bad register: ");
3874
 
3875
  printf (_("Contents of the %s section:\n"), section->name);
3876
 
3877
  while (start < end)
3878
    {
3879
      unsigned char *saved_start;
3880
      unsigned char *block_end;
3881
      unsigned long length;
3882
      unsigned long cie_id;
3883
      Frame_Chunk *fc;
3884
      Frame_Chunk *cie;
3885
      int need_col_headers = 1;
3886
      unsigned char *augmentation_data = NULL;
3887
      unsigned long augmentation_data_len = 0;
3888
      int encoded_ptr_size = eh_addr_size;
3889
      int offset_size;
3890
      int initial_length_size;
3891
 
3892
      saved_start = start;
3893
      length = byte_get (start, 4); start += 4;
3894
 
3895
      if (length == 0)
3896
        {
3897
          printf ("\n%08lx ZERO terminator\n\n",
3898
                    (unsigned long)(saved_start - section_start));
3899
          continue;
3900
        }
3901
 
3902
      if (length == 0xffffffff)
3903
        {
3904
          length = byte_get (start, 8);
3905
          start += 8;
3906
          offset_size = 8;
3907
          initial_length_size = 12;
3908
        }
3909
      else
3910
        {
3911
          offset_size = 4;
3912
          initial_length_size = 4;
3913
        }
3914
 
3915
      block_end = saved_start + length + initial_length_size;
3916
      if (block_end > end)
3917
        {
3918
          warn ("Invalid length %#08lx in FDE at %#08lx\n",
3919
                length, (unsigned long)(saved_start - section_start));
3920
          block_end = end;
3921
        }
3922
      cie_id = byte_get (start, offset_size); start += offset_size;
3923
 
3924
      if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
3925
        {
3926
          int version;
3927
 
3928
          fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
3929
          memset (fc, 0, sizeof (Frame_Chunk));
3930
 
3931
          fc->next = chunks;
3932
          chunks = fc;
3933
          fc->chunk_start = saved_start;
3934
          fc->ncols = 0;
3935
          fc->col_type = (short int *) xmalloc (sizeof (short int));
3936
          fc->col_offset = (int *) xmalloc (sizeof (int));
3937
          frame_need_space (fc, max_regs - 1);
3938
 
3939
          version = *start++;
3940
 
3941
          fc->augmentation = (char *) start;
3942
          start = (unsigned char *) strchr ((char *) start, '\0') + 1;
3943
 
3944
          if (fc->augmentation[0] == 'z')
3945
            {
3946
              fc->code_factor = LEB ();
3947
              fc->data_factor = SLEB ();
3948
              if (version == 1)
3949
                {
3950
                  fc->ra = GET (1);
3951
                }
3952
              else
3953
                {
3954
                  fc->ra = LEB ();
3955
                }
3956
              augmentation_data_len = LEB ();
3957
              augmentation_data = start;
3958
              start += augmentation_data_len;
3959
            }
3960
          else if (strcmp (fc->augmentation, "eh") == 0)
3961
            {
3962
              start += eh_addr_size;
3963
              fc->code_factor = LEB ();
3964
              fc->data_factor = SLEB ();
3965
              if (version == 1)
3966
                {
3967
                  fc->ra = GET (1);
3968
                }
3969
              else
3970
                {
3971
                  fc->ra = LEB ();
3972
                }
3973
            }
3974
          else
3975
            {
3976
              fc->code_factor = LEB ();
3977
              fc->data_factor = SLEB ();
3978
              if (version == 1)
3979
                {
3980
                  fc->ra = GET (1);
3981
                }
3982
              else
3983
                {
3984
                  fc->ra = LEB ();
3985
                }
3986
            }
3987
          cie = fc;
3988
 
3989
          if (do_debug_frames_interp)
3990
            printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
3991
                    (unsigned long)(saved_start - section_start), length, cie_id,
3992
                    fc->augmentation, fc->code_factor, fc->data_factor,
3993
                    fc->ra);
3994
          else
3995
            {
3996
              printf ("\n%08lx %08lx %08lx CIE\n",
3997
                      (unsigned long)(saved_start - section_start), length, cie_id);
3998
              printf ("  Version:               %d\n", version);
3999
              printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
4000
              printf ("  Code alignment factor: %u\n", fc->code_factor);
4001
              printf ("  Data alignment factor: %d\n", fc->data_factor);
4002
              printf ("  Return address column: %d\n", fc->ra);
4003
 
4004
              if (augmentation_data_len)
4005
                {
4006
                  unsigned long i;
4007
                  printf ("  Augmentation data:    ");
4008
                  for (i = 0; i < augmentation_data_len; ++i)
4009
                    printf (" %02x", augmentation_data[i]);
4010
                  putchar ('\n');
4011
                }
4012
              putchar ('\n');
4013
            }
4014
 
4015
          if (augmentation_data_len)
4016
            {
4017
              unsigned char *p, *q;
4018
              p = (unsigned char *) fc->augmentation + 1;
4019
              q = augmentation_data;
4020
 
4021
              while (1)
4022
                {
4023
                  if (*p == 'L')
4024
                    q++;
4025
                  else if (*p == 'P')
4026
                    q += 1 + size_of_encoded_value (*q);
4027
                  else if (*p == 'R')
4028
                    fc->fde_encoding = *q++;
4029
                  else
4030
                    break;
4031
                  p++;
4032
                }
4033
 
4034
              if (fc->fde_encoding)
4035
                encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
4036
            }
4037
 
4038
          frame_need_space (fc, fc->ra);
4039
        }
4040
      else
4041
        {
4042
          unsigned char *look_for;
4043
          static Frame_Chunk fde_fc;
4044
 
4045
          fc = & fde_fc;
4046
          memset (fc, 0, sizeof (Frame_Chunk));
4047
 
4048
          look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
4049
 
4050
          for (cie = chunks; cie ; cie = cie->next)
4051
            if (cie->chunk_start == look_for)
4052
              break;
4053
 
4054
          if (!cie)
4055
            {
4056
              warn ("Invalid CIE pointer %#08lx in FDE at %#08lx\n",
4057
                    cie_id, (unsigned long)(saved_start - section_start));
4058
              fc->ncols = 0;
4059
              fc->col_type = (short int *) xmalloc (sizeof (short int));
4060
              fc->col_offset = (int *) xmalloc (sizeof (int));
4061
              frame_need_space (fc, max_regs - 1);
4062
              cie = fc;
4063
              fc->augmentation = "";
4064
              fc->fde_encoding = 0;
4065
            }
4066
          else
4067
            {
4068
              fc->ncols = cie->ncols;
4069
              fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
4070
              fc->col_offset =  (int *) xcmalloc (fc->ncols, sizeof (int));
4071
              memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
4072
              memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
4073
              fc->augmentation = cie->augmentation;
4074
              fc->code_factor = cie->code_factor;
4075
              fc->data_factor = cie->data_factor;
4076
              fc->cfa_reg = cie->cfa_reg;
4077
              fc->cfa_offset = cie->cfa_offset;
4078
              fc->ra = cie->ra;
4079
              frame_need_space (fc, max_regs - 1);
4080
              fc->fde_encoding = cie->fde_encoding;
4081
            }
4082
 
4083
          if (fc->fde_encoding)
4084
            encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
4085
 
4086
          fc->pc_begin = get_encoded_value (start, fc->fde_encoding);
4087
          if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
4088
            fc->pc_begin += section->address + (start - section_start);
4089
          start += encoded_ptr_size;
4090
          fc->pc_range = byte_get (start, encoded_ptr_size);
4091
          start += encoded_ptr_size;
4092
 
4093
          if (cie->augmentation[0] == 'z')
4094
            {
4095
              augmentation_data_len = LEB ();
4096
              augmentation_data = start;
4097
              start += augmentation_data_len;
4098
            }
4099
 
4100
          printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
4101
                  (unsigned long)(saved_start - section_start), length, cie_id,
4102
                  (unsigned long)(cie->chunk_start - section_start),
4103
                  fc->pc_begin, fc->pc_begin + fc->pc_range);
4104
          if (! do_debug_frames_interp && augmentation_data_len)
4105
            {
4106
              unsigned long i;
4107
 
4108
              printf ("  Augmentation data:    ");
4109
              for (i = 0; i < augmentation_data_len; ++i)
4110
                printf (" %02x", augmentation_data[i]);
4111
              putchar ('\n');
4112
              putchar ('\n');
4113
            }
4114
        }
4115
 
4116
      /* At this point, fc is the current chunk, cie (if any) is set, and
4117
         we're about to interpret instructions for the chunk.  */
4118
      /* ??? At present we need to do this always, since this sizes the
4119
         fc->col_type and fc->col_offset arrays, which we write into always.
4120
         We should probably split the interpreted and non-interpreted bits
4121
         into two different routines, since there's so much that doesn't
4122
         really overlap between them.  */
4123
      if (1 || do_debug_frames_interp)
4124
        {
4125
          /* Start by making a pass over the chunk, allocating storage
4126
             and taking note of what registers are used.  */
4127
          unsigned char *tmp = start;
4128
 
4129
          while (start < block_end)
4130
            {
4131
              unsigned op, opa;
4132
              unsigned long reg, tmp;
4133
 
4134
              op = *start++;
4135
              opa = op & 0x3f;
4136
              if (op & 0xc0)
4137
                op &= 0xc0;
4138
 
4139
              /* Warning: if you add any more cases to this switch, be
4140
                 sure to add them to the corresponding switch below.  */
4141
              switch (op)
4142
                {
4143
                case DW_CFA_advance_loc:
4144
                  break;
4145
                case DW_CFA_offset:
4146
                  LEB ();
4147
                  if (frame_need_space (fc, opa) >= 0)
4148
                    fc->col_type[opa] = DW_CFA_undefined;
4149
                  break;
4150
                case DW_CFA_restore:
4151
                  if (frame_need_space (fc, opa) >= 0)
4152
                    fc->col_type[opa] = DW_CFA_undefined;
4153
                  break;
4154
                case DW_CFA_set_loc:
4155
                  start += encoded_ptr_size;
4156
                  break;
4157
                case DW_CFA_advance_loc1:
4158
                  start += 1;
4159
                  break;
4160
                case DW_CFA_advance_loc2:
4161
                  start += 2;
4162
                  break;
4163
                case DW_CFA_advance_loc4:
4164
                  start += 4;
4165
                  break;
4166
                case DW_CFA_offset_extended:
4167
                case DW_CFA_val_offset:
4168
                  reg = LEB (); LEB ();
4169
                  if (frame_need_space (fc, reg) >= 0)
4170
                    fc->col_type[reg] = DW_CFA_undefined;
4171
                  break;
4172
                case DW_CFA_restore_extended:
4173
                  reg = LEB ();
4174
                  frame_need_space (fc, reg);
4175
                  if (frame_need_space (fc, reg) >= 0)
4176
                    fc->col_type[reg] = DW_CFA_undefined;
4177
                  break;
4178
                case DW_CFA_undefined:
4179
                  reg = LEB ();
4180
                  if (frame_need_space (fc, reg) >= 0)
4181
                    fc->col_type[reg] = DW_CFA_undefined;
4182
                  break;
4183
                case DW_CFA_same_value:
4184
                  reg = LEB ();
4185
                  if (frame_need_space (fc, reg) >= 0)
4186
                    fc->col_type[reg] = DW_CFA_undefined;
4187
                  break;
4188
                case DW_CFA_register:
4189
                  reg = LEB (); LEB ();
4190
                  if (frame_need_space (fc, reg) >= 0)
4191
                    fc->col_type[reg] = DW_CFA_undefined;
4192
                  break;
4193
                case DW_CFA_def_cfa:
4194
                  LEB (); LEB ();
4195
                  break;
4196
                case DW_CFA_def_cfa_register:
4197
                  LEB ();
4198
                  break;
4199
                case DW_CFA_def_cfa_offset:
4200
                  LEB ();
4201
                  break;
4202
                case DW_CFA_def_cfa_expression:
4203
                  tmp = LEB ();
4204
                  start += tmp;
4205
                  break;
4206
                case DW_CFA_expression:
4207
                case DW_CFA_val_expression:
4208
                  reg = LEB ();
4209
                  tmp = LEB ();
4210
                  start += tmp;
4211
                  if (frame_need_space (fc, reg) >= 0)
4212
                    fc->col_type[reg] = DW_CFA_undefined;
4213
                  break;
4214
                case DW_CFA_offset_extended_sf:
4215
                case DW_CFA_val_offset_sf:
4216
                  reg = LEB (); SLEB ();
4217
                  if (frame_need_space (fc, reg) >= 0)
4218
                    fc->col_type[reg] = DW_CFA_undefined;
4219
                  break;
4220
                case DW_CFA_def_cfa_sf:
4221
                  LEB (); SLEB ();
4222
                  break;
4223
                case DW_CFA_def_cfa_offset_sf:
4224
                  SLEB ();
4225
                  break;
4226
                case DW_CFA_MIPS_advance_loc8:
4227
                  start += 8;
4228
                  break;
4229
                case DW_CFA_GNU_args_size:
4230
                  LEB ();
4231
                  break;
4232
                case DW_CFA_GNU_negative_offset_extended:
4233
                  reg = LEB (); LEB ();
4234
                  if (frame_need_space (fc, reg) >= 0)
4235
                    fc->col_type[reg] = DW_CFA_undefined;
4236
                  break;
4237
                default:
4238
                  break;
4239
                }
4240
            }
4241
          start = tmp;
4242
        }
4243
 
4244
      /* Now we know what registers are used, make a second pass over
4245
         the chunk, this time actually printing out the info.  */
4246
 
4247
      while (start < block_end)
4248
        {
4249
          unsigned op, opa;
4250
          unsigned long ul, reg, roffs;
4251
          long l, ofs;
4252
          dwarf_vma vma;
4253
          const char *reg_prefix = "";
4254
 
4255
          op = *start++;
4256
          opa = op & 0x3f;
4257
          if (op & 0xc0)
4258
            op &= 0xc0;
4259
 
4260
          /* Warning: if you add any more cases to this switch, be
4261
             sure to add them to the corresponding switch above.  */
4262
          switch (op)
4263
            {
4264
            case DW_CFA_advance_loc:
4265
              if (do_debug_frames_interp)
4266
                frame_display_row (fc, &need_col_headers, &max_regs);
4267
              else
4268
                printf ("  DW_CFA_advance_loc: %d to %08lx\n",
4269
                        opa * fc->code_factor,
4270
                        fc->pc_begin + opa * fc->code_factor);
4271
              fc->pc_begin += opa * fc->code_factor;
4272
              break;
4273
 
4274
            case DW_CFA_offset:
4275
              roffs = LEB ();
4276
              if (opa >= (unsigned int) fc->ncols)
4277
                reg_prefix = bad_reg;
4278
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4279
                printf ("  DW_CFA_offset: %s%s at cfa%+ld\n",
4280
                        reg_prefix, regname (opa, 0),
4281
                        roffs * fc->data_factor);
4282
              if (*reg_prefix == '\0')
4283
                {
4284
                  fc->col_type[opa] = DW_CFA_offset;
4285
                  fc->col_offset[opa] = roffs * fc->data_factor;
4286
                }
4287
              break;
4288
 
4289
            case DW_CFA_restore:
4290
              if (opa >= (unsigned int) cie->ncols
4291
                  || opa >= (unsigned int) fc->ncols)
4292
                reg_prefix = bad_reg;
4293
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4294
                printf ("  DW_CFA_restore: %s%s\n",
4295
                        reg_prefix, regname (opa, 0));
4296
              if (*reg_prefix == '\0')
4297
                {
4298
                  fc->col_type[opa] = cie->col_type[opa];
4299
                  fc->col_offset[opa] = cie->col_offset[opa];
4300
                }
4301
              break;
4302
 
4303
            case DW_CFA_set_loc:
4304
              vma = get_encoded_value (start, fc->fde_encoding);
4305
              if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
4306
                vma += section->address + (start - section_start);
4307
              start += encoded_ptr_size;
4308
              if (do_debug_frames_interp)
4309
                frame_display_row (fc, &need_col_headers, &max_regs);
4310
              else
4311
                printf ("  DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
4312
              fc->pc_begin = vma;
4313
              break;
4314
 
4315
            case DW_CFA_advance_loc1:
4316
              ofs = byte_get (start, 1); start += 1;
4317
              if (do_debug_frames_interp)
4318
                frame_display_row (fc, &need_col_headers, &max_regs);
4319
              else
4320
                printf ("  DW_CFA_advance_loc1: %ld to %08lx\n",
4321
                        ofs * fc->code_factor,
4322
                        fc->pc_begin + ofs * fc->code_factor);
4323
              fc->pc_begin += ofs * fc->code_factor;
4324
              break;
4325
 
4326
            case DW_CFA_advance_loc2:
4327
              ofs = byte_get (start, 2); start += 2;
4328
              if (do_debug_frames_interp)
4329
                frame_display_row (fc, &need_col_headers, &max_regs);
4330
              else
4331
                printf ("  DW_CFA_advance_loc2: %ld to %08lx\n",
4332
                        ofs * fc->code_factor,
4333
                        fc->pc_begin + ofs * fc->code_factor);
4334
              fc->pc_begin += ofs * fc->code_factor;
4335
              break;
4336
 
4337
            case DW_CFA_advance_loc4:
4338
              ofs = byte_get (start, 4); start += 4;
4339
              if (do_debug_frames_interp)
4340
                frame_display_row (fc, &need_col_headers, &max_regs);
4341
              else
4342
                printf ("  DW_CFA_advance_loc4: %ld to %08lx\n",
4343
                        ofs * fc->code_factor,
4344
                        fc->pc_begin + ofs * fc->code_factor);
4345
              fc->pc_begin += ofs * fc->code_factor;
4346
              break;
4347
 
4348
            case DW_CFA_offset_extended:
4349
              reg = LEB ();
4350
              roffs = LEB ();
4351
              if (reg >= (unsigned int) fc->ncols)
4352
                reg_prefix = bad_reg;
4353
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4354
                printf ("  DW_CFA_offset_extended: %s%s at cfa%+ld\n",
4355
                        reg_prefix, regname (reg, 0),
4356
                        roffs * fc->data_factor);
4357
              if (*reg_prefix == '\0')
4358
                {
4359
                  fc->col_type[reg] = DW_CFA_offset;
4360
                  fc->col_offset[reg] = roffs * fc->data_factor;
4361
                }
4362
              break;
4363
 
4364
            case DW_CFA_val_offset:
4365
              reg = LEB ();
4366
              roffs = LEB ();
4367
              if (reg >= (unsigned int) fc->ncols)
4368
                reg_prefix = bad_reg;
4369
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4370
                printf ("  DW_CFA_val_offset: %s%s at cfa%+ld\n",
4371
                        reg_prefix, regname (reg, 0),
4372
                        roffs * fc->data_factor);
4373
              if (*reg_prefix == '\0')
4374
                {
4375
                  fc->col_type[reg] = DW_CFA_val_offset;
4376
                  fc->col_offset[reg] = roffs * fc->data_factor;
4377
                }
4378
              break;
4379
 
4380
            case DW_CFA_restore_extended:
4381
              reg = LEB ();
4382
              if (reg >= (unsigned int) cie->ncols
4383
                  || reg >= (unsigned int) fc->ncols)
4384
                reg_prefix = bad_reg;
4385
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4386
                printf ("  DW_CFA_restore_extended: %s%s\n",
4387
                        reg_prefix, regname (reg, 0));
4388
              if (*reg_prefix == '\0')
4389
                {
4390
                  fc->col_type[reg] = cie->col_type[reg];
4391
                  fc->col_offset[reg] = cie->col_offset[reg];
4392
                }
4393
              break;
4394
 
4395
            case DW_CFA_undefined:
4396
              reg = LEB ();
4397
              if (reg >= (unsigned int) fc->ncols)
4398
                reg_prefix = bad_reg;
4399
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4400
                printf ("  DW_CFA_undefined: %s%s\n",
4401
                        reg_prefix, regname (reg, 0));
4402
              if (*reg_prefix == '\0')
4403
                {
4404
                  fc->col_type[reg] = DW_CFA_undefined;
4405
                  fc->col_offset[reg] = 0;
4406
                }
4407
              break;
4408
 
4409
            case DW_CFA_same_value:
4410
              reg = LEB ();
4411
              if (reg >= (unsigned int) fc->ncols)
4412
                reg_prefix = bad_reg;
4413
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4414
                printf ("  DW_CFA_same_value: %s%s\n",
4415
                        reg_prefix, regname (reg, 0));
4416
              if (*reg_prefix == '\0')
4417
                {
4418
                  fc->col_type[reg] = DW_CFA_same_value;
4419
                  fc->col_offset[reg] = 0;
4420
                }
4421
              break;
4422
 
4423
            case DW_CFA_register:
4424
              reg = LEB ();
4425
              roffs = LEB ();
4426
              if (reg >= (unsigned int) fc->ncols)
4427
                reg_prefix = bad_reg;
4428
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4429
                {
4430
                  printf ("  DW_CFA_register: %s%s in ",
4431
                          reg_prefix, regname (reg, 0));
4432
                  puts (regname (roffs, 0));
4433
                }
4434
              if (*reg_prefix == '\0')
4435
                {
4436
                  fc->col_type[reg] = DW_CFA_register;
4437
                  fc->col_offset[reg] = roffs;
4438
                }
4439
              break;
4440
 
4441
            case DW_CFA_remember_state:
4442
              if (! do_debug_frames_interp)
4443
                printf ("  DW_CFA_remember_state\n");
4444
              rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
4445
              rs->ncols = fc->ncols;
4446
              rs->col_type = (short int *) xcmalloc (rs->ncols,
4447
                                                     sizeof (short int));
4448
              rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (int));
4449
              memcpy (rs->col_type, fc->col_type, rs->ncols);
4450
              memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
4451
              rs->next = remembered_state;
4452
              remembered_state = rs;
4453
              break;
4454
 
4455
            case DW_CFA_restore_state:
4456
              if (! do_debug_frames_interp)
4457
                printf ("  DW_CFA_restore_state\n");
4458
              rs = remembered_state;
4459
              if (rs)
4460
                {
4461
                  remembered_state = rs->next;
4462
                  frame_need_space (fc, rs->ncols - 1);
4463
                  memcpy (fc->col_type, rs->col_type, rs->ncols);
4464
                  memcpy (fc->col_offset, rs->col_offset,
4465
                          rs->ncols * sizeof (int));
4466
                  free (rs->col_type);
4467
                  free (rs->col_offset);
4468
                  free (rs);
4469
                }
4470
              else if (do_debug_frames_interp)
4471
                printf ("Mismatched DW_CFA_restore_state\n");
4472
              break;
4473
 
4474
            case DW_CFA_def_cfa:
4475
              fc->cfa_reg = LEB ();
4476
              fc->cfa_offset = LEB ();
4477
              fc->cfa_exp = 0;
4478
              if (! do_debug_frames_interp)
4479
                printf ("  DW_CFA_def_cfa: %s ofs %d\n",
4480
                        regname (fc->cfa_reg, 0), fc->cfa_offset);
4481
              break;
4482
 
4483
            case DW_CFA_def_cfa_register:
4484
              fc->cfa_reg = LEB ();
4485
              fc->cfa_exp = 0;
4486
              if (! do_debug_frames_interp)
4487
                printf ("  DW_CFA_def_cfa_register: %s\n",
4488
                        regname (fc->cfa_reg, 0));
4489
              break;
4490
 
4491
            case DW_CFA_def_cfa_offset:
4492
              fc->cfa_offset = LEB ();
4493
              if (! do_debug_frames_interp)
4494
                printf ("  DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
4495
              break;
4496
 
4497
            case DW_CFA_nop:
4498
              if (! do_debug_frames_interp)
4499
                printf ("  DW_CFA_nop\n");
4500
              break;
4501
 
4502
            case DW_CFA_def_cfa_expression:
4503
              ul = LEB ();
4504
              if (! do_debug_frames_interp)
4505
                {
4506
                  printf ("  DW_CFA_def_cfa_expression (");
4507
                  decode_location_expression (start, eh_addr_size, ul, 0,
4508
                                              section);
4509
                  printf (")\n");
4510
                }
4511
              fc->cfa_exp = 1;
4512
              start += ul;
4513
              break;
4514
 
4515
            case DW_CFA_expression:
4516
              reg = LEB ();
4517
              ul = LEB ();
4518
              if (reg >= (unsigned int) fc->ncols)
4519
                reg_prefix = bad_reg;
4520
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4521
                {
4522
                  printf ("  DW_CFA_expression: %s%s (",
4523
                          reg_prefix, regname (reg, 0));
4524
                  decode_location_expression (start, eh_addr_size,
4525
                                              ul, 0, section);
4526
                  printf (")\n");
4527
                }
4528
              if (*reg_prefix == '\0')
4529
                fc->col_type[reg] = DW_CFA_expression;
4530
              start += ul;
4531
              break;
4532
 
4533
            case DW_CFA_val_expression:
4534
              reg = LEB ();
4535
              ul = LEB ();
4536
              if (reg >= (unsigned int) fc->ncols)
4537
                reg_prefix = bad_reg;
4538
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4539
                {
4540
                  printf ("  DW_CFA_val_expression: %s%s (",
4541
                          reg_prefix, regname (reg, 0));
4542
                  decode_location_expression (start, eh_addr_size, ul, 0,
4543
                                              section);
4544
                  printf (")\n");
4545
                }
4546
              if (*reg_prefix == '\0')
4547
                fc->col_type[reg] = DW_CFA_val_expression;
4548
              start += ul;
4549
              break;
4550
 
4551
            case DW_CFA_offset_extended_sf:
4552
              reg = LEB ();
4553
              l = SLEB ();
4554
              if (frame_need_space (fc, reg) < 0)
4555
                reg_prefix = bad_reg;
4556
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4557
                printf ("  DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
4558
                        reg_prefix, regname (reg, 0),
4559
                        l * fc->data_factor);
4560
              if (*reg_prefix == '\0')
4561
                {
4562
                  fc->col_type[reg] = DW_CFA_offset;
4563
                  fc->col_offset[reg] = l * fc->data_factor;
4564
                }
4565
              break;
4566
 
4567
            case DW_CFA_val_offset_sf:
4568
              reg = LEB ();
4569
              l = SLEB ();
4570
              if (frame_need_space (fc, reg) < 0)
4571
                reg_prefix = bad_reg;
4572
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4573
                printf ("  DW_CFA_val_offset_sf: %s%s at cfa%+ld\n",
4574
                        reg_prefix, regname (reg, 0),
4575
                        l * fc->data_factor);
4576
              if (*reg_prefix == '\0')
4577
                {
4578
                  fc->col_type[reg] = DW_CFA_val_offset;
4579
                  fc->col_offset[reg] = l * fc->data_factor;
4580
                }
4581
              break;
4582
 
4583
            case DW_CFA_def_cfa_sf:
4584
              fc->cfa_reg = LEB ();
4585
              fc->cfa_offset = SLEB ();
4586
              fc->cfa_offset = fc->cfa_offset * fc->data_factor;
4587
              fc->cfa_exp = 0;
4588
              if (! do_debug_frames_interp)
4589
                printf ("  DW_CFA_def_cfa_sf: %s ofs %d\n",
4590
                        regname (fc->cfa_reg, 0), fc->cfa_offset);
4591
              break;
4592
 
4593
            case DW_CFA_def_cfa_offset_sf:
4594
              fc->cfa_offset = SLEB ();
4595
              fc->cfa_offset = fc->cfa_offset * fc->data_factor;
4596
              if (! do_debug_frames_interp)
4597
                printf ("  DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
4598
              break;
4599
 
4600
            case DW_CFA_MIPS_advance_loc8:
4601
              ofs = byte_get (start, 8); start += 8;
4602
              if (do_debug_frames_interp)
4603
                frame_display_row (fc, &need_col_headers, &max_regs);
4604
              else
4605
                printf ("  DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
4606
                        ofs * fc->code_factor,
4607
                        fc->pc_begin + ofs * fc->code_factor);
4608
              fc->pc_begin += ofs * fc->code_factor;
4609
              break;
4610
 
4611
            case DW_CFA_GNU_window_save:
4612
              if (! do_debug_frames_interp)
4613
                printf ("  DW_CFA_GNU_window_save\n");
4614
              break;
4615
 
4616
            case DW_CFA_GNU_args_size:
4617
              ul = LEB ();
4618
              if (! do_debug_frames_interp)
4619
                printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
4620
              break;
4621
 
4622
            case DW_CFA_GNU_negative_offset_extended:
4623
              reg = LEB ();
4624
              l = - LEB ();
4625
              if (frame_need_space (fc, reg) < 0)
4626
                reg_prefix = bad_reg;
4627
              if (! do_debug_frames_interp || *reg_prefix != '\0')
4628
                printf ("  DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
4629
                        reg_prefix, regname (reg, 0),
4630
                        l * fc->data_factor);
4631
              if (*reg_prefix == '\0')
4632
                {
4633
                  fc->col_type[reg] = DW_CFA_offset;
4634
                  fc->col_offset[reg] = l * fc->data_factor;
4635
                }
4636
              break;
4637
 
4638
            default:
4639
              if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
4640
                printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
4641
              else
4642
                warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
4643
              start = block_end;
4644
            }
4645
        }
4646
 
4647
      if (do_debug_frames_interp)
4648
        frame_display_row (fc, &need_col_headers, &max_regs);
4649
 
4650
      start = block_end;
4651
    }
4652
 
4653
  printf ("\n");
4654
 
4655
  return 1;
4656
}
4657
 
4658
#undef GET
4659
#undef LEB
4660
#undef SLEB
4661
 
4662
static int
4663
display_debug_not_supported (struct dwarf_section *section,
4664
                             void *file ATTRIBUTE_UNUSED)
4665
{
4666
  printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
4667
            section->name);
4668
 
4669
  return 1;
4670
}
4671
 
4672
void *
4673
cmalloc (size_t nmemb, size_t size)
4674
{
4675
  /* Check for overflow.  */
4676
  if (nmemb >= ~(size_t) 0 / size)
4677
    return NULL;
4678
  else
4679
    return malloc (nmemb * size);
4680
}
4681
 
4682
void *
4683
xcmalloc (size_t nmemb, size_t size)
4684
{
4685
  /* Check for overflow.  */
4686
  if (nmemb >= ~(size_t) 0 / size)
4687
    return NULL;
4688
  else
4689
    return xmalloc (nmemb * size);
4690
}
4691
 
4692
void *
4693
xcrealloc (void *ptr, size_t nmemb, size_t size)
4694
{
4695
  /* Check for overflow.  */
4696
  if (nmemb >= ~(size_t) 0 / size)
4697
    return NULL;
4698
  else
4699
    return xrealloc (ptr, nmemb * size);
4700
}
4701
 
4702
void
4703
error (const char *message, ...)
4704
{
4705
  va_list args;
4706
 
4707
  va_start (args, message);
4708
  fprintf (stderr, _("%s: Error: "), program_name);
4709
  vfprintf (stderr, message, args);
4710
  va_end (args);
4711
}
4712
 
4713
void
4714
warn (const char *message, ...)
4715
{
4716
  va_list args;
4717
 
4718
  va_start (args, message);
4719
  fprintf (stderr, _("%s: Warning: "), program_name);
4720
  vfprintf (stderr, message, args);
4721
  va_end (args);
4722
}
4723
 
4724
void
4725
free_debug_memory (void)
4726
{
4727
  unsigned int i;
4728
 
4729
  free_abbrevs ();
4730
 
4731
  for (i = 0; i < max; i++)
4732
    free_debug_section ((enum dwarf_section_display_enum) i);
4733
 
4734
  if (debug_information != NULL)
4735
    {
4736
      if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
4737
        {
4738
          for (i = 0; i < num_debug_info_entries; i++)
4739
            {
4740
              if (!debug_information [i].max_loc_offsets)
4741
                {
4742
                  free (debug_information [i].loc_offsets);
4743
                  free (debug_information [i].have_frame_base);
4744
                }
4745
              if (!debug_information [i].max_range_lists)
4746
                free (debug_information [i].range_lists);
4747
            }
4748
        }
4749
 
4750
      free (debug_information);
4751
      debug_information = NULL;
4752
      num_debug_info_entries = 0;
4753
    }
4754
}
4755
 
4756
void
4757
dwarf_select_sections_by_names (const char *names)
4758
{
4759
  typedef struct
4760
  {
4761
    const char * option;
4762
    int *        variable;
4763
    int val;
4764
  }
4765
  debug_dump_long_opts;
4766
 
4767
  static const debug_dump_long_opts opts_table [] =
4768
    {
4769
      /* Please keep this table alpha- sorted.  */
4770
      { "Ranges", & do_debug_ranges, 1 },
4771
      { "abbrev", & do_debug_abbrevs, 1 },
4772
      { "aranges", & do_debug_aranges, 1 },
4773
      { "frames", & do_debug_frames, 1 },
4774
      { "frames-interp", & do_debug_frames_interp, 1 },
4775
      { "info", & do_debug_info, 1 },
4776
      { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility.  */
4777
      { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
4778
      { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
4779
      { "loc",  & do_debug_loc, 1 },
4780
      { "macro", & do_debug_macinfo, 1 },
4781
      { "pubnames", & do_debug_pubnames, 1 },
4782
      /* This entry is for compatability
4783
         with earlier versions of readelf.  */
4784
      { "ranges", & do_debug_aranges, 1 },
4785
      { "str", & do_debug_str, 1 },
4786
      { NULL, NULL, 0 }
4787
    };
4788
 
4789
  const char *p;
4790
 
4791
  p = names;
4792
  while (*p)
4793
    {
4794
      const debug_dump_long_opts * entry;
4795
 
4796
      for (entry = opts_table; entry->option; entry++)
4797
        {
4798
          size_t len = strlen (entry->option);
4799
 
4800
          if (strncmp (p, entry->option, len) == 0
4801
              && (p[len] == ',' || p[len] == '\0'))
4802
            {
4803
              * entry->variable |= entry->val;
4804
 
4805
              /* The --debug-dump=frames-interp option also
4806
                 enables the --debug-dump=frames option.  */
4807
              if (do_debug_frames_interp)
4808
                do_debug_frames = 1;
4809
 
4810
              p += len;
4811
              break;
4812
            }
4813
        }
4814
 
4815
      if (entry->option == NULL)
4816
        {
4817
          warn (_("Unrecognized debug option '%s'\n"), p);
4818
          p = strchr (p, ',');
4819
          if (p == NULL)
4820
            break;
4821
        }
4822
 
4823
      if (*p == ',')
4824
        p++;
4825
    }
4826
}
4827
 
4828
void
4829
dwarf_select_sections_by_letters (const char *letters)
4830
{
4831
  unsigned int index = 0;
4832
 
4833
  while (letters[index])
4834
    switch (letters[index++])
4835
      {
4836
      case 'i':
4837
        do_debug_info = 1;
4838
        break;
4839
 
4840
      case 'a':
4841
        do_debug_abbrevs = 1;
4842
        break;
4843
 
4844
      case 'l':
4845
        do_debug_lines |= FLAG_DEBUG_LINES_RAW;
4846
        break;
4847
 
4848
      case 'L':
4849
        do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
4850
        break;
4851
 
4852
      case 'p':
4853
        do_debug_pubnames = 1;
4854
        break;
4855
 
4856
      case 'r':
4857
        do_debug_aranges = 1;
4858
        break;
4859
 
4860
      case 'R':
4861
        do_debug_ranges = 1;
4862
        break;
4863
 
4864
      case 'F':
4865
        do_debug_frames_interp = 1;
4866
      case 'f':
4867
        do_debug_frames = 1;
4868
        break;
4869
 
4870
      case 'm':
4871
        do_debug_macinfo = 1;
4872
        break;
4873
 
4874
      case 's':
4875
        do_debug_str = 1;
4876
        break;
4877
 
4878
      case 'o':
4879
        do_debug_loc = 1;
4880
        break;
4881
 
4882
      default:
4883
        warn (_("Unrecognized debug option '%s'\n"), optarg);
4884
        break;
4885
      }
4886
}
4887
 
4888
void
4889
dwarf_select_sections_all (void)
4890
{
4891
  do_debug_info = 1;
4892
  do_debug_abbrevs = 1;
4893
  do_debug_lines = FLAG_DEBUG_LINES_RAW;
4894
  do_debug_pubnames = 1;
4895
  do_debug_aranges = 1;
4896
  do_debug_ranges = 1;
4897
  do_debug_frames = 1;
4898
  do_debug_macinfo = 1;
4899
  do_debug_str = 1;
4900
  do_debug_loc = 1;
4901
}
4902
 
4903
struct dwarf_section_display debug_displays[] =
4904
{
4905
  { { ".debug_abbrev",          ".zdebug_abbrev",       NULL,   NULL,   0,       0 },
4906
    display_debug_abbrev,               &do_debug_abbrevs,      0 },
4907
  { { ".debug_aranges",         ".zdebug_aranges",      NULL,   NULL,   0,       0 },
4908
    display_debug_aranges,              &do_debug_aranges,      1 },
4909
  { { ".debug_frame",           ".zdebug_frame",        NULL,   NULL,   0,       0 },
4910
    display_debug_frames,               &do_debug_frames,       1 },
4911
  { { ".debug_info",            ".zdebug_info",         NULL,   NULL,   0,       0 },
4912
    display_debug_info,                 &do_debug_info,         1 },
4913
  { { ".debug_line",            ".zdebug_line",         NULL,   NULL,   0,       0 },
4914
    display_debug_lines,                &do_debug_lines,        1 },
4915
  { { ".debug_pubnames",        ".zdebug_pubnames",     NULL,   NULL,   0,       0 },
4916
    display_debug_pubnames,             &do_debug_pubnames,     0 },
4917
  { { ".eh_frame",              "",                     NULL,   NULL,   0,       0 },
4918
    display_debug_frames,               &do_debug_frames,       1 },
4919
  { { ".debug_macinfo",         ".zdebug_macinfo",      NULL,   NULL,   0,       0 },
4920
    display_debug_macinfo,              &do_debug_macinfo,      0 },
4921
  { { ".debug_str",             ".zdebug_str",          NULL,   NULL,   0,       0 },
4922
    display_debug_str,                  &do_debug_str,          0 },
4923
  { { ".debug_loc",             ".zdebug_loc",          NULL,   NULL,   0,       0 },
4924
    display_debug_loc,                  &do_debug_loc,          1 },
4925
  { { ".debug_pubtypes",        ".zdebug_pubtypes",     NULL,   NULL,   0,       0 },
4926
    display_debug_pubnames,             &do_debug_pubnames,     0 },
4927
  { { ".debug_ranges",          ".zdebug_ranges",       NULL,   NULL,   0,       0 },
4928
    display_debug_ranges,               &do_debug_ranges,       1 },
4929
  { { ".debug_static_func",     ".zdebug_static_func",  NULL,   NULL,   0,       0 },
4930
    display_debug_not_supported,        NULL,                   0 },
4931
  { { ".debug_static_vars",     ".zdebug_static_vars",  NULL,   NULL,   0,       0 },
4932
    display_debug_not_supported,        NULL,                   0 },
4933
  { { ".debug_types",           ".zdebug_types",        NULL,   NULL,   0,       0 },
4934
    display_debug_not_supported,        NULL,                   0 },
4935
  { { ".debug_weaknames",       ".zdebug_weaknames",    NULL,   NULL,   0,       0 },
4936
    display_debug_not_supported,        NULL,                   0 }
4937
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.