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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [gdb/] [dwarf2cfi.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
/* Stack unwinding code based on dwarf2 frame info for GDB, the GNU debugger.
2
   Copyright 2001, 2002 Free Software Foundation, Inc.
3
   Contributed by Jiri Smid, SuSE Labs.
4
   Based on code written by Daniel Berlin (dan@dberlin.org).
5
 
6
   This file is part of GDB.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 2 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 59 Temple Place - Suite 330,
21
   Boston, MA 02111-1307, USA.  */
22
 
23
#include "defs.h"
24
#include "gdbcore.h"
25
#include "symtab.h"
26
#include "symfile.h"
27
#include "objfiles.h"
28
#include "target.h"
29
#include "elf/dwarf2.h"
30
#include "inferior.h"
31
#include "regcache.h"
32
#include "dwarf2cfi.h"
33
#include "gdb_assert.h"
34
 
35
/* Common Information Entry - holds information that is shared among many
36
   Frame Descriptors.  */
37
struct cie_unit
38
{
39
  /* Offset of this unit in .debug_frame or .eh_frame.  */
40
  ULONGEST offset;
41
 
42
  /* A null-terminated string that identifies the augmentation to this CIE or
43
     to the FDEs that use it.  */
44
  char *augmentation;
45
 
46
  /* A constant that is factored out of all advance location instructions.  */
47
  unsigned int code_align;
48
 
49
  /* A constant that is factored out of all offset instructions.  */
50
  int data_align;
51
 
52
  /* A constant that indicates which regiter represents the return address
53
     of a function.  */
54
  unsigned char ra;
55
 
56
  /* Indicates how addresses are encoded.  */
57
  unsigned char addr_encoding;
58
 
59
  /* Pointer and length of the cie program.  */
60
  char *data;
61
  unsigned int data_length;
62
 
63
  struct objfile *objfile;
64
 
65
  /* Next in chain.  */
66
  struct cie_unit *next;
67
};
68
 
69
/* Frame Description Entry.  */
70
struct fde_unit
71
{
72
  /* Address of the first location associated with this entry.  */
73
  CORE_ADDR initial_location;
74
 
75
  /* Length of program section described by this entry.  */
76
  CORE_ADDR address_range;
77
 
78
  /* Pointer to asociated CIE.  */
79
  struct cie_unit *cie_ptr;
80
 
81
  /* Pointer and length of the cie program.  */
82
  char *data;
83
  unsigned int data_length;
84
};
85
 
86
struct fde_array
87
{
88
  struct fde_unit **array;
89
  int elems;
90
  int array_size;
91
};
92
 
93
struct context_reg
94
{
95
  union
96
  {
97
    unsigned int reg;
98
    long offset;
99
    CORE_ADDR addr;
100
  }
101
  loc;
102
  enum
103
  {
104
    REG_CTX_UNSAVED,
105
    REG_CTX_SAVED_OFFSET,
106
    REG_CTX_SAVED_REG,
107
    REG_CTX_SAVED_ADDR,
108
    REG_CTX_VALUE,
109
  }
110
  how;
111
};
112
 
113
/* This is the register and unwind state for a particular frame.  */
114
struct context
115
{
116
  struct context_reg *reg;
117
 
118
  CORE_ADDR cfa;
119
  CORE_ADDR ra;
120
  void *lsda;
121
  int args_size;
122
};
123
 
124
struct frame_state_reg
125
{
126
  union
127
  {
128
    unsigned int reg;
129
    long offset;
130
    unsigned char *exp;
131
  }
132
  loc;
133
  enum
134
  {
135
    REG_UNSAVED,
136
    REG_SAVED_OFFSET,
137
    REG_SAVED_REG,
138
    REG_SAVED_EXP,
139
  }
140
  how;
141
};
142
 
143
struct frame_state
144
{
145
  /* Each register save state can be described in terms of a CFA slot,
146
     another register, or a location expression.  */
147
  struct frame_state_regs
148
  {
149
    struct frame_state_reg *reg;
150
 
151
    /* Used to implement DW_CFA_remember_state.  */
152
    struct frame_state_regs *prev;
153
  }
154
  regs;
155
 
156
  /* The CFA can be described in terms of a reg+offset or a
157
     location expression.  */
158
  long cfa_offset;
159
  int cfa_reg;
160
  unsigned char *cfa_exp;
161
  enum
162
  {
163
    CFA_UNSET,
164
    CFA_REG_OFFSET,
165
    CFA_EXP,
166
  }
167
  cfa_how;
168
 
169
  /* The PC described by the current frame state.  */
170
  CORE_ADDR pc;
171
 
172
  /* The information we care about from the CIE/FDE.  */
173
  int data_align;
174
  unsigned int code_align;
175
  unsigned char retaddr_column;
176
  unsigned char addr_encoding;
177
 
178
  struct objfile *objfile;
179
};
180
 
181
enum ptr_encoding
182
{
183
  PE_absptr = DW_EH_PE_absptr,
184
  PE_pcrel = DW_EH_PE_pcrel,
185
  PE_textrel = DW_EH_PE_textrel,
186
  PE_datarel = DW_EH_PE_datarel,
187
  PE_funcrel = DW_EH_PE_funcrel
188
};
189
 
190
#define UNWIND_CONTEXT(fi) ((struct context *) (fi->context))
191
 
192
 
193
static struct cie_unit *cie_chunks;
194
static struct fde_array fde_chunks;
195
/* Obstack for allocating temporary storage used during unwind operations.  */
196
static struct obstack unwind_tmp_obstack;
197
 
198
extern file_ptr dwarf_frame_offset;
199
extern unsigned int dwarf_frame_size;
200
extern file_ptr dwarf_eh_frame_offset;
201
extern unsigned int dwarf_eh_frame_size;
202
 
203
 
204
extern char *dwarf2_read_section (struct objfile *objfile, file_ptr offset,
205
                                  unsigned int size);
206
 
207
static struct fde_unit *fde_unit_alloc (void);
208
static struct cie_unit *cie_unit_alloc (void);
209
static void fde_chunks_need_space ();
210
 
211
static struct context *context_alloc ();
212
static struct frame_state *frame_state_alloc ();
213
static void unwind_tmp_obstack_init ();
214
static void unwind_tmp_obstack_free ();
215
static void context_cpy (struct context *dst, struct context *src);
216
 
217
static unsigned int read_1u (bfd * abfd, char **p);
218
static int read_1s (bfd * abfd, char **p);
219
static unsigned int read_2u (bfd * abfd, char **p);
220
static int read_2s (bfd * abfd, char **p);
221
static unsigned int read_4u (bfd * abfd, char **p);
222
static int read_4s (bfd * abfd, char **p);
223
static ULONGEST read_8u (bfd * abfd, char **p);
224
static LONGEST read_8s (bfd * abfd, char **p);
225
 
226
static ULONGEST read_uleb128 (bfd * abfd, char **p);
227
static LONGEST read_sleb128 (bfd * abfd, char **p);
228
static CORE_ADDR read_pointer (bfd * abfd, char **p);
229
static CORE_ADDR read_encoded_pointer (bfd * abfd, char **p,
230
                                       unsigned char encoding);
231
static enum ptr_encoding pointer_encoding (unsigned char encoding);
232
 
233
static LONGEST read_initial_length (bfd * abfd, char *buf, int *bytes_read);
234
static ULONGEST read_length (bfd * abfd, char *buf, int *bytes_read,
235
                             int dwarf64);
236
 
237
static int is_cie (ULONGEST cie_id, int dwarf64);
238
static int compare_fde_unit (const void *a, const void *b);
239
void dwarf2_build_frame_info (struct objfile *objfile);
240
 
241
static void execute_cfa_program (struct objfile *objfile, char *insn_ptr,
242
                                 char *insn_end, struct context *context,
243
                                 struct frame_state *fs);
244
static struct fde_unit *get_fde_for_addr (CORE_ADDR pc);
245
static void frame_state_for (struct context *context, struct frame_state *fs);
246
static void get_reg (char *reg, struct context *context, int regnum);
247
static CORE_ADDR execute_stack_op (struct objfile *objfile,
248
                                   char *op_ptr, char *op_end,
249
                                   struct context *context,
250
                                   CORE_ADDR initial);
251
static void update_context (struct context *context, struct frame_state *fs,
252
                            int chain);
253
 
254
 
255
/* Memory allocation functions.  */
256
static struct fde_unit *
257
fde_unit_alloc (void)
258
{
259
  struct fde_unit *fde;
260
 
261
  fde = (struct fde_unit *) xmalloc (sizeof (struct fde_unit));
262
  memset (fde, 0, sizeof (struct fde_unit));
263
  return fde;
264
}
265
 
266
static struct cie_unit *
267
cie_unit_alloc (void)
268
{
269
  struct cie_unit *cie;
270
 
271
  cie = (struct cie_unit *) xmalloc (sizeof (struct cie_unit));
272
  memset (cie, 0, sizeof (struct cie_unit));
273
  return cie;
274
}
275
 
276
static void
277
fde_chunks_need_space (void)
278
{
279
  if (fde_chunks.elems < fde_chunks.array_size)
280
    return;
281
  fde_chunks.array_size =
282
    fde_chunks.array_size ? 2 * fde_chunks.array_size : 1024;
283
  fde_chunks.array =
284
    xrealloc (fde_chunks.array,
285
              sizeof (struct fde_unit) * fde_chunks.array_size);
286
}
287
 
288
/* Alocate a new `struct context' on temporary obstack.  */
289
static struct context *
290
context_alloc (void)
291
{
292
  struct context *context;
293
 
294
  int regs_size = sizeof (struct context_reg) * NUM_REGS;
295
 
296
  context = (struct context *) obstack_alloc (&unwind_tmp_obstack,
297
                                              sizeof (struct context));
298
  memset (context, 0, sizeof (struct context));
299
  context->reg = (struct context_reg *) obstack_alloc (&unwind_tmp_obstack,
300
                                                       regs_size);
301
  memset (context->reg, 0, regs_size);
302
  return context;
303
}
304
 
305
/* Alocate a new `struct frame_state' on temporary obstack.  */
306
static struct frame_state *
307
frame_state_alloc (void)
308
{
309
  struct frame_state *fs;
310
 
311
  int regs_size = sizeof (struct frame_state_reg) * NUM_REGS;
312
 
313
  fs = (struct frame_state *) obstack_alloc (&unwind_tmp_obstack,
314
                                             sizeof (struct frame_state));
315
  memset (fs, 0, sizeof (struct frame_state));
316
  fs->regs.reg =
317
    (struct frame_state_reg *) obstack_alloc (&unwind_tmp_obstack, regs_size);
318
  memset (fs->regs.reg, 0, regs_size);
319
  return fs;
320
}
321
 
322
static void
323
unwind_tmp_obstack_init (void)
324
{
325
  obstack_init (&unwind_tmp_obstack);
326
}
327
 
328
static void
329
unwind_tmp_obstack_free (void)
330
{
331
  obstack_free (&unwind_tmp_obstack, NULL);
332
  unwind_tmp_obstack_init ();
333
}
334
 
335
static void
336
context_cpy (struct context *dst, struct context *src)
337
{
338
  int regs_size = sizeof (struct context_reg) * NUM_REGS;
339
  struct context_reg *dreg;
340
 
341
  /* Structure dst contains a pointer to an array of
342
   * registers of a given frame as well as src does. This
343
   * array was already allocated before dst was passed to
344
   * context_cpy but the pointer to it was overriden by
345
   * '*dst = *src' and the array was lost. This led to the
346
   * situation, that we've had a copy of src placed in dst,
347
   * but both of them pointed to the same regs array and
348
   * thus we've sometimes blindly rewritten it.  Now we save
349
   * the pointer before copying src to dst, return it back
350
   * after that and copy the registers into their new place
351
   * finally.   ---   mludvig@suse.cz  */
352
  dreg = dst->reg;
353
  *dst = *src;
354
  dst->reg = dreg;
355
 
356
  memcpy (dst->reg, src->reg, regs_size);
357
}
358
 
359
static unsigned int
360
read_1u (bfd * abfd, char **p)
361
{
362
  unsigned ret;
363
 
364
  ret = bfd_get_8 (abfd, (bfd_byte *) * p);
365
  (*p)++;
366
  return ret;
367
}
368
 
369
static int
370
read_1s (bfd * abfd, char **p)
371
{
372
  int ret;
373
 
374
  ret = bfd_get_signed_8 (abfd, (bfd_byte *) * p);
375
  (*p)++;
376
  return ret;
377
}
378
 
379
static unsigned int
380
read_2u (bfd * abfd, char **p)
381
{
382
  unsigned ret;
383
 
384
  ret = bfd_get_16 (abfd, (bfd_byte *) * p);
385
  (*p)++;
386
  return ret;
387
}
388
 
389
static int
390
read_2s (bfd * abfd, char **p)
391
{
392
  int ret;
393
 
394
  ret = bfd_get_signed_16 (abfd, (bfd_byte *) * p);
395
  (*p) += 2;
396
  return ret;
397
}
398
 
399
static unsigned int
400
read_4u (bfd * abfd, char **p)
401
{
402
  unsigned int ret;
403
 
404
  ret = bfd_get_32 (abfd, (bfd_byte *) * p);
405
  (*p) += 4;
406
  return ret;
407
}
408
 
409
static int
410
read_4s (bfd * abfd, char **p)
411
{
412
  int ret;
413
 
414
  ret = bfd_get_signed_32 (abfd, (bfd_byte *) * p);
415
  (*p) += 4;
416
  return ret;
417
}
418
 
419
static ULONGEST
420
read_8u (bfd * abfd, char **p)
421
{
422
  ULONGEST ret;
423
 
424
  ret = bfd_get_64 (abfd, (bfd_byte *) * p);
425
  (*p) += 8;
426
  return ret;
427
}
428
 
429
static LONGEST
430
read_8s (bfd * abfd, char **p)
431
{
432
  LONGEST ret;
433
 
434
  ret = bfd_get_signed_64 (abfd, (bfd_byte *) * p);
435
  (*p) += 8;
436
  return ret;
437
}
438
 
439
static ULONGEST
440
read_uleb128 (bfd * abfd, char **p)
441
{
442
  ULONGEST ret;
443
  int i, shift;
444
  unsigned char byte;
445
 
446
  ret = 0;
447
  shift = 0;
448
  i = 0;
449
  while (1)
450
    {
451
      byte = bfd_get_8 (abfd, (bfd_byte *) * p);
452
      (*p)++;
453
      ret |= ((unsigned long) (byte & 127) << shift);
454
      if ((byte & 128) == 0)
455
        {
456
          break;
457
        }
458
      shift += 7;
459
    }
460
  return ret;
461
}
462
 
463
static LONGEST
464
read_sleb128 (bfd * abfd, char **p)
465
{
466
  LONGEST ret;
467
  int i, shift, size, num_read;
468
  unsigned char byte;
469
 
470
  ret = 0;
471
  shift = 0;
472
  size = 32;
473
  num_read = 0;
474
  i = 0;
475
  while (1)
476
    {
477
      byte = bfd_get_8 (abfd, (bfd_byte *) * p);
478
      (*p)++;
479
      ret |= ((long) (byte & 127) << shift);
480
      shift += 7;
481
      if ((byte & 128) == 0)
482
        {
483
          break;
484
        }
485
    }
486
  if ((shift < size) && (byte & 0x40))
487
    {
488
      ret |= -(1 << shift);
489
    }
490
  return ret;
491
}
492
 
493
static CORE_ADDR
494
read_pointer (bfd * abfd, char **p)
495
{
496
  switch (TARGET_ADDR_BIT / TARGET_CHAR_BIT)
497
    {
498
    case 4:
499
      return read_4u (abfd, p);
500
    case 8:
501
      return read_8u (abfd, p);
502
    default:
503
      error ("dwarf cfi error: unsupported target address length.");
504
    }
505
}
506
 
507
/* This functions only reads appropriate amount of data from *p
508
 * and returns the resulting value. Calling function must handle
509
 * different encoding possibilities itself!  */
510
static CORE_ADDR
511
read_encoded_pointer (bfd * abfd, char **p, unsigned char encoding)
512
{
513
  CORE_ADDR ret;
514
 
515
  switch (encoding & 0x0f)
516
    {
517
    case DW_EH_PE_absptr:
518
      ret = read_pointer (abfd, p);
519
      break;
520
 
521
    case DW_EH_PE_uleb128:
522
      ret = read_uleb128 (abfd, p);
523
      break;
524
    case DW_EH_PE_sleb128:
525
      ret = read_sleb128 (abfd, p);
526
      break;
527
 
528
    case DW_EH_PE_udata2:
529
      ret = read_2u (abfd, p);
530
      break;
531
    case DW_EH_PE_udata4:
532
      ret = read_4u (abfd, p);
533
      break;
534
    case DW_EH_PE_udata8:
535
      ret = read_8u (abfd, p);
536
      break;
537
 
538
    case DW_EH_PE_sdata2:
539
      ret = read_2s (abfd, p);
540
      break;
541
    case DW_EH_PE_sdata4:
542
      ret = read_4s (abfd, p);
543
      break;
544
    case DW_EH_PE_sdata8:
545
      ret = read_8s (abfd, p);
546
      break;
547
 
548
    default:
549
      internal_error (__FILE__, __LINE__,
550
                      "read_encoded_pointer: unknown pointer encoding");
551
    }
552
 
553
  return ret;
554
}
555
 
556
/* Variable 'encoding' carries 3 different flags:
557
 * - encoding & 0x0f : size of the address (handled in read_encoded_pointer())
558
 * - encoding & 0x70 : type (absolute, relative, ...)
559
 * - encoding & 0x80 : indirect flag (DW_EH_PE_indirect == 0x80).  */
560
enum ptr_encoding
561
pointer_encoding (unsigned char encoding)
562
{
563
  int ret;
564
 
565
  if (encoding & DW_EH_PE_indirect)
566
    warning ("CFI: Unsupported pointer encoding: DW_EH_PE_indirect");
567
 
568
  switch (encoding & 0x70)
569
    {
570
    case DW_EH_PE_absptr:
571
    case DW_EH_PE_pcrel:
572
    case DW_EH_PE_textrel:
573
    case DW_EH_PE_datarel:
574
    case DW_EH_PE_funcrel:
575
      ret = encoding & 0x70;
576
      break;
577
    default:
578
      internal_error (__FILE__, __LINE__, "CFI: unknown pointer encoding");
579
    }
580
  return ret;
581
}
582
 
583
static LONGEST
584
read_initial_length (bfd * abfd, char *buf, int *bytes_read)
585
{
586
  LONGEST ret = 0;
587
 
588
  ret = bfd_get_32 (abfd, (bfd_byte *) buf);
589
 
590
  if (ret == 0xffffffff)
591
    {
592
      ret = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
593
      *bytes_read = 12;
594
    }
595
  else
596
    {
597
      *bytes_read = 4;
598
    }
599
 
600
  return ret;
601
}
602
 
603
static ULONGEST
604
read_length (bfd * abfd, char *buf, int *bytes_read, int dwarf64)
605
{
606
  if (dwarf64)
607
    {
608
      *bytes_read = 8;
609
      return read_8u (abfd, &buf);
610
    }
611
  else
612
    {
613
      *bytes_read = 4;
614
      return read_4u (abfd, &buf);
615
    }
616
}
617
 
618
static void
619
execute_cfa_program (struct objfile *objfile, char *insn_ptr, char *insn_end,
620
                     struct context *context, struct frame_state *fs)
621
{
622
  struct frame_state_regs *unused_rs = NULL;
623
 
624
  /* Don't allow remember/restore between CIE and FDE programs.  */
625
  fs->regs.prev = NULL;
626
 
627
  while (insn_ptr < insn_end && fs->pc < context->ra)
628
    {
629
      unsigned char insn = *insn_ptr++;
630
      ULONGEST reg, uoffset;
631
      LONGEST offset;
632
 
633
      if (insn & DW_CFA_advance_loc)
634
        fs->pc += (insn & 0x3f) * fs->code_align;
635
      else if (insn & DW_CFA_offset)
636
        {
637
          reg = insn & 0x3f;
638
          uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
639
          offset = (long) uoffset *fs->data_align;
640
          fs->regs.reg[reg].how = REG_SAVED_OFFSET;
641
          fs->regs.reg[reg].loc.offset = offset;
642
        }
643
      else if (insn & DW_CFA_restore)
644
        {
645
          reg = insn & 0x3f;
646
          fs->regs.reg[reg].how = REG_UNSAVED;
647
        }
648
      else
649
        switch (insn)
650
          {
651
          case DW_CFA_set_loc:
652
            fs->pc = read_encoded_pointer (objfile->obfd, &insn_ptr,
653
                                           fs->addr_encoding);
654
 
655
            if (pointer_encoding (fs->addr_encoding) != PE_absptr)
656
              warning ("CFI: DW_CFA_set_loc uses relative addressing");
657
 
658
            break;
659
 
660
          case DW_CFA_advance_loc1:
661
            fs->pc += read_1u (objfile->obfd, &insn_ptr);
662
            break;
663
          case DW_CFA_advance_loc2:
664
            fs->pc += read_2u (objfile->obfd, &insn_ptr);
665
            break;
666
          case DW_CFA_advance_loc4:
667
            fs->pc += read_4u (objfile->obfd, &insn_ptr);
668
            break;
669
 
670
          case DW_CFA_offset_extended:
671
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
672
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
673
            offset = (long) uoffset *fs->data_align;
674
            fs->regs.reg[reg].how = REG_SAVED_OFFSET;
675
            fs->regs.reg[reg].loc.offset = offset;
676
            break;
677
 
678
          case DW_CFA_restore_extended:
679
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
680
            fs->regs.reg[reg].how = REG_UNSAVED;
681
            break;
682
 
683
          case DW_CFA_undefined:
684
          case DW_CFA_same_value:
685
          case DW_CFA_nop:
686
            break;
687
 
688
          case DW_CFA_register:
689
            {
690
              ULONGEST reg2;
691
              reg = read_uleb128 (objfile->obfd, &insn_ptr);
692
              reg2 = read_uleb128 (objfile->obfd, &insn_ptr);
693
              fs->regs.reg[reg].how = REG_SAVED_REG;
694
              fs->regs.reg[reg].loc.reg = reg2;
695
            }
696
            break;
697
 
698
          case DW_CFA_remember_state:
699
            {
700
              struct frame_state_regs *new_rs;
701
              if (unused_rs)
702
                {
703
                  new_rs = unused_rs;
704
                  unused_rs = unused_rs->prev;
705
                }
706
              else
707
                new_rs = xmalloc (sizeof (struct frame_state_regs));
708
 
709
              *new_rs = fs->regs;
710
              fs->regs.prev = new_rs;
711
            }
712
            break;
713
 
714
          case DW_CFA_restore_state:
715
            {
716
              struct frame_state_regs *old_rs = fs->regs.prev;
717
              fs->regs = *old_rs;
718
              old_rs->prev = unused_rs;
719
              unused_rs = old_rs;
720
            }
721
            break;
722
 
723
          case DW_CFA_def_cfa:
724
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
725
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
726
            fs->cfa_reg = reg;
727
            fs->cfa_offset = uoffset;
728
            fs->cfa_how = CFA_REG_OFFSET;
729
            break;
730
 
731
          case DW_CFA_def_cfa_register:
732
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
733
            fs->cfa_reg = reg;
734
            fs->cfa_how = CFA_REG_OFFSET;
735
            break;
736
 
737
          case DW_CFA_def_cfa_offset:
738
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
739
            fs->cfa_offset = uoffset;
740
            break;
741
 
742
          case DW_CFA_def_cfa_expression:
743
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
744
            fs->cfa_exp = insn_ptr;
745
            fs->cfa_how = CFA_EXP;
746
            insn_ptr += uoffset;
747
            break;
748
 
749
          case DW_CFA_expression:
750
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
751
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
752
            fs->regs.reg[reg].how = REG_SAVED_EXP;
753
            fs->regs.reg[reg].loc.exp = insn_ptr;
754
            insn_ptr += uoffset;
755
            break;
756
 
757
            /* From the 2.1 draft.  */
758
          case DW_CFA_offset_extended_sf:
759
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
760
            offset = read_sleb128 (objfile->obfd, &insn_ptr);
761
            offset *= fs->data_align;
762
            fs->regs.reg[reg].how = REG_SAVED_OFFSET;
763
            fs->regs.reg[reg].loc.offset = offset;
764
            break;
765
 
766
          case DW_CFA_def_cfa_sf:
767
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
768
            offset = read_sleb128 (objfile->obfd, &insn_ptr);
769
            fs->cfa_offset = offset;
770
            fs->cfa_reg = reg;
771
            fs->cfa_how = CFA_REG_OFFSET;
772
            break;
773
 
774
          case DW_CFA_def_cfa_offset_sf:
775
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
776
            fs->cfa_offset = uoffset;
777
            /* cfa_how deliberately not set.  */
778
            break;
779
 
780
          case DW_CFA_GNU_window_save:
781
            /* ??? Hardcoded for SPARC register window configuration.  */
782
            for (reg = 16; reg < 32; ++reg)
783
              {
784
                fs->regs.reg[reg].how = REG_SAVED_OFFSET;
785
                fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
786
              }
787
            break;
788
 
789
          case DW_CFA_GNU_args_size:
790
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
791
            context->args_size = uoffset;
792
            break;
793
 
794
          case DW_CFA_GNU_negative_offset_extended:
795
            /* Obsoleted by DW_CFA_offset_extended_sf, but used by
796
               older PowerPC code.  */
797
            reg = read_uleb128 (objfile->obfd, &insn_ptr);
798
            uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
799
            offset = (long) uoffset *fs->data_align;
800
            fs->regs.reg[reg].how = REG_SAVED_OFFSET;
801
            fs->regs.reg[reg].loc.offset = -offset;
802
            break;
803
 
804
          default:
805
            error ("dwarf cfi error: unknown cfa instruction %d.", insn);
806
          }
807
    }
808
}
809
 
810
static struct fde_unit *
811
get_fde_for_addr (CORE_ADDR pc)
812
{
813
  size_t lo, hi;
814
  struct fde_unit *fde = NULL;
815
  lo = 0;
816
  hi = fde_chunks.elems;
817
 
818
  while (lo < hi)
819
    {
820
      size_t i = (lo + hi) / 2;
821
      fde = fde_chunks.array[i];
822
      if (pc < fde->initial_location)
823
        hi = i;
824
      else if (pc >= fde->initial_location + fde->address_range)
825
        lo = i + 1;
826
      else
827
        return fde;
828
    }
829
  return 0;
830
}
831
 
832
static void
833
frame_state_for (struct context *context, struct frame_state *fs)
834
{
835
  struct fde_unit *fde;
836
  struct cie_unit *cie;
837
 
838
  context->args_size = 0;
839
  context->lsda = 0;
840
 
841
  fde = get_fde_for_addr (context->ra - 1);
842
 
843
  if (fde == NULL)
844
    return;
845
 
846
  fs->pc = fde->initial_location;
847
 
848
  gdb_assert (fde->cie_ptr != NULL);
849
 
850
  cie = fde->cie_ptr;
851
 
852
  fs->code_align = cie->code_align;
853
  fs->data_align = cie->data_align;
854
  fs->retaddr_column = cie->ra;
855
  fs->addr_encoding = cie->addr_encoding;
856
  fs->objfile = cie->objfile;
857
 
858
  execute_cfa_program (cie->objfile, cie->data,
859
                       cie->data + cie->data_length, context, fs);
860
  execute_cfa_program (cie->objfile, fde->data,
861
                       fde->data + fde->data_length, context, fs);
862
}
863
 
864
static void
865
get_reg (char *reg, struct context *context, int regnum)
866
{
867
  switch (context->reg[regnum].how)
868
    {
869
    case REG_CTX_UNSAVED:
870
      read_register_gen (regnum, reg);
871
      break;
872
    case REG_CTX_SAVED_OFFSET:
873
      target_read_memory (context->cfa + context->reg[regnum].loc.offset,
874
                          reg, REGISTER_RAW_SIZE (regnum));
875
      break;
876
    case REG_CTX_SAVED_REG:
877
      read_register_gen (context->reg[regnum].loc.reg, reg);
878
      break;
879
    case REG_CTX_SAVED_ADDR:
880
      target_read_memory (context->reg[regnum].loc.addr,
881
                          reg, REGISTER_RAW_SIZE (regnum));
882
      break;
883
    case REG_CTX_VALUE:
884
      memcpy (reg, &context->reg[regnum].loc.addr,
885
              REGISTER_RAW_SIZE (regnum));
886
      break;
887
    default:
888
      internal_error (__FILE__, __LINE__, "get_reg: unknown register rule");
889
    }
890
}
891
 
892
/* Decode a DW_OP stack program.  Return the top of stack.  Push INITIAL
893
   onto the stack to start.  */
894
static CORE_ADDR
895
execute_stack_op (struct objfile *objfile,
896
                  char *op_ptr, char *op_end, struct context *context,
897
                  CORE_ADDR initial)
898
{
899
  CORE_ADDR stack[64];          /* ??? Assume this is enough. */
900
  int stack_elt;
901
 
902
  stack[0] = initial;
903
  stack_elt = 1;
904
 
905
  while (op_ptr < op_end)
906
    {
907
      enum dwarf_location_atom op = *op_ptr++;
908
      CORE_ADDR result;
909
      ULONGEST reg;
910
      LONGEST offset;
911
 
912
      switch (op)
913
        {
914
        case DW_OP_lit0:
915
        case DW_OP_lit1:
916
        case DW_OP_lit2:
917
        case DW_OP_lit3:
918
        case DW_OP_lit4:
919
        case DW_OP_lit5:
920
        case DW_OP_lit6:
921
        case DW_OP_lit7:
922
        case DW_OP_lit8:
923
        case DW_OP_lit9:
924
        case DW_OP_lit10:
925
        case DW_OP_lit11:
926
        case DW_OP_lit12:
927
        case DW_OP_lit13:
928
        case DW_OP_lit14:
929
        case DW_OP_lit15:
930
        case DW_OP_lit16:
931
        case DW_OP_lit17:
932
        case DW_OP_lit18:
933
        case DW_OP_lit19:
934
        case DW_OP_lit20:
935
        case DW_OP_lit21:
936
        case DW_OP_lit22:
937
        case DW_OP_lit23:
938
        case DW_OP_lit24:
939
        case DW_OP_lit25:
940
        case DW_OP_lit26:
941
        case DW_OP_lit27:
942
        case DW_OP_lit28:
943
        case DW_OP_lit29:
944
        case DW_OP_lit30:
945
        case DW_OP_lit31:
946
          result = op - DW_OP_lit0;
947
          break;
948
 
949
        case DW_OP_addr:
950
          result = read_pointer (objfile->obfd, &op_ptr);
951
          break;
952
 
953
        case DW_OP_const1u:
954
          result = read_1u (objfile->obfd, &op_ptr);
955
          break;
956
        case DW_OP_const1s:
957
          result = read_1s (objfile->obfd, &op_ptr);
958
          break;
959
        case DW_OP_const2u:
960
          result = read_2u (objfile->obfd, &op_ptr);
961
          break;
962
        case DW_OP_const2s:
963
          result = read_2s (objfile->obfd, &op_ptr);
964
          break;
965
        case DW_OP_const4u:
966
          result = read_4u (objfile->obfd, &op_ptr);
967
          break;
968
        case DW_OP_const4s:
969
          result = read_4s (objfile->obfd, &op_ptr);
970
          break;
971
        case DW_OP_const8u:
972
          result = read_8u (objfile->obfd, &op_ptr);
973
          break;
974
        case DW_OP_const8s:
975
          result = read_8s (objfile->obfd, &op_ptr);
976
          break;
977
        case DW_OP_constu:
978
          result = read_uleb128 (objfile->obfd, &op_ptr);
979
          break;
980
        case DW_OP_consts:
981
          result = read_sleb128 (objfile->obfd, &op_ptr);
982
          break;
983
 
984
        case DW_OP_reg0:
985
        case DW_OP_reg1:
986
        case DW_OP_reg2:
987
        case DW_OP_reg3:
988
        case DW_OP_reg4:
989
        case DW_OP_reg5:
990
        case DW_OP_reg6:
991
        case DW_OP_reg7:
992
        case DW_OP_reg8:
993
        case DW_OP_reg9:
994
        case DW_OP_reg10:
995
        case DW_OP_reg11:
996
        case DW_OP_reg12:
997
        case DW_OP_reg13:
998
        case DW_OP_reg14:
999
        case DW_OP_reg15:
1000
        case DW_OP_reg16:
1001
        case DW_OP_reg17:
1002
        case DW_OP_reg18:
1003
        case DW_OP_reg19:
1004
        case DW_OP_reg20:
1005
        case DW_OP_reg21:
1006
        case DW_OP_reg22:
1007
        case DW_OP_reg23:
1008
        case DW_OP_reg24:
1009
        case DW_OP_reg25:
1010
        case DW_OP_reg26:
1011
        case DW_OP_reg27:
1012
        case DW_OP_reg28:
1013
        case DW_OP_reg29:
1014
        case DW_OP_reg30:
1015
        case DW_OP_reg31:
1016
          get_reg ((char *) &result, context, op - DW_OP_reg0);
1017
          break;
1018
        case DW_OP_regx:
1019
          reg = read_uleb128 (objfile->obfd, &op_ptr);
1020
          get_reg ((char *) &result, context, reg);
1021
          break;
1022
 
1023
        case DW_OP_breg0:
1024
        case DW_OP_breg1:
1025
        case DW_OP_breg2:
1026
        case DW_OP_breg3:
1027
        case DW_OP_breg4:
1028
        case DW_OP_breg5:
1029
        case DW_OP_breg6:
1030
        case DW_OP_breg7:
1031
        case DW_OP_breg8:
1032
        case DW_OP_breg9:
1033
        case DW_OP_breg10:
1034
        case DW_OP_breg11:
1035
        case DW_OP_breg12:
1036
        case DW_OP_breg13:
1037
        case DW_OP_breg14:
1038
        case DW_OP_breg15:
1039
        case DW_OP_breg16:
1040
        case DW_OP_breg17:
1041
        case DW_OP_breg18:
1042
        case DW_OP_breg19:
1043
        case DW_OP_breg20:
1044
        case DW_OP_breg21:
1045
        case DW_OP_breg22:
1046
        case DW_OP_breg23:
1047
        case DW_OP_breg24:
1048
        case DW_OP_breg25:
1049
        case DW_OP_breg26:
1050
        case DW_OP_breg27:
1051
        case DW_OP_breg28:
1052
        case DW_OP_breg29:
1053
        case DW_OP_breg30:
1054
        case DW_OP_breg31:
1055
          offset = read_sleb128 (objfile->obfd, &op_ptr);
1056
          get_reg ((char *) &result, context, op - DW_OP_breg0);
1057
          result += offset;
1058
          break;
1059
        case DW_OP_bregx:
1060
          reg = read_uleb128 (objfile->obfd, &op_ptr);
1061
          offset = read_sleb128 (objfile->obfd, &op_ptr);
1062
          get_reg ((char *) &result, context, reg);
1063
          result += offset;
1064
          break;
1065
 
1066
        case DW_OP_dup:
1067
          if (stack_elt < 1)
1068
            internal_error (__FILE__, __LINE__, "execute_stack_op error");
1069
          result = stack[stack_elt - 1];
1070
          break;
1071
 
1072
        case DW_OP_drop:
1073
          if (--stack_elt < 0)
1074
            internal_error (__FILE__, __LINE__, "execute_stack_op error");
1075
          goto no_push;
1076
 
1077
        case DW_OP_pick:
1078
          offset = *op_ptr++;
1079
          if (offset >= stack_elt - 1)
1080
            internal_error (__FILE__, __LINE__, "execute_stack_op error");
1081
          result = stack[stack_elt - 1 - offset];
1082
          break;
1083
 
1084
        case DW_OP_over:
1085
          if (stack_elt < 2)
1086
            internal_error (__FILE__, __LINE__, "execute_stack_op error");
1087
          result = stack[stack_elt - 2];
1088
          break;
1089
 
1090
        case DW_OP_rot:
1091
          {
1092
            CORE_ADDR t1, t2, t3;
1093
 
1094
            if (stack_elt < 3)
1095
              internal_error (__FILE__, __LINE__, "execute_stack_op error");
1096
            t1 = stack[stack_elt - 1];
1097
            t2 = stack[stack_elt - 2];
1098
            t3 = stack[stack_elt - 3];
1099
            stack[stack_elt - 1] = t2;
1100
            stack[stack_elt - 2] = t3;
1101
            stack[stack_elt - 3] = t1;
1102
            goto no_push;
1103
          }
1104
 
1105
        case DW_OP_deref:
1106
        case DW_OP_deref_size:
1107
        case DW_OP_abs:
1108
        case DW_OP_neg:
1109
        case DW_OP_not:
1110
        case DW_OP_plus_uconst:
1111
          /* Unary operations.  */
1112
          if (--stack_elt < 0)
1113
            internal_error (__FILE__, __LINE__, "execute_stack_op error");
1114
          result = stack[stack_elt];
1115
 
1116
          switch (op)
1117
            {
1118
            case DW_OP_deref:
1119
              {
1120
                int len = TARGET_ADDR_BIT / TARGET_CHAR_BIT;
1121
                if (len != 4 && len != 8)
1122
                  internal_error (__FILE__, __LINE__,
1123
                                  "execute_stack_op error");
1124
                result = read_memory_unsigned_integer (result, len);
1125
              }
1126
              break;
1127
 
1128
            case DW_OP_deref_size:
1129
              {
1130
                int len = *op_ptr++;
1131
                if (len != 1 && len != 2 && len != 4 && len !=8)
1132
                  internal_error (__FILE__, __LINE__,
1133
                                  "execute_stack_op error");
1134
                result = read_memory_unsigned_integer (result, len);
1135
              }
1136
              break;
1137
 
1138
            case DW_OP_abs:
1139
              if (result < 0)
1140
                result = -result;
1141
              break;
1142
            case DW_OP_neg:
1143
              result = -result;
1144
              break;
1145
            case DW_OP_not:
1146
              result = ~result;
1147
              break;
1148
            case DW_OP_plus_uconst:
1149
              result += read_uleb128 (objfile->obfd, &op_ptr);
1150
              break;
1151
            default:
1152
              break;
1153
            }
1154
          break;
1155
 
1156
        case DW_OP_and:
1157
        case DW_OP_div:
1158
        case DW_OP_minus:
1159
        case DW_OP_mod:
1160
        case DW_OP_mul:
1161
        case DW_OP_or:
1162
        case DW_OP_plus:
1163
        case DW_OP_le:
1164
        case DW_OP_ge:
1165
        case DW_OP_eq:
1166
        case DW_OP_lt:
1167
        case DW_OP_gt:
1168
        case DW_OP_ne:
1169
          {
1170
            /* Binary operations.  */
1171
            CORE_ADDR first, second;
1172
            if ((stack_elt -= 2) < 0)
1173
              internal_error (__FILE__, __LINE__, "execute_stack_op error");
1174
            second = stack[stack_elt];
1175
            first = stack[stack_elt + 1];
1176
 
1177
            switch (op)
1178
              {
1179
              case DW_OP_and:
1180
                result = second & first;
1181
                break;
1182
              case DW_OP_div:
1183
                result = (LONGEST) second / (LONGEST) first;
1184
                break;
1185
              case DW_OP_minus:
1186
                result = second - first;
1187
                break;
1188
              case DW_OP_mod:
1189
                result = (LONGEST) second % (LONGEST) first;
1190
                break;
1191
              case DW_OP_mul:
1192
                result = second * first;
1193
                break;
1194
              case DW_OP_or:
1195
                result = second | first;
1196
                break;
1197
              case DW_OP_plus:
1198
                result = second + first;
1199
                break;
1200
              case DW_OP_shl:
1201
                result = second << first;
1202
                break;
1203
              case DW_OP_shr:
1204
                result = second >> first;
1205
                break;
1206
              case DW_OP_shra:
1207
                result = (LONGEST) second >> first;
1208
                break;
1209
              case DW_OP_xor:
1210
                result = second ^ first;
1211
                break;
1212
              case DW_OP_le:
1213
                result = (LONGEST) first <= (LONGEST) second;
1214
                break;
1215
              case DW_OP_ge:
1216
                result = (LONGEST) first >= (LONGEST) second;
1217
                break;
1218
              case DW_OP_eq:
1219
                result = (LONGEST) first == (LONGEST) second;
1220
                break;
1221
              case DW_OP_lt:
1222
                result = (LONGEST) first < (LONGEST) second;
1223
                break;
1224
              case DW_OP_gt:
1225
                result = (LONGEST) first > (LONGEST) second;
1226
                break;
1227
              case DW_OP_ne:
1228
                result = (LONGEST) first != (LONGEST) second;
1229
                break;
1230
              default:
1231
                error ("execute_stack_op: Unknown DW_OP_ value");
1232
                break;
1233
              }
1234
          }
1235
          break;
1236
 
1237
        case DW_OP_skip:
1238
          offset = read_2s (objfile->obfd, &op_ptr);
1239
          op_ptr += offset;
1240
          goto no_push;
1241
 
1242
        case DW_OP_bra:
1243
          if (--stack_elt < 0)
1244
            internal_error (__FILE__, __LINE__, "execute_stack_op error");
1245
          offset = read_2s (objfile->obfd, &op_ptr);
1246
          if (stack[stack_elt] != 0)
1247
            op_ptr += offset;
1248
          goto no_push;
1249
 
1250
        case DW_OP_nop:
1251
          goto no_push;
1252
 
1253
        default:
1254
          internal_error (__FILE__, __LINE__, "execute_stack_op error");
1255
        }
1256
 
1257
      /* Most things push a result value.  */
1258
      if ((size_t) stack_elt >= sizeof (stack) / sizeof (*stack))
1259
        internal_error (__FILE__, __LINE__, "execute_stack_op error");
1260
      stack[++stack_elt] = result;
1261
    no_push:;
1262
    }
1263
 
1264
  /* We were executing this program to get a value.  It should be
1265
     at top of stack.  */
1266
  if (--stack_elt < 0)
1267
    internal_error (__FILE__, __LINE__, "execute_stack_op error");
1268
  return stack[stack_elt];
1269
}
1270
 
1271
static void
1272
update_context (struct context *context, struct frame_state *fs, int chain)
1273
{
1274
  struct context *orig_context;
1275
  CORE_ADDR cfa = 0;
1276
  long i;
1277
 
1278
  unwind_tmp_obstack_init ();
1279
 
1280
  orig_context = context_alloc ();
1281
  context_cpy (orig_context, context);
1282
 
1283
  /* Compute this frame's CFA.  */
1284
  switch (fs->cfa_how)
1285
    {
1286
    case CFA_REG_OFFSET:
1287
      get_reg ((char *) &cfa, context, fs->cfa_reg);
1288
      cfa += fs->cfa_offset;
1289
      break;
1290
 
1291
    case CFA_EXP:
1292
      /* ??? No way of knowing what register number is the stack pointer
1293
         to do the same sort of handling as above.  Assume that if the
1294
         CFA calculation is so complicated as to require a stack program
1295
         that this will not be a problem.  */
1296
      {
1297
        char *exp = fs->cfa_exp;
1298
        ULONGEST len;
1299
 
1300
        len = read_uleb128 (fs->objfile->obfd, &exp);
1301
        cfa = (CORE_ADDR) execute_stack_op (fs->objfile, exp,
1302
                                            exp + len, context, 0);
1303
        break;
1304
      }
1305
    default:
1306
      break;
1307
    }
1308
  context->cfa = cfa;
1309
 
1310
  if (!chain)
1311
    orig_context->cfa = cfa;
1312
 
1313
  /* Compute the addresses of all registers saved in this frame.  */
1314
  for (i = 0; i < NUM_REGS; ++i)
1315
    switch (fs->regs.reg[i].how)
1316
      {
1317
      case REG_UNSAVED:
1318
        if (i == SP_REGNUM)
1319
          {
1320
            context->reg[i].how = REG_CTX_VALUE;
1321
            context->reg[i].loc.addr = cfa;
1322
          }
1323
        else
1324
          context->reg[i].how = REG_CTX_UNSAVED;
1325
        break;
1326
      case REG_SAVED_OFFSET:
1327
        context->reg[i].how = REG_CTX_SAVED_OFFSET;
1328
        context->reg[i].loc.offset = fs->regs.reg[i].loc.offset;
1329
        break;
1330
      case REG_SAVED_REG:
1331
        switch (orig_context->reg[fs->regs.reg[i].loc.reg].how)
1332
          {
1333
          case REG_CTX_UNSAVED:
1334
            context->reg[i].how = REG_CTX_UNSAVED;
1335
            break;
1336
          case REG_CTX_SAVED_OFFSET:
1337
            context->reg[i].how = REG_CTX_SAVED_OFFSET;
1338
            context->reg[i].loc.offset = orig_context->cfa - context->cfa +
1339
              orig_context->reg[fs->regs.reg[i].loc.reg].loc.offset;
1340
            break;
1341
          case REG_CTX_SAVED_REG:
1342
            context->reg[i].how = REG_CTX_SAVED_REG;
1343
            context->reg[i].loc.reg =
1344
              orig_context->reg[fs->regs.reg[i].loc.reg].loc.reg;
1345
            break;
1346
          case REG_CTX_SAVED_ADDR:
1347
            context->reg[i].how = REG_CTX_SAVED_ADDR;
1348
            context->reg[i].loc.addr =
1349
              orig_context->reg[fs->regs.reg[i].loc.reg].loc.addr;
1350
            break;
1351
          default:
1352
            internal_error (__FILE__, __LINE__, "bad switch");
1353
          }
1354
        break;
1355
      case REG_SAVED_EXP:
1356
        {
1357
          char *exp = fs->regs.reg[i].loc.exp;
1358
          ULONGEST len;
1359
          CORE_ADDR val;
1360
 
1361
          len = read_uleb128 (fs->objfile->obfd, &exp);
1362
          val = execute_stack_op (fs->objfile, exp, exp + len,
1363
                                  orig_context, cfa);
1364
          context->reg[i].how = REG_CTX_SAVED_ADDR;
1365
          context->reg[i].loc.addr = val;
1366
        }
1367
        break;
1368
      default:
1369
        internal_error (__FILE__, __LINE__, "bad switch");
1370
      }
1371
  get_reg ((char *) &context->ra, context, fs->retaddr_column);
1372
  unwind_tmp_obstack_free ();
1373
}
1374
 
1375
static int
1376
is_cie (ULONGEST cie_id, int dwarf64)
1377
{
1378
  return dwarf64 ? (cie_id == 0xffffffffffffffff) : (cie_id == 0xffffffff);
1379
}
1380
 
1381
static int
1382
compare_fde_unit (const void *a, const void *b)
1383
{
1384
  struct fde_unit **first, **second;
1385
  first = (struct fde_unit **) a;
1386
  second = (struct fde_unit **) b;
1387
  if ((*first)->initial_location > (*second)->initial_location)
1388
    return 1;
1389
  else if ((*first)->initial_location < (*second)->initial_location)
1390
    return -1;
1391
  else
1392
    return 0;
1393
}
1394
 
1395
/*  Build the cie_chunks and fde_chunks tables from informations
1396
    found in .debug_frame and .eh_frame sections.  */
1397
/* We can handle both of these sections almost in the same way, however there
1398
   are some exceptions:
1399
   - CIE ID is -1 in debug_frame, but 0 in eh_frame
1400
   - eh_frame may contain some more information that are used only by gcc
1401
     (eg. personality pointer, LSDA pointer, ...). Most of them we can ignore.
1402
   - In debug_frame FDE's item cie_id contains offset of it's parent CIE.
1403
     In eh_frame FDE's item cie_id is a relative pointer to the parent CIE.
1404
     Anyway we don't need to bother with this, because we are smart enough
1405
     to keep the pointer to the parent CIE of oncomming FDEs in 'last_cie'.
1406
   - Although debug_frame items can contain Augmentation as well as
1407
     eh_frame ones, I have never seen them non-empty. Thus only in eh_frame
1408
     we can encounter for example non-absolute pointers (Aug. 'R').
1409
                                                              -- mludvig  */
1410
static void
1411
parse_frame_info (struct objfile *objfile, file_ptr frame_offset,
1412
                  unsigned int frame_size, int eh_frame)
1413
{
1414
  bfd *abfd = objfile->obfd;
1415
  asection *curr_section_ptr;
1416
  char *start = NULL;
1417
  char *end = NULL;
1418
  char *frame_buffer = NULL;
1419
  char *curr_section_name, *aug_data;
1420
  struct cie_unit *last_cie = NULL;
1421
  int last_dup_fde = 0;
1422
  int aug_len, i;
1423
  CORE_ADDR curr_section_vma = 0;
1424
 
1425
  unwind_tmp_obstack_init ();
1426
 
1427
  frame_buffer = dwarf2_read_section (objfile, frame_offset, frame_size);
1428
 
1429
  start = frame_buffer;
1430
  end = frame_buffer + frame_size;
1431
 
1432
  curr_section_name = eh_frame ? ".eh_frame" : ".debug_frame";
1433
  curr_section_ptr = bfd_get_section_by_name (abfd, curr_section_name);
1434
  if (curr_section_ptr)
1435
    curr_section_vma = curr_section_ptr->vma;
1436
 
1437
  if (start)
1438
    {
1439
      while (start < end)
1440
        {
1441
          unsigned long length;
1442
          ULONGEST cie_id;
1443
          ULONGEST unit_offset = start - frame_buffer;
1444
          int bytes_read, dwarf64;
1445
          char *block_end;
1446
 
1447
          length = read_initial_length (abfd, start, &bytes_read);
1448
          start += bytes_read;
1449
          dwarf64 = (bytes_read == 12);
1450
          block_end = start + length;
1451
 
1452
          if (length == 0)
1453
            {
1454
              start = block_end;
1455
              continue;
1456
            }
1457
 
1458
          cie_id = read_length (abfd, start, &bytes_read, dwarf64);
1459
          start += bytes_read;
1460
 
1461
          if ((eh_frame && cie_id == 0) || is_cie (cie_id, dwarf64))
1462
            {
1463
              struct cie_unit *cie = cie_unit_alloc ();
1464
              char *aug;
1465
 
1466
              cie->objfile = objfile;
1467
              cie->next = cie_chunks;
1468
              cie_chunks = cie;
1469
 
1470
              cie->objfile = objfile;
1471
 
1472
              cie->offset = unit_offset;
1473
 
1474
              start++;          /* version */
1475
 
1476
              cie->augmentation = aug = start;
1477
              while (*start++); /* Skips last NULL as well */
1478
 
1479
              cie->code_align = read_uleb128 (abfd, &start);
1480
              cie->data_align = read_sleb128 (abfd, &start);
1481
              cie->ra = read_1u (abfd, &start);
1482
 
1483
              /* Augmentation:
1484
                 z      Indicates that a uleb128 is present to size the
1485
                 augmentation section.
1486
                 L      Indicates the encoding (and thus presence) of
1487
                 an LSDA pointer in the FDE augmentation.
1488
                 R      Indicates a non-default pointer encoding for
1489
                 FDE code pointers.
1490
                 P      Indicates the presence of an encoding + language
1491
                 personality routine in the CIE augmentation.
1492
 
1493
                 [This info comes from GCC's dwarf2out.c]
1494
               */
1495
              if (*aug == 'z')
1496
                {
1497
                  aug_len = read_uleb128 (abfd, &start);
1498
                  aug_data = start;
1499
                  start += aug_len;
1500
                  ++aug;
1501
                }
1502
 
1503
              cie->data = start;
1504
              cie->data_length = block_end - cie->data;
1505
 
1506
              while (*aug != '\0')
1507
                {
1508
                  if (aug[0] == 'e' && aug[1] == 'h')
1509
                    {
1510
                      aug_data += sizeof (void *);
1511
                      aug++;
1512
                    }
1513
                  else if (aug[0] == 'R')
1514
                    cie->addr_encoding = *aug_data++;
1515
                  else if (aug[0] == 'P')
1516
                    {
1517
                      CORE_ADDR pers_addr;
1518
                      int pers_addr_enc;
1519
 
1520
                      pers_addr_enc = *aug_data++;
1521
                      /* We don't need pers_addr value and so we
1522
                         don't care about it's encoding.  */
1523
                      pers_addr = read_encoded_pointer (abfd, &aug_data,
1524
                                                        pers_addr_enc);
1525
                    }
1526
                  else if (aug[0] == 'L' && eh_frame)
1527
                    {
1528
                      int lsda_addr_enc;
1529
 
1530
                      /* Perhaps we should save this to CIE for later use?
1531
                         Do we need it for something in GDB?  */
1532
                      lsda_addr_enc = *aug_data++;
1533
                    }
1534
                  else
1535
                    warning ("CFI warning: unknown augmentation \"%c\""
1536
                             " in \"%s\" of\n"
1537
                             "\t%s", aug[0], curr_section_name,
1538
                             objfile->name);
1539
                  aug++;
1540
                }
1541
 
1542
              last_cie = cie;
1543
            }
1544
          else
1545
            {
1546
              struct fde_unit *fde;
1547
              struct cie_unit *cie;
1548
              int dup = 0;
1549
              CORE_ADDR init_loc;
1550
 
1551
              /* We assume that debug_frame is in order
1552
                 CIE,FDE,CIE,FDE,FDE,...  and thus the CIE for this FDE
1553
                 should be stored in last_cie pointer. If not, we'll
1554
                 try to find it by the older way.  */
1555
              if (last_cie)
1556
                cie = last_cie;
1557
              else
1558
                {
1559
                  warning ("CFI: last_cie == NULL. "
1560
                           "Perhaps a malformed %s section in '%s'...?\n",
1561
                           curr_section_name, objfile->name);
1562
 
1563
                  cie = cie_chunks;
1564
                  while (cie)
1565
                    {
1566
                      if (cie->objfile == objfile)
1567
                        {
1568
                          if (eh_frame &&
1569
                              (cie->offset ==
1570
                               (unit_offset + bytes_read - cie_id)))
1571
                            break;
1572
                          if (!eh_frame && (cie->offset == cie_id))
1573
                            break;
1574
                        }
1575
 
1576
                      cie = cie->next;
1577
                    }
1578
                  if (!cie)
1579
                    error ("CFI: can't find CIE pointer");
1580
                }
1581
 
1582
              init_loc = read_encoded_pointer (abfd, &start,
1583
                                               cie->addr_encoding);
1584
 
1585
              switch (pointer_encoding (cie->addr_encoding))
1586
                {
1587
                case PE_absptr:
1588
                  break;
1589
                case PE_pcrel:
1590
                  /* start-frame_buffer gives offset from
1591
                     the beginning of actual section.  */
1592
                  init_loc += curr_section_vma + start - frame_buffer;
1593
                  break;
1594
                default:
1595
                  warning ("CFI: Unsupported pointer encoding\n");
1596
                }
1597
 
1598
              /* For relocatable objects we must add an offset telling
1599
                 where the section is actually mapped in the memory.  */
1600
              init_loc += ANOFFSET (objfile->section_offsets,
1601
                                    SECT_OFF_TEXT (objfile));
1602
 
1603
              /* If we have both .debug_frame and .eh_frame present in
1604
                 a file, we must eliminate duplicate FDEs. For now we'll
1605
                 run through all entries in fde_chunks and check it one
1606
                 by one. Perhaps in the future we can implement a faster
1607
                 searching algorithm.  */
1608
              /* eh_frame==2 indicates, that this file has an already
1609
                 parsed .debug_frame too. When eh_frame==1 it means, that no
1610
                 .debug_frame is present and thus we don't need to check for
1611
                 duplicities. eh_frame==0 means, that we parse .debug_frame
1612
                 and don't need to care about duplicate FDEs, because
1613
                 .debug_frame is parsed first.  */
1614
              if (eh_frame == 2)
1615
                for (i = 0; eh_frame == 2 && i < fde_chunks.elems; i++)
1616
                  {
1617
                    /* We assume that FDEs in .debug_frame and .eh_frame
1618
                       have the same order (if they are present, of course).
1619
                       If we find a duplicate entry for one FDE and save
1620
                       it's index to last_dup_fde it's very likely, that
1621
                       we'll find an entry for the following FDE right after
1622
                       the previous one. Thus in many cases we'll run this
1623
                       loop only once.  */
1624
                    last_dup_fde = (last_dup_fde + i) % fde_chunks.elems;
1625
                    if (fde_chunks.array[last_dup_fde]->initial_location
1626
                        == init_loc)
1627
                      {
1628
                        dup = 1;
1629
                        break;
1630
                      }
1631
                  }
1632
 
1633
              /* Allocate a new entry only if this FDE isn't a duplicate of
1634
                 something we have already seen.   */
1635
              if (!dup)
1636
                {
1637
                  fde_chunks_need_space ();
1638
                  fde = fde_unit_alloc ();
1639
 
1640
                  fde_chunks.array[fde_chunks.elems++] = fde;
1641
 
1642
                  fde->initial_location = init_loc;
1643
                  fde->address_range = read_encoded_pointer (abfd, &start,
1644
                                                             cie->
1645
                                                             addr_encoding);
1646
 
1647
                  fde->cie_ptr = cie;
1648
 
1649
                  /* Here we intentionally ignore augmentation data
1650
                     from FDE, because we don't need them.  */
1651
                  if (cie->augmentation[0] == 'z')
1652
                    start += read_uleb128 (abfd, &start);
1653
 
1654
                  fde->data = start;
1655
                  fde->data_length = block_end - start;
1656
                }
1657
            }
1658
          start = block_end;
1659
        }
1660
      qsort (fde_chunks.array, fde_chunks.elems,
1661
             sizeof (struct fde_unit *), compare_fde_unit);
1662
    }
1663
}
1664
 
1665
/* We must parse both .debug_frame section and .eh_frame because
1666
 * not all frames must be present in both of these sections. */
1667
void
1668
dwarf2_build_frame_info (struct objfile *objfile)
1669
{
1670
  int after_debug_frame = 0;
1671
 
1672
  /* If we have .debug_frame then the parser is called with
1673
     eh_frame==0 for .debug_frame and eh_frame==2 for .eh_frame,
1674
     otherwise it's only called once for .eh_frame with argument
1675
     eh_frame==1.  */
1676
 
1677
  if (dwarf_frame_offset)
1678
    {
1679
      parse_frame_info (objfile, dwarf_frame_offset,
1680
                        dwarf_frame_size, 0 /* = debug_frame */ );
1681
      after_debug_frame = 1;
1682
    }
1683
 
1684
  if (dwarf_eh_frame_offset)
1685
    parse_frame_info (objfile, dwarf_eh_frame_offset, dwarf_eh_frame_size,
1686
                      1 /* = eh_frame */  + after_debug_frame);
1687
}
1688
 
1689
/* Return the frame address.  */
1690
CORE_ADDR
1691
cfi_read_fp (void)
1692
{
1693
  struct context *context;
1694
  struct frame_state *fs;
1695
  CORE_ADDR cfa;
1696
 
1697
  unwind_tmp_obstack_init ();
1698
 
1699
  context = context_alloc ();
1700
  fs = frame_state_alloc ();
1701
 
1702
  context->ra = read_pc () + 1;
1703
 
1704
  frame_state_for (context, fs);
1705
  update_context (context, fs, 0);
1706
 
1707
  cfa = context->cfa;
1708
 
1709
  unwind_tmp_obstack_free ();
1710
 
1711
  return cfa;
1712
}
1713
 
1714
/* Store the frame address.  This function is not used.  */
1715
 
1716
void
1717
cfi_write_fp (CORE_ADDR val)
1718
{
1719
  struct context *context;
1720
  struct frame_state *fs;
1721
 
1722
  unwind_tmp_obstack_init ();
1723
 
1724
  context = context_alloc ();
1725
  fs = frame_state_alloc ();
1726
 
1727
  context->ra = read_pc () + 1;
1728
 
1729
  frame_state_for (context, fs);
1730
 
1731
  if (fs->cfa_how == CFA_REG_OFFSET)
1732
    {
1733
      val -= fs->cfa_offset;
1734
      write_register_gen (fs->cfa_reg, (char *) &val);
1735
    }
1736
  else
1737
    warning ("Can't write fp.");
1738
 
1739
  unwind_tmp_obstack_free ();
1740
}
1741
 
1742
/* Restore the machine to the state it had before the current frame
1743
   was created.  */
1744
void
1745
cfi_pop_frame (struct frame_info *fi)
1746
{
1747
  char *regbuf = alloca (MAX_REGISTER_RAW_SIZE);
1748
  int regnum;
1749
 
1750
  for (regnum = 0; regnum < NUM_REGS; regnum++)
1751
    {
1752
      get_reg (regbuf, UNWIND_CONTEXT (fi), regnum);
1753
      write_register_bytes (REGISTER_BYTE (regnum), regbuf,
1754
                            REGISTER_RAW_SIZE (regnum));
1755
    }
1756
  write_register (PC_REGNUM, UNWIND_CONTEXT (fi)->ra);
1757
 
1758
  flush_cached_frames ();
1759
}
1760
 
1761
/* Determine the address of the calling function's frame.  */
1762
CORE_ADDR
1763
cfi_frame_chain (struct frame_info *fi)
1764
{
1765
  struct context *context;
1766
  struct frame_state *fs;
1767
  CORE_ADDR cfa;
1768
 
1769
  unwind_tmp_obstack_init ();
1770
 
1771
  context = context_alloc ();
1772
  fs = frame_state_alloc ();
1773
  context_cpy (context, UNWIND_CONTEXT (fi));
1774
 
1775
  /* outermost frame */
1776
  if (context->ra == 0)
1777
    {
1778
      unwind_tmp_obstack_free ();
1779
      return 0;
1780
    }
1781
 
1782
  frame_state_for (context, fs);
1783
  update_context (context, fs, 1);
1784
 
1785
  cfa = context->cfa;
1786
  unwind_tmp_obstack_free ();
1787
 
1788
  return cfa;
1789
}
1790
 
1791
/* Sets the pc of the frame.  */
1792
void
1793
cfi_init_frame_pc (int fromleaf, struct frame_info *fi)
1794
{
1795
  if (fi->next)
1796
    get_reg ((char *) &(fi->pc), UNWIND_CONTEXT (fi->next), PC_REGNUM);
1797
  else
1798
    fi->pc = read_pc ();
1799
}
1800
 
1801
/* Initialize unwind context informations of the frame.  */
1802
void
1803
cfi_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1804
{
1805
  struct frame_state *fs;
1806
 
1807
  unwind_tmp_obstack_init ();
1808
 
1809
  fs = frame_state_alloc ();
1810
  fi->context = frame_obstack_alloc (sizeof (struct context));
1811
  UNWIND_CONTEXT (fi)->reg =
1812
    frame_obstack_alloc (sizeof (struct context_reg) * NUM_REGS);
1813
  memset (UNWIND_CONTEXT (fi)->reg, 0,
1814
          sizeof (struct context_reg) * NUM_REGS);
1815
 
1816
  if (fi->next)
1817
    {
1818
      context_cpy (UNWIND_CONTEXT (fi), UNWIND_CONTEXT (fi->next));
1819
      frame_state_for (UNWIND_CONTEXT (fi), fs);
1820
      update_context (UNWIND_CONTEXT (fi), fs, 1);
1821
    }
1822
  else
1823
    {
1824
      UNWIND_CONTEXT (fi)->ra = fi->pc + 1;
1825
      frame_state_for (UNWIND_CONTEXT (fi), fs);
1826
      update_context (UNWIND_CONTEXT (fi), fs, 0);
1827
    }
1828
 
1829
  unwind_tmp_obstack_free ();
1830
}
1831
 
1832
/* Obtain return address of the frame.  */
1833
CORE_ADDR
1834
cfi_get_ra (struct frame_info *fi)
1835
{
1836
  return UNWIND_CONTEXT (fi)->ra;
1837
}
1838
 
1839
/* Find register number REGNUM relative to FRAME and put its
1840
   (raw) contents in *RAW_BUFFER.  Set *OPTIMIZED if the variable
1841
   was optimized out (and thus can't be fetched).  If the variable
1842
   was fetched from memory, set *ADDRP to where it was fetched from,
1843
   otherwise it was fetched from a register.
1844
 
1845
   The argument RAW_BUFFER must point to aligned memory.  */
1846
void
1847
cfi_get_saved_register (char *raw_buffer,
1848
                        int *optimized,
1849
                        CORE_ADDR *addrp,
1850
                        struct frame_info *frame,
1851
                        int regnum, enum lval_type *lval)
1852
{
1853
  if (!target_has_registers)
1854
    error ("No registers.");
1855
 
1856
  /* Normal systems don't optimize out things with register numbers.  */
1857
  if (optimized != NULL)
1858
    *optimized = 0;
1859
 
1860
  if (addrp)                    /* default assumption: not found in memory */
1861
    *addrp = 0;
1862
 
1863
  if (!frame->next)
1864
    {
1865
      read_register_gen (regnum, raw_buffer);
1866
      if (lval != NULL)
1867
        *lval = lval_register;
1868
      if (addrp != NULL)
1869
        *addrp = REGISTER_BYTE (regnum);
1870
    }
1871
  else
1872
    {
1873
      frame = frame->next;
1874
      switch (UNWIND_CONTEXT (frame)->reg[regnum].how)
1875
        {
1876
        case REG_CTX_UNSAVED:
1877
          read_register_gen (regnum, raw_buffer);
1878
          if (lval != NULL)
1879
            *lval = not_lval;
1880
          if (optimized != NULL)
1881
            *optimized = 1;
1882
          break;
1883
        case REG_CTX_SAVED_OFFSET:
1884
          target_read_memory (UNWIND_CONTEXT (frame)->cfa +
1885
                              UNWIND_CONTEXT (frame)->reg[regnum].loc.offset,
1886
                              raw_buffer, REGISTER_RAW_SIZE (regnum));
1887
          if (lval != NULL)
1888
            *lval = lval_memory;
1889
          if (addrp != NULL)
1890
            *addrp =
1891
              UNWIND_CONTEXT (frame)->cfa +
1892
              UNWIND_CONTEXT (frame)->reg[regnum].loc.offset;
1893
          break;
1894
        case REG_CTX_SAVED_REG:
1895
          read_register_gen (UNWIND_CONTEXT (frame)->reg[regnum].loc.reg,
1896
                             raw_buffer);
1897
          if (lval != NULL)
1898
            *lval = lval_register;
1899
          if (addrp != NULL)
1900
            *addrp =
1901
              REGISTER_BYTE (UNWIND_CONTEXT (frame)->reg[regnum].loc.reg);
1902
          break;
1903
        case REG_CTX_SAVED_ADDR:
1904
          target_read_memory (UNWIND_CONTEXT (frame)->reg[regnum].loc.addr,
1905
                              raw_buffer, REGISTER_RAW_SIZE (regnum));
1906
          if (lval != NULL)
1907
            *lval = lval_memory;
1908
          if (addrp != NULL)
1909
            *addrp = UNWIND_CONTEXT (frame)->reg[regnum].loc.addr;
1910
          break;
1911
        case REG_CTX_VALUE:
1912
          memcpy (raw_buffer, &UNWIND_CONTEXT (frame)->reg[regnum].loc.addr,
1913
                  REGISTER_RAW_SIZE (regnum));
1914
          if (lval != NULL)
1915
            *lval = not_lval;
1916
          if (optimized != NULL)
1917
            *optimized = 0;
1918
          break;
1919
        default:
1920
          internal_error (__FILE__, __LINE__,
1921
                          "cfi_get_saved_register: unknown register rule");
1922
        }
1923
    }
1924
}
1925
 
1926
/*  Return the register that the function uses for a frame pointer,
1927
    plus any necessary offset to be applied to the register before
1928
    any frame pointer offsets.  */
1929
void
1930
cfi_virtual_frame_pointer (CORE_ADDR pc, int *frame_reg,
1931
                           LONGEST * frame_offset)
1932
{
1933
  struct context *context;
1934
  struct frame_state *fs;
1935
 
1936
  unwind_tmp_obstack_init ();
1937
 
1938
  context = context_alloc ();
1939
  fs = frame_state_alloc ();
1940
 
1941
  context->ra = read_pc () + 1;
1942
 
1943
  frame_state_for (context, fs);
1944
 
1945
  if (fs->cfa_how == CFA_REG_OFFSET)
1946
    {
1947
      *frame_reg = fs->cfa_reg;
1948
      *frame_offset = fs->cfa_offset;
1949
    }
1950
  else
1951
    error ("dwarf cfi error: CFA is not defined as CFA_REG_OFFSET");
1952
 
1953
  unwind_tmp_obstack_free ();
1954
}

powered by: WebSVN 2.1.0

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