1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2010, 2011 by FERMI NATIONAL ACCELERATOR LABORATORY
3
4This file is part of libunwind.
5
6Permission is hereby granted, free of charge, to any person obtaining
7a copy of this software and associated documentation files (the
8"Software"), to deal in the Software without restriction, including
9without limitation the rights to use, copy, modify, merge, publish,
10distribute, sublicense, and/or sell copies of the Software, and to
11permit persons to whom the Software is furnished to do so, subject to
12the following conditions:
13
14The above copyright notice and this permission notice shall be
15included in all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24
25#include "unwind_i.h"
26#include "dwarf_i.h"
27#include "offsets.h"
28
29HIDDEN void
30tdep_stash_frame (struct dwarf_cursor *d, struct dwarf_reg_state *rs)
31{
32  struct cursor *c = (struct cursor *) dwarf_to_cursor (d);
33  unw_tdep_frame_t *f = &c->frame_info;
34
35  Debug (4, "ip=0x%lx cfa=0x%lx type %d cfa [where=%d val=%ld] cfaoff=%ld"
36         " ra=0x%lx rbp [where=%d val=%ld @0x%lx] rsp [where=%d val=%ld @0x%lx]\n",
37         d->ip, d->cfa, f->frame_type,
38         rs->reg[DWARF_CFA_REG_COLUMN].where,
39         rs->reg[DWARF_CFA_REG_COLUMN].val,
40         rs->reg[DWARF_CFA_OFF_COLUMN].val,
41         DWARF_GET_LOC(d->loc[d->ret_addr_column]),
42         rs->reg[RBP].where, rs->reg[RBP].val, DWARF_GET_LOC(d->loc[RBP]),
43         rs->reg[RSP].where, rs->reg[RSP].val, DWARF_GET_LOC(d->loc[RSP]));
44
45  /* A standard frame is defined as:
46      - CFA is register-relative offset off RBP or RSP;
47      - Return address is saved at CFA-8;
48      - RBP is unsaved or saved at CFA+offset, offset != -1;
49      - RSP is unsaved or saved at CFA+offset, offset != -1.  */
50  if (f->frame_type == UNW_X86_64_FRAME_OTHER
51      && (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_REG)
52      && (rs->reg[DWARF_CFA_REG_COLUMN].val == RBP
53          || rs->reg[DWARF_CFA_REG_COLUMN].val == RSP)
54      && labs((long) rs->reg[DWARF_CFA_OFF_COLUMN].val) < (1 << 29)
55      && DWARF_GET_LOC(d->loc[d->ret_addr_column]) == d->cfa-8
56      && (rs->reg[RBP].where == DWARF_WHERE_UNDEF
57          || rs->reg[RBP].where == DWARF_WHERE_SAME
58          || (rs->reg[RBP].where == DWARF_WHERE_CFAREL
59              && labs((long) rs->reg[RBP].val) < (1 << 14)
60              && rs->reg[RBP].val+1 != 0))
61      && (rs->reg[RSP].where == DWARF_WHERE_UNDEF
62          || rs->reg[RSP].where == DWARF_WHERE_SAME
63          || (rs->reg[RSP].where == DWARF_WHERE_CFAREL
64              && labs((long) rs->reg[RSP].val) < (1 << 14)
65              && rs->reg[RSP].val+1 != 0)))
66  {
67    /* Save information for a standard frame. */
68    f->frame_type = UNW_X86_64_FRAME_STANDARD;
69    f->cfa_reg_sp = (rs->reg[DWARF_CFA_REG_COLUMN].val == RSP);
70    f->cfa_reg_offset = rs->reg[DWARF_CFA_OFF_COLUMN].val;
71    if (rs->reg[RBP].where == DWARF_WHERE_CFAREL)
72      f->fp_cfa_offset = rs->reg[RBP].val;
73    if (rs->reg[RSP].where == DWARF_WHERE_CFAREL)
74      f->sp_cfa_offset = rs->reg[RSP].val;
75    Debug (4, " standard frame\n");
76  }
77
78#ifdef __linux__
79  /* Signal frame was detected via augmentation in tdep_fetch_frame()  */
80  else if (f->frame_type == UNW_X86_64_FRAME_SIGRETURN)
81  {
82    /* Later we are going to fish out {RBP,RSP,RIP} from sigcontext via
83       their ucontext_t offsets.  Confirm DWARF info agrees with the
84       offsets we expect.  */
85
86#ifndef NDEBUG
87    const unw_word_t uc = c->sigcontext_addr;
88
89    assert (DWARF_GET_LOC(d->loc[RIP]) - uc == UC_MCONTEXT_GREGS_RIP);
90    assert (DWARF_GET_LOC(d->loc[RBP]) - uc == UC_MCONTEXT_GREGS_RBP);
91    assert (DWARF_GET_LOC(d->loc[RSP]) - uc == UC_MCONTEXT_GREGS_RSP);
92#endif
93
94    Debug (4, " sigreturn frame\n");
95  }
96#endif
97
98  /* PLT and guessed RBP-walked frames are handled in unw_step(). */
99  else
100  {
101    Debug (4, " unusual frame\n");
102  }
103}
104
105#ifdef __linux__
106
107HIDDEN void
108tdep_fetch_frame (struct dwarf_cursor *dw, unw_word_t ip, int need_unwind_info)
109{
110  struct cursor *c = (struct cursor *) dw;
111  assert(! need_unwind_info || dw->pi_valid);
112  assert(! need_unwind_info || dw->pi.unwind_info);
113  if (dw->pi_valid
114      && dw->pi.unwind_info
115      && ((struct dwarf_cie_info *) dw->pi.unwind_info)->signal_frame)
116    c->sigcontext_format = X86_64_SCF_LINUX_RT_SIGFRAME;
117  else
118    c->sigcontext_format = X86_64_SCF_NONE;
119
120  Debug(5, "fetch frame ip=0x%lx cfa=0x%lx format=%d\n",
121        dw->ip, dw->cfa, c->sigcontext_format);
122}
123
124HIDDEN void
125tdep_cache_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
126{
127  struct cursor *c = (struct cursor *) dw;
128  rs->signal_frame = c->sigcontext_format;
129
130  Debug(5, "cache frame ip=0x%lx cfa=0x%lx format=%d\n",
131        dw->ip, dw->cfa, c->sigcontext_format);
132}
133
134HIDDEN void
135tdep_reuse_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
136{
137  struct cursor *c = (struct cursor *) dw;
138  c->sigcontext_format = rs->signal_frame;
139  if (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME)
140  {
141    c->frame_info.frame_type = UNW_X86_64_FRAME_SIGRETURN;
142    /* Offset from cfa to ucontext_t in signal frame.  */
143    c->frame_info.cfa_reg_offset = 0;
144    c->sigcontext_addr = dw->cfa;
145  }
146  else
147    c->sigcontext_addr = 0;
148
149  Debug(5, "reuse frame ip=0x%lx cfa=0x%lx format=%d addr=0x%lx offset=%+d\n",
150        dw->ip, dw->cfa, c->sigcontext_format, c->sigcontext_addr,
151        (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME
152         ? c->frame_info.cfa_reg_offset : 0));
153}
154
155#endif
156