• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef ECMASCRIPT_DEOPTIMIZER_RELCATOR_H
17 #define ECMASCRIPT_DEOPTIMIZER_RELCATOR_H
18 
19 #include "ecmascript/common.h"
20 #if !WIN_OR_MAC_OR_IOS_PLATFORM
21 
22 #include <elf.h>
23 #include <optional>
24 #include "ecmascript/ecma_macros.h"
25 
26 namespace panda::ecmascript {
27 struct RelocateTextInfo {
28     uintptr_t textAddr_ {0};
29     uintptr_t relaTextAddr_ {0};
30     uintptr_t relaTextSize_ {0};
31 };
32 
33 struct SymAndStrTabInfo {
34     uintptr_t symAddr_ {0};
35     uintptr_t symSize_ {0};
36     uintptr_t strAddr_ {0};
37     uintptr_t strSize_ {0};
38 };
39 
40 class Relocator {
41 public:
Relocator(RelocateTextInfo relaText,SymAndStrTabInfo symAndStrTabInfo)42     PUBLIC_API Relocator(RelocateTextInfo relaText, SymAndStrTabInfo symAndStrTabInfo)
43         :relocateTextInfo_(relaText), symAndStrTabInfo_(symAndStrTabInfo) {};
44 
45     PUBLIC_API bool RelocateBySymbolId(Elf64_Word symbolId, uintptr_t patchAddr);
46     PUBLIC_API bool RelocateBySymbol(const char* symbol, uintptr_t patchAddr);
47     PUBLIC_API void DumpRelocateText();
48     ~Relocator() = default;
49 private:
50     bool HasSymStrTable() const;
51     bool HasRelocateText() const;
52     std::optional<Elf64_Word> GetSymbol(const char* symbol) const;
53     bool Relocate(Elf64_Rela *sec, uintptr_t symbolAddr, uintptr_t patchAddr);
54     static bool BlInRange(intptr_t imm);
55 
GetSymbol(Elf64_Rela * cur)56     Elf64_Word GetSymbol(Elf64_Rela *cur) const
57     {
58         Elf64_Word id = (cur->r_info >> 32); // 32: get high 32 bits
59         return id;
60     }
61 
GetType(Elf64_Rela * cur)62     Elf64_Word GetType(Elf64_Rela *cur) const
63     {
64         return (cur->r_info & 0xffffffffL); // 0xffffffff :get lower 32 bits
65     }
66 
67     // These accessors and mutators are identical to those defined for ELF32
68     // symbol table entries.
GetBinding(Elf64_Sym * cur)69     unsigned char GetBinding(Elf64_Sym *cur) const
70     {
71         return cur->st_info >> 4; // 4: offset
72     }
73 
GetType(Elf64_Sym * cur)74     unsigned char GetType(Elf64_Sym *cur) const
75     {
76         return cur->st_info & 0x0f; // f: get lowest 4 bits
77     }
78 
79     RelocateTextInfo relocateTextInfo_ {0};
80     SymAndStrTabInfo symAndStrTabInfo_ {0};
81 };
82 }  // panda::ecmascript
83 #endif
84 #endif  // ECMASCRIPT_DEOPTIMIZER_RELCATOR_H
85 
86