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 #include "ecmascript/compiler/aot_file/an_file_info.h"
17
18 #include <cerrno>
19 #include "ecmascript/compiler/aot_file/elf_builder.h"
20 #include "ecmascript/compiler/aot_file/elf_reader.h"
21
22 namespace panda::ecmascript {
Save(const std::string & filename,Triple triple)23 bool AnFileInfo::Save(const std::string &filename, Triple triple)
24 {
25 std::string realPath;
26 if (!RealPath(filename, realPath, false)) {
27 return false;
28 }
29 const char *rawPath = realPath.c_str();
30 if (!TryRemoveAnFile(rawPath)) {
31 LOG_COMPILER(ERROR) << "unlink an file fail";
32 return false;
33 };
34
35 std::ofstream file(rawPath, std::ofstream::binary);
36 SetStubNum(entries_.size());
37 AddFuncEntrySec();
38
39 ElfBuilder builder(des_, GetDumpSectionNames());
40 llvm::ELF::Elf64_Ehdr header;
41 builder.PackELFHeader(header, base::FileHeaderBase::ToVersionNumber(AOTFileVersion::AN_VERSION), triple);
42 file.write(reinterpret_cast<char *>(&header), sizeof(llvm::ELF::Elf64_Ehdr));
43 builder.PackELFSections(file);
44 builder.PackELFSegment(file);
45 file.close();
46 return true;
47 }
48
LoadInternal(const std::string & filename)49 bool AnFileInfo::LoadInternal(const std::string &filename)
50 {
51 if (fileMapMem_.GetOriginAddr() == nullptr) {
52 LOG_ECMA(ERROR) << "File mmap failed";
53 return false;
54 }
55
56 moduleNum_ = 1;
57 des_.resize(moduleNum_);
58 ModuleSectionDes &des = des_[0];
59
60 ElfReader reader(fileMapMem_);
61 std::vector<ElfSecName> secs = GetDumpSectionNames();
62 if (!reader.VerifyELFHeader(base::FileHeaderBase::ToVersionNumber(AOTFileVersion::AN_VERSION),
63 AOTFileVersion::AN_STRICT_MATCH)) {
64 return false;
65 }
66 reader.ParseELFSections(des, secs);
67 if (!reader.ParseELFSegment()) {
68 LOG_ECMA(ERROR) << "modify mmap area permission failed";
69 return false;
70 }
71 ParseFunctionEntrySection(des);
72
73 UpdateFuncEntries();
74
75 LOG_COMPILER(INFO) << "loaded an file: " << filename.c_str();
76 isLoad_ = true;
77 return true;
78 }
79
Load(const std::string & filename)80 bool AnFileInfo::Load(const std::string &filename)
81 {
82 std::string realPath;
83 if (!RealPath(filename, realPath, false)) {
84 LOG_COMPILER(ERROR) << "Can not load aot file from path [ " << filename << " ], "
85 << "please execute ark_aot_compiler with options --aot-file.";
86 return false;
87 }
88 if (!FileExist(realPath.c_str())) {
89 LOG_ECMA(WARN) << "File not exist. file: " << realPath;
90 return false;
91 }
92
93 fileMapMem_ = FileMap(realPath.c_str(), FILE_RDONLY, PAGE_PROT_READ);
94 return LoadInternal(filename);
95 }
96
97 #if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM)
Load(const std::string & filename,std::function<bool (std::string fileName,uint8_t ** buff,size_t * buffSize)> ReadAOTCallBack)98 bool AnFileInfo::Load(const std::string &filename, [[maybe_unused]] std::function<bool
99 (std::string fileName, uint8_t **buff, size_t *buffSize)> ReadAOTCallBack)
100 {
101 std::string fileName = filename;
102 uint8_t *buff = nullptr;
103 size_t buffSize = 0;
104 size_t found = filename.find_last_of("/");
105 if (found != std::string::npos) {
106 fileName = filename.substr(found + 1);
107 }
108
109 LOG_ECMA(INFO) << "Call JsAotReader to load: " << fileName;
110 if (ReadAOTCallBack(fileName, &buff, &buffSize)) {
111 void* newBuff = nullptr;
112 if (posix_memalign(&newBuff, sysconf(_SC_PAGESIZE), buffSize) != 0) {
113 LOG_ECMA(ERROR) << "posix_memalign failed!";
114 return false;
115 }
116 std::copy(reinterpret_cast<char*>(buff), reinterpret_cast<char*>(buff) + buffSize,
117 reinterpret_cast<char*>(newBuff));
118 fileMapMem_ = MemMap(newBuff, buffSize);
119 }
120
121 return LoadInternal(filename);
122 }
123 #endif
124
TryRemoveAnFile(const char * filename)125 bool AnFileInfo::TryRemoveAnFile(const char *filename)
126 {
127 if (!FileExist(filename)) {
128 return true;
129 }
130 if (Unlink(filename) == -1) {
131 LOG_COMPILER(ERROR) << "remove " << filename << " failed and errno is " << errno;
132 return false;
133 }
134 return true;
135 }
136
ParseFunctionEntrySection(ModuleSectionDes & des)137 void AnFileInfo::ParseFunctionEntrySection(ModuleSectionDes &des)
138 {
139 uint64_t secAddr = des.GetSecAddr(ElfSecName::ARK_FUNCENTRY);
140 uint32_t secSize = des.GetSecSize(ElfSecName::ARK_FUNCENTRY);
141 FuncEntryDes *entryDes = reinterpret_cast<FuncEntryDes *>(secAddr);
142 entryNum_ = secSize / sizeof(FuncEntryDes);
143 entries_.assign(entryDes, entryDes + entryNum_);
144 des.SetStartIndex(0);
145 des.SetFuncCount(entryNum_);
146 }
147
UpdateFuncEntries()148 void AnFileInfo::UpdateFuncEntries()
149 {
150 ModuleSectionDes &des = des_[0];
151 size_t len = entries_.size();
152 for (size_t i = 0; i < len; i++) {
153 FuncEntryDes &funcDes = entries_[i];
154 funcDes.codeAddr_ += des.GetSecAddr(ElfSecName::TEXT);
155 if (funcDes.isMainFunc_) {
156 EntryKey key = std::make_pair(funcDes.abcIndexInAi_, funcDes.indexInKindOrMethodId_);
157 mainEntryMap_[key] = MainFuncEntry { funcDes.codeAddr_, funcDes.fpDeltaPrevFrameSp_, funcDes.isFastCall_ };
158 #ifndef NDEBUG
159 LOG_COMPILER(INFO) << "AnFileInfo Load main method id: " << funcDes.indexInKindOrMethodId_
160 << " code addr: " << reinterpret_cast<void *>(funcDes.codeAddr_);
161 #endif
162 }
163 }
164 }
165
GetDumpSectionNames()166 const std::vector<ElfSecName> &AnFileInfo::GetDumpSectionNames()
167 {
168 static const std::vector<ElfSecName> secNames = {
169 ElfSecName::TEXT,
170 ElfSecName::STRTAB,
171 ElfSecName::SYMTAB,
172 ElfSecName::SHSTRTAB,
173 ElfSecName::ARK_STACKMAP,
174 ElfSecName::ARK_FUNCENTRY
175 };
176 return secNames;
177 }
178
Destroy()179 void AnFileInfo::Destroy()
180 {
181 mainEntryMap_.clear();
182 isLoad_ = false;
183 curTextSecOffset_ = 0;
184 AOTFileInfo::Destroy();
185 }
186
Dump() const187 void AnFileInfo::Dump() const
188 {
189 LOG_COMPILER(INFO) << "An file loading: ";
190 int i = 0;
191 for (const ModuleSectionDes &d : des_) {
192 i++;
193 for (const auto &s : d.GetSectionsInfo()) {
194 std::string name = d.GetSecName(s.first);
195 uint32_t size = d.GetSecSize(s.first);
196 uint64_t addr = d.GetSecAddr(s.first);
197 LOG_COMPILER(INFO) << " - module-" << i << " <" << name << "> [0x" << std::hex << addr << ", 0x"
198 << std::hex << addr + size << "]";
199 }
200 }
201 }
202
IsLoadMain(uint32_t fileIndex,const JSPandaFile * jsPandaFile,const CString & entry) const203 bool AnFileInfo::IsLoadMain(uint32_t fileIndex, const JSPandaFile *jsPandaFile, const CString &entry) const
204 {
205 auto methodId = jsPandaFile->GetMainMethodIndex(entry);
206 #ifndef NDEBUG
207 LOG_COMPILER(INFO) << "AnFileInfo IsLoadMain method id: " << methodId << " entry: " << entry;
208 #endif
209 auto it = mainEntryMap_.find(std::make_pair(fileIndex, methodId));
210 return it != mainEntryMap_.end();
211 }
212
AddFuncEntrySec()213 void AnFileInfo::AddFuncEntrySec()
214 {
215 ModuleSectionDes &des = des_[ElfBuilder::FuncEntryModuleDesIndex];
216 // add section
217 uint64_t funcEntryAddr = reinterpret_cast<uint64_t>(entries_.data());
218 uint32_t funcEntrySize = sizeof(FuncEntryDes) * entryNum_;
219 des.SetSecAddrAndSize(ElfSecName::ARK_FUNCENTRY, funcEntryAddr, funcEntrySize);
220 }
221
GenerateMethodToEntryIndexMap()222 void AnFileInfo::GenerateMethodToEntryIndexMap()
223 {
224 const std::vector<AOTFileInfo::FuncEntryDes> &entries = GetStubs();
225 uint32_t entriesSize = entries.size();
226 for (uint32_t i = 0; i < entriesSize; ++i) {
227 const AOTFileInfo::FuncEntryDes &entry = entries[i];
228 std::string &fileName = entryIdxToFileNameMap_.at(i);
229 auto key = std::make_pair(fileName, entry.indexInKindOrMethodId_);
230 methodToEntryIndexMap_[key] = i;
231 }
232 }
233 } // namespace panda::ecmascript
234