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