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 "ucontext_i.h"
27
28HIDDEN void
29tdep_stash_frame (struct dwarf_cursor *d, struct dwarf_reg_state *rs)
30{
31  struct cursor *c = (struct cursor *) dwarf_to_cursor (d);
32  unw_tdep_frame_t *f = &c->frame_info;
33
34  Debug (4, "ip=0x%lx cfa=0x%lx type %d cfa [where=%d val=%ld] cfaoff=%ld"
35	 " ra=0x%lx rbp [where=%d val=%ld @0x%lx] rsp [where=%d val=%ld @0x%lx]\n",
36	 d->ip, d->cfa, f->frame_type,
37	 rs->reg[DWARF_CFA_REG_COLUMN].where,
38	 rs->reg[DWARF_CFA_REG_COLUMN].val,
39	 rs->reg[DWARF_CFA_OFF_COLUMN].val,
40	 DWARF_GET_LOC(d->loc[d->ret_addr_column]),
41	 rs->reg[RBP].where, rs->reg[RBP].val, DWARF_GET_LOC(d->loc[RBP]),
42	 rs->reg[RSP].where, rs->reg[RSP].val, DWARF_GET_LOC(d->loc[RSP]));
43
44  /* A standard frame is defined as:
45      - CFA is register-relative offset off RBP or RSP;
46      - Return address is saved at CFA-8;
47      - RBP is unsaved or saved at CFA+offset, offset != -1;
48      - RSP is unsaved or saved at CFA+offset, offset != -1.  */
49  if (f->frame_type == UNW_X86_64_FRAME_OTHER
50      && (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_REG)
51      && (rs->reg[DWARF_CFA_REG_COLUMN].val == RBP
52	  || rs->reg[DWARF_CFA_REG_COLUMN].val == RSP)
53      && labs(rs->reg[DWARF_CFA_OFF_COLUMN].val) < (1 << 29)
54      && DWARF_GET_LOC(d->loc[d->ret_addr_column]) == d->cfa-8
55      && (rs->reg[RBP].where == DWARF_WHERE_UNDEF
56	  || rs->reg[RBP].where == DWARF_WHERE_SAME
57	  || (rs->reg[RBP].where == DWARF_WHERE_CFAREL
58	      && labs(rs->reg[RBP].val) < (1 << 14)
59	      && rs->reg[RBP].val+1 != 0))
60      && (rs->reg[RSP].where == DWARF_WHERE_UNDEF
61	  || rs->reg[RSP].where == DWARF_WHERE_SAME
62	  || (rs->reg[RSP].where == DWARF_WHERE_CFAREL
63	      && labs(rs->reg[RSP].val) < (1 << 14)
64	      && rs->reg[RSP].val+1 != 0)))
65  {
66    /* Save information for a standard frame. */
67    f->frame_type = UNW_X86_64_FRAME_STANDARD;
68    f->cfa_reg_rsp = (rs->reg[DWARF_CFA_REG_COLUMN].val == RSP);
69    f->cfa_reg_offset = rs->reg[DWARF_CFA_OFF_COLUMN].val;
70    if (rs->reg[RBP].where == DWARF_WHERE_CFAREL)
71      f->rbp_cfa_offset = rs->reg[RBP].val;
72    if (rs->reg[RSP].where == DWARF_WHERE_CFAREL)
73      f->rsp_cfa_offset = rs->reg[RSP].val;
74    Debug (4, " standard frame\n");
75  }
76
77  /* Signal frame was detected via augmentation in tdep_fetch_frame()  */
78  else if (f->frame_type == UNW_X86_64_FRAME_SIGRETURN)
79  {
80    /* Later we are going to fish out {RBP,RSP,RIP} from sigcontext via
81       their ucontext_t offsets.  Confirm DWARF info agrees with the
82       offsets we expect.  */
83
84#ifndef NDEBUG
85    const unw_word_t uc = c->sigcontext_addr;
86
87    assert (DWARF_GET_LOC(d->loc[RIP]) - uc == UC_MCONTEXT_GREGS_RIP);
88    assert (DWARF_GET_LOC(d->loc[RBP]) - uc == UC_MCONTEXT_GREGS_RBP);
89    assert (DWARF_GET_LOC(d->loc[RSP]) - uc == UC_MCONTEXT_GREGS_RSP);
90#endif
91
92    Debug (4, " sigreturn frame\n");
93  }
94
95  /* PLT and guessed RBP-walked frames are handled in unw_step(). */
96  else
97    /* ANDROID support update. */
98    {
99      Debug (4, " unusual frame\n");
100    }
101    /* End of ANDROID update. */
102}
103