1 
2 /* libunwind - a platform-independent unwind library
3    Copyright 2011 Linaro Limited
4 
5 This file is part of libunwind.
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25 
26 // Copyright (c) 2010 Google Inc.
27 // All rights reserved.
28 //
29 // Redistribution and use in source and binary forms, with or without
30 // modification, are permitted provided that the following conditions are
31 // met:
32 //
33 //     * Redistributions of source code must retain the above copyright
34 // notice, this list of conditions and the following disclaimer.
35 //     * Redistributions in binary form must reproduce the above
36 // copyright notice, this list of conditions and the following disclaimer
37 // in the documentation and/or other materials provided with the
38 // distribution.
39 //     * Neither the name of Google Inc. nor the names of its
40 // contributors may be used to endorse or promote products derived from
41 // this software without specific prior written permission.
42 //
43 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 
55 
56 // Derived from libunwind, with extensive modifications.
57 
58 #include "common/arm_ex_to_module.h"
59 
60 #include <stdio.h>
61 #include <assert.h>
62 
63 // For big-picture comments on how the EXIDX reader works,
64 // see arm_ex_reader.cc.
65 
66 #define ARM_EXBUF_START(x) (((x) >> 4) & 0x0f)
67 #define ARM_EXBUF_COUNT(x) ((x) & 0x0f)
68 #define ARM_EXBUF_END(x)   (ARM_EXBUF_START(x) + ARM_EXBUF_COUNT(x))
69 
70 using google_breakpad::Module;
71 
72 namespace arm_ex_to_module {
73 
74 static const char* const regnames[] = {
75  "r0", "r1", "r2",  "r3",  "r4",  "r5", "r6", "r7",
76  "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
77  "f0", "f1", "f2",  "f3",  "f4",  "f5", "f6", "f7",
78  "fps", "cpsr"
79 };
80 
81 // Translate command from extab_data to command for Module.
TranslateCmd(const struct extab_data * edata,Module::StackFrameEntry * entry,string & vsp)82 int ARMExToModule::TranslateCmd(const struct extab_data* edata,
83                                 Module::StackFrameEntry* entry, string& vsp) {
84   int ret = 0;
85   switch (edata->cmd) {
86     case ARM_EXIDX_CMD_FINISH:
87       /* Copy LR to PC if there isn't currently a rule for PC in force. */
88       if (entry->initial_rules.find("pc")
89           == entry->initial_rules.end()) {
90         if (entry->initial_rules.find("lr")
91             == entry->initial_rules.end()) {
92           entry->initial_rules["pc"] = "lr";
93         } else {
94           entry->initial_rules["pc"] = entry->initial_rules["lr"];
95         }
96       }
97       break;
98     case ARM_EXIDX_CMD_SUB_FROM_VSP:
99       {
100         char c[16];
101         sprintf(c, " %d -", edata->data);
102         vsp += c;
103       }
104       break;
105     case ARM_EXIDX_CMD_ADD_TO_VSP:
106       {
107         char c[16];
108         sprintf(c, " %d +", edata->data);
109         vsp += c;
110       }
111       break;
112     case ARM_EXIDX_CMD_REG_POP:
113       for (unsigned int i = 0; i < 16; i++) {
114         if (edata->data & (1 << i)) {
115           entry->initial_rules[regnames[i]]
116             = vsp + " ^";
117           vsp += " 4 +";
118         }
119       }
120       /* Set cfa in case the SP got popped. */
121       if (edata->data & (1 << 13)) {
122         vsp = entry->initial_rules["sp"];
123       }
124       break;
125     case ARM_EXIDX_CMD_REG_TO_SP: {
126       assert (edata->data < 16);
127       const char* const regname = regnames[edata->data];
128       if (entry->initial_rules.find(regname) == entry->initial_rules.end()) {
129         entry->initial_rules["sp"] = regname;
130       } else {
131         entry->initial_rules["sp"] = entry->initial_rules[regname];
132       }
133       vsp = entry->initial_rules["sp"];
134       break;
135     }
136     case ARM_EXIDX_CMD_VFP_POP:
137       /* Don't recover VFP registers, but be sure to adjust the stack
138          pointer. */
139       for (unsigned int i = ARM_EXBUF_START(edata->data);
140            i <= ARM_EXBUF_END(edata->data); i++) {
141         vsp += " 8 +";
142       }
143       if (!(edata->data & ARM_EXIDX_VFP_FSTMD)) {
144         vsp += " 4 +";
145       }
146       break;
147     case ARM_EXIDX_CMD_WREG_POP:
148       for (unsigned int i = ARM_EXBUF_START(edata->data);
149            i <= ARM_EXBUF_END(edata->data); i++) {
150         vsp += " 8 +";
151       }
152       break;
153     case ARM_EXIDX_CMD_WCGR_POP:
154       // Pop wCGR registers under mask {wCGR3,2,1,0}, hence "i < 4"
155       for (unsigned int i = 0; i < 4; i++) {
156         if (edata->data & (1 << i)) {
157           vsp += " 4 +";
158         }
159       }
160       break;
161     case ARM_EXIDX_CMD_REFUSED:
162     case ARM_EXIDX_CMD_RESERVED:
163       ret = -1;
164       break;
165   }
166   return ret;
167 }
168 
HasStackFrame(uintptr_t addr,size_t size)169 bool ARMExToModule::HasStackFrame(uintptr_t addr, size_t size) {
170   // Invariant: the range [addr,covered) is covered by existing stack
171   // frame entries.
172   uintptr_t covered = addr;
173   while (covered < addr + size) {
174     const Module::StackFrameEntry *old_entry =
175       module_->FindStackFrameEntryByAddress(covered);
176     if (!old_entry) {
177       return false;
178     }
179     covered = old_entry->address + old_entry->size;
180   }
181   return true;
182 }
183 
AddStackFrame(uintptr_t addr,size_t size)184 void ARMExToModule::AddStackFrame(uintptr_t addr, size_t size) {
185   stack_frame_entry_ = new Module::StackFrameEntry;
186   stack_frame_entry_->address = addr;
187   stack_frame_entry_->size = size;
188   stack_frame_entry_->initial_rules[".cfa"] = "sp";
189   vsp_ = "sp";
190 }
191 
ImproveStackFrame(const struct extab_data * edata)192 int ARMExToModule::ImproveStackFrame(const struct extab_data* edata) {
193   return TranslateCmd(edata, stack_frame_entry_, vsp_) ;
194 }
195 
DeleteStackFrame()196 void ARMExToModule::DeleteStackFrame() {
197   delete stack_frame_entry_;
198 }
199 
SubmitStackFrame()200 void ARMExToModule::SubmitStackFrame() {
201   // return address always winds up in pc
202   stack_frame_entry_->initial_rules[".ra"]
203     = stack_frame_entry_->initial_rules["pc"];
204   // the final value of vsp is the new value of sp
205   stack_frame_entry_->initial_rules["sp"] = vsp_;
206   module_->AddStackFrameEntry(stack_frame_entry_);
207 }
208 
209 }  // namespace arm_ex_to_module
210