libdd-libunwind-sys 1.0.2

Rust bindings for using unwind library inside libdatadog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* libunwind - a platform-independent unwind library
   Copyright (C) 2006-2007 IBM
   Contributed by
     Corey Ashford <cjashfor@us.ibm.com>
     Jose Flavio Aguilar Paulino <jflavio@br.ibm.com> <joseflavio@gmail.com>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#include "unwind_i.h"
#include "ucontext_i.h"
#include "remote.h"
#include <signal.h>

/* This definition originates in /usr/include/asm-ppc64/ptrace.h, but is
   defined there only when __KERNEL__ is defined.  We reproduce it here for
   our use at the user level in order to locate the ucontext record, which
   appears to be at this offset relative to the stack pointer when in the
   context of the signal handler return trampoline code -
   __kernel_sigtramp_rt64.  */
#define __SIGNAL_FRAMESIZE 128

/* This definition comes from the document "64-bit PowerPC ELF Application
   Binary Interface Supplement 1.9", section 3.2.2.
   http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK */

typedef struct
{
  long unsigned back_chain;
  long unsigned cr_save;
  long unsigned lr_save;
  /* many more fields here, but they are unused by this code */
} stack_frame_t;

//! Recognise PLT entries
/*! For example:
   0x000000001000d1f0 <+0>:     18 00 41 f8     std     r2,24(r1)
   0x000000001000d1f4 <+4>:     30 87 82 e9     ld      r12,-30928(r2)
   0x000000001000d1f8 <+8>:     a6 03 89 7d     mtctr   r12
   0x000000001000d1fc <+12>:    20 04 80 4e     bctr

  \note The current implementation only supports little endian modes.
*/
static int
_is_plt_entry (struct dwarf_cursor *c)
{
  unw_word_t w0, w1;
  unw_accessors_t *a;

  if (c->as->big_endian)
    {
      return 0;
    }

  /*
    A PLT (Procedure Linkage Table) is used by the dynamic linker to map the
    relative address of a position independent function call onto the real
    address of the function. If we attempt to unwind from any instruction
    inside the PLT, and the PLT is missing DWARF unwind information, then
    conventional unwinding will fail because although the function has been
    "called" we have not yet entered the prologue and set-up the stack frame.

    This code looks to see if the instruction is anywhere within a "recognised"
    PLT entry (note that the IP could be anywhere within the PLT, so we have to
    examine nearby instructions).
  */

  struct instruction_entry
    {
      uint32_t pattern;
      uint32_t mask;
    } instructions[4] =
    {
      // ppc64le
      {0xf8410018,0xffffffff},
      {0xe9820000,0xffff0000},
      {0x7d8903a6,0xffffffff},
      {0x4e800420,0xffffffff},
    };

  a = unw_get_accessors (c->as);

  if( (*a->access_mem) (c->as, c->ip, &w0, 0, c->as_arg) < 0 )
    {
      return 0;
    }

  /*
    NB: the following code is endian sensitive!

    The current implementation is for little-endian modes, big-endian modes
    will see the first instruction in the high bits of w0, and the second
    instruction in the low bits of w0. Some tweaks will be needed to read from
    the correct part of the word to support big endian modes.
  */
  if(( w0      & instructions[0].mask) == instructions[0].pattern &&
     ((w0>>32) & instructions[1].mask) == instructions[1].pattern)
  {
    if( (*a->access_mem) (c->as, c->ip+8, &w1, 0, c->as_arg) >= 0 &&
        ( w1      & instructions[2].mask) == instructions[2].pattern &&
        ((w1>>32) & instructions[3].mask) == instructions[3].pattern )
      {
        return 1;
      }
    else
      {
        return 0;
      }
  }
  else if(( w0 & instructions[2].mask) == instructions[2].pattern &&
     ((w0>>32) & instructions[3].mask) == instructions[3].pattern)
  {
    w1 = w0;
    if( (*a->access_mem) (c->as, c->ip-8, &w0, 0, c->as_arg) >= 0 &&
        ( w0      & instructions[0].mask) == instructions[0].pattern &&
        ((w0>>32) & instructions[1].mask) == instructions[1].pattern )
      {
        return 1;
      }
    else
      {
        return 0;
      }
  }
  else if(( w0 & instructions[1].mask) == instructions[1].pattern &&
     ((w0>>32) & instructions[2].mask) == instructions[2].pattern)
  {
    if( (*a->access_mem) (c->as, c->ip-4, &w0, 0, c->as_arg) < 0 ||
        (*a->access_mem) (c->as, c->ip+4, &w1, 0, c->as_arg) < 0 )
    {
      return 0;
    }
  }
  else if( (w0 & instructions[3].mask) == instructions[3].pattern )
  {
    if( (*a->access_mem) (c->as, c->ip-12, &w0, 0, c->as_arg) < 0 ||
        (*a->access_mem) (c->as, c->ip-4, &w1, 0, c->as_arg) < 0 )
    {
      return 0;
    }
  }

  if(( w0      & instructions[0].mask) == instructions[0].pattern &&
     ((w0>>32) & instructions[1].mask) == instructions[1].pattern &&
     ( w1      & instructions[2].mask) == instructions[2].pattern &&
     ((w1>>32) & instructions[3].mask) == instructions[3].pattern )
    {
      return 1;
    }
  else
    {
      return 0;
    }
}


