• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, const std::vector<ElfSecName> &sections);
34     ~ElfBuilder();
35     static constexpr uint32_t FuncEntryModuleDesIndex = 0;
36     void PackELFHeader(llvm::ELF::Elf64_Ehdr &header, uint32_t version, Triple triple);
37     void PackELFSections(std::ofstream &elfFile);
38     void PackELFSegment(std::ofstream &elfFile);
39     void MergeTextSections(std::ofstream &elfFile,
40         std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
41     void MergeArkStackMapSections(std::ofstream &elfFile,
42         std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
43     void MergeStrtabSections(std::ofstream &elfFile,
44         std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
45     void MergeSymtabSections(std::ofstream &elfFile,
46         std::vector<ModuleSectionDes::ModuleRegionInfo> &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset);
47     static llvm::ELF::Elf64_Word FindShName(std::string name, uintptr_t strTabPtr, int strTabSize);
GetFullSecInfo()48     std::map<ElfSecName, std::pair<uint64_t, uint32_t>> GetFullSecInfo() const
49     {
50         return des_[FullSecIndex].GetSectionsInfo();
51     }
SetEnableSecDump(bool flag)52     void SetEnableSecDump(bool flag)
53     {
54         enableSecDump_ = flag;
55     }
56 
57 private:
58     uint32_t GetShIndex(ElfSecName section) const;
59     int GetSegmentNum() const;
60     int GetSecNum() const;
61     unsigned GetPFlag(ElfSecName segment) const;
62     ElfSecName GetSegmentName(const ElfSecName &secName) const;
63     std::pair<uint64_t, uint32_t> FindShStrTab() const;
64     void AllocateShdr(std::unique_ptr<llvm::ELF::Elf64_Shdr []> &shdr, const uint32_t &secNum);
65     llvm::ELF::Elf64_Off ComputeEndAddrOfShdr(const uint32_t &secNum) const;
66     bool SupportELF();
67     void DumpSection() const;
68     void AddShStrTabSection();
69     void Initialize();
70     void SetLastSection();
71     void RemoveNotNeedSection();
72     void FixSymtab(llvm::ELF::Elf64_Shdr* shdr);
73 
74     static constexpr uint32_t ASMSTUB_MODULE_NUM = 3;
75     static constexpr uint32_t ShStrTableModuleDesIndex = 0;
76     static constexpr uint32_t FullSecIndex = 0;
77 
78     std::vector<ModuleSectionDes> des_ {};
79     std::unique_ptr<char []> shStrTabPtr_ {nullptr};
80     std::map<ElfSecName, llvm::ELF::Elf64_Shdr> sectionToShdr_;
81     std::map<ElfSecName, llvm::ELF::Elf64_Xword> sectionToAlign_;
82     std::map<ElfSecName, ElfSecName> sectionToSegment_;
83     std::map<ElfSecName, uintptr_t> sectionToFileOffset_;
84     std::map<ElfSecName, unsigned> segmentToFlag_;
85     std::vector<ElfSecName> sections_ {};
86     std::set<ElfSecName> segments_;
87     std::vector<uint32_t> stubTextOffset_ {};
88     bool enableSecDump_ {false};
89     ElfSecName lastDataSection {ElfSecName::NONE};
90     ElfSecName lastCodeSection {ElfSecName::NONE};
91 };
92 }  // namespace panda::ecmascript
93 #endif  // ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H
94