Fix some minor issues found via static analysis
[platform/upstream/libunwind.git] / src / x86_64 / Gstash_frame.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2010, 2011 by FERMI NATIONAL ACCELERATOR LABORATORY
3
4 This file is part of libunwind.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24
25 #include "unwind_i.h"
26 #include "ucontext_i.h"
27
28 HIDDEN void
29 tdep_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.where[DWARF_CFA_REG_COLUMN],
38          rs->reg.val[DWARF_CFA_REG_COLUMN],
39          rs->reg.val[DWARF_CFA_OFF_COLUMN],
40          DWARF_GET_LOC(d->loc[rs->ret_addr_column]),
41          rs->reg.where[RBP], rs->reg.val[RBP], DWARF_GET_LOC(d->loc[RBP]),
42          rs->reg.where[RSP], rs->reg.val[RSP], DWARF_GET_LOC(d->loc[RSP]));
43
44   if (rs->reg.where[DWARF_CFA_REG_COLUMN] == DWARF_WHERE_EXPR &&
45     rs->reg.where[RBP] == DWARF_WHERE_EXPR) {
46     /* Check for GCC generated alignment frame for rsp.  A simple
47      * def_cfa_expr that loads a constant offset from rbp, where the
48      * addres of the rip was pushed on the stack */
49     unw_word_t cfa_addr = rs->reg.val[DWARF_CFA_REG_COLUMN];
50     unw_word_t rbp_addr = rs->reg.val[RBP];
51     unw_word_t cfa_offset;
52
53     int ret = dwarf_stack_aligned(d, cfa_addr, rbp_addr, &cfa_offset);
54     if (ret) {
55       f->frame_type = UNW_X86_64_FRAME_ALIGNED;
56       f->cfa_reg_offset = cfa_offset;
57       f->cfa_reg_rsp = 0;
58     }
59   }
60
61   /* A standard frame is defined as:
62       - CFA is register-relative offset off RBP or RSP;
63       - Return address is saved at CFA-8;
64       - RBP is unsaved or saved at CFA+offset, offset != -1;
65       - RSP is unsaved or saved at CFA+offset, offset != -1.  */
66   if (f->frame_type == UNW_X86_64_FRAME_OTHER
67       && (rs->reg.where[DWARF_CFA_REG_COLUMN] == DWARF_WHERE_REG)
68       && (rs->reg.val[DWARF_CFA_REG_COLUMN] == RBP
69           || rs->reg.val[DWARF_CFA_REG_COLUMN] == RSP)
70       && labs((long) rs->reg.val[DWARF_CFA_OFF_COLUMN]) < (1 << 28)
71       && DWARF_GET_LOC(d->loc[rs->ret_addr_column]) == d->cfa-8
72       && (rs->reg.where[RBP] == DWARF_WHERE_UNDEF
73           || rs->reg.where[RBP] == DWARF_WHERE_SAME
74           || (rs->reg.where[RBP] == DWARF_WHERE_CFAREL
75               && labs((long) rs->reg.val[RBP]) < (1 << 14)
76               && rs->reg.val[RBP]+1 != 0))
77       && (rs->reg.where[RSP] == DWARF_WHERE_UNDEF
78           || rs->reg.where[RSP] == DWARF_WHERE_SAME
79           || (rs->reg.where[RSP] == DWARF_WHERE_CFAREL
80               && labs((long) rs->reg.val[RSP]) < (1 << 14)
81               && rs->reg.val[RSP]+1 != 0)))
82   {
83     /* Save information for a standard frame. */
84     f->frame_type = UNW_X86_64_FRAME_STANDARD;
85     f->cfa_reg_rsp = (rs->reg.val[DWARF_CFA_REG_COLUMN] == RSP);
86     f->cfa_reg_offset = rs->reg.val[DWARF_CFA_OFF_COLUMN];
87     if (rs->reg.where[RBP] == DWARF_WHERE_CFAREL)
88       f->rbp_cfa_offset = rs->reg.val[RBP];
89     if (rs->reg.where[RSP] == DWARF_WHERE_CFAREL)
90       f->rsp_cfa_offset = rs->reg.val[RSP];
91     Debug (4, " standard frame\n");
92   }
93
94   /* Signal frame was detected via augmentation in tdep_fetch_frame()  */
95   else if (f->frame_type == UNW_X86_64_FRAME_SIGRETURN)
96   {
97     /* Later we are going to fish out {RBP,RSP,RIP} from sigcontext via
98        their ucontext_t offsets.  Confirm DWARF info agrees with the
99        offsets we expect.  */
100
101 #ifndef NDEBUG
102     const unw_word_t uc = c->sigcontext_addr;
103
104     assert (DWARF_GET_LOC(d->loc[RIP]) - uc == UC_MCONTEXT_GREGS_RIP);
105     assert (DWARF_GET_LOC(d->loc[RBP]) - uc == UC_MCONTEXT_GREGS_RBP);
106     assert (DWARF_GET_LOC(d->loc[RSP]) - uc == UC_MCONTEXT_GREGS_RSP);
107 #endif
108
109     Debug (4, " sigreturn frame\n");
110   }
111
112   else if (f->frame_type == UNW_X86_64_FRAME_ALIGNED) {
113     Debug (4, " aligned frame, offset %li\n", f->cfa_reg_offset);
114   }
115
116   /* PLT and guessed RBP-walked frames are handled in unw_step(). */
117   else
118     Debug (4, " unusual frame\n");
119 }