• 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 #ifndef ECMASCRIPT_COMPILER_AOT_FILE_ELF_READER_H
17 #define ECMASCRIPT_COMPILER_AOT_FILE_ELF_READER_H
18 
19 #include <utility>
20 #include <stdint.h>
21 #include <string>
22 #include "ecmascript/compiler/aot_file/aot_file_manager.h"
23 #include "ecmascript/compiler/binary_section.h"
24 namespace panda::ecmascript {
25 
26 class ModuleSectionDes;
27 
28 class ElfReader {
29 public:
ElfReader(const MemMap & fileMapMem)30     ElfReader(const MemMap &fileMapMem) : fileMapMem_(fileMapMem) {};
ElfReader(ExecutedMemoryAllocator::ExeMem & stubsMem)31     ElfReader(ExecutedMemoryAllocator::ExeMem &stubsMem) : stubsMem_(stubsMem) {};
32     ~ElfReader() = default;
33     bool VerifyELFHeader(uint32_t version, bool strictMatch);
34     void ParseELFSections(ModuleSectionDes &des, std::vector<ElfSecName> &secs);
35     void ParseELFSections(std::vector<ModuleSectionDes> &des, std::vector<ElfSecName> &secs);
36     void ParseELFSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
37         std::vector<ElfSecName> &secs);
38     bool ParseELFSegment();
39     ModuleSectionDes::ModuleRegionInfo *GetCurModuleInfo(uint32_t i, llvm::ELF::Elf64_Off offset);
40     void SeparateTextSections(std::vector<ModuleSectionDes> &des, const uintptr_t &secAddr,
41         llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &moduleInfoOffset);
42     void SeparateArkStackMapSections(std::vector<ModuleSectionDes> &des, const uintptr_t &secAddr,
43         llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &moduleInfoOffset);
44     void SeparateTextSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
45         const uint64_t &secAddr, llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &curShOffset);
46     void SeparateArkStackMapSections(BinaryBufferParser &parser, std::vector<ModuleSectionDes> &des,
47         const uint64_t &secAddr, llvm::ELF::Elf64_Off &secOffset, const llvm::ELF::Elf64_Off &curShOffset);
48 
49 private:
GetModuleNum(size_t moduleInfoSize)50     static uint32_t GetModuleNum(size_t moduleInfoSize)
51     {
52         ASSERT(moduleInfoSize % sizeof(ModuleSectionDes::ModuleRegionInfo) == 0);
53         return moduleInfoSize / sizeof(ModuleSectionDes::ModuleRegionInfo);
54     }
55 
56     static constexpr uint32_t ASMSTUB_MODULE_NUM = 3;
57     ExecutedMemoryAllocator::ExeMem stubsMem_ {};
58     MemMap fileMapMem_ {};
59     std::vector<ModuleSectionDes::ModuleRegionInfo> moduleInfo_;
60 };
61 }  // namespace panda::ecmascript
62 #endif  // ECMASCRIPT_COMPILER_AOT_FILE_ELF_READER_H
63