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/stub_file_info.h"
17 #include "ecmascript/compiler/aot_file/elf_builder.h"
18 #include "ecmascript/compiler/aot_file/elf_reader.h"
19
20 #ifndef PANDA_TARGET_OHOS
21 extern const uint8_t _binary_stub_an_start[];
22 extern const uint32_t _binary_stub_an_length;
23 #endif
24
25 namespace panda::ecmascript {
Save(const std::string & filename,Triple triple)26 void StubFileInfo::Save(const std::string &filename, Triple triple)
27 {
28 std::string realPath;
29 if (!RealPath(filename, realPath, false)) {
30 return;
31 }
32 std::fstream file(realPath.c_str(),
33 std::fstream::binary | std::fstream::in | std::fstream::out | std::fstream::trunc);
34 if (!file.is_open()) {
35 LOG_ECMA(FATAL) << "Failed to open file : " << realPath.c_str();
36 UNREACHABLE();
37 }
38 ASSERT(GetCodeUnitsNum() == ASMSTUB_MODULE_NUM);
39 SetStubNum(entries_.size());
40 ModuleSectionDes &des = des_[0];
41 size_t lastModuleSectionIdx = ASMSTUB_MODULE_NUM - 1;
42 // add section
43 uint64_t funcEntryAddr = reinterpret_cast<uint64_t>(entries_.data());
44 uint32_t funcEntrySize = sizeof(FuncEntryDes) * entryNum_;
45 des.SetSecAddrAndSize(ElfSecName::ARK_FUNCENTRY, funcEntryAddr, funcEntrySize);
46 uint64_t asmStubAddr = GetAsmStubAddr();
47 uint32_t asmStubSize = GetAsmStubSize();
48 des.SetSecAddrAndSize(ElfSecName::ARK_ASMSTUB, asmStubAddr, asmStubSize);
49 std::vector<uint32_t> moduleInfo = {1};
50 uint64_t secSizeInfoAddr = reinterpret_cast<uint64_t>(moduleInfo.data());
51 des.SetSecAddrAndSize(ElfSecName::ARK_MODULEINFO, secSizeInfoAddr, sizeof(uint32_t));
52 des_[lastModuleSectionIdx].AddAsmStubELFInfo(asmStubELFInfo_);
53 #if ENABLE_NEXT_OPTIMIZATION
54 ElfBuilder builder(des_, GetDumpSectionNames(), true, triple);
55 #else
56 ElfBuilder builder(des_, GetDumpSectionNames(), false, triple);
57 #endif
58 llvm::ELF::Elf64_Ehdr header;
59 builder.PackELFHeader(header, base::FileHeaderBase::ToVersionNumber(AOTFileVersion::AN_VERSION), triple);
60 file.write(reinterpret_cast<char *>(&header), sizeof(llvm::ELF::Elf64_Ehdr));
61 builder.PackELFSections(file);
62 builder.PackELFSegment(file);
63 file.close();
64 }
65
MmapLoad(const std::string & fileName)66 bool StubFileInfo::MmapLoad(const std::string &fileName)
67 {
68 std::string filename = fileName.empty() ? STUB_AN_FILE : fileName;
69 std::string realPath;
70 if (!RealPath(filename, realPath, false)) {
71 LOG_COMPILER(ERROR) << "Can not load stub file from path [ " << filename << " ], "
72 << "please execute ark_stub_compiler with options --stub-file.";
73 return false;
74 }
75
76 if (!FileExist(realPath.c_str())) {
77 LOG_ECMA(WARN) << "File not exist. file: " << realPath;
78 return false;
79 }
80
81 fileMapMem_ = FileMap(realPath.c_str(), FILE_RDONLY, PAGE_PROT_READ);
82 if (fileMapMem_.GetOriginAddr() == nullptr) {
83 LOG_ECMA(ERROR) << "File mmap failed";
84 return false;
85 }
86
87 ElfReader reader(fileMapMem_);
88 std::vector<ElfSecName> secs = GetDumpSectionNames();
89 reader.ParseELFSections(des_, secs);
90 moduleNum_ = des_.size();
91 ASSERT(moduleNum_ == ASMSTUB_MODULE_NUM);
92
93 if (!reader.ParseELFSegment()) {
94 LOG_ECMA(ERROR) << "modify mmap area permission failed";
95 return false;
96 }
97
98 ModuleSectionDes &des = des_[0];
99 uint64_t funcEntryAddr = des.GetSecAddr(ElfSecName::ARK_FUNCENTRY);
100 uint32_t funcEntrySize = des.GetSecSize(ElfSecName::ARK_FUNCENTRY);
101 FuncEntryDes *entryDes = reinterpret_cast<FuncEntryDes *>(funcEntryAddr);
102 entryNum_ = funcEntrySize / sizeof(FuncEntryDes);
103 entries_.assign(entryDes, entryDes + entryNum_);
104 uint64_t asmStubAddr = des.GetSecAddr(ElfSecName::ARK_ASMSTUB);
105 uint32_t asmStubSize = des.GetSecSize(ElfSecName::ARK_ASMSTUB);
106 SetAsmStubAddr(asmStubAddr);
107 SetAsmStubSize(asmStubSize);
108
109 for (auto &entry : entries_) {
110 if (entry.IsGeneralRTStub()) {
111 uint64_t begin = GetAsmStubAddr();
112 entry.codeAddr_ += begin;
113 } else {
114 auto moduleDes = des_[entry.moduleIndex_];
115 entry.codeAddr_ += moduleDes.GetSecAddr(ElfSecName::TEXT);
116 }
117 }
118 LOG_COMPILER(INFO) << "loaded stub file successfully";
119 return true;
120 }
121 #ifndef PANDA_TARGET_OHOS
Load()122 bool StubFileInfo::Load()
123 {
124 if (_binary_stub_an_length <= 1) {
125 LOG_FULL(FATAL) << "stub.an length <= 1, is default and invalid.";
126 return false;
127 }
128
129 BinaryBufferParser binBufparser(const_cast<uint8_t *>(_binary_stub_an_start), _binary_stub_an_length);
130 moduleNum_ = ASMSTUB_MODULE_NUM;
131 des_.resize(moduleNum_);
132
133 ExecutedMemoryAllocator::AllocateBuf(_binary_stub_an_length, stubsMem_, PAGE_PROT_READWRITE);
134
135 ElfReader reader(stubsMem_);
136 std::vector<ElfSecName> secs = GetDumpSectionNames();
137 reader.ParseELFSections(binBufparser, des_, secs);
138
139 ModuleSectionDes &des = des_[0];
140 uint64_t funcEntryAddr = des.GetSecAddr(ElfSecName::ARK_FUNCENTRY);
141 uint32_t funcEntrySize = des.GetSecSize(ElfSecName::ARK_FUNCENTRY);
142 FuncEntryDes *entryDes = reinterpret_cast<FuncEntryDes *>(funcEntryAddr);
143 entryNum_ = funcEntrySize / sizeof(FuncEntryDes);
144 entries_.assign(entryDes, entryDes + entryNum_);
145 uint64_t asmStubAddr = des.GetSecAddr(ElfSecName::ARK_ASMSTUB);
146 uint32_t asmStubSize = des.GetSecSize(ElfSecName::ARK_ASMSTUB);
147 SetAsmStubAddr(asmStubAddr);
148 SetAsmStubSize(asmStubSize);
149
150 for (auto &entry : entries_) {
151 if (entry.IsGeneralRTStub()) {
152 uint64_t begin = GetAsmStubAddr();
153 entry.codeAddr_ += begin;
154 } else {
155 auto moduleDes = des_[entry.moduleIndex_];
156 entry.codeAddr_ += moduleDes.GetSecAddr(ElfSecName::TEXT);
157 }
158 }
159 LOG_COMPILER(INFO) << "loaded stub file successfully";
160 if (!PageProtect(stubsMem_.addr_, stubsMem_.size_, PAGE_PROT_EXEC_READ)) {
161 return false;
162 }
163 return true;
164 }
165 #endif
GetDumpSectionNames()166 const std::vector<ElfSecName> &StubFileInfo::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 ElfSecName::ARK_ASMSTUB,
176 ElfSecName::ARK_MODULEINFO
177 };
178 return secNames;
179 }
180
Dump() const181 void StubFileInfo::Dump() const
182 {
183 uint64_t asmAddr = GetAsmStubAddr();
184 uint64_t asmSize = GetAsmStubSize();
185
186 LOG_COMPILER(ERROR) << "Stub file loading: ";
187 LOG_COMPILER(ERROR) << " - asm stubs [0x" << std::hex << asmAddr << ", 0x" << std::hex << asmAddr + asmSize << "]";
188
189 for (const ModuleSectionDes &d : des_) {
190 for (const auto &s : d.GetSectionsInfo()) {
191 std::string name = d.GetSecName(s.first);
192 uint32_t size = d.GetSecSize(s.first);
193 uint64_t addr = d.GetSecAddr(s.first);
194 LOG_COMPILER(ERROR) << " - section = " << name << " [0x" << std::hex << addr << ", 0x" << std::hex
195 << addr + size << "]";
196 }
197 }
198 }
199 } // namespace panda::ecmascript
200