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_STUB_FILE_INFO_H
16#define ECMASCRIPT_COMPILER_AOT_FILE_STUB_FILE_INFO_H
17
18#include "ecmascript/compiler/aot_file/aot_file_info.h"
19#include "ecmascript/compiler/aot_file/gdb_jit.h"
20
21namespace panda::ecmascript {
22class PUBLIC_API StubFileInfo : public AOTFileInfo {
23public:
24    StubFileInfo() = default;
25    ~StubFileInfo() override = default;
26    void Save(const std::string &filename, Triple triple);
27
28    void AddModuleDes(ModuleSectionDes &moduleDes)
29    {
30        des_.emplace_back(moduleDes);
31        for (auto &s : moduleDes.GetSectionsInfo()) {
32            auto sec = ElfSection(s.first);
33            if (sec.isSequentialAOTSec()) {
34                accumulateTotalSize(s.second.second);
35            }
36        }
37        accumulateTotalSize(moduleDes.GetArkStackMapSize());
38    }
39
40    uint64_t GetAsmStubAddr() const
41    {
42        return reinterpret_cast<uint64_t>(asmStubAddr_);
43    }
44
45    uint32_t GetAsmStubSize() const
46    {
47        return static_cast<uint32_t>(asmStubSize_);
48    }
49
50    void SetAsmStubAddr(void *addr)
51    {
52        asmStubAddr_ = addr;
53    }
54
55    void SetAsmStubAddr(uintptr_t addr)
56    {
57        asmStubAddr_ = reinterpret_cast<void *>(addr);
58    }
59
60    void SetAsmStubSize(size_t size)
61    {
62        asmStubSize_ = size;
63    }
64
65    void FillAsmStubTempHolder(uint8_t *buffer, size_t bufferSize)
66    {
67        asmStubTempHolder_.resize(bufferSize);
68        if (memcpy_s(asmStubTempHolder_.data(), bufferSize, buffer, bufferSize) != EOK) {
69            LOG_FULL(FATAL) << "memcpy_s failed";
70            return;
71        }
72        SetAsmStubAddr(asmStubTempHolder_.data());
73        SetAsmStubSize(bufferSize);
74    }
75
76    void AddAsmStubELFInfo(const std::vector<const CallSignature*> &asmCallSigns,
77                           const std::vector<size_t> &stubsOffset)
78    {
79        ASSERT(stubsOffset.size() > 0);
80        if (asmCallSigns.size() != stubsOffset.size() - 1) {
81            return;
82        }
83        size_t stubsNum = asmCallSigns.size();
84        for (size_t i = 0; i < stubsNum; i++) {
85            std::string name = asmCallSigns[i]->GetName();
86            uint32_t offset = static_cast<uint32_t>(stubsOffset[i]);
87            asmStubELFInfo_.emplace_back(std::make_pair(name, offset));
88        }
89        asmStubELFInfo_.emplace_back(std::make_pair("asm_stub", static_cast<uint32_t>(stubsOffset[stubsNum])));
90    }
91
92    void RegisterToDebugger()
93    {
94        if (fileMapMem_.GetOriginAddr() != nullptr) {
95            jit_debug::RegisterStubAnToDebugger((const char *)fileMapMem_.GetOriginAddr());
96        }
97    }
98
99    void UnregisterFromDebugger()
100    {
101        if (fileMapMem_.GetOriginAddr() != nullptr) {
102            jit_debug::UnregisterStubAnFromDebugger((const char *)fileMapMem_.GetOriginAddr());
103        }
104    }
105
106    void Dump() const DUMP_API_ATTR;
107
108    void Destroy() override {};
109private:
110    static constexpr uint32_t ASMSTUB_MODULE_NUM = 4;
111
112    bool MmapLoad(const std::string &fileName);
113#ifndef PANDA_TARGET_OHOS
114    bool Load();
115#endif
116    const std::vector<ElfSecName> &GetDumpSectionNames();
117    void *asmStubAddr_ {nullptr};
118    size_t asmStubSize_ {0};
119    std::vector<int> asmStubTempHolder_ {};
120    std::vector<std::pair<std::string, uint32_t>> asmStubELFInfo_ {};
121
122    friend class AnFileDataManager;
123};
124}  // namespace panda::ecmascript
125#endif  // ECMASCRIPT_COMPILER_AOT_FILE_STUB_FILE_INFO_H
126