2cce409d53e98ab6683bdce5354e003d75e7db47
[platform/upstream/libunwind.git] / src / arm / Gstash_frame.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2010, 2011 by FERMI NATIONAL ACCELERATOR LABORATORY
3    Copyright (C) 2014 CERN and Aalto University
4         Contributed by Filip Nyback
5
6 This file is part of libunwind.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26
27 #include "unwind_i.h"
28
29 HIDDEN void
30 tdep_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%x cfa=0x%x type %d cfa [where=%d val=%d] cfaoff=%d"
36          " ra=0x%x r7 [where=%d val=%d @0x%x] lr [where=%d val=%d @0x%x] "
37          "sp [where=%d val=%d @0x%x]\n",
38          d->ip, d->cfa, f->frame_type,
39          rs->reg[DWARF_CFA_REG_COLUMN].where,
40          rs->reg[DWARF_CFA_REG_COLUMN].val,
41          rs->reg[DWARF_CFA_OFF_COLUMN].val,
42          DWARF_GET_LOC(d->loc[d->ret_addr_column]),
43          rs->reg[R7].where, rs->reg[R7].val, DWARF_GET_LOC(d->loc[R7]),
44          rs->reg[LR].where, rs->reg[LR].val, DWARF_GET_LOC(d->loc[LR]),
45          rs->reg[SP].where, rs->reg[SP].val, DWARF_GET_LOC(d->loc[SP]));
46
47   /* A standard frame is defined as:
48       - CFA is register-relative offset off R7 or SP;
49       - Return address is saved in LR;
50       - R7 is unsaved or saved at CFA+offset, offset != -1;
51       - LR is unsaved or saved at CFA+offset, offset != -1;
52       - SP is unsaved or saved at CFA+offset, offset != -1.  */
53   if (f->frame_type == UNW_ARM_FRAME_OTHER
54       && (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_REG)
55       && (rs->reg[DWARF_CFA_REG_COLUMN].val == R7
56           || rs->reg[DWARF_CFA_REG_COLUMN].val == SP)
57       && labs(rs->reg[DWARF_CFA_OFF_COLUMN].val) < (1 << 29)
58       && d->ret_addr_column == LR
59       && (rs->reg[R7].where == DWARF_WHERE_UNDEF
60           || rs->reg[R7].where == DWARF_WHERE_SAME
61           || (rs->reg[R7].where == DWARF_WHERE_CFAREL
62               && labs(rs->reg[R7].val) < (1 << 29)
63               && rs->reg[R7].val+1 != 0))
64       && (rs->reg[LR].where == DWARF_WHERE_UNDEF
65           || rs->reg[LR].where == DWARF_WHERE_SAME
66           || (rs->reg[LR].where == DWARF_WHERE_CFAREL
67               && labs(rs->reg[LR].val) < (1 << 29)
68               && rs->reg[LR].val+1 != 0))
69       && (rs->reg[SP].where == DWARF_WHERE_UNDEF
70           || rs->reg[SP].where == DWARF_WHERE_SAME
71           || (rs->reg[SP].where == DWARF_WHERE_CFAREL
72               && labs(rs->reg[SP].val) < (1 << 29)
73               && rs->reg[SP].val+1 != 0)))
74   {
75     /* Save information for a standard frame. */
76     f->frame_type = UNW_ARM_FRAME_STANDARD;
77     f->cfa_reg_sp = (rs->reg[DWARF_CFA_REG_COLUMN].val == SP);
78     f->cfa_reg_offset = rs->reg[DWARF_CFA_OFF_COLUMN].val;
79     if (rs->reg[R7].where == DWARF_WHERE_CFAREL)
80       f->r7_cfa_offset = rs->reg[R7].val;
81     if (rs->reg[LR].where == DWARF_WHERE_CFAREL)
82       f->lr_cfa_offset = rs->reg[LR].val;
83     if (rs->reg[SP].where == DWARF_WHERE_CFAREL)
84       f->sp_cfa_offset = rs->reg[SP].val;
85     Debug (4, " standard frame\n");
86   }
87   else
88     Debug (4, " unusual frame\n");
89 }
90