RegsArm.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <functional>
  19. #include <unwindstack/Elf.h>
  20. #include <unwindstack/MachineArm.h>
  21. #include <unwindstack/MapInfo.h>
  22. #include <unwindstack/Memory.h>
  23. #include <unwindstack/RegsArm.h>
  24. #include <unwindstack/UcontextArm.h>
  25. #include <unwindstack/UserArm.h>
  26. namespace unwindstack {
  27. RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
  28. ArchEnum RegsArm::Arch() {
  29. return ARCH_ARM;
  30. }
  31. uint64_t RegsArm::pc() {
  32. return regs_[ARM_REG_PC];
  33. }
  34. uint64_t RegsArm::sp() {
  35. return regs_[ARM_REG_SP];
  36. }
  37. void RegsArm::set_pc(uint64_t pc) {
  38. regs_[ARM_REG_PC] = pc;
  39. }
  40. void RegsArm::set_sp(uint64_t sp) {
  41. regs_[ARM_REG_SP] = sp;
  42. }
  43. uint64_t RegsArm::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
  44. if (!elf->valid()) {
  45. return 2;
  46. }
  47. uint64_t load_bias = elf->GetLoadBias();
  48. if (rel_pc < load_bias) {
  49. if (rel_pc < 2) {
  50. return 0;
  51. }
  52. return 2;
  53. }
  54. uint64_t adjusted_rel_pc = rel_pc - load_bias;
  55. if (adjusted_rel_pc < 5) {
  56. if (adjusted_rel_pc < 2) {
  57. return 0;
  58. }
  59. return 2;
  60. }
  61. if (adjusted_rel_pc & 1) {
  62. // This is a thumb instruction, it could be 2 or 4 bytes.
  63. uint32_t value;
  64. if (!elf->memory()->ReadFully(adjusted_rel_pc - 5, &value, sizeof(value)) ||
  65. (value & 0xe000f000) != 0xe000f000) {
  66. return 2;
  67. }
  68. }
  69. return 4;
  70. }
  71. bool RegsArm::SetPcFromReturnAddress(Memory*) {
  72. uint32_t lr = regs_[ARM_REG_LR];
  73. if (regs_[ARM_REG_PC] == lr) {
  74. return false;
  75. }
  76. regs_[ARM_REG_PC] = lr;
  77. return true;
  78. }
  79. void RegsArm::IterateRegisters(std::function<void(const char*, uint64_t)> fn) {
  80. fn("r0", regs_[ARM_REG_R0]);
  81. fn("r1", regs_[ARM_REG_R1]);
  82. fn("r2", regs_[ARM_REG_R2]);
  83. fn("r3", regs_[ARM_REG_R3]);
  84. fn("r4", regs_[ARM_REG_R4]);
  85. fn("r5", regs_[ARM_REG_R5]);
  86. fn("r6", regs_[ARM_REG_R6]);
  87. fn("r7", regs_[ARM_REG_R7]);
  88. fn("r8", regs_[ARM_REG_R8]);
  89. fn("r9", regs_[ARM_REG_R9]);
  90. fn("r10", regs_[ARM_REG_R10]);
  91. fn("r11", regs_[ARM_REG_R11]);
  92. fn("ip", regs_[ARM_REG_R12]);
  93. fn("sp", regs_[ARM_REG_SP]);
  94. fn("lr", regs_[ARM_REG_LR]);
  95. fn("pc", regs_[ARM_REG_PC]);
  96. }
  97. Regs* RegsArm::Read(void* remote_data) {
  98. arm_user_regs* user = reinterpret_cast<arm_user_regs*>(remote_data);
  99. RegsArm* regs = new RegsArm();
  100. memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
  101. return regs;
  102. }
  103. Regs* RegsArm::CreateFromUcontext(void* ucontext) {
  104. arm_ucontext_t* arm_ucontext = reinterpret_cast<arm_ucontext_t*>(ucontext);
  105. RegsArm* regs = new RegsArm();
  106. memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
  107. return regs;
  108. }
  109. bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) {
  110. uint32_t data;
  111. Memory* elf_memory = elf->memory();
  112. // Read from elf memory since it is usually more expensive to read from
  113. // process memory.
  114. if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
  115. return false;
  116. }
  117. uint64_t offset = 0;
  118. if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
  119. uint64_t sp = regs_[ARM_REG_SP];
  120. // non-RT sigreturn call.
  121. // __restore:
  122. //
  123. // Form 1 (arm):
  124. // 0x77 0x70 mov r7, #0x77
  125. // 0xa0 0xe3 svc 0x00000000
  126. //
  127. // Form 2 (arm):
  128. // 0x77 0x00 0x90 0xef svc 0x00900077
  129. //
  130. // Form 3 (thumb):
  131. // 0x77 0x27 movs r7, #77
  132. // 0x00 0xdf svc 0
  133. if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
  134. return false;
  135. }
  136. if (data == 0x5ac3c35a) {
  137. // SP + uc_mcontext offset + r0 offset.
  138. offset = sp + 0x14 + 0xc;
  139. } else {
  140. // SP + r0 offset
  141. offset = sp + 0xc;
  142. }
  143. } else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
  144. uint64_t sp = regs_[ARM_REG_SP];
  145. // RT sigreturn call.
  146. // __restore_rt:
  147. //
  148. // Form 1 (arm):
  149. // 0xad 0x70 mov r7, #0xad
  150. // 0xa0 0xe3 svc 0x00000000
  151. //
  152. // Form 2 (arm):
  153. // 0xad 0x00 0x90 0xef svc 0x009000ad
  154. //
  155. // Form 3 (thumb):
  156. // 0xad 0x27 movs r7, #ad
  157. // 0x00 0xdf svc 0
  158. if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
  159. return false;
  160. }
  161. if (data == sp + 8) {
  162. // SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
  163. offset = sp + 8 + 0x80 + 0x14 + 0xc;
  164. } else {
  165. // SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
  166. offset = sp + 0x80 + 0x14 + 0xc;
  167. }
  168. }
  169. if (offset == 0) {
  170. return false;
  171. }
  172. if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
  173. return false;
  174. }
  175. return true;
  176. }
  177. Regs* RegsArm::Clone() {
  178. return new RegsArm(*this);
  179. }
  180. } // namespace unwindstack