• 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 
GetSymbol(Elf64_Rela * cur)55     Elf64_Word GetSymbol(Elf64_Rela *cur) const
56     {
57         Elf64_Word id = (cur->r_info >> 32); // 32: get high 32 bits
58         return id;
59     }
60 
GetType(Elf64_Rela * cur)61     Elf64_Word GetType(Elf64_Rela *cur) const
62     {
63         return (cur->r_info & 0xffffffffL); // 0xffffffff :get lower 32 bits
64     }
65 
66     // These accessors and mutators are identical to those defined for ELF32
67     // symbol table entries.
GetBinding(Elf64_Sym * cur)68     unsigned char GetBinding(Elf64_Sym *cur) const
69     {
70         return cur->st_info >> 4; // 4: offset
71     }
72 
GetType(Elf64_Sym * cur)73     unsigned char GetType(Elf64_Sym *cur) const
74     {
75         return cur->st_info & 0x0f; // f: get lowest 4 bits
76     }
77 
78     RelocateTextInfo relocateTextInfo_ {0};
79     SymAndStrTabInfo symAndStrTabInfo_ {0};
80 };
81 }  // panda::ecmascript
82 #endif
83 #endif  // ECMASCRIPT_DEOPTIMIZER_RELCATOR_H
84 
85