1 /* 2 * Copyright (c) 2021-2022 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_BINARY_SECTION_H 17 #define ECMASCRIPT_COMPILER_BINARY_SECTION_H 18 19 #include <map> 20 #include <cstring> 21 22 #include "ecmascript/common.h" 23 24 namespace panda::ecmascript { 25 enum class ElfSecName : uint8_t { 26 NONE, 27 RODATA, 28 RODATA_CST4, 29 RODATA_CST8, 30 RODATA_CST16, 31 RODATA_CST32, 32 TEXT, 33 DATA, 34 GOT, 35 RELATEXT, 36 STRTAB, 37 SYMTAB, 38 LLVM_STACKMAP, 39 SIZE 40 }; 41 42 enum ElfSecFeature : uint8_t { 43 NOT_VALID, 44 VALID_NOT_SEQUENTIAL = 1, 45 VALID_AND_SEQUENTIAL = (1 << 1) | 1, 46 47 VALID_MASK = 0x1, 48 SEQUENTIAL_MASK = 0x2 49 }; 50 51 class PUBLIC_API ElfSection { 52 public: 53 ElfSection() = delete; 54 ElfSection(ElfSecName idx)55 explicit ElfSection(ElfSecName idx) 56 { 57 value_ = idx; 58 } ElfSection(size_t idx)59 explicit ElfSection(size_t idx) 60 { 61 value_ = static_cast<ElfSecName>(idx); 62 } ElfSection(std::string str)63 explicit ElfSection(std::string str) 64 { 65 if (str.compare(".rodata") == 0) { 66 value_ = ElfSecName::RODATA; 67 } else if (str.compare(".rodata.cst4") == 0) { 68 value_ = ElfSecName::RODATA_CST4; 69 } else if (str.compare(".rodata.cst8") == 0) { 70 value_ = ElfSecName::RODATA_CST8; 71 } else if (str.compare(".rodata.cst16") == 0) { 72 value_ = ElfSecName::RODATA_CST16; 73 } else if (str.compare(".rodata.cst32") == 0) { 74 value_ = ElfSecName::RODATA_CST32; 75 } else if (str.compare(".text") == 0) { 76 value_ = ElfSecName::TEXT; 77 } else if (str.compare(".data") == 0) { 78 value_ = ElfSecName::DATA; 79 } else if (str.compare(".got") == 0) { 80 value_ = ElfSecName::GOT; 81 } else if (str.compare(".rela.text") == 0) { 82 value_ = ElfSecName::RELATEXT; 83 } else if (str.compare(".strtab") == 0) { 84 value_ = ElfSecName::STRTAB; 85 } else if (str.compare(".symtab") == 0) { 86 value_ = ElfSecName::SYMTAB; 87 } else if (str.compare(".llvm_stackmaps") == 0) { 88 value_ = ElfSecName::LLVM_STACKMAP; 89 } 90 } 91 isValidAOTSec()92 bool isValidAOTSec() const 93 { 94 auto idx = static_cast<size_t>(value_); 95 return static_cast<uint8_t>(AOTSecFeatureTable_[idx]) & ElfSecFeature::VALID_MASK; 96 } 97 isSequentialAOTSec()98 bool isSequentialAOTSec() const 99 { 100 auto idx = static_cast<size_t>(value_); 101 return static_cast<uint8_t>(AOTSecFeatureTable_[idx]) & ElfSecFeature::SEQUENTIAL_MASK; 102 } 103 GetElfEnumValue()104 ElfSecName GetElfEnumValue() const 105 { 106 return value_; 107 } 108 GetIntIndex()109 int GetIntIndex() const 110 { 111 return static_cast<int>(value_); 112 } 113 114 // RO data section needs 16 bytes alignment InRodataSection()115 bool InRodataSection() const 116 { 117 return ElfSecName::RODATA <= value_ && value_ <= ElfSecName::RODATA_CST8; 118 } 119 private: 120 ElfSecName value_ {ElfSecName::NONE}; 121 static constexpr size_t AOTSecFeatureTable_[static_cast<size_t>(ElfSecName::SIZE)] = { 122 ElfSecFeature::NOT_VALID, 123 ElfSecFeature::VALID_AND_SEQUENTIAL, 124 ElfSecFeature::VALID_AND_SEQUENTIAL, 125 ElfSecFeature::VALID_AND_SEQUENTIAL, 126 ElfSecFeature::VALID_AND_SEQUENTIAL, 127 ElfSecFeature::VALID_AND_SEQUENTIAL, 128 ElfSecFeature::VALID_AND_SEQUENTIAL, 129 ElfSecFeature::VALID_AND_SEQUENTIAL, 130 ElfSecFeature::VALID_AND_SEQUENTIAL, 131 ElfSecFeature::VALID_AND_SEQUENTIAL, 132 ElfSecFeature::VALID_AND_SEQUENTIAL, 133 ElfSecFeature::VALID_AND_SEQUENTIAL, 134 ElfSecFeature::VALID_NOT_SEQUENTIAL, 135 }; 136 }; 137 } 138 #endif