int
unw_is_plt_entry (unw_cursor_t *uc)
{
	return _is_plt_entry (&((struct cursor *)uc)->dwarf);
}


int
unw_step (unw_cursor_t * cursor)
{
  struct cursor *c = (struct cursor *) cursor;
  stack_frame_t dummy;
  unw_word_t back_chain_offset, lr_save_offset, v_regs_ptr;
  struct dwarf_loc back_chain_loc, lr_save_loc, sp_loc, ip_loc, v_regs_loc;
  int ret, i;
  int validate = c->dwarf.as->validate;

  Debug (1, "(cursor=%p, ip=0x%016lx)\n", c, (unsigned long) c->dwarf.ip);

  /* Try DWARF-based unwinding... */

  c->dwarf.as->validate = 1;
  ret = dwarf_step (&c->dwarf);
  c->dwarf.as->validate = validate;

  if (ret < 0 && ret != -UNW_ENOINFO)
    {
      Debug (2, "returning %d\n", ret);
      return ret;
    }

  if (unlikely (ret < 0))
    {
      if (likely (unw_is_signal_frame (cursor) <= 0))
        {
          /* DWARF failed. */
          if (_is_plt_entry (&c->dwarf))
            {
              Debug (2, "found plt entry\n");

              /*
                Fallback mode that uses the link register. This is important
                for cases where a function without unwind information has been
                called, but not yet set-up its stack frame (basically PLT calls).

                In this case we can't trust c->dwarf.cfa (the stack frame)
                because it will currently point to the caller's stack frame -
                but we can use the current value of the link register to get
                back to the caller. We then have to hope that libunwind manages
                to resume unwinding properly from the caller IP.
              */
              c->dwarf.loc[UNW_PPC64_NIP] = c->dwarf.loc[UNW_PPC64_LR];
              c->dwarf.loc[UNW_PPC64_LR] = DWARF_NULL_LOC;
              if (!DWARF_IS_NULL_LOC (c->dwarf.loc[UNW_PPC64_NIP]))
                {
                  ret = dwarf_get (&c->dwarf, c->dwarf.loc[UNW_PPC64_NIP], &c->dwarf.ip);
                  if (ret < 0)
                    {
                      Debug (2, "failed to get pc from link register: %d\n", ret);
                      return ret;
                    }
                  Debug (2, "link register = 0x%016lx\n", c->dwarf.ip);
                  ret = 1;
                }
              else
                {
                  Debug (2, "link register was not saved\n");
                  c->dwarf.ip = 0;
                }
            }
          else
            {
              /*
                Fallback mode that uses a conventional stack unwinding. This is
                important for functions without proper DWARF unwind information,
                in particular without this fallback the clone() function will not
                unwind properly.

                We do the unwind by first looking for the caller stack pointer
                saved in the current stack frame. This will point to the caller's
                'linkage area'. If the caller stack pointer is null, we assume we
                have reached the top of the stack.

                The address 24(SP) (where SP is the caller stack pointer) contains
                the saved 'link register', the link register is effectively the
                return address for the called function - so we can use the link
                register to get an IP inside the calling function.

                Note: there is no requirement for leaf functions to save the
                stack pointer or link register, I'm not entirely sure why
                libunwind doesn't handle this case.
              */
              Debug (2, "fallback\n");

              back_chain_offset = ((void *) &dummy.back_chain - (void *) &dummy);
              lr_save_offset = ((void *) &dummy.lr_save - (void *) &dummy);

              back_chain_loc = DWARF_LOC (c->dwarf.cfa + back_chain_offset, 0);

              if ((ret =
                   dwarf_get (&c->dwarf, back_chain_loc, &c->dwarf.cfa)) < 0)
                {
                  Debug (2,
                     "Unable to retrieve CFA from back chain in stack frame - %d\n",
                     ret);
                  return ret;
                }
              if (c->dwarf.cfa == 0)
                /* Unless the cursor or stack is corrupt or uninitialized we've most
                   likely hit the top of the stack */
                return 0;

              lr_save_loc = DWARF_LOC (c->dwarf.cfa + lr_save_offset, 0);

              if ((ret = dwarf_get (&c->dwarf, lr_save_loc, &c->dwarf.ip)) < 0)
                {
                  Debug (2,
                     "Unable to retrieve IP from lr save in stack frame - %d\n",
                     ret);
                  return ret;
                }
              ret = 1;
              /* Mark all registers unsaved */
              for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
                c->dwarf.loc[i] = DWARF_NULL_LOC;
            }
        }
      else
        {
          /* Find the sigcontext record by taking the CFA and adjusting by
             the dummy signal frame size.

             Note that there isn't any way to determined if SA_SIGINFO was
             set in the sa_flags parameter to sigaction when the signal
             handler was established.  If it was not set, the ucontext
             record is not required to be on the stack, in which case the
             following code will likely cause a seg fault or other crash
             condition.  */

          unw_word_t ucontext = c->dwarf.cfa + __SIGNAL_FRAMESIZE;

          Debug (1, "signal frame, skip over trampoline\n");

          c->sigcontext_format = PPC_SCF_LINUX_RT_SIGFRAME;
          c->sigcontext_addr = ucontext;

          sp_loc = DWARF_LOC ((ucontext + UC_MCONTEXT_GREGS_R1), 0);
          ip_loc = DWARF_LOC ((ucontext + UC_MCONTEXT_GREGS_NIP), 0);

          ret = dwarf_get (&c->dwarf, sp_loc, &c->dwarf.cfa);
          if (ret < 0)
            {
              Debug (2, "returning %d\n", ret);
              return ret;
            }
          ret = dwarf_get (&c->dwarf, ip_loc, &c->dwarf.ip);
          if (ret < 0)
            {
              Debug (2, "returning %d\n", ret);
              return ret;
            }

          /* Instead of just restoring the non-volatile registers, do all
             of the registers for now.  This will incur a performance hit,
             but it's rare enough not to cause too much of a problem, and
             might be useful in some cases.  */
          c->dwarf.loc[UNW_PPC64_R0] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R0, 0);
          c->dwarf.loc[UNW_PPC64_R1] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R1, 0);
          c->dwarf.loc[UNW_PPC64_R2] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R2, 0);
          c->dwarf.loc[UNW_PPC64_R3] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R3, 0);
          c->dwarf.loc[UNW_PPC64_R4] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R4, 0);
          c->dwarf.loc[UNW_PPC64_R5] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R5, 0);
          c->dwarf.loc[UNW_PPC64_R6] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R6, 0);
          c->dwarf.loc[UNW_PPC64_R7] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R7, 0);
          c->dwarf.loc[UNW_PPC64_R8] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R8, 0);
          c->dwarf.loc[UNW_PPC64_R9] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R9, 0);
          c->dwarf.loc[UNW_PPC64_R10] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R10, 0);
          c->dwarf.loc[UNW_PPC64_R11] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R11, 0);
          c->dwarf.loc[UNW_PPC64_R12] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R12, 0);
          c->dwarf.loc[UNW_PPC64_R13] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R13, 0);
          c->dwarf.loc[UNW_PPC64_R14] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R14, 0);
          c->dwarf.loc[UNW_PPC64_R15] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R15, 0);
          c->dwarf.loc[UNW_PPC64_R16] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R16, 0);
          c->dwarf.loc[UNW_PPC64_R17] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R17, 0);
          c->dwarf.loc[UNW_PPC64_R18] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R18, 0);
          c->dwarf.loc[UNW_PPC64_R19] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R19, 0);
          c->dwarf.loc[UNW_PPC64_R20] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R20, 0);
          c->dwarf.loc[UNW_PPC64_R21] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R21, 0);
          c->dwarf.loc[UNW_PPC64_R22] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R22, 0);
          c->dwarf.loc[UNW_PPC64_R23] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R23, 0);
          c->dwarf.loc[UNW_PPC64_R24] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R24, 0);
          c->dwarf.loc[UNW_PPC64_R25] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R25, 0);
          c->dwarf.loc[UNW_PPC64_R26] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R26, 0);
          c->dwarf.loc[UNW_PPC64_R27] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R27, 0);
          c->dwarf.loc[UNW_PPC64_R28] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R28, 0);
          c->dwarf.loc[UNW_PPC64_R29] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R29, 0);
          c->dwarf.loc[UNW_PPC64_R30] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R30, 0);
          c->dwarf.loc[UNW_PPC64_R31] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R31, 0);

          c->dwarf.loc[UNW_PPC64_LR] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_LINK, 0);
          c->dwarf.loc[UNW_PPC64_CTR] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_CTR, 0);
          /* This CR0 assignment is probably wrong.  There are 8 dwarf columns
             assigned to the CR registers, but only one CR register in the
             mcontext structure */
          c->dwarf.loc[UNW_PPC64_CR0] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_CCR, 0);
          c->dwarf.loc[UNW_PPC64_XER] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_XER, 0);
          c->dwarf.loc[UNW_PPC64_NIP] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_NIP, 0);

          /* TODO: Is there a way of obtaining the value of the
             pseudo frame pointer (which is sp + some fixed offset, I
             assume), based on the contents of the ucontext record
             structure?  For now, set this loc to null. */
          c->dwarf.loc[UNW_PPC64_FRAME_POINTER] = DWARF_NULL_LOC;

          c->dwarf.loc[UNW_PPC64_F0] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R0, 0);
          c->dwarf.loc[UNW_PPC64_F1] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R1, 0);
          c->dwarf.loc[UNW_PPC64_F2] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R2, 0);
          c->dwarf.loc[UNW_PPC64_F3] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R3, 0);
          c->dwarf.loc[UNW_PPC64_F4] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R4, 0);
          c->dwarf.loc[UNW_PPC64_F5] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R5, 0);
          c->dwarf.loc[UNW_PPC64_F6] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R6, 0);
          c->dwarf.loc[UNW_PPC64_F7] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R7, 0);
          c->dwarf.loc[UNW_PPC64_F8] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R8, 0);
          c->dwarf.loc[UNW_PPC64_F9] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R9, 0);
          c->dwarf.loc[UNW_PPC64_F10] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R10, 0);
          c->dwarf.loc[UNW_PPC64_F11] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R11, 0);
          c->dwarf.loc[UNW_PPC64_F12] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R12, 0);
          c->dwarf.loc[UNW_PPC64_F13] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R13, 0);
          c->dwarf.loc[UNW_PPC64_F14] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R14, 0);
          c->dwarf.loc[UNW_PPC64_F15] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R15, 0);
          c->dwarf.loc[UNW_PPC64_F16] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R16, 0);
          c->dwarf.loc[UNW_PPC64_F17] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R17, 0);
          c->dwarf.loc[UNW_PPC64_F18] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R18, 0);
          c->dwarf.loc[UNW_PPC64_F19] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R19, 0);
          c->dwarf.loc[UNW_PPC64_F20] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R20, 0);
          c->dwarf.loc[UNW_PPC64_F21] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R21, 0);
          c->dwarf.loc[UNW_PPC64_F22] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R22, 0);
          c->dwarf.loc[UNW_PPC64_F23] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R23, 0);
          c->dwarf.loc[UNW_PPC64_F24] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R24, 0);
          c->dwarf.loc[UNW_PPC64_F25] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R25, 0);
          c->dwarf.loc[UNW_PPC64_F26] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R26, 0);
          c->dwarf.loc[UNW_PPC64_F27] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R27, 0);
          c->dwarf.loc[UNW_PPC64_F28] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R28, 0);
          c->dwarf.loc[UNW_PPC64_F29] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R29, 0);
          c->dwarf.loc[UNW_PPC64_F30] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R30, 0);
          c->dwarf.loc[UNW_PPC64_F31] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R31, 0);
          /* Note that there is no .eh_section register column for the
             FPSCR register.  I don't know why this is.  */

