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