1 /* 2 * Copyright (c) 2021-2024 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 COMPILER_AOT_AOT_BULDER_AOT_FILE_BUILDER_H 17 #define COMPILER_AOT_AOT_BULDER_AOT_FILE_BUILDER_H 18 19 #include "aot/compiled_method.h" 20 #include "aot/aot_file.h" 21 #include "elf_builder.h" 22 #include "utils/arch.h" 23 #include "utils/arena_containers.h" 24 #include "utils/bit_vector.h" 25 #include "optimizer/ir/runtime_interface.h" 26 #include <string> 27 #include <vector> 28 #include "mem/gc/gc_types.h" 29 30 namespace ark { 31 class Class; 32 } // namespace ark 33 34 namespace ark::compiler { 35 36 template <Arch ARCH, bool IS_JIT_MODE> 37 class ElfBuilder; 38 39 struct RoData { 40 std::vector<uint8_t> content; 41 std::string name; 42 size_t alignment; 43 }; 44 45 class AotBuilder : public ElfWriter { 46 public: SetGcType(uint32_t gcType)47 void SetGcType(uint32_t gcType) 48 { 49 gcType_ = gcType; 50 } GetGcType()51 uint32_t GetGcType() const 52 { 53 return gcType_; 54 } 55 GetIntfInlineCacheIndex()56 uint64_t *GetIntfInlineCacheIndex() 57 { 58 return &intfInlineCacheIndex_; 59 } 60 61 int Write(const std::string &cmdline, const std::string &fileName); 62 63 void StartFile(const std::string &name, uint32_t checksum); 64 void EndFile(); 65 SetRoDataSections(std::vector<RoData> roDatas)66 void SetRoDataSections(std::vector<RoData> roDatas) 67 { 68 roDatas_ = std::move(roDatas); 69 } 70 GetGotPlt()71 auto *GetGotPlt() 72 { 73 return &gotPlt_; 74 } 75 GetGotVirtIndexes()76 auto *GetGotVirtIndexes() 77 { 78 return &gotVirtIndexes_; 79 } 80 GetGotClass()81 auto *GetGotClass() 82 { 83 return &gotClass_; 84 } 85 GetGotString()86 auto *GetGotString() 87 { 88 return &gotString_; 89 } 90 GetGotIntfInlineCache()91 auto *GetGotIntfInlineCache() 92 { 93 return &gotIntfInlineCache_; 94 } 95 GetGotCommon()96 auto *GetGotCommon() 97 { 98 return &gotCommon_; 99 } 100 SetBootAot(bool bootAot)101 void SetBootAot(bool bootAot) 102 { 103 bootAot_ = bootAot; 104 } 105 SetWithCha(bool withCha)106 void SetWithCha(bool withCha) 107 { 108 withCha_ = withCha; 109 } 110 SetGenerateSymbols(bool generateSymbols)111 void SetGenerateSymbols(bool generateSymbols) 112 { 113 generateSymbols_ = generateSymbols; 114 } 115 116 void AddClassHashTable(const panda_file::File &pandaFile); 117 InsertEntityPairHeader(uint32_t classHash,uint32_t classId)118 void InsertEntityPairHeader(uint32_t classHash, uint32_t classId) 119 { 120 entityPairHeaders_.emplace_back(); 121 auto &entityPair = entityPairHeaders_.back(); 122 entityPair.descriptorHash = classHash; 123 entityPair.entityIdOffset = classId; 124 } 125 GetEntityPairHeaders()126 auto *GetEntityPairHeaders() const 127 { 128 return &entityPairHeaders_; 129 } 130 InsertClassHashTableSize(uint32_t size)131 void InsertClassHashTableSize(uint32_t size) 132 { 133 classHashTablesSize_.emplace_back(size); 134 } 135 GetClassHashTableSize()136 auto *GetClassHashTableSize() const 137 { 138 return &classHashTablesSize_; 139 } 140 141 protected: 142 template <Arch ARCH> 143 int PrepareElfBuilder(ElfBuilder<ARCH> &builder, const std::string &cmdline, const std::string &fileName); 144 145 private: 146 template <Arch ARCH> 147 int WriteImpl(const std::string &cmdline, const std::string &fileName); 148 149 template <Arch ARCH> 150 void GenerateSymbols(ElfBuilder<ARCH> &builder); 151 152 template <Arch ARCH> 153 void EmitPlt(Span<typename ArchTraits<ARCH>::WordType> ptrView, size_t gotDataSize); 154 155 void FillHeader(const std::string &cmdline, const std::string &fileName); 156 157 void ResolveConflictClassHashTable(const panda_file::File &pandaFile, std::vector<unsigned int> conflictEntityTable, 158 size_t conflictNum, std::vector<panda_file::EntityPairHeader> &entityPairs); 159 160 private: 161 std::string fileName_; 162 compiler::AotHeader aotHeader_ {}; 163 uint32_t gcType_ {static_cast<uint32_t>(mem::GCType::INVALID_GC)}; 164 uint64_t intfInlineCacheIndex_ {0}; 165 std::vector<compiler::RoData> roDatas_; 166 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> gotPlt_; 167 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> gotVirtIndexes_; 168 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> gotClass_; 169 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> gotString_; 170 std::map<std::pair<const panda_file::File *, uint64_t>, int32_t> gotIntfInlineCache_; 171 std::map<std::pair<const panda_file::File *, uint64_t>, int32_t> gotCommon_; 172 bool bootAot_ {false}; 173 bool withCha_ {true}; 174 bool generateSymbols_ {false}; 175 176 std::vector<panda_file::EntityPairHeader> entityPairHeaders_; 177 std::vector<uint32_t> classHashTablesSize_; 178 friend class CodeDataProvider; 179 friend class JitCodeDataProvider; 180 }; 181 182 } // namespace ark::compiler 183 184 #endif // COMPILER_AOT_AOT_BULDER_AOT_FILE_BUILDER_H 185