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