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 
16 #ifndef ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H
17 #define ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H
18 
19 #include <map>
20 #include <set>
21 #include <utility>
22 #include <stdint.h>
23 #include <string>
24 #include "ecmascript/compiler/aot_file/aot_file_manager.h"
25 #include "ecmascript/compiler/binary_section.h"
26 
27 namespace panda::ecmascript {
28 
29 class ModuleSectionDes;
30 
31 class ElfBuilder {
32 public:
33     ElfBuilder(const std::vector<ModuleSectionDes> &des,
34                const std::vector<ElfSecName> &sections);
35     ~ElfBuilder();
36     static constexpr uint32_t FuncEntryModuleDesIndex = 0;
37     void PackELFHeader(llvm::ELF::Elf64_Ehdr &header, uint32_t version, Triple triple);
38     void PackELFSections(std::ofstream &elfFile);
39     void PackELFSegment(std::ofstream &elfFile);
40     void MergeTextSections(std::ofstream &elfFile,
41         std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
42     void MergeArkStackMapSections(std::ofstream &elfFile,
43         std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
44     void MergeStrtabSections(std::ofstream &elfFile,
45         std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
46     void MergeSymtabSections(std::ofstream &elfFile, std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo,
47         llvm::ELF::Elf64_Off &curSecOffset, llvm::ELF::Elf64_Off &asmStubOffset);
48     uint32_t AddAsmStubStrTab(std::ofstream &elfFile,
49         const std::vector<std::pair<std::string, uint32_t>> &asmStubELFInfo);
50     static llvm::ELF::Elf64_Word FindShName(std::string name, uintptr_t strTabPtr, int strTabSize);
GetFullSecInfo() const51     std::map<ElfSecName, std::pair<uint64_t, uint32_t>> GetFullSecInfo() const
52     {
53         return des_[FullSecIndex].GetSectionsInfo();
54     }
SetEnableSecDump(bool flag)55     void SetEnableSecDump(bool flag)
56     {
57         enableSecDump_ = flag;
58     }
59 
60 private:
61     uint32_t GetShIndex(ElfSecName section) const;
62     int GetSegmentNum() const;
63     int GetSecNum() const;
64     unsigned GetPFlag(ElfSecName segment) const;
65     ElfSecName GetSegmentName(const ElfSecName &secName) const;
66     std::pair<uint64_t, uint32_t> FindShStrTab() const;
67     void AllocateShdr(std::unique_ptr<llvm::ELF::Elf64_Shdr []> &shdr, const uint32_t &secNum);
68     llvm::ELF::Elf64_Off ComputeEndAddrOfShdr(const uint32_t &secNum) const;
69     bool SupportELF();
70     void DumpSection() const;
71     void AddShStrTabSection();
72     void Initialize();
73     void SetLastSection();
74     void RemoveNotNeedSection();
75     void FixSymtab(llvm::ELF::Elf64_Shdr* shdr);
76 
77     static constexpr uint32_t ASMSTUB_MODULE_NUM = 4;
78     static constexpr uint32_t ShStrTableModuleDesIndex = 0;
79     static constexpr uint32_t FullSecIndex = 0;
80 
81     std::vector<ModuleSectionDes> des_ {};
82     std::unique_ptr<char []> shStrTabPtr_ {nullptr};
83     std::map<ElfSecName, llvm::ELF::Elf64_Shdr> sectionToShdr_;
84     std::map<ElfSecName, llvm::ELF::Elf64_Xword> sectionToAlign_;
85     std::map<ElfSecName, ElfSecName> sectionToSegment_;
86     std::map<ElfSecName, uintptr_t> sectionToFileOffset_;
87     std::map<ElfSecName, unsigned> segmentToFlag_;
88     std::vector<ElfSecName> sections_ {};
89     std::set<ElfSecName> segments_;
90     std::vector<uint32_t> stubTextOffset_ {};
91     std::vector<uint32_t> asmStubStrName_ {};
92     bool enableSecDump_ {false};
93     ElfSecName lastDataSection {ElfSecName::NONE};
94     ElfSecName lastCodeSection {ElfSecName::NONE};
95 };
96 }  // namespace panda::ecmascript
97 #endif  // ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H
98