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_BUILDER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <map>
204514f5e3Sopenharmony_ci#include <set>
214514f5e3Sopenharmony_ci#include <utility>
224514f5e3Sopenharmony_ci#include <stdint.h>
234514f5e3Sopenharmony_ci#include <string>
244514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/aot_file_manager.h"
254514f5e3Sopenharmony_ci#include "ecmascript/compiler/binary_section.h"
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_cinamespace panda::ecmascript {
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ciclass ModuleSectionDes;
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ciclass ElfBuilder {
324514f5e3Sopenharmony_cipublic:
334514f5e3Sopenharmony_ci    ElfBuilder(const std::vector<ModuleSectionDes> &des,
344514f5e3Sopenharmony_ci               const std::vector<ElfSecName> &sections);
354514f5e3Sopenharmony_ci    ~ElfBuilder();
364514f5e3Sopenharmony_ci    static constexpr uint32_t FuncEntryModuleDesIndex = 0;
374514f5e3Sopenharmony_ci    void PackELFHeader(llvm::ELF::Elf64_Ehdr &header, uint32_t version, Triple triple);
384514f5e3Sopenharmony_ci    void PackELFSections(std::ofstream &elfFile);
394514f5e3Sopenharmony_ci    void PackELFSegment(std::ofstream &elfFile);
404514f5e3Sopenharmony_ci    void MergeTextSections(std::ofstream &elfFile,
414514f5e3Sopenharmony_ci        std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
424514f5e3Sopenharmony_ci    void MergeArkStackMapSections(std::ofstream &elfFile,
434514f5e3Sopenharmony_ci        std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
444514f5e3Sopenharmony_ci    void MergeStrtabSections(std::ofstream &elfFile,
454514f5e3Sopenharmony_ci        std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
464514f5e3Sopenharmony_ci    void MergeSymtabSections(std::ofstream &elfFile, std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo,
474514f5e3Sopenharmony_ci        llvm::ELF::Elf64_Off &curSecOffset, llvm::ELF::Elf64_Off &asmStubOffset);
484514f5e3Sopenharmony_ci    uint32_t AddAsmStubStrTab(std::ofstream &elfFile,
494514f5e3Sopenharmony_ci        const std::vector<std::pair<std::string, uint32_t>> &asmStubELFInfo);
504514f5e3Sopenharmony_ci    static llvm::ELF::Elf64_Word FindShName(std::string name, uintptr_t strTabPtr, int strTabSize);
514514f5e3Sopenharmony_ci    std::map<ElfSecName, std::pair<uint64_t, uint32_t>> GetFullSecInfo() const
524514f5e3Sopenharmony_ci    {
534514f5e3Sopenharmony_ci        return des_[FullSecIndex].GetSectionsInfo();
544514f5e3Sopenharmony_ci    }
554514f5e3Sopenharmony_ci    void SetEnableSecDump(bool flag)
564514f5e3Sopenharmony_ci    {
574514f5e3Sopenharmony_ci        enableSecDump_ = flag;
584514f5e3Sopenharmony_ci    }
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ciprivate:
614514f5e3Sopenharmony_ci    uint32_t GetShIndex(ElfSecName section) const;
624514f5e3Sopenharmony_ci    int GetSegmentNum() const;
634514f5e3Sopenharmony_ci    int GetSecNum() const;
644514f5e3Sopenharmony_ci    unsigned GetPFlag(ElfSecName segment) const;
654514f5e3Sopenharmony_ci    ElfSecName GetSegmentName(const ElfSecName &secName) const;
664514f5e3Sopenharmony_ci    std::pair<uint64_t, uint32_t> FindShStrTab() const;
674514f5e3Sopenharmony_ci    void AllocateShdr(std::unique_ptr<llvm::ELF::Elf64_Shdr []> &shdr, const uint32_t &secNum);
684514f5e3Sopenharmony_ci    llvm::ELF::Elf64_Off ComputeEndAddrOfShdr(const uint32_t &secNum) const;
694514f5e3Sopenharmony_ci    bool SupportELF();
704514f5e3Sopenharmony_ci    void DumpSection() const;
714514f5e3Sopenharmony_ci    void AddShStrTabSection();
724514f5e3Sopenharmony_ci    void Initialize();
734514f5e3Sopenharmony_ci    void SetLastSection();
744514f5e3Sopenharmony_ci    void RemoveNotNeedSection();
754514f5e3Sopenharmony_ci    void FixSymtab(llvm::ELF::Elf64_Shdr* shdr);
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    static constexpr uint32_t ASMSTUB_MODULE_NUM = 4;
784514f5e3Sopenharmony_ci    static constexpr uint32_t ShStrTableModuleDesIndex = 0;
794514f5e3Sopenharmony_ci    static constexpr uint32_t FullSecIndex = 0;
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    std::vector<ModuleSectionDes> des_ {};
824514f5e3Sopenharmony_ci    std::unique_ptr<char []> shStrTabPtr_ {nullptr};
834514f5e3Sopenharmony_ci    std::map<ElfSecName, llvm::ELF::Elf64_Shdr> sectionToShdr_;
844514f5e3Sopenharmony_ci    std::map<ElfSecName, llvm::ELF::Elf64_Xword> sectionToAlign_;
854514f5e3Sopenharmony_ci    std::map<ElfSecName, ElfSecName> sectionToSegment_;
864514f5e3Sopenharmony_ci    std::map<ElfSecName, uintptr_t> sectionToFileOffset_;
874514f5e3Sopenharmony_ci    std::map<ElfSecName, unsigned> segmentToFlag_;
884514f5e3Sopenharmony_ci    std::vector<ElfSecName> sections_ {};
894514f5e3Sopenharmony_ci    std::set<ElfSecName> segments_;
904514f5e3Sopenharmony_ci    std::vector<uint32_t> stubTextOffset_ {};
914514f5e3Sopenharmony_ci    std::vector<uint32_t> asmStubStrName_ {};
924514f5e3Sopenharmony_ci    bool enableSecDump_ {false};
934514f5e3Sopenharmony_ci    ElfSecName lastDataSection {ElfSecName::NONE};
944514f5e3Sopenharmony_ci    ElfSecName lastCodeSection {ElfSecName::NONE};
954514f5e3Sopenharmony_ci};
964514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
974514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H
98