1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/jit/rewriter/reloc_rewriter_aarch64.h"
17 #include "ecmascript/compiler/assembler/aarch64/assembler_aarch64_constants.h"
18 #include <set>
19
20 namespace panda::ecmascript::kungfu {
21 using namespace panda::ecmascript;
22
RewriteRelocInfo(uint8_t * codeAddr,uint8_t * jitAllocAddr,RelocMap & relocInfo)23 bool RelocWriterAArch64::RewriteRelocInfo(uint8_t *codeAddr, uint8_t *jitAllocAddr, RelocMap &relocInfo)
24 {
25 for (auto [startPc, endPc, dest] : relocInfo) {
26 intptr_t distance = dest - (intptr_t) jitAllocAddr - endPc;
27 intptr_t imm = distance / 4;
28 if (distance > MAX_JUMP_SIZE || distance < -MAX_JUMP_SIZE) {
29 continue;
30 } else {
31 size_t numMovInstr = (endPc - startPc) / INSTRUCT_SIZE;
32 for (size_t i = 0; i < numMovInstr; i++) {
33 uint32_t nopInstr = aarch64::NopOpCode::Nop;
34 memcpy_s(codeAddr + startPc + i * INSTRUCT_SIZE,
35 INSTRUCT_SIZE, reinterpret_cast<uint8_t *>(&nopInstr), INSTRUCT_SIZE);
36 }
37 uint32_t blInstr = aarch64::CallOpCode::BL | ((imm << aarch64::BRANCH_Imm26_LOWBITS) &
38 aarch64::BRANCH_Imm26_MASK);
39 memcpy_s(codeAddr + endPc, INSTRUCT_SIZE, reinterpret_cast<uint8_t *>(&blInstr), INSTRUCT_SIZE);
40 }
41 }
42 return true;
43 }
44 } // namespace panda::ecmascript::kungfu
45