• 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     static llvm::ELF::Elf64_Word FindShName(std::string name, uintptr_t strTabPtr, int strTabSize);
GetFullSecInfo()44     std::map<ElfSecName, std::pair<uint64_t, uint32_t>> GetFullSecInfo() const
45     {
46         return des_[FullSecIndex].GetSectionsInfo();
47     }
SetEnableSecDump(bool flag)48     void SetEnableSecDump(bool flag)
49     {
50         enableSecDump_ = flag;
51     }
52 
53 private:
54     uint32_t GetShIndex(ElfSecName section) const;
55     int GetSegmentNum() const;
56     int GetSecNum() const;
57     unsigned GetPFlag(ElfSecName segment) const;
58     ElfSecName GetSegmentName(const ElfSecName &secName) const;
59     std::pair<uint64_t, uint32_t> FindShStrTab() const;
60     void AllocateShdr(std::unique_ptr<llvm::ELF::Elf64_Shdr []> &shdr, const uint32_t &secNum);
61     llvm::ELF::Elf64_Off ComputeEndAddrOfShdr(const uint32_t &secNum) const;
62     bool SupportELF();
63     void DumpSection() const;
64     void AddShStrTabSection();
65     void Initialize();
66     void SetLastSection();
67     void RemoveNotNeedSection();
68 
69     static constexpr uint32_t ASMSTUB_MODULE_NUM = 3;
70     static constexpr uint32_t ShStrTableModuleDesIndex = 0;
71     static constexpr uint32_t FullSecIndex = 0;
72     std::vector<ModuleSectionDes> des_ {};
73     std::unique_ptr<char []> shStrTabPtr_ {nullptr};
74     std::map<ElfSecName, llvm::ELF::Elf64_Shdr> sectionToShdr_;
75     std::map<ElfSecName, llvm::ELF::Elf64_Xword> sectionToAlign_;
76     std::map<ElfSecName, ElfSecName> sectionToSegment_;
77     std::map<ElfSecName, uintptr_t> sectionToFileOffset_;
78     std::map<ElfSecName, unsigned> segmentToFlag_;
79     std::vector<ElfSecName> sections_ {};
80     std::set<ElfSecName> segments_;
81     bool enableSecDump_ {false};
82     ElfSecName lastDataSection {ElfSecName::NONE};
83     ElfSecName lastCodeSection {ElfSecName::NONE};
84 };
85 }  // namespace panda::ecmascript
86 #endif  // ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H
87