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
164514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/an_file_info.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include <cerrno>
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/elf_builder.h"
204514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/elf_reader.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_cinamespace panda::ecmascript {
234514f5e3Sopenharmony_cibool AnFileInfo::Save(const std::string &filename, Triple triple)
244514f5e3Sopenharmony_ci{
254514f5e3Sopenharmony_ci    std::string realPath;
264514f5e3Sopenharmony_ci    if (!RealPath(filename, realPath, false)) {
274514f5e3Sopenharmony_ci        return false;
284514f5e3Sopenharmony_ci    }
294514f5e3Sopenharmony_ci    const char *rawPath = realPath.c_str();
304514f5e3Sopenharmony_ci    TryRemoveAnFile(rawPath);
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci    std::ofstream file(rawPath, std::ofstream::binary);
334514f5e3Sopenharmony_ci    SetStubNum(entries_.size());
344514f5e3Sopenharmony_ci    AddFuncEntrySec();
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    ElfBuilder builder(des_, GetDumpSectionNames());
374514f5e3Sopenharmony_ci    llvm::ELF::Elf64_Ehdr header;
384514f5e3Sopenharmony_ci    builder.PackELFHeader(header, base::FileHeaderBase::ToVersionNumber(AOTFileVersion::AN_VERSION), triple);
394514f5e3Sopenharmony_ci    file.write(reinterpret_cast<char *>(&header), sizeof(llvm::ELF::Elf64_Ehdr));
404514f5e3Sopenharmony_ci    builder.PackELFSections(file);
414514f5e3Sopenharmony_ci    builder.PackELFSegment(file);
424514f5e3Sopenharmony_ci    file.close();
434514f5e3Sopenharmony_ci    return true;
444514f5e3Sopenharmony_ci}
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_cibool AnFileInfo::LoadInternal(const std::string &filename)
474514f5e3Sopenharmony_ci{
484514f5e3Sopenharmony_ci    if (fileMapMem_.GetOriginAddr() == nullptr) {
494514f5e3Sopenharmony_ci        LOG_ECMA(ERROR) << "File mmap failed";
504514f5e3Sopenharmony_ci        return false;
514514f5e3Sopenharmony_ci    }
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    moduleNum_ = 1;
544514f5e3Sopenharmony_ci    des_.resize(moduleNum_);
554514f5e3Sopenharmony_ci    ModuleSectionDes &des = des_[0];
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    ElfReader reader(fileMapMem_);
584514f5e3Sopenharmony_ci    std::vector<ElfSecName> secs = GetDumpSectionNames();
594514f5e3Sopenharmony_ci    if (!reader.VerifyELFHeader(base::FileHeaderBase::ToVersionNumber(AOTFileVersion::AN_VERSION),
604514f5e3Sopenharmony_ci                                AOTFileVersion::AN_STRICT_MATCH)) {
614514f5e3Sopenharmony_ci        return false;
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci    reader.ParseELFSections(des, secs);
644514f5e3Sopenharmony_ci    if (!reader.ParseELFSegment()) {
654514f5e3Sopenharmony_ci        LOG_ECMA(ERROR) << "modify mmap area permission failed";
664514f5e3Sopenharmony_ci        return false;
674514f5e3Sopenharmony_ci    }
684514f5e3Sopenharmony_ci    ParseFunctionEntrySection(des);
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci    UpdateFuncEntries();
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    LOG_COMPILER(INFO) << "loaded an file: " << filename.c_str();
734514f5e3Sopenharmony_ci    isLoad_ = true;
744514f5e3Sopenharmony_ci    return true;
754514f5e3Sopenharmony_ci}
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_cibool AnFileInfo::Load(const std::string &filename)
784514f5e3Sopenharmony_ci{
794514f5e3Sopenharmony_ci    std::string realPath;
804514f5e3Sopenharmony_ci    if (!RealPath(filename, realPath, false)) {
814514f5e3Sopenharmony_ci        LOG_COMPILER(ERROR) << "Can not load aot file from path [ " << filename << " ], "
824514f5e3Sopenharmony_ci                            << "please execute ark_aot_compiler with options --aot-file.";
834514f5e3Sopenharmony_ci        return false;
844514f5e3Sopenharmony_ci    }
854514f5e3Sopenharmony_ci    if (!FileExist(realPath.c_str())) {
864514f5e3Sopenharmony_ci        LOG_ECMA(WARN) << "File not exist. file: " << realPath;
874514f5e3Sopenharmony_ci        return false;
884514f5e3Sopenharmony_ci    }
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci    fileMapMem_ = FileMap(realPath.c_str(), FILE_RDONLY, PAGE_PROT_READ);
914514f5e3Sopenharmony_ci    return LoadInternal(filename);
924514f5e3Sopenharmony_ci}
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci#if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM)
954514f5e3Sopenharmony_cibool AnFileInfo::Load(const std::string &filename, [[maybe_unused]] std::function<bool
964514f5e3Sopenharmony_ci    (std::string fileName, uint8_t **buff, size_t *buffSize)> ReadAOTCallBack)
974514f5e3Sopenharmony_ci{
984514f5e3Sopenharmony_ci    std::string fileName = filename;
994514f5e3Sopenharmony_ci    uint8_t *buff = nullptr;
1004514f5e3Sopenharmony_ci    size_t buffSize = 0;
1014514f5e3Sopenharmony_ci    size_t found = filename.find_last_of("/");
1024514f5e3Sopenharmony_ci    if (found != std::string::npos) {
1034514f5e3Sopenharmony_ci        fileName = filename.substr(found + 1);
1044514f5e3Sopenharmony_ci    }
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    LOG_ECMA(INFO) << "Call JsAotReader to load: " << fileName;
1074514f5e3Sopenharmony_ci    if (ReadAOTCallBack(fileName, &buff, &buffSize)) {
1084514f5e3Sopenharmony_ci        void* newBuff = nullptr;
1094514f5e3Sopenharmony_ci        if (posix_memalign(&newBuff, sysconf(_SC_PAGESIZE), buffSize) != 0) {
1104514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "posix_memalign failed!";
1114514f5e3Sopenharmony_ci            return false;
1124514f5e3Sopenharmony_ci        }
1134514f5e3Sopenharmony_ci        std::copy(reinterpret_cast<char*>(buff), reinterpret_cast<char*>(buff) + buffSize,
1144514f5e3Sopenharmony_ci                  reinterpret_cast<char*>(newBuff));
1154514f5e3Sopenharmony_ci        fileMapMem_ = MemMap(newBuff, buffSize);
1164514f5e3Sopenharmony_ci    }
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci    return LoadInternal(filename);
1194514f5e3Sopenharmony_ci}
1204514f5e3Sopenharmony_ci#endif
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_civoid AnFileInfo::TryRemoveAnFile(const char *filename)
1234514f5e3Sopenharmony_ci{
1244514f5e3Sopenharmony_ci    if (!FileExist(filename)) {
1254514f5e3Sopenharmony_ci        return;
1264514f5e3Sopenharmony_ci    }
1274514f5e3Sopenharmony_ci    if (Unlink(filename) == -1) {
1284514f5e3Sopenharmony_ci        LOG_COMPILER(ERROR) << "remove " << filename << " failed and errno is " << errno;
1294514f5e3Sopenharmony_ci    }
1304514f5e3Sopenharmony_ci}
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_civoid AnFileInfo::ParseFunctionEntrySection(ModuleSectionDes &des)
1334514f5e3Sopenharmony_ci{
1344514f5e3Sopenharmony_ci    uint64_t secAddr = des.GetSecAddr(ElfSecName::ARK_FUNCENTRY);
1354514f5e3Sopenharmony_ci    uint32_t secSize = des.GetSecSize(ElfSecName::ARK_FUNCENTRY);
1364514f5e3Sopenharmony_ci    FuncEntryDes *entryDes = reinterpret_cast<FuncEntryDes *>(secAddr);
1374514f5e3Sopenharmony_ci    entryNum_ = secSize / sizeof(FuncEntryDes);
1384514f5e3Sopenharmony_ci    entries_.assign(entryDes, entryDes + entryNum_);
1394514f5e3Sopenharmony_ci    des.SetStartIndex(0);
1404514f5e3Sopenharmony_ci    des.SetFuncCount(entryNum_);
1414514f5e3Sopenharmony_ci}
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_civoid AnFileInfo::UpdateFuncEntries()
1444514f5e3Sopenharmony_ci{
1454514f5e3Sopenharmony_ci    ModuleSectionDes &des = des_[0];
1464514f5e3Sopenharmony_ci    size_t len = entries_.size();
1474514f5e3Sopenharmony_ci    for (size_t i = 0; i < len; i++) {
1484514f5e3Sopenharmony_ci        FuncEntryDes &funcDes = entries_[i];
1494514f5e3Sopenharmony_ci        funcDes.codeAddr_ += des.GetSecAddr(ElfSecName::TEXT);
1504514f5e3Sopenharmony_ci        if (funcDes.isMainFunc_) {
1514514f5e3Sopenharmony_ci            EntryKey key = std::make_pair(funcDes.abcIndexInAi_, funcDes.indexInKindOrMethodId_);
1524514f5e3Sopenharmony_ci            mainEntryMap_[key] = MainFuncEntry { funcDes.codeAddr_, funcDes.fpDeltaPrevFrameSp_, funcDes.isFastCall_ };
1534514f5e3Sopenharmony_ci#ifndef NDEBUG
1544514f5e3Sopenharmony_ci            LOG_COMPILER(INFO) << "AnFileInfo Load main method id: " << funcDes.indexInKindOrMethodId_
1554514f5e3Sopenharmony_ci                               << " code addr: " << reinterpret_cast<void *>(funcDes.codeAddr_);
1564514f5e3Sopenharmony_ci#endif
1574514f5e3Sopenharmony_ci        }
1584514f5e3Sopenharmony_ci    }
1594514f5e3Sopenharmony_ci}
1604514f5e3Sopenharmony_ci
1614514f5e3Sopenharmony_ciconst std::vector<ElfSecName> &AnFileInfo::GetDumpSectionNames()
1624514f5e3Sopenharmony_ci{
1634514f5e3Sopenharmony_ci    static const std::vector<ElfSecName> secNames = {
1644514f5e3Sopenharmony_ci        ElfSecName::TEXT,
1654514f5e3Sopenharmony_ci        ElfSecName::STRTAB,
1664514f5e3Sopenharmony_ci        ElfSecName::SYMTAB,
1674514f5e3Sopenharmony_ci        ElfSecName::SHSTRTAB,
1684514f5e3Sopenharmony_ci        ElfSecName::ARK_STACKMAP,
1694514f5e3Sopenharmony_ci        ElfSecName::ARK_FUNCENTRY
1704514f5e3Sopenharmony_ci    };
1714514f5e3Sopenharmony_ci    return secNames;
1724514f5e3Sopenharmony_ci}
1734514f5e3Sopenharmony_ci
1744514f5e3Sopenharmony_civoid AnFileInfo::Destroy()
1754514f5e3Sopenharmony_ci{
1764514f5e3Sopenharmony_ci    mainEntryMap_.clear();
1774514f5e3Sopenharmony_ci    isLoad_ = false;
1784514f5e3Sopenharmony_ci    curTextSecOffset_ = 0;
1794514f5e3Sopenharmony_ci    AOTFileInfo::Destroy();
1804514f5e3Sopenharmony_ci}
1814514f5e3Sopenharmony_ci
1824514f5e3Sopenharmony_civoid AnFileInfo::Dump() const
1834514f5e3Sopenharmony_ci{
1844514f5e3Sopenharmony_ci    LOG_COMPILER(INFO) << "An file loading: ";
1854514f5e3Sopenharmony_ci    int i = 0;
1864514f5e3Sopenharmony_ci    for (const ModuleSectionDes &d : des_) {
1874514f5e3Sopenharmony_ci        i++;
1884514f5e3Sopenharmony_ci        for (const auto &s : d.GetSectionsInfo()) {
1894514f5e3Sopenharmony_ci            std::string name = d.GetSecName(s.first);
1904514f5e3Sopenharmony_ci            uint32_t size = d.GetSecSize(s.first);
1914514f5e3Sopenharmony_ci            uint64_t addr = d.GetSecAddr(s.first);
1924514f5e3Sopenharmony_ci            LOG_COMPILER(INFO) << " - module-" << i << " <" << name << "> [0x" << std::hex << addr << ", 0x"
1934514f5e3Sopenharmony_ci                                << std::hex << addr + size << "]";
1944514f5e3Sopenharmony_ci        }
1954514f5e3Sopenharmony_ci    }
1964514f5e3Sopenharmony_ci}
1974514f5e3Sopenharmony_ci
1984514f5e3Sopenharmony_cibool AnFileInfo::IsLoadMain(uint32_t fileIndex, const JSPandaFile *jsPandaFile, const CString &entry) const
1994514f5e3Sopenharmony_ci{
2004514f5e3Sopenharmony_ci    auto methodId = jsPandaFile->GetMainMethodIndex(entry);
2014514f5e3Sopenharmony_ci#ifndef NDEBUG
2024514f5e3Sopenharmony_ci    LOG_COMPILER(INFO) << "AnFileInfo IsLoadMain method id: " << methodId << " entry: " << entry;
2034514f5e3Sopenharmony_ci#endif
2044514f5e3Sopenharmony_ci    auto it = mainEntryMap_.find(std::make_pair(fileIndex, methodId));
2054514f5e3Sopenharmony_ci    return it != mainEntryMap_.end();
2064514f5e3Sopenharmony_ci}
2074514f5e3Sopenharmony_ci
2084514f5e3Sopenharmony_civoid AnFileInfo::AddFuncEntrySec()
2094514f5e3Sopenharmony_ci{
2104514f5e3Sopenharmony_ci    ModuleSectionDes &des = des_[ElfBuilder::FuncEntryModuleDesIndex];
2114514f5e3Sopenharmony_ci    // add section
2124514f5e3Sopenharmony_ci    uint64_t funcEntryAddr = reinterpret_cast<uint64_t>(entries_.data());
2134514f5e3Sopenharmony_ci    uint32_t funcEntrySize = sizeof(FuncEntryDes) * entryNum_;
2144514f5e3Sopenharmony_ci    des.SetSecAddrAndSize(ElfSecName::ARK_FUNCENTRY, funcEntryAddr, funcEntrySize);
2154514f5e3Sopenharmony_ci}
2164514f5e3Sopenharmony_ci
2174514f5e3Sopenharmony_civoid AnFileInfo::GenerateMethodToEntryIndexMap()
2184514f5e3Sopenharmony_ci{
2194514f5e3Sopenharmony_ci    const std::vector<AOTFileInfo::FuncEntryDes> &entries = GetStubs();
2204514f5e3Sopenharmony_ci    uint32_t entriesSize = entries.size();
2214514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < entriesSize; ++i) {
2224514f5e3Sopenharmony_ci        const AOTFileInfo::FuncEntryDes &entry = entries[i];
2234514f5e3Sopenharmony_ci        std::string &fileName = entryIdxToFileNameMap_.at(i);
2244514f5e3Sopenharmony_ci        auto key = std::make_pair(fileName, entry.indexInKindOrMethodId_);
2254514f5e3Sopenharmony_ci        methodToEntryIndexMap_[key] = i;
2264514f5e3Sopenharmony_ci    }
2274514f5e3Sopenharmony_ci}
2284514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
229