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