#if defined(__linux__)
          v_regs_loc = DWARF_LOC (ucontext + UC_MCONTEXT_V_REGS, 0);
          ret = dwarf_get (&c->dwarf, v_regs_loc, &v_regs_ptr);
#elif defined(__FreeBSD__)
          /* Offset into main structure. */
          v_regs_ptr = (ucontext + UC_MCONTEXT_V_REGS);
          ret = 0;
#endif

          if (ret < 0)
            {
              Debug (2, "returning %d\n", ret);
              return ret;
            }
          if (v_regs_ptr != 0)
            {
              /* The v_regs_ptr is not null.  Set all of the AltiVec locs */

              c->dwarf.loc[UNW_PPC64_V0] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R0, 0);
              c->dwarf.loc[UNW_PPC64_V1] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R1, 0);
              c->dwarf.loc[UNW_PPC64_V2] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R2, 0);
              c->dwarf.loc[UNW_PPC64_V3] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R3, 0);
              c->dwarf.loc[UNW_PPC64_V4] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R4, 0);
              c->dwarf.loc[UNW_PPC64_V5] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R5, 0);
              c->dwarf.loc[UNW_PPC64_V6] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R6, 0);
              c->dwarf.loc[UNW_PPC64_V7] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R7, 0);
              c->dwarf.loc[UNW_PPC64_V8] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R8, 0);
              c->dwarf.loc[UNW_PPC64_V9] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R9, 0);
              c->dwarf.loc[UNW_PPC64_V10] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R10, 0);
              c->dwarf.loc[UNW_PPC64_V11] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R11, 0);
              c->dwarf.loc[UNW_PPC64_V12] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R12, 0);
              c->dwarf.loc[UNW_PPC64_V13] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R13, 0);
              c->dwarf.loc[UNW_PPC64_V14] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R14, 0);
              c->dwarf.loc[UNW_PPC64_V15] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R15, 0);
              c->dwarf.loc[UNW_PPC64_V16] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R16, 0);
              c->dwarf.loc[UNW_PPC64_V17] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R17, 0);
              c->dwarf.loc[UNW_PPC64_V18] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R18, 0);
              c->dwarf.loc[UNW_PPC64_V19] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R19, 0);
              c->dwarf.loc[UNW_PPC64_V20] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R20, 0);
              c->dwarf.loc[UNW_PPC64_V21] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R21, 0);
              c->dwarf.loc[UNW_PPC64_V22] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R22, 0);
              c->dwarf.loc[UNW_PPC64_V23] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R23, 0);
              c->dwarf.loc[UNW_PPC64_V24] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R24, 0);
              c->dwarf.loc[UNW_PPC64_V25] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R25, 0);
              c->dwarf.loc[UNW_PPC64_V26] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R26, 0);
              c->dwarf.loc[UNW_PPC64_V27] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R27, 0);
              c->dwarf.loc[UNW_PPC64_V28] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R28, 0);
              c->dwarf.loc[UNW_PPC64_V29] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R29, 0);
              c->dwarf.loc[UNW_PPC64_V30] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R30, 0);
              c->dwarf.loc[UNW_PPC64_V31] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R31, 0);
              c->dwarf.loc[UNW_PPC64_VRSAVE] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_VRSAVE, 0);
              c->dwarf.loc[UNW_PPC64_VSCR] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_VSCR, 0);
            }
          else
            {
              c->dwarf.loc[UNW_PPC64_V0] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V1] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V2] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V3] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V4] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V5] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V6] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V7] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V8] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V9] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V10] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V11] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V12] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V13] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V14] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V15] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V16] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V17] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V18] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V19] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V20] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V21] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V22] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V23] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V24] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V25] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V26] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V27] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V28] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V29] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V30] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V31] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_VRSAVE] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_VSCR] = DWARF_NULL_LOC;
            }
          ret = 1;
        }
    }

  if (c->dwarf.ip == 0)
    {
      /* Unless the cursor or stack is corrupt or uninitialized,
         we've most likely hit the top of the stack */
      Debug (2, "returning 0\n");
      return 0;
    }

  // on ppc64, R2 register is used as pointer to TOC
  // section which is used for symbol lookup in PIC code
  // ppc64 linker generates "ld r2, 40(r1)" (ELFv1) or
  // "ld r2, 24(r1)" (ELFv2) instruction after each
  // @plt call. We need restore R2, but only for @plt calls
  {
    unw_word_t ip = c->dwarf.ip;
    unw_addr_space_t as = c->dwarf.as;
    unw_accessors_t *a = unw_get_accessors_int (as);
    void *arg = c->dwarf.as_arg;
    uint32_t toc_save = (as->abi == UNW_PPC64_ABI_ELFv2)? 24 : 40;
    int32_t inst;

    if (fetch32 (as, a, &ip, &inst, arg) >= 0
	&& (uint32_t)inst == (0xE8410000U + toc_save))
      {
	// @plt call, restoring R2 from CFA+toc_save
	c->dwarf.loc[UNW_PPC64_R2] = DWARF_LOC(c->dwarf.cfa + toc_save, 0);
      }
  }

  Debug (2, "returning %d with last return statement\n", ret);
  return ret;
}