14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_AOT_FILE_MODULE_SECTION_DES_H 164514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_AOT_FILE_MODULE_SECTION_DES_H 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include <cstdint> 194514f5e3Sopenharmony_ci#include <memory> 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci#include "ecmascript/base/number_helper.h" 224514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/binary_buffer_parser.h" 234514f5e3Sopenharmony_ci#include "ecmascript/compiler/binary_section.h" 244514f5e3Sopenharmony_ci 254514f5e3Sopenharmony_cinamespace panda::ecmascript { 264514f5e3Sopenharmony_ciclass ModuleSectionDes { 274514f5e3Sopenharmony_cipublic: 284514f5e3Sopenharmony_ci struct ModuleRegionInfo { 294514f5e3Sopenharmony_ci uint32_t startIndex {0}; 304514f5e3Sopenharmony_ci uint32_t funcCount {0}; 314514f5e3Sopenharmony_ci uint32_t rodataSizeBeforeText {0}; 324514f5e3Sopenharmony_ci uint32_t rodataSizeAfterText {0}; 334514f5e3Sopenharmony_ci uint32_t textSize {0}; 344514f5e3Sopenharmony_ci uint32_t stackMapSize {0}; 354514f5e3Sopenharmony_ci uint32_t strtabSize {0}; 364514f5e3Sopenharmony_ci uint32_t symtabSize {0}; 374514f5e3Sopenharmony_ci }; 384514f5e3Sopenharmony_ci static std::string GetSecName(ElfSecName idx); 394514f5e3Sopenharmony_ci 404514f5e3Sopenharmony_ci void UpdateRODataInfo(uint64_t textAddr, uint64_t &addrBeforeText, uint32_t &sizeBeforeText, 414514f5e3Sopenharmony_ci uint64_t &addrAfterText, uint32_t &sizeAfterText, ElfSecName sec) const 424514f5e3Sopenharmony_ci { 434514f5e3Sopenharmony_ci if (sectionsInfo_.find(sec) == sectionsInfo_.end()) { 444514f5e3Sopenharmony_ci return; 454514f5e3Sopenharmony_ci } 464514f5e3Sopenharmony_ci uint64_t curSectionAddr = GetSecAddr(sec); 474514f5e3Sopenharmony_ci ASSERT(curSectionAddr != 0); 484514f5e3Sopenharmony_ci ASSERT(curSectionAddr != textAddr); 494514f5e3Sopenharmony_ci if (curSectionAddr < textAddr) { 504514f5e3Sopenharmony_ci addrBeforeText = (curSectionAddr < addrBeforeText) ? curSectionAddr : addrBeforeText; 514514f5e3Sopenharmony_ci sizeBeforeText += GetSecSize(sec); 524514f5e3Sopenharmony_ci } else { 534514f5e3Sopenharmony_ci addrAfterText = (curSectionAddr < addrAfterText) ? curSectionAddr : addrAfterText; 544514f5e3Sopenharmony_ci sizeAfterText += GetSecSize(sec); 554514f5e3Sopenharmony_ci } 564514f5e3Sopenharmony_ci } 574514f5e3Sopenharmony_ci 584514f5e3Sopenharmony_ci std::tuple<uint64_t, uint32_t, uint64_t, uint32_t> GetMergedRODataAddrAndSize(uint64_t textAddr) const 594514f5e3Sopenharmony_ci { 604514f5e3Sopenharmony_ci uint64_t addrBeforeText = base::MAX_UINT64_VALUE; 614514f5e3Sopenharmony_ci uint32_t sizeBeforeText = 0; 624514f5e3Sopenharmony_ci uint64_t addrAfterText = base::MAX_UINT64_VALUE; 634514f5e3Sopenharmony_ci uint32_t sizeAfterText = 0; 644514f5e3Sopenharmony_ci for (uint8_t i = static_cast<uint8_t>(ElfSecName::RODATA); i <= static_cast<uint8_t>(ElfSecName::RODATA_CST32); 654514f5e3Sopenharmony_ci i++) { 664514f5e3Sopenharmony_ci UpdateRODataInfo(textAddr, addrBeforeText, sizeBeforeText, addrAfterText, sizeAfterText, 674514f5e3Sopenharmony_ci static_cast<ElfSecName>(i)); 684514f5e3Sopenharmony_ci } 694514f5e3Sopenharmony_ci return std::make_tuple(addrBeforeText, sizeBeforeText, addrAfterText, sizeAfterText); 704514f5e3Sopenharmony_ci } 714514f5e3Sopenharmony_ci 724514f5e3Sopenharmony_ci void SetArkStackMapPtr(std::shared_ptr<uint8_t> ptr) 734514f5e3Sopenharmony_ci { 744514f5e3Sopenharmony_ci arkStackMapPtr_ = std::move(ptr); 754514f5e3Sopenharmony_ci } 764514f5e3Sopenharmony_ci 774514f5e3Sopenharmony_ci std::shared_ptr<uint8_t> GetArkStackMapSharePtr() const 784514f5e3Sopenharmony_ci { 794514f5e3Sopenharmony_ci return arkStackMapPtr_; 804514f5e3Sopenharmony_ci } 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ci std::map<ElfSecName, std::pair<uint64_t, uint32_t>> &GetSectionsInfo() 834514f5e3Sopenharmony_ci { 844514f5e3Sopenharmony_ci return sectionsInfo_; 854514f5e3Sopenharmony_ci } 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci const std::map<ElfSecName, std::pair<uint64_t, uint32_t>> &GetSectionsInfo() const 884514f5e3Sopenharmony_ci { 894514f5e3Sopenharmony_ci return sectionsInfo_; 904514f5e3Sopenharmony_ci } 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci void SetArkStackMapPtr(uint8_t *ptr) 934514f5e3Sopenharmony_ci { 944514f5e3Sopenharmony_ci arkStackMapRawPtr_ = ptr; 954514f5e3Sopenharmony_ci } 964514f5e3Sopenharmony_ci 974514f5e3Sopenharmony_ci uint8_t *GetArkStackMapRawPtr() const 984514f5e3Sopenharmony_ci { 994514f5e3Sopenharmony_ci return arkStackMapRawPtr_; 1004514f5e3Sopenharmony_ci } 1014514f5e3Sopenharmony_ci 1024514f5e3Sopenharmony_ci void SetArkStackMapSize(uint32_t size) 1034514f5e3Sopenharmony_ci { 1044514f5e3Sopenharmony_ci arkStackMapSize_ = size; 1054514f5e3Sopenharmony_ci } 1064514f5e3Sopenharmony_ci 1074514f5e3Sopenharmony_ci uint32_t GetArkStackMapSize() const 1084514f5e3Sopenharmony_ci { 1094514f5e3Sopenharmony_ci return arkStackMapSize_; 1104514f5e3Sopenharmony_ci } 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ci void SetStartIndex(uint32_t index) 1134514f5e3Sopenharmony_ci { 1144514f5e3Sopenharmony_ci startIndex_ = index; 1154514f5e3Sopenharmony_ci } 1164514f5e3Sopenharmony_ci 1174514f5e3Sopenharmony_ci uint32_t GetStartIndex() const 1184514f5e3Sopenharmony_ci { 1194514f5e3Sopenharmony_ci return startIndex_; 1204514f5e3Sopenharmony_ci } 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci void SetFuncCount(uint32_t cnt) 1234514f5e3Sopenharmony_ci { 1244514f5e3Sopenharmony_ci funcCount_ = cnt; 1254514f5e3Sopenharmony_ci } 1264514f5e3Sopenharmony_ci 1274514f5e3Sopenharmony_ci uint32_t GetFuncCount() const 1284514f5e3Sopenharmony_ci { 1294514f5e3Sopenharmony_ci return funcCount_; 1304514f5e3Sopenharmony_ci } 1314514f5e3Sopenharmony_ci 1324514f5e3Sopenharmony_ci ModuleSectionDes() = default; 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ci void EraseSec(ElfSecName idx) 1354514f5e3Sopenharmony_ci { 1364514f5e3Sopenharmony_ci sectionsInfo_.erase(idx); 1374514f5e3Sopenharmony_ci } 1384514f5e3Sopenharmony_ci 1394514f5e3Sopenharmony_ci void SetSecAddrAndSize(ElfSecName idx, uint64_t addr, uint32_t size) 1404514f5e3Sopenharmony_ci { 1414514f5e3Sopenharmony_ci sectionsInfo_[idx].first = addr; 1424514f5e3Sopenharmony_ci sectionsInfo_[idx].second = size; 1434514f5e3Sopenharmony_ci } 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_ci uint64_t GetSecAddr(const ElfSecName idx) const 1464514f5e3Sopenharmony_ci { 1474514f5e3Sopenharmony_ci auto it = sectionsInfo_.find(idx); 1484514f5e3Sopenharmony_ci return it == sectionsInfo_.end() ? 0 : it->second.first; 1494514f5e3Sopenharmony_ci } 1504514f5e3Sopenharmony_ci 1514514f5e3Sopenharmony_ci uint32_t GetSecSize(const ElfSecName idx) const 1524514f5e3Sopenharmony_ci { 1534514f5e3Sopenharmony_ci auto it = sectionsInfo_.find(idx); 1544514f5e3Sopenharmony_ci return it == sectionsInfo_.end() ? 0 : it->second.second; 1554514f5e3Sopenharmony_ci } 1564514f5e3Sopenharmony_ci 1574514f5e3Sopenharmony_ci uint32_t GetSecInfosSize() const 1584514f5e3Sopenharmony_ci { 1594514f5e3Sopenharmony_ci return sectionsInfo_.size(); 1604514f5e3Sopenharmony_ci } 1614514f5e3Sopenharmony_ci 1624514f5e3Sopenharmony_ci bool ContainCode(uintptr_t pc) const 1634514f5e3Sopenharmony_ci { 1644514f5e3Sopenharmony_ci uint64_t stubStartAddr = GetSecAddr(ElfSecName::TEXT); 1654514f5e3Sopenharmony_ci uint64_t stubEndAddr = stubStartAddr + GetSecSize(ElfSecName::TEXT); 1664514f5e3Sopenharmony_ci return (pc >= stubStartAddr && pc <= stubEndAddr); 1674514f5e3Sopenharmony_ci } 1684514f5e3Sopenharmony_ci 1694514f5e3Sopenharmony_ci void AddArkStackMapSection() 1704514f5e3Sopenharmony_ci { 1714514f5e3Sopenharmony_ci std::shared_ptr<uint8_t> ptr = GetArkStackMapSharePtr(); 1724514f5e3Sopenharmony_ci uint64_t arkStackMapAddr = reinterpret_cast<uint64_t>(ptr.get()); 1734514f5e3Sopenharmony_ci uint32_t arkStackMapSize = GetArkStackMapSize(); 1744514f5e3Sopenharmony_ci if (arkStackMapSize > 0) { 1754514f5e3Sopenharmony_ci sectionsInfo_[ElfSecName::ARK_STACKMAP] = std::pair(arkStackMapAddr, arkStackMapSize); 1764514f5e3Sopenharmony_ci } 1774514f5e3Sopenharmony_ci } 1784514f5e3Sopenharmony_ci 1794514f5e3Sopenharmony_ci bool HasAsmStubStrTab() const 1804514f5e3Sopenharmony_ci { 1814514f5e3Sopenharmony_ci return asmStubELFInfo_.size() != 0; 1824514f5e3Sopenharmony_ci } 1834514f5e3Sopenharmony_ci 1844514f5e3Sopenharmony_ci const std::vector<std::pair<std::string, uint32_t>>& GetAsmStubELFInfo() 1854514f5e3Sopenharmony_ci { 1864514f5e3Sopenharmony_ci return asmStubELFInfo_; 1874514f5e3Sopenharmony_ci } 1884514f5e3Sopenharmony_ci 1894514f5e3Sopenharmony_ci void AddAsmStubELFInfo(std::vector<std::pair<std::string, uint32_t>> &asmStubELFInfo) 1904514f5e3Sopenharmony_ci { 1914514f5e3Sopenharmony_ci asmStubELFInfo_ = asmStubELFInfo; 1924514f5e3Sopenharmony_ci } 1934514f5e3Sopenharmony_ci 1944514f5e3Sopenharmony_ciprivate: 1954514f5e3Sopenharmony_ci static constexpr int DECIMAL_LENS = 2; 1964514f5e3Sopenharmony_ci static constexpr int HUNDRED_TIME = 100; 1974514f5e3Sopenharmony_ci static constexpr int PERCENT_LENS = 4; 1984514f5e3Sopenharmony_ci 1994514f5e3Sopenharmony_ci std::vector<std::pair<std::string, uint32_t>> asmStubELFInfo_ {}; 2004514f5e3Sopenharmony_ci std::map<ElfSecName, std::pair<uint64_t, uint32_t>> sectionsInfo_ {}; 2014514f5e3Sopenharmony_ci uint32_t startIndex_ {static_cast<uint32_t>(-1)}; // record current module first function index in AOTFileInfo 2024514f5e3Sopenharmony_ci uint32_t funcCount_ {0}; 2034514f5e3Sopenharmony_ci std::shared_ptr<uint8_t> arkStackMapPtr_ {nullptr}; 2044514f5e3Sopenharmony_ci uint32_t arkStackMapSize_ {0}; 2054514f5e3Sopenharmony_ci uint8_t *arkStackMapRawPtr_ {nullptr}; 2064514f5e3Sopenharmony_ci}; 2074514f5e3Sopenharmony_ci} // namespace panda::ecmascript 2084514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_AOT_FILE_MODULE_SECTION_DES_H 209