1 /* 2 * Copyright (c) 2023 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 #ifndef ECMASCRIPT_COMPILER_AOT_FILE_MODULE_SECTION_DES_H 16 #define ECMASCRIPT_COMPILER_AOT_FILE_MODULE_SECTION_DES_H 17 18 #include <cstdint> 19 #include <memory> 20 21 #include "ecmascript/base/number_helper.h" 22 #include "ecmascript/compiler/aot_file/binary_buffer_parser.h" 23 #include "ecmascript/compiler/binary_section.h" 24 25 namespace panda::ecmascript { 26 class ModuleSectionDes { 27 public: 28 struct ModuleRegionInfo { 29 uint32_t startIndex {0}; 30 uint32_t funcCount {0}; 31 uint32_t rodataSize {0}; 32 uint32_t textSize {0}; 33 uint32_t stackMapSize {0}; 34 uint32_t rodataAfterText {0}; 35 }; 36 static std::string GetSecName(ElfSecName idx); 37 UpdateRODataInfo(uint64_t & addr,uint32_t & size,ElfSecName sec)38 void UpdateRODataInfo(uint64_t &addr, uint32_t &size, ElfSecName sec) const 39 { 40 if (sectionsInfo_.find(sec) == sectionsInfo_.end()) { 41 return; 42 } 43 uint64_t curSectionAddr = GetSecAddr(sec); 44 addr = curSectionAddr < addr ? curSectionAddr : addr; 45 size += GetSecSize(sec); 46 } 47 GetMergedRODataAddrAndSize()48 std::tuple<uint64_t, uint32_t> GetMergedRODataAddrAndSize() const 49 { 50 uint64_t addr = base::MAX_UINT64_VALUE; 51 uint32_t size = 0; 52 UpdateRODataInfo(addr, size, ElfSecName::RODATA); 53 UpdateRODataInfo(addr, size, ElfSecName::RODATA_CST4); 54 UpdateRODataInfo(addr, size, ElfSecName::RODATA_CST8); 55 UpdateRODataInfo(addr, size, ElfSecName::RODATA_CST16); 56 UpdateRODataInfo(addr, size, ElfSecName::RODATA_CST32); 57 return std::make_tuple(addr, size); 58 } 59 SetArkStackMapPtr(std::shared_ptr<uint8_t> ptr)60 void SetArkStackMapPtr(std::shared_ptr<uint8_t> ptr) 61 { 62 arkStackMapPtr_ = std::move(ptr); 63 } 64 GetArkStackMapSharePtr()65 std::shared_ptr<uint8_t> GetArkStackMapSharePtr() const 66 { 67 return arkStackMapPtr_; 68 } 69 GetSectionsInfo()70 std::map<ElfSecName, std::pair<uint64_t, uint32_t>> &GetSectionsInfo() 71 { 72 return sectionsInfo_; 73 } 74 GetSectionsInfo()75 const std::map<ElfSecName, std::pair<uint64_t, uint32_t>> &GetSectionsInfo() const 76 { 77 return sectionsInfo_; 78 } 79 SetArkStackMapPtr(uint8_t * ptr)80 void SetArkStackMapPtr(uint8_t *ptr) 81 { 82 arkStackMapRawPtr_ = ptr; 83 } 84 GetArkStackMapRawPtr()85 uint8_t *GetArkStackMapRawPtr() const 86 { 87 return arkStackMapRawPtr_; 88 } 89 SetArkStackMapSize(uint32_t size)90 void SetArkStackMapSize(uint32_t size) 91 { 92 arkStackMapSize_ = size; 93 } 94 GetArkStackMapSize()95 uint32_t GetArkStackMapSize() const 96 { 97 return arkStackMapSize_; 98 } 99 SetStartIndex(uint32_t index)100 void SetStartIndex(uint32_t index) 101 { 102 startIndex_ = index; 103 } 104 GetStartIndex()105 uint32_t GetStartIndex() const 106 { 107 return startIndex_; 108 } 109 SetFuncCount(uint32_t cnt)110 void SetFuncCount(uint32_t cnt) 111 { 112 funcCount_ = cnt; 113 } 114 GetFuncCount()115 uint32_t GetFuncCount() const 116 { 117 return funcCount_; 118 } 119 120 ModuleSectionDes() = default; 121 EraseSec(ElfSecName idx)122 void EraseSec(ElfSecName idx) 123 { 124 sectionsInfo_.erase(idx); 125 } 126 SetSecAddrAndSize(ElfSecName idx,uint64_t addr,uint32_t size)127 void SetSecAddrAndSize(ElfSecName idx, uint64_t addr, uint32_t size) 128 { 129 sectionsInfo_[idx].first = addr; 130 sectionsInfo_[idx].second = size; 131 } 132 GetSecAddr(const ElfSecName idx)133 uint64_t GetSecAddr(const ElfSecName idx) const 134 { 135 auto it = sectionsInfo_.find(idx); 136 return it == sectionsInfo_.end() ? 0 : it->second.first; 137 } 138 GetSecSize(const ElfSecName idx)139 uint32_t GetSecSize(const ElfSecName idx) const 140 { 141 auto it = sectionsInfo_.find(idx); 142 return it == sectionsInfo_.end() ? 0 : it->second.second; 143 } 144 GetSecInfosSize()145 uint32_t GetSecInfosSize() const 146 { 147 return sectionsInfo_.size(); 148 } 149 ContainCode(uintptr_t pc)150 bool ContainCode(uintptr_t pc) const 151 { 152 uint64_t stubStartAddr = GetSecAddr(ElfSecName::TEXT); 153 uint64_t stubEndAddr = stubStartAddr + GetSecSize(ElfSecName::TEXT); 154 return (pc >= stubStartAddr && pc <= stubEndAddr); 155 } 156 AddArkStackMapSection()157 void AddArkStackMapSection() 158 { 159 std::shared_ptr<uint8_t> ptr = GetArkStackMapSharePtr(); 160 uint64_t arkStackMapAddr = reinterpret_cast<uint64_t>(ptr.get()); 161 uint32_t arkStackMapSize = GetArkStackMapSize(); 162 if (arkStackMapSize > 0) { 163 sectionsInfo_[ElfSecName::ARK_STACKMAP] = std::pair(arkStackMapAddr, arkStackMapSize); 164 } 165 } 166 167 private: 168 static constexpr int DECIMAL_LENS = 2; 169 static constexpr int HUNDRED_TIME = 100; 170 static constexpr int PERCENT_LENS = 4; 171 172 std::map<ElfSecName, std::pair<uint64_t, uint32_t>> sectionsInfo_ {}; 173 uint32_t startIndex_ {static_cast<uint32_t>(-1)}; // record current module first function index in AOTFileInfo 174 uint32_t funcCount_ {0}; 175 std::shared_ptr<uint8_t> arkStackMapPtr_ {nullptr}; 176 uint32_t arkStackMapSize_ {0}; 177 uint8_t *arkStackMapRawPtr_ {nullptr}; 178 }; 179 } // namespace panda::ecmascript 180 #endif // ECMASCRIPT_COMPILER_AOT_FILE_MODULE_SECTION_DES_H 181