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#ifndef ECMASCRIPT_COMPILER_AOT_FILE_ELF_READER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_AOT_FILE_ELF_READER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <utility>
204514f5e3Sopenharmony_ci#include <stdint.h>
214514f5e3Sopenharmony_ci#include <string>
224514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/aot_file_manager.h"
234514f5e3Sopenharmony_ci#include "ecmascript/compiler/binary_section.h"
244514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/elf_checker.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::ecmascript {
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ciclass ModuleSectionDes;
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ciclass ElfReader {
314514f5e3Sopenharmony_cipublic:
324514f5e3Sopenharmony_ci    ElfReader(const MemMap &fileMapMem) : fileMapMem_(fileMapMem) {};
334514f5e3Sopenharmony_ci    ElfReader(ExecutedMemoryAllocator::ExeMem &stubsMem) : stubsMem_(stubsMem) {};
344514f5e3Sopenharmony_ci    ~ElfReader() = default;
354514f5e3Sopenharmony_ci    bool VerifyELFHeader(uint32_t version, bool strictMatch);
364514f5e3Sopenharmony_ci    void ParseELFSections(ModuleSectionDes &des, std::vector<ElfSecName> &secs);
374514f5e3Sopenharmony_ci    void ParseELFSections(std::vector<ModuleSectionDes> &des, std::vector<ElfSecName> &secs);
384514f5e3Sopenharmony_ci    void ParseELFSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
394514f5e3Sopenharmony_ci        std::vector<ElfSecName> &secs);
404514f5e3Sopenharmony_ci    bool ParseELFSegment();
414514f5e3Sopenharmony_ci    ModuleSectionDes::ModuleRegionInfo *GetCurModuleInfo(uint32_t i, llvm::ELF::Elf64_Off offset);
424514f5e3Sopenharmony_ci    void SeparateTextSections(std::vector<ModuleSectionDes> &des, const uintptr_t &secAddr,
434514f5e3Sopenharmony_ci        llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &moduleInfoOffset);
444514f5e3Sopenharmony_ci    void SeparateArkStackMapSections(std::vector<ModuleSectionDes> &des, const uintptr_t &secAddr,
454514f5e3Sopenharmony_ci        llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &moduleInfoOffset);
464514f5e3Sopenharmony_ci    void SeparateStrtabSections(std::vector<ModuleSectionDes> &des, const uintptr_t &secAddr,
474514f5e3Sopenharmony_ci        llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &moduleInfoOffset);
484514f5e3Sopenharmony_ci    void SeparateSymtabSections(std::vector<ModuleSectionDes> &des, const uintptr_t &secAddr,
494514f5e3Sopenharmony_ci        llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &moduleInfoOffset);
504514f5e3Sopenharmony_ci    void SeparateTextSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
514514f5e3Sopenharmony_ci        const uint64_t &secAddr, llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &curShOffset);
524514f5e3Sopenharmony_ci    void SeparateArkStackMapSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
534514f5e3Sopenharmony_ci        const uint64_t &secAddr, llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &curShOffset);
544514f5e3Sopenharmony_ci    void SeparateStrtabSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
554514f5e3Sopenharmony_ci        const uintptr_t &secAddr, llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &curShOffset);
564514f5e3Sopenharmony_ci    void SeparateSymtabSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
574514f5e3Sopenharmony_ci        const uintptr_t &secAddr, llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &curShOffset);
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ciprivate:
604514f5e3Sopenharmony_ci    static uint32_t GetModuleNum(size_t moduleInfoSize)
614514f5e3Sopenharmony_ci    {
624514f5e3Sopenharmony_ci        ASSERT(moduleInfoSize % sizeof(ModuleSectionDes::ModuleRegionInfo) == 0);
634514f5e3Sopenharmony_ci        return moduleInfoSize / sizeof(ModuleSectionDes::ModuleRegionInfo);
644514f5e3Sopenharmony_ci    }
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    static constexpr uint32_t ASMSTUB_MODULE_NUM = 4;
674514f5e3Sopenharmony_ci    ExecutedMemoryAllocator::ExeMem stubsMem_ {};
684514f5e3Sopenharmony_ci    MemMap fileMapMem_ {};
694514f5e3Sopenharmony_ci    std::vector<ModuleSectionDes::ModuleRegionInfo> moduleInfo_;
704514f5e3Sopenharmony_ci};
714514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
724514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_COMPILER_AOT_FILE_ELF_READER_H
73