• 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/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     fileMapMem_ = FileMap(realPath.c_str(), FILE_RDONLY, PAGE_PROT_READ);
70     if (fileMapMem_.GetOriginAddr() == nullptr) {
71         LOG_ECMA(ERROR) << "File mmap failed";
72         return false;
73     }
74     PagePreRead(fileMapMem_.GetOriginAddr(), fileMapMem_.GetSize());
75 
76     ElfReader reader(fileMapMem_);
77     std::vector<ElfSecName> secs = GetDumpSectionNames();
78     reader.ParseELFSections(des_, secs);
79     moduleNum_ = des_.size();
80     ASSERT(moduleNum_ == ASMSTUB_MODULE_NUM);
81 
82     if (!reader.ParseELFSegment()) {
83         LOG_ECMA(ERROR) << "modify mmap area permission failed";
84         return false;
85     }
86 
87     ModuleSectionDes &des = des_[0];
88     uint64_t funcEntryAddr = des.GetSecAddr(ElfSecName::ARK_FUNCENTRY);
89     uint32_t funcEntrySize = des.GetSecSize(ElfSecName::ARK_FUNCENTRY);
90     FuncEntryDes *entryDes = reinterpret_cast<FuncEntryDes *>(funcEntryAddr);
91     entryNum_ = funcEntrySize / sizeof(FuncEntryDes);
92     entries_.assign(entryDes, entryDes + entryNum_);
93     uint64_t asmStubAddr = des.GetSecAddr(ElfSecName::ARK_ASMSTUB);
94     uint32_t asmStubSize = des.GetSecSize(ElfSecName::ARK_ASMSTUB);
95     SetAsmStubAddr(asmStubAddr);
96     SetAsmStubSize(asmStubSize);
97 
98     for (auto &entry : entries_) {
99         if (entry.IsGeneralRTStub()) {
100             uint64_t begin = GetAsmStubAddr();
101             entry.codeAddr_ += begin;
102         } else {
103             auto moduleDes = des_[entry.moduleIndex_];
104             entry.codeAddr_ += moduleDes.GetSecAddr(ElfSecName::TEXT);
105         }
106     }
107     LOG_COMPILER(INFO) << "loaded stub file successfully";
108     return true;
109 }
110 
Load()111 bool StubFileInfo::Load()
112 {
113     if (_binary_stub_an_length <= 1) {
114         LOG_FULL(FATAL) << "stub.an length <= 1, is default and invalid.";
115         return false;
116     }
117 
118     BinaryBufferParser binBufparser(const_cast<uint8_t *>(_binary_stub_an_start), _binary_stub_an_length);
119     moduleNum_ = ASMSTUB_MODULE_NUM;
120     des_.resize(moduleNum_);
121 
122     ExecutedMemoryAllocator::AllocateBuf(_binary_stub_an_length, stubsMem_);
123 
124     ElfReader reader(stubsMem_);
125     std::vector<ElfSecName> secs = GetDumpSectionNames();
126     reader.ParseELFSections(binBufparser, des_, secs);
127 
128     ModuleSectionDes &des = des_[0];
129     uint64_t funcEntryAddr = des.GetSecAddr(ElfSecName::ARK_FUNCENTRY);
130     uint32_t funcEntrySize = des.GetSecSize(ElfSecName::ARK_FUNCENTRY);
131     FuncEntryDes *entryDes = reinterpret_cast<FuncEntryDes *>(funcEntryAddr);
132     entryNum_ = funcEntrySize / sizeof(FuncEntryDes);
133     entries_.assign(entryDes, entryDes + entryNum_);
134     uint64_t asmStubAddr = des.GetSecAddr(ElfSecName::ARK_ASMSTUB);
135     uint32_t asmStubSize = des.GetSecSize(ElfSecName::ARK_ASMSTUB);
136     SetAsmStubAddr(asmStubAddr);
137     SetAsmStubSize(asmStubSize);
138 
139     for (auto &entry : entries_) {
140         if (entry.IsGeneralRTStub()) {
141             uint64_t begin = GetAsmStubAddr();
142             entry.codeAddr_ += begin;
143         } else {
144             auto moduleDes = des_[entry.moduleIndex_];
145             entry.codeAddr_ += moduleDes.GetSecAddr(ElfSecName::TEXT);
146         }
147     }
148     LOG_COMPILER(INFO) << "loaded stub file successfully";
149     PageProtect(stubsMem_.addr_, stubsMem_.size_, PAGE_PROT_EXEC_READ);
150     return true;
151 }
152 
GetDumpSectionNames()153 const std::vector<ElfSecName> &StubFileInfo::GetDumpSectionNames()
154 {
155     static const std::vector<ElfSecName> secNames = {
156         ElfSecName::TEXT,
157         ElfSecName::SHSTRTAB,
158         ElfSecName::ARK_STACKMAP,
159         ElfSecName::ARK_FUNCENTRY,
160         ElfSecName::ARK_ASMSTUB,
161         ElfSecName::ARK_MODULEINFO
162     };
163     return secNames;
164 }
165 
Dump() const166 void StubFileInfo::Dump() const
167 {
168     uint64_t asmAddr = GetAsmStubAddr();
169     uint64_t asmSize = GetAsmStubSize();
170 
171     LOG_COMPILER(ERROR) << "Stub file loading: ";
172     LOG_COMPILER(ERROR) << " - asm stubs [0x" << std::hex << asmAddr << ", 0x" << std::hex << asmAddr + asmSize << "]";
173 
174     for (const ModuleSectionDes &d : des_) {
175         for (const auto &s : d.GetSectionsInfo()) {
176             std::string name = d.GetSecName(s.first);
177             uint32_t size = d.GetSecSize(s.first);
178             uint64_t addr = d.GetSecAddr(s.first);
179             LOG_COMPILER(ERROR) << " - section = " << name << " [0x" << std::hex << addr << ", 0x" << std::hex
180                                 << addr + size << "]";
181         }
182     }
183 }
184 }  // namespace panda::ecmascript
185