1 /*
2  * Copyright (C) 2014 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 
17 #include "berberis/assembler/machine_code.h"
18 
19 #include <string>
20 
21 #include "berberis/base/bit_util.h"
22 #include "berberis/base/logging.h"
23 
24 namespace berberis {
25 
AddrOf(uint32_t offset)26 uint8_t* MachineCode::AddrOf(uint32_t offset) {
27   CHECK_LT(offset, code_.size());
28   return &code_[offset];
29 }
30 
AddrOf(uint32_t offset) const31 const uint8_t* MachineCode::AddrOf(uint32_t offset) const {
32   CHECK_LT(offset, code_.size());
33   return &code_[offset];
34 }
35 
Grow(uint32_t count)36 uint32_t MachineCode::Grow(uint32_t count) {
37   size_t old_size = code_.size();
38   code_.resize(old_size + count);
39   return old_size;
40 }
41 
print_halfbyte(uint8_t b)42 inline char print_halfbyte(uint8_t b) {
43   return b < 0xa ? b + '0' : (b - 0xa) + 'a';
44 }
45 
print_byte(uint8_t b)46 inline std::string print_byte(uint8_t b) {
47   std::string byte_str = "";
48   byte_str += print_halfbyte(b >> 4);
49   byte_str += print_halfbyte(b & 0xf);
50   return byte_str;
51 }
52 
AsString(std::string * result,InstructionSize insn_size) const53 void MachineCode::AsString(std::string* result, InstructionSize insn_size) const {
54   if (insn_size == InstructionSize::OneByte) {
55     for (uint8_t insn : code_) {
56       *result += print_byte(insn);
57       *result += ' ';
58     }
59   } else {
60     for (uint32_t i = 0; i + 3 < code_.size(); i += 4) {
61       *result += print_byte(code_[i + 3]);
62       *result += print_byte(code_[i + 2]);
63       *result += print_byte(code_[i + 1]);
64       *result += print_byte(code_[i]);
65       *result += ' ';
66     }
67   }
68 }
69 
PerformRelocations(const uint8_t * code,RecoveryMap * recovery_map)70 void MachineCode::PerformRelocations(const uint8_t* code, RecoveryMap* recovery_map) {
71   for (const auto& rel : relocations_) {
72     switch (rel.type) {
73       case RelocationType::RelocAbsToDisp32: {
74         intptr_t start = reinterpret_cast<intptr_t>(code);
75         intptr_t pc = start + rel.pc;
76         intptr_t disp = rel.data - pc;
77         CHECK(IsInRange<int32_t>(disp));
78         *AddrAs<int32_t>(rel.dst) = disp;
79         break;
80       }
81       case RelocationType::RelocRecoveryPoint: {
82         uintptr_t start = reinterpret_cast<uintptr_t>(code);
83         uintptr_t fault_addr = start + rel.pc;
84         uintptr_t recovery_addr = start + rel.data;
85         (*recovery_map)[fault_addr] = recovery_addr;
86         break;
87       }
88     }
89   }
90 }
91 
DumpCode(InstructionSize insn_size) const92 void MachineCode::DumpCode(InstructionSize insn_size) const {
93   std::string code_str;
94   AsString(&code_str, insn_size);
95   ALOGE("%s\n", code_str.c_str());
96 }
97 
98 }  // namespace berberis
99