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