14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_BINARY_SECTION_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_BINARY_SECTION_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <string>
204514f5e3Sopenharmony_ci#include <map>
214514f5e3Sopenharmony_ci#include "ecmascript/common.h"
224514f5e3Sopenharmony_ci#include "llvm/BinaryFormat/ELF.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::ecmascript {
254514f5e3Sopenharmony_cienum class ElfSecName : uint8_t {
264514f5e3Sopenharmony_ci    NONE,
274514f5e3Sopenharmony_ci    RODATA,
284514f5e3Sopenharmony_ci    RODATA_CST4,
294514f5e3Sopenharmony_ci    RODATA_CST8,
304514f5e3Sopenharmony_ci    RODATA_CST16,
314514f5e3Sopenharmony_ci    RODATA_CST32,
324514f5e3Sopenharmony_ci    TEXT,
334514f5e3Sopenharmony_ci    ARK_ASMSTUB,
344514f5e3Sopenharmony_ci    DATA,
354514f5e3Sopenharmony_ci    GOT,
364514f5e3Sopenharmony_ci    RELATEXT,
374514f5e3Sopenharmony_ci    STRTAB,
384514f5e3Sopenharmony_ci    SYMTAB,
394514f5e3Sopenharmony_ci    SHSTRTAB,
404514f5e3Sopenharmony_ci    LLVM_STACKMAP,
414514f5e3Sopenharmony_ci    ARK_FUNCENTRY,
424514f5e3Sopenharmony_ci    ARK_STACKMAP,
434514f5e3Sopenharmony_ci    ARK_MODULEINFO,
444514f5e3Sopenharmony_ci    SIZE
454514f5e3Sopenharmony_ci};
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_cienum ElfSecFeature : uint8_t {
484514f5e3Sopenharmony_ci    NOT_VALID,
494514f5e3Sopenharmony_ci    VALID_NOT_SEQUENTIAL = 1,
504514f5e3Sopenharmony_ci    VALID_AND_SEQUENTIAL = (1 << 1) | 1,
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    VALID_MASK = 0x1,
534514f5e3Sopenharmony_ci    SEQUENTIAL_MASK = 0x2
544514f5e3Sopenharmony_ci};
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ciclass PUBLIC_API ElfSection {
574514f5e3Sopenharmony_cipublic:
584514f5e3Sopenharmony_ci    ElfSection() = delete;
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    explicit ElfSection(ElfSecName idx)
614514f5e3Sopenharmony_ci    {
624514f5e3Sopenharmony_ci        value_ = idx;
634514f5e3Sopenharmony_ci        InitShTypeAndFlag();
644514f5e3Sopenharmony_ci    }
654514f5e3Sopenharmony_ci    explicit ElfSection(size_t idx)
664514f5e3Sopenharmony_ci    {
674514f5e3Sopenharmony_ci        value_ = static_cast<ElfSecName>(idx);
684514f5e3Sopenharmony_ci        InitShTypeAndFlag();
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci    explicit ElfSection(std::string str)
714514f5e3Sopenharmony_ci    {
724514f5e3Sopenharmony_ci        if (str.compare(".rodata") == 0) {
734514f5e3Sopenharmony_ci            value_ = ElfSecName::RODATA;
744514f5e3Sopenharmony_ci        } else if (str.compare(".rodata.cst4") == 0) {
754514f5e3Sopenharmony_ci            value_ = ElfSecName::RODATA_CST4;
764514f5e3Sopenharmony_ci        } else if (str.compare(".rodata.cst8") == 0) {
774514f5e3Sopenharmony_ci            value_ = ElfSecName::RODATA_CST8;
784514f5e3Sopenharmony_ci        } else if (str.compare(".rodata.cst16") == 0) {
794514f5e3Sopenharmony_ci            value_ = ElfSecName::RODATA_CST16;
804514f5e3Sopenharmony_ci        } else if (str.compare(".rodata.cst32") == 0) {
814514f5e3Sopenharmony_ci            value_ = ElfSecName::RODATA_CST32;
824514f5e3Sopenharmony_ci        } else if (str.compare(".text") == 0) {
834514f5e3Sopenharmony_ci            value_ = ElfSecName::TEXT;
844514f5e3Sopenharmony_ci        } else if (str.compare(".data") == 0) {
854514f5e3Sopenharmony_ci            value_ = ElfSecName::DATA;
864514f5e3Sopenharmony_ci        } else if (str.compare(".got") == 0) {
874514f5e3Sopenharmony_ci            value_ = ElfSecName::GOT;
884514f5e3Sopenharmony_ci        } else if (str.compare(".rela.text") == 0) {
894514f5e3Sopenharmony_ci            value_ = ElfSecName::RELATEXT;
904514f5e3Sopenharmony_ci        } else if (str.compare(".strtab") == 0) {
914514f5e3Sopenharmony_ci            value_ = ElfSecName::STRTAB;
924514f5e3Sopenharmony_ci        } else if (str.compare(".symtab") == 0) {
934514f5e3Sopenharmony_ci            value_ = ElfSecName::SYMTAB;
944514f5e3Sopenharmony_ci        } else if (str.compare(".shstrtab") == 0) {
954514f5e3Sopenharmony_ci            value_ = ElfSecName::SHSTRTAB;
964514f5e3Sopenharmony_ci        }  else if (str.compare(".llvm_stackmaps") == 0) {
974514f5e3Sopenharmony_ci            value_ = ElfSecName::LLVM_STACKMAP;
984514f5e3Sopenharmony_ci        } else if (str.compare(".ark_stackmaps") == 0) {
994514f5e3Sopenharmony_ci            value_ = ElfSecName::ARK_STACKMAP;
1004514f5e3Sopenharmony_ci        } else if (str.compare(".ark_funcentry") == 0) {
1014514f5e3Sopenharmony_ci            value_ = ElfSecName::ARK_FUNCENTRY;
1024514f5e3Sopenharmony_ci        } else if (str.compare(".ark_asmstub") == 0) {
1034514f5e3Sopenharmony_ci            value_ = ElfSecName::ARK_ASMSTUB;
1044514f5e3Sopenharmony_ci        } else if (str.compare(".ark_moduleinfo") == 0) {
1054514f5e3Sopenharmony_ci            value_ = ElfSecName::ARK_MODULEINFO;
1064514f5e3Sopenharmony_ci        }
1074514f5e3Sopenharmony_ci        InitShTypeAndFlag();
1084514f5e3Sopenharmony_ci    }
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    bool ShouldDumpToAOTFile() const
1114514f5e3Sopenharmony_ci    {
1124514f5e3Sopenharmony_ci        bool saveForAot = false;
1134514f5e3Sopenharmony_ci        switch (value_) {
1144514f5e3Sopenharmony_ci            case ElfSecName::TEXT:
1154514f5e3Sopenharmony_ci            case ElfSecName::STRTAB:
1164514f5e3Sopenharmony_ci            case ElfSecName::SYMTAB:
1174514f5e3Sopenharmony_ci            case ElfSecName::SHSTRTAB:
1184514f5e3Sopenharmony_ci            case ElfSecName::ARK_FUNCENTRY:
1194514f5e3Sopenharmony_ci            case ElfSecName::ARK_ASMSTUB:
1204514f5e3Sopenharmony_ci            case ElfSecName::ARK_STACKMAP:
1214514f5e3Sopenharmony_ci            case ElfSecName::ARK_MODULEINFO: {
1224514f5e3Sopenharmony_ci                saveForAot = true;
1234514f5e3Sopenharmony_ci                break;
1244514f5e3Sopenharmony_ci            }
1254514f5e3Sopenharmony_ci            default: {
1264514f5e3Sopenharmony_ci                break;
1274514f5e3Sopenharmony_ci            }
1284514f5e3Sopenharmony_ci        }
1294514f5e3Sopenharmony_ci        return saveForAot;
1304514f5e3Sopenharmony_ci    }
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ci    ElfSecName Value() const
1334514f5e3Sopenharmony_ci    {
1344514f5e3Sopenharmony_ci        return value_;
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    int Entsize() const
1384514f5e3Sopenharmony_ci    {
1394514f5e3Sopenharmony_ci        if (value_ == ElfSecName::RELATEXT || value_ == ElfSecName::SYMTAB) {
1404514f5e3Sopenharmony_ci            return FIX_SIZE;
1414514f5e3Sopenharmony_ci        }
1424514f5e3Sopenharmony_ci        return 0;
1434514f5e3Sopenharmony_ci    }
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ci    int Link() const
1464514f5e3Sopenharmony_ci    {
1474514f5e3Sopenharmony_ci        // The strtab index is 2 inside An file.
1484514f5e3Sopenharmony_ci        return value_ == ElfSecName::SYMTAB ? 2 : 0;
1494514f5e3Sopenharmony_ci    }
1504514f5e3Sopenharmony_ci
1514514f5e3Sopenharmony_ci    void InitShTypeAndFlag()
1524514f5e3Sopenharmony_ci    {
1534514f5e3Sopenharmony_ci        std::map<ElfSecName, std::pair<unsigned, unsigned>> nameToTypeAndFlag = {
1544514f5e3Sopenharmony_ci            {ElfSecName::RODATA, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE}},
1554514f5e3Sopenharmony_ci            {ElfSecName::RODATA_CST4, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE}},
1564514f5e3Sopenharmony_ci            {ElfSecName::RODATA_CST8, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE}},
1574514f5e3Sopenharmony_ci            {ElfSecName::RODATA_CST16, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE}},
1584514f5e3Sopenharmony_ci            {ElfSecName::RODATA_CST32, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE}},
1594514f5e3Sopenharmony_ci            {ElfSecName::TEXT, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR}},
1604514f5e3Sopenharmony_ci            {ElfSecName::ARK_ASMSTUB, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR}},
1614514f5e3Sopenharmony_ci            {ElfSecName::DATA, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE}},
1624514f5e3Sopenharmony_ci            {ElfSecName::GOT, {llvm::ELF::SHT_PROGBITS, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE}},
1634514f5e3Sopenharmony_ci            {ElfSecName::RELATEXT, {llvm::ELF::SHT_RELA, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE}},
1644514f5e3Sopenharmony_ci            {ElfSecName::STRTAB, {llvm::ELF::SHT_STRTAB, llvm::ELF::SHF_ALLOC}},
1654514f5e3Sopenharmony_ci            {ElfSecName::SYMTAB, {llvm::ELF::SHT_SYMTAB, llvm::ELF::SHF_ALLOC}},
1664514f5e3Sopenharmony_ci            {ElfSecName::SHSTRTAB, {llvm::ELF::SHT_STRTAB, llvm::ELF::SHF_ALLOC}},
1674514f5e3Sopenharmony_ci            {ElfSecName::LLVM_STACKMAP, {llvm::ELF::SHT_RELA, llvm::ELF::SHF_ALLOC}},
1684514f5e3Sopenharmony_ci            {ElfSecName::ARK_FUNCENTRY, {llvm::ELF::SHF_WRITE, llvm::ELF::SHF_ALLOC}},
1694514f5e3Sopenharmony_ci            {ElfSecName::ARK_STACKMAP, {llvm::ELF::SHF_WRITE, llvm::ELF::SHF_ALLOC}},
1704514f5e3Sopenharmony_ci            {ElfSecName::ARK_MODULEINFO, {llvm::ELF::SHF_WRITE, llvm::ELF::SHF_ALLOC}},
1714514f5e3Sopenharmony_ci        };
1724514f5e3Sopenharmony_ci        auto it = nameToTypeAndFlag.find(value_);
1734514f5e3Sopenharmony_ci        if (it == nameToTypeAndFlag.end()) {
1744514f5e3Sopenharmony_ci            return;
1754514f5e3Sopenharmony_ci        }
1764514f5e3Sopenharmony_ci        ASSERT(it != nameToTypeAndFlag.end());
1774514f5e3Sopenharmony_ci        type_ = it->second.first;
1784514f5e3Sopenharmony_ci        flag_ = it->second.second;
1794514f5e3Sopenharmony_ci    }
1804514f5e3Sopenharmony_ci
1814514f5e3Sopenharmony_ci    unsigned Type() const
1824514f5e3Sopenharmony_ci    {
1834514f5e3Sopenharmony_ci        return type_;
1844514f5e3Sopenharmony_ci    }
1854514f5e3Sopenharmony_ci
1864514f5e3Sopenharmony_ci    unsigned Flag() const
1874514f5e3Sopenharmony_ci    {
1884514f5e3Sopenharmony_ci        return flag_;
1894514f5e3Sopenharmony_ci    }
1904514f5e3Sopenharmony_ci
1914514f5e3Sopenharmony_ci    bool isValidAOTSec() const
1924514f5e3Sopenharmony_ci    {
1934514f5e3Sopenharmony_ci        auto idx = static_cast<size_t>(value_);
1944514f5e3Sopenharmony_ci        return static_cast<uint8_t>(AOTSecFeatureTable_[idx]) & ElfSecFeature::VALID_MASK;
1954514f5e3Sopenharmony_ci    }
1964514f5e3Sopenharmony_ci
1974514f5e3Sopenharmony_ci    bool isSequentialAOTSec() const
1984514f5e3Sopenharmony_ci    {
1994514f5e3Sopenharmony_ci        auto idx = static_cast<size_t>(value_);
2004514f5e3Sopenharmony_ci        return static_cast<uint8_t>(AOTSecFeatureTable_[idx]) & ElfSecFeature::SEQUENTIAL_MASK;
2014514f5e3Sopenharmony_ci    }
2024514f5e3Sopenharmony_ci
2034514f5e3Sopenharmony_ci    ElfSecName GetElfEnumValue() const
2044514f5e3Sopenharmony_ci    {
2054514f5e3Sopenharmony_ci        return value_;
2064514f5e3Sopenharmony_ci    }
2074514f5e3Sopenharmony_ci
2084514f5e3Sopenharmony_ci    int GetIntIndex() const
2094514f5e3Sopenharmony_ci    {
2104514f5e3Sopenharmony_ci        return static_cast<int>(value_);
2114514f5e3Sopenharmony_ci    }
2124514f5e3Sopenharmony_ci
2134514f5e3Sopenharmony_ci    // RO data section needs 16 bytes alignment
2144514f5e3Sopenharmony_ci    bool InRodataSection() const
2154514f5e3Sopenharmony_ci    {
2164514f5e3Sopenharmony_ci        return ElfSecName::RODATA <= value_ && value_ <= ElfSecName::RODATA_CST32;
2174514f5e3Sopenharmony_ci    }
2184514f5e3Sopenharmony_ciprivate:
2194514f5e3Sopenharmony_ci    static int const FIX_SIZE = 24; // 24:Elf_Rel
2204514f5e3Sopenharmony_ci    ElfSecName value_ {ElfSecName::NONE};
2214514f5e3Sopenharmony_ci    unsigned type_ {0};
2224514f5e3Sopenharmony_ci    unsigned flag_ {0};
2234514f5e3Sopenharmony_ci
2244514f5e3Sopenharmony_ci    static constexpr size_t AOTSecFeatureTable_[static_cast<size_t>(ElfSecName::SIZE)] = {
2254514f5e3Sopenharmony_ci        ElfSecFeature::NOT_VALID,
2264514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2274514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2284514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2294514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2304514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2314514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2324514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2334514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2344514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2354514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2364514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2374514f5e3Sopenharmony_ci        ElfSecFeature::VALID_AND_SEQUENTIAL,
2384514f5e3Sopenharmony_ci        ElfSecFeature::VALID_NOT_SEQUENTIAL,
2394514f5e3Sopenharmony_ci        ElfSecFeature::VALID_NOT_SEQUENTIAL,
2404514f5e3Sopenharmony_ci    };
2414514f5e3Sopenharmony_ci};
2424514f5e3Sopenharmony_ci}
2434514f5e3Sopenharmony_ci#endif