From ce1b5e0849e437b15eac8a440d6f7143f5b3e695 Mon Sep 17 00:00:00 2001 From: wuzhangda Date: Sat, 13 May 2023 13:36:26 +0800 Subject: [PATCH] Add .strtab And .symtab to Aot An File Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I71WRT Signed-off-by: wuzhangda Change-Id: I71561c4fa4a156cf4592430834d93085368d3a58 --- ecmascript/compiler/aot_file/an_file_info.cpp | 17 +- ecmascript/compiler/aot_file/an_file_info.h | 1 + ecmascript/compiler/aot_file/aot_version.h | 2 +- ecmascript/compiler/aot_file/elf_builder.cpp | 162 +++++++++--------- ecmascript/compiler/aot_file/elf_builder.h | 23 +-- ecmascript/compiler/aot_file/elf_reader.cpp | 4 +- .../compiler/aot_file/module_section_des.cpp | 2 + .../compiler/aot_file/module_section_des.h | 10 ++ .../compiler/aot_file/stub_file_info.cpp | 2 +- ecmascript/compiler/binary_section.h | 34 +--- ecmascript/platform/unix/map.cpp | 5 +- 11 files changed, 134 insertions(+), 128 deletions(-) diff --git a/ecmascript/compiler/aot_file/an_file_info.cpp b/ecmascript/compiler/aot_file/an_file_info.cpp index 63f562af0d..729914fbe7 100644 --- a/ecmascript/compiler/aot_file/an_file_info.cpp +++ b/ecmascript/compiler/aot_file/an_file_info.cpp @@ -32,11 +32,7 @@ void AnFileInfo::Save(const std::string &filename, Triple triple) std::ofstream file(realPath.c_str(), std::ofstream::binary); SetStubNum(entries_.size()); - ModuleSectionDes &des = des_[0]; - // add section - uint64_t funcEntryAddr = reinterpret_cast(entries_.data()); - uint32_t funcEntrySize = sizeof(FuncEntryDes) * entryNum_; - des.SetSecAddrAndSize(ElfSecName::ARK_FUNCENTRY, funcEntryAddr, funcEntrySize); + AddFuncEntrySec(); ElfBuilder builder(des_, GetDumpSectionNames()); llvm::ELF::Elf64_Ehdr header; @@ -120,6 +116,8 @@ const std::vector &AnFileInfo::GetDumpSectionNames() static const std::vector secNames = { ElfSecName::TEXT, ElfSecName::STRTAB, + ElfSecName::SYMTAB, + ElfSecName::SHSTRTAB, ElfSecName::ARK_STACKMAP, ElfSecName::ARK_FUNCENTRY }; @@ -159,4 +157,13 @@ bool AnFileInfo::IsLoadMain(const JSPandaFile *jsPandaFile, const CString &entry auto it = mainEntryMap_.find(methodId); return it != mainEntryMap_.end(); } + +void AnFileInfo::AddFuncEntrySec() +{ + ModuleSectionDes &des = des_[ElfBuilder::FuncEntryModuleDesIndex]; + // add section + uint64_t funcEntryAddr = reinterpret_cast(entries_.data()); + uint32_t funcEntrySize = sizeof(FuncEntryDes) * entryNum_; + des.SetSecAddrAndSize(ElfSecName::ARK_FUNCENTRY, funcEntryAddr, funcEntrySize); +} } // namespace panda::ecmascript diff --git a/ecmascript/compiler/aot_file/an_file_info.h b/ecmascript/compiler/aot_file/an_file_info.h index e10cb9b369..fd7910814a 100644 --- a/ecmascript/compiler/aot_file/an_file_info.h +++ b/ecmascript/compiler/aot_file/an_file_info.h @@ -76,6 +76,7 @@ private: bool Load(const std::string &filename); void ParseFunctionEntrySection(ModuleSectionDes &moduleDes); void UpdateFuncEntries(); + void AddFuncEntrySec(); uint64_t curTextSecOffset_ {0}; std::unordered_map mainEntryMap_ {}; bool isLoad_ {false}; diff --git a/ecmascript/compiler/aot_file/aot_version.h b/ecmascript/compiler/aot_file/aot_version.h index 8d1f018576..0e50be1746 100644 --- a/ecmascript/compiler/aot_file/aot_version.h +++ b/ecmascript/compiler/aot_file/aot_version.h @@ -25,7 +25,7 @@ public: // Release Version Snapshot Version // 3.2 0.0.0.x // 4.0 4.0.0.x - static constexpr base::FileHeader::VersionType AN_VERSION = {4, 0, 0, 2}; + static constexpr base::FileHeader::VersionType AN_VERSION = {4, 0, 0, 3}; static constexpr bool AN_STRICT_MATCH = true; static constexpr base::FileHeader::VersionType AI_VERSION = {4, 0, 0, 1}; static constexpr bool AI_STRICT_MATCH = true; diff --git a/ecmascript/compiler/aot_file/elf_builder.cpp b/ecmascript/compiler/aot_file/elf_builder.cpp index 72cf7e9ba2..9b50a5797e 100644 --- a/ecmascript/compiler/aot_file/elf_builder.cpp +++ b/ecmascript/compiler/aot_file/elf_builder.cpp @@ -19,9 +19,10 @@ #include "securec.h" namespace panda::ecmascript { -void ElfBuilder::ModifyStrTabSection() +void ElfBuilder::AddShStrTabSection() { - std::map> §ions = des_[0].GetSectionsInfo(); + std::map> §ions = + des_[ShStrTableModuleDesIndex].GetSectionsInfo(); uint32_t size = 1; for (auto &s : sections_) { @@ -29,8 +30,8 @@ void ElfBuilder::ModifyStrTabSection() size = size + str.size() + 1; } - strTabPtr_ = std::make_unique(size); - char *dst = strTabPtr_.get(); + shStrTabPtr_ = std::make_unique(size); + char *dst = shStrTabPtr_.get(); dst[0] = 0x0; uint32_t i = 1; for (auto &s: sections_) { @@ -45,10 +46,10 @@ void ElfBuilder::ModifyStrTabSection() dst[i + copySize] = 0x0; i = i + copySize + 1; } - if (sections.find(ElfSecName::STRTAB) != sections.end()) { - sections.erase(ElfSecName::STRTAB); + if (sections.find(ElfSecName::SHSTRTAB) != sections.end()) { + sections.erase(ElfSecName::SHSTRTAB); } - sections[ElfSecName::STRTAB] = std::make_pair(reinterpret_cast(strTabPtr_.get()), size); + sections[ElfSecName::SHSTRTAB] = std::make_pair(reinterpret_cast(shStrTabPtr_.get()), size); if (enableSecDump_) { DumpSection(); } @@ -56,7 +57,7 @@ void ElfBuilder::ModifyStrTabSection() void ElfBuilder::DumpSection() const { - const std::map> §ions = GetCurrentSecInfo(); + const std::map> §ions = GetFullSecInfo(); // dump for (auto &s : sections) { ElfSection section = ElfSection(s.first); @@ -68,24 +69,19 @@ void ElfBuilder::DumpSection() const } } -void ElfBuilder::AddArkStackMapSection() -{ - for (auto &des : des_) { - std::map> §ions = des.GetSectionsInfo(); - std::shared_ptr ptr = des.GetArkStackMapSharePtr(); - uint64_t arkStackMapAddr = reinterpret_cast(ptr.get()); - uint32_t arkStackMapSize = des.GetArkStackMapSize(); - if (arkStackMapSize > 0) { - sections[ElfSecName::ARK_STACKMAP] = std::pair(arkStackMapAddr, arkStackMapSize); - } - } -} - ElfBuilder::ElfBuilder(const std::vector &des, const std::vector §ions): des_(des), sections_(sections) { - AddArkStackMapSection(); - ModifyStrTabSection(); + Initialize(); + AddShStrTabSection(); + RemoveNotNeedSection(); +} + +void ElfBuilder::Initialize() +{ + for (size_t i = 0; i < des_.size(); i++) { + des_[i].AddArkStackMapSection(); + } sectionToAlign_ = { {ElfSecName::RODATA, AOTFileInfo::TEXT_SEC_ALIGN}, {ElfSecName::RODATA_CST4, AOTFileInfo::TEXT_SEC_ALIGN}, @@ -94,6 +90,8 @@ ElfBuilder::ElfBuilder(const std::vector &des, {ElfSecName::RODATA_CST32, AOTFileInfo::TEXT_SEC_ALIGN}, {ElfSecName::TEXT, AOTFileInfo::TEXT_SEC_ALIGN}, {ElfSecName::STRTAB, 1}, + {ElfSecName::SYMTAB, AOTFileInfo::DATA_SEC_ALIGN}, + {ElfSecName::SHSTRTAB, AOTFileInfo::DATA_SEC_ALIGN}, {ElfSecName::ARK_STACKMAP, AOTFileInfo::DATA_SEC_ALIGN}, {ElfSecName::ARK_FUNCENTRY, AOTFileInfo::DATA_SEC_ALIGN}, {ElfSecName::ARK_ASMSTUB, AOTFileInfo::DATA_SEC_ALIGN}, @@ -108,6 +106,8 @@ ElfBuilder::ElfBuilder(const std::vector &des, {ElfSecName::RODATA_CST32, ElfSecName::TEXT}, {ElfSecName::TEXT, ElfSecName::TEXT}, {ElfSecName::STRTAB, ElfSecName::DATA}, + {ElfSecName::SYMTAB, ElfSecName::DATA}, + {ElfSecName::SHSTRTAB, ElfSecName::DATA}, {ElfSecName::ARK_STACKMAP, ElfSecName::DATA}, {ElfSecName::ARK_FUNCENTRY, ElfSecName::DATA}, {ElfSecName::ARK_ASMSTUB, ElfSecName::TEXT}, @@ -119,12 +119,12 @@ ElfBuilder::ElfBuilder(const std::vector &des, {ElfSecName::DATA, llvm::ELF::PF_R}, }; - RemoveNotNeedSection(); + SetLastSection(); } void ElfBuilder::RemoveNotNeedSection() { - const std::map> §ions = GetCurrentSecInfo(); + const std::map> §ions = GetFullSecInfo(); for (size_t i = 0; i < sections_.size();) { if (sections.find(sections_[i]) == sections.end()) { auto it = sections_.begin() + i; @@ -137,7 +137,7 @@ void ElfBuilder::RemoveNotNeedSection() ElfBuilder::~ElfBuilder() { - strTabPtr_ = nullptr; + shStrTabPtr_ = nullptr; } uint32_t ElfBuilder::GetShIndex(ElfSecName section) const @@ -220,7 +220,7 @@ void ElfBuilder::PackELFHeader(llvm::ELF::Elf64_Ehdr &header, uint32_t version, // number of section headers header.e_shnum = GetSecNum() + 1; // 1: skip null section and ark stackmap // section header string table index - header.e_shstrndx = static_cast(GetShIndex(ElfSecName::STRTAB)); + header.e_shstrndx = static_cast(GetShIndex(ElfSecName::SHSTRTAB)); // section header stub sec info index header.e_flags = static_cast(GetShIndex(ElfSecName::ARK_MODULEINFO)); // phr @@ -230,7 +230,7 @@ void ElfBuilder::PackELFHeader(llvm::ELF::Elf64_Ehdr &header, uint32_t version, int ElfBuilder::GetSegmentNum() const { - const std::map> §ions = GetCurrentSecInfo(); + const std::map> §ions = GetFullSecInfo(); std::set segments; for (auto &s: sections) { ElfSection section = ElfSection(s.first); @@ -245,10 +245,9 @@ int ElfBuilder::GetSegmentNum() const return segments.size(); } -ElfSecName ElfBuilder::FindLastSection(ElfSecName segment) const +void ElfBuilder::SetLastSection() { - ElfSecName ans = ElfSecName::NONE; - const std::map> §ions = GetCurrentSecInfo(); + const std::map> §ions = GetFullSecInfo(); for (auto &s: sections) { ElfSection section = ElfSection(s.first); if (!section.ShouldDumpToAOTFile()) { @@ -257,12 +256,12 @@ ElfSecName ElfBuilder::FindLastSection(ElfSecName segment) const auto it = sectionToSegment_.find(s.first); ASSERT(it != sectionToSegment_.end()); ElfSecName name = it->second; - if (name != segment) { - continue; + if (name == ElfSecName::TEXT) { + lastCodeSection = std::max(lastCodeSection, s.first); + } else { + lastDataSection = std::max(lastDataSection, s.first); } - ans = std::max(ans, s.first); } - return ans; } llvm::ELF::Elf64_Word ElfBuilder::FindShName(std::string name, uintptr_t strTabPtr, int strTabSize) @@ -282,21 +281,21 @@ llvm::ELF::Elf64_Word ElfBuilder::FindShName(std::string name, uintptr_t strTabP return ans; } -std::pair ElfBuilder::FindStrTab() const +std::pair ElfBuilder::FindShStrTab() const { - const std::map> §ions = GetCurrentSecInfo(); - uint64_t strTabAddr = 0; - uint32_t strTabSize = 0; + const std::map> §ions = GetFullSecInfo(); + uint64_t shStrTabAddr = 0; + uint32_t shStrTabSize = 0; for (auto &s: sections) { - uint32_t curSecSize = des_[0].GetSecSize(s.first); - uint64_t curSecAddr = des_[0].GetSecAddr(s.first); - if (s.first == ElfSecName::STRTAB) { - strTabSize = curSecSize; - strTabAddr = curSecAddr; + uint32_t curSecSize = des_[ShStrTableModuleDesIndex].GetSecSize(s.first); + uint64_t curSecAddr = des_[ShStrTableModuleDesIndex].GetSecAddr(s.first); + if (s.first == ElfSecName::SHSTRTAB) { + shStrTabSize = curSecSize; + shStrTabAddr = curSecAddr; break; } } - return std::make_pair(strTabAddr, strTabSize); + return std::make_pair(shStrTabAddr, shStrTabSize); } void ElfBuilder::AllocateShdr(std::unique_ptr &shdr, const uint32_t &secNum) @@ -370,28 +369,30 @@ void ElfBuilder::MergeArkStackMapSections(std::ofstream &file, /* section of aot.an layout as follows: -There are 5 section headers, starting at offset 0x40: - -Section Headers: - [Nr] Name Type Address Offset Size EntSize Flags Link Info Align - [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 - [ 1] .text PROGBITS 0000000000001000 00001000 00000000000013a0 0000000000000000 AX 0 0 16 - [ 2] .strtab STRTAB 0000000000003000 00003000 000000000000003d 0000000000000000 A 0 0 1 - [ 3] .ark_funcentry PROGBITS 0000000000003040 00003040 00000000000007e0 0000000000000000 A 0 0 8 - [ 4] .ark_stackmaps PROGBITS 0000000000003820 00003820 0000000000000892 0000000000000000 A 0 0 8 - -section of stub.an layout as follows: There are 7 section headers, starting at offset 0x40: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 - [ 1] .text PROGBITS 0000000000001000 00001000 000000000008148e 0000000000000000 AX 0 0 16 - [ 2] .ark_asmstub PROGBITS 0000000000082490 00082490 0000000000002dc0 0000000000000000 AX 0 0 8 - [ 3] .strtab STRTAB 0000000000086000 00086000 0000000000000048 0000000000000000 A 0 0 1 - [ 4] .ark_funcentry PROGBITS 0000000000086048 00086048 0000000000023ca0 0000000000000000 A 0 0 8 - [ 5] .ark_stackmaps PROGBITS 00000000000a9ce8 000a9ce8 00000000000119cc 0000000000000000 A 0 0 8 - [ 6] .ark_moduleinfo PROGBITS 00000000000bb6b8 000bb6b8 000000000000003c 0000000000000000 A 0 0 8 + [ 1] .text PROGBITS 0000000000001000 00001000 0000000000000f61 0000000000000000 AX 0 0 16 + [ 2] .strtab STRTAB 0000000000002000 00002000 0000000000000187 0000000000000000 A 0 0 1 + [ 3] .symtab SYMTAB 0000000000002188 00002188 00000000000001c8 0000000000000018 A 1 0 8 + [ 4] .shstrtab STRTAB 0000000000002350 00002350 000000000000003f 0000000000000000 A 0 0 8 + [ 5] .ark_funcentry PROGBITS 0000000000002390 00002390 00000000000006c0 0000000000000000 A 0 0 8 + [ 6] .ark_stackmaps PROGBITS 0000000000002a50 00002a50 000000000000070e 0000000000000000 A 0 0 8 + +section of stub.an layout as follows: +There are 7 section headers, starting at offset 0x40: + + [Nr] Name Type Address Offset Size EntSize Flags Link Info Align + [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 + [ 1] .text PROGBITS 0000000000001000 00001000 000000000008225e 0000000000000000 AX 0 0 16 + [ 2] .ark_asmstub PROGBITS 0000000000083260 00083260 0000000000002dc0 0000000000000000 AX 0 0 8 + [ 3] .shstrtab STRTAB 0000000000087000 00087000 000000000000004c 0000000000000000 A 0 0 8 + [ 4] .ark_funcentry PROGBITS 0000000000087050 00087050 0000000000023ca0 0000000000000000 A 0 0 8 + [ 5] .ark_stackmaps PROGBITS 00000000000aacf0 000aacf0 0000000000011e90 0000000000000000 A 0 0 8 + [ 6] .ark_moduleinfo PROGBITS 00000000000bcb80 000bcb80 000000000000003c 0000000000000000 A 0 0 8 + Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), @@ -401,7 +402,7 @@ Key to Flags: void ElfBuilder::PackELFSections(std::ofstream &file) { uint32_t moduleNum = des_.size(); - const std::map> §ions = GetCurrentSecInfo(); + const std::map> §ions = GetFullSecInfo(); uint32_t secNum = sections.size() + 1; // 1 : section id = 0 is null section std::unique_ptr shdr; AllocateShdr(shdr, secNum); @@ -410,12 +411,12 @@ void ElfBuilder::PackELFSections(std::ofstream &file) file.seekp(curSecOffset); int i = 1; // 1: skip null section - auto strTab = FindStrTab(); + auto shStrTab = FindShStrTab(); for (auto const &[secName, secInfo] : sections) { auto &curShdr = shdr[i]; ElfSection section = ElfSection(secName); - if (!section.ShouldDumpToStubFile()) { + if (!section.ShouldDumpToAOTFile()) { continue; } curShdr.sh_addralign = sectionToAlign_[secName]; @@ -429,7 +430,7 @@ void ElfBuilder::PackELFSections(std::ofstream &file) curSecOffset = AlignUp(curSecOffset, AOTFileInfo::TEXT_SEC_ALIGN); file.seekp(curSecOffset); } - llvm::ELF::Elf64_Word shName = FindShName(secNameStr, strTab.first, strTab.second); + llvm::ELF::Elf64_Word shName = FindShName(secNameStr, shStrTab.first, shStrTab.second); ASSERT(shName != static_cast(-1)); curShdr.sh_name = shName; curShdr.sh_type = section.Type(); @@ -458,24 +459,23 @@ void ElfBuilder::PackELFSections(std::ofstream &file) break; } case ElfSecName::STRTAB: + case ElfSecName::SYMTAB: + case ElfSecName::SHSTRTAB: case ElfSecName::ARK_FUNCENTRY: case ElfSecName::ARK_ASMSTUB: { - uint32_t curSecSize = GetCurrentDes().GetSecSize(secName); - uint64_t curSecAddr = GetCurrentDes().GetSecAddr(secName); + uint32_t curSecSize = des_[FullSecIndex].GetSecSize(secName); + uint64_t curSecAddr = des_[FullSecIndex].GetSecAddr(secName); file.write(reinterpret_cast(curSecAddr), curSecSize); curSecOffset += curSecSize; curShdr.sh_size = curSecSize; break; } - case ElfSecName::RODATA_CST8: // dealt at the same time of ElfSecName::TEXT - break; default: { - LOG_ECMA(FATAL) << "this section should not dump to stub file"; + LOG_ECMA(FATAL) << "this section should not dump to an file"; break; } } - ElfSecName lastSecName = FindLastSection(segName); - if (secName == lastSecName) { + if (secName == lastDataSection || secName == lastCodeSection) { curSecOffset = AlignUp(curSecOffset, PageSize()); file.seekp(curSecOffset); } @@ -505,13 +505,13 @@ There are 2 program headers, starting at offset 16384 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align - LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000 0x000000000000114a 0x0000000000002000 R E 0x1000 - LOAD 0x0000000000003000 0x0000000000003000 0x0000000000003000 0x0000000000000e54 0x0000000000001000 R 0x1000 + LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000 0x0000000000000f61 0x0000000000001000 R E 0x1000 + LOAD 0x0000000000002000 0x0000000000002000 0x0000000000002000 0x000000000000115e 0x0000000000002000 R 0x1000 Section to Segment mapping: Segment Sections... - 00 .rodata.cst8 .text - 01 .strtab .ark_funcentry .ark_stackmaps + 00 .text + 01 .strtab .symtab .shstrtab .ark_funcentry .ark_stackmaps ------------------------------------------------------------------------------------------------------------------------------ Stub Elf file Entry point 0x0 @@ -519,13 +519,13 @@ There are 2 program headers, starting at offset 770048 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align - LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000085250 0x0000000000086000 R E 0x1000 - LOAD 0x0000000000086000 0x0000000000086000 0x0000000000086000 0x00000000000356f4 0x0000000000036000 R 0x1000 + LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000 0x0000000000085020 0x0000000000086000 R E 0x1000 + LOAD 0x0000000000087000 0x0000000000087000 0x0000000000087000 0x0000000000035bbc 0x0000000000036000 R 0x1000 Section to Segment mapping: Segment Sections... 00 .text .ark_asmstub - 01 .strtab .ark_funcentry .ark_stackmaps .ark_moduleinfo + 01 .shstrtab .ark_funcentry .ark_stackmaps .ark_moduleinfo */ void ElfBuilder::PackELFSegment(std::ofstream &file) { @@ -542,7 +542,7 @@ void ElfBuilder::PackELFSegment(std::ofstream &file) std::map segmentToMaxAddress; std::set segments; // SecName -> addr & size - const std::map> §ions = GetCurrentSecInfo(); + const std::map> §ions = GetFullSecInfo(); llvm::ELF::Elf64_Off offset = e_phoff; for (auto &s: sections) { ElfSection section = ElfSection(s.first); diff --git a/ecmascript/compiler/aot_file/elf_builder.h b/ecmascript/compiler/aot_file/elf_builder.h index 7deef480cd..a1722003fa 100644 --- a/ecmascript/compiler/aot_file/elf_builder.h +++ b/ecmascript/compiler/aot_file/elf_builder.h @@ -32,6 +32,7 @@ class ElfBuilder { public: ElfBuilder(const std::vector &des, const std::vector §ions); ~ElfBuilder(); + static constexpr uint32_t FuncEntryModuleDesIndex = 0; void PackELFHeader(llvm::ELF::Elf64_Ehdr &header, uint32_t version, Triple triple); void PackELFSections(std::ofstream &elfFile); void PackELFSegment(std::ofstream &elfFile); @@ -40,13 +41,9 @@ public: void MergeArkStackMapSections(std::ofstream &elfFile, std::vector &moduleInfo, llvm::ELF::Elf64_Off &curSecOffset); static llvm::ELF::Elf64_Word FindShName(std::string name, uintptr_t strTabPtr, int strTabSize); - std::map> GetCurrentSecInfo() const + std::map> GetFullSecInfo() const { - return des_[0].GetSectionsInfo(); - } - ModuleSectionDes GetCurrentDes() const - { - return des_[0]; + return des_[FullSecIndex].GetSectionsInfo(); } void SetEnableSecDump(bool flag) { @@ -55,23 +52,25 @@ public: private: uint32_t GetShIndex(ElfSecName section) const; - ElfSecName FindLastSection(ElfSecName segment) const; int GetSegmentNum() const; int GetSecNum() const; unsigned GetPFlag(ElfSecName segment) const; ElfSecName GetSegmentName(const ElfSecName &secName) const; - std::pair FindStrTab() const; + std::pair FindShStrTab() const; void AllocateShdr(std::unique_ptr &shdr, const uint32_t &secNum); llvm::ELF::Elf64_Off ComputeEndAddrOfShdr(const uint32_t &secNum) const; bool SupportELF(); - void AddArkStackMapSection(); void DumpSection() const; - void ModifyStrTabSection(); + void AddShStrTabSection(); + void Initialize(); + void SetLastSection(); void RemoveNotNeedSection(); static constexpr uint32_t ASMSTUB_MODULE_NUM = 3; + static constexpr uint32_t ShStrTableModuleDesIndex = 0; + static constexpr uint32_t FullSecIndex = 0; std::vector des_ {}; - std::unique_ptr strTabPtr_ {nullptr}; + std::unique_ptr shStrTabPtr_ {nullptr}; std::map sectionToShdr_; std::map sectionToAlign_; std::map sectionToSegment_; @@ -80,6 +79,8 @@ private: std::vector sections_ {}; std::set segments_; bool enableSecDump_ {false}; + ElfSecName lastDataSection {ElfSecName::NONE}; + ElfSecName lastCodeSection {ElfSecName::NONE}; }; } // namespace panda::ecmascript #endif // ECMASCRIPT_COMPILER_AOT_FILE_ELF_BUILDER_H diff --git a/ecmascript/compiler/aot_file/elf_reader.cpp b/ecmascript/compiler/aot_file/elf_reader.cpp index 7962576ac6..c057381188 100644 --- a/ecmascript/compiler/aot_file/elf_reader.cpp +++ b/ecmascript/compiler/aot_file/elf_reader.cpp @@ -129,7 +129,7 @@ void ElfReader::ParseELFSections(std::vector &des, std::vector ASSERT(static_cast(secOffset) == secSize); break; } - case ElfSecName::STRTAB: + case ElfSecName::SHSTRTAB: case ElfSecName::ARK_FUNCENTRY: case ElfSecName::ARK_ASMSTUB: case ElfSecName::ARK_MODULEINFO: { @@ -200,7 +200,7 @@ void ElfReader::ParseELFSections(BinaryBufferParser &parser, ASSERT(static_cast(secOffset) == secSize); break; } - case ElfSecName::STRTAB: + case ElfSecName::SHSTRTAB: case ElfSecName::ARK_FUNCENTRY: case ElfSecName::ARK_ASMSTUB: case ElfSecName::ARK_MODULEINFO: { diff --git a/ecmascript/compiler/aot_file/module_section_des.cpp b/ecmascript/compiler/aot_file/module_section_des.cpp index c5b9b47046..a836de4636 100644 --- a/ecmascript/compiler/aot_file/module_section_des.cpp +++ b/ecmascript/compiler/aot_file/module_section_des.cpp @@ -43,6 +43,8 @@ std::string ModuleSectionDes::GetSecName(const ElfSecName idx) return ".strtab"; case ElfSecName::SYMTAB: return ".symtab"; + case ElfSecName::SHSTRTAB: + return ".shstrtab"; case ElfSecName::LLVM_STACKMAP: return ".llvm_stackmaps"; case ElfSecName::ARK_STACKMAP: diff --git a/ecmascript/compiler/aot_file/module_section_des.h b/ecmascript/compiler/aot_file/module_section_des.h index 40be3eb40a..5e292e21ed 100644 --- a/ecmascript/compiler/aot_file/module_section_des.h +++ b/ecmascript/compiler/aot_file/module_section_des.h @@ -130,6 +130,16 @@ public: return (pc >= stubStartAddr && pc <= stubEndAddr); } + void AddArkStackMapSection() + { + std::shared_ptr ptr = GetArkStackMapSharePtr(); + uint64_t arkStackMapAddr = reinterpret_cast(ptr.get()); + uint32_t arkStackMapSize = GetArkStackMapSize(); + if (arkStackMapSize > 0) { + sectionsInfo_[ElfSecName::ARK_STACKMAP] = std::pair(arkStackMapAddr, arkStackMapSize); + } + } + private: static constexpr int DECIMAL_LENS = 2; static constexpr int HUNDRED_TIME = 100; diff --git a/ecmascript/compiler/aot_file/stub_file_info.cpp b/ecmascript/compiler/aot_file/stub_file_info.cpp index 274ffd3e10..5fabb372fc 100644 --- a/ecmascript/compiler/aot_file/stub_file_info.cpp +++ b/ecmascript/compiler/aot_file/stub_file_info.cpp @@ -154,7 +154,7 @@ const std::vector &StubFileInfo::GetDumpSectionNames() { static const std::vector secNames = { ElfSecName::TEXT, - ElfSecName::STRTAB, + ElfSecName::SHSTRTAB, ElfSecName::ARK_STACKMAP, ElfSecName::ARK_FUNCENTRY, ElfSecName::ARK_ASMSTUB, diff --git a/ecmascript/compiler/binary_section.h b/ecmascript/compiler/binary_section.h index 14efaa0735..7a221e269b 100644 --- a/ecmascript/compiler/binary_section.h +++ b/ecmascript/compiler/binary_section.h @@ -36,6 +36,7 @@ enum class ElfSecName : uint8_t { RELATEXT, STRTAB, SYMTAB, + SHSTRTAB, LLVM_STACKMAP, ARK_FUNCENTRY, ARK_STACKMAP, @@ -90,7 +91,9 @@ public: value_ = ElfSecName::STRTAB; } else if (str.compare(".symtab") == 0) { value_ = ElfSecName::SYMTAB; - } else if (str.compare(".llvm_stackmaps") == 0) { + } else if (str.compare(".shstrtab") == 0) { + value_ = ElfSecName::SHSTRTAB; + } else if (str.compare(".llvm_stackmaps") == 0) { value_ = ElfSecName::LLVM_STACKMAP; } else if (str.compare(".ark_stackmaps") == 0) { value_ = ElfSecName::ARK_STACKMAP; @@ -108,13 +111,10 @@ public: { bool saveForAot = false; switch (value_) { - case ElfSecName::RODATA: - case ElfSecName::RODATA_CST4: - case ElfSecName::RODATA_CST8: - case ElfSecName::RODATA_CST16: - case ElfSecName::RODATA_CST32: case ElfSecName::TEXT: case ElfSecName::STRTAB: + case ElfSecName::SYMTAB: + case ElfSecName::SHSTRTAB: case ElfSecName::ARK_FUNCENTRY: case ElfSecName::ARK_ASMSTUB: case ElfSecName::ARK_STACKMAP: @@ -129,26 +129,6 @@ public: return saveForAot; } - bool ShouldDumpToStubFile() const - { - bool saveForStub = false; - switch (value_) { - case ElfSecName::TEXT: - case ElfSecName::STRTAB: - case ElfSecName::ARK_FUNCENTRY: - case ElfSecName::ARK_ASMSTUB: - case ElfSecName::ARK_STACKMAP: - case ElfSecName::ARK_MODULEINFO: { - saveForStub = true; - break; - } - default: { - break; - } - } - return saveForStub; - } - ElfSecName Value() const { return value_; @@ -182,6 +162,7 @@ public: {ElfSecName::RELATEXT, {llvm::ELF::SHT_RELA, llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE}}, {ElfSecName::STRTAB, {llvm::ELF::SHT_STRTAB, llvm::ELF::SHF_ALLOC}}, {ElfSecName::SYMTAB, {llvm::ELF::SHT_SYMTAB, llvm::ELF::SHF_ALLOC}}, + {ElfSecName::SHSTRTAB, {llvm::ELF::SHT_STRTAB, llvm::ELF::SHF_ALLOC}}, {ElfSecName::LLVM_STACKMAP, {llvm::ELF::SHT_RELA, llvm::ELF::SHF_ALLOC}}, {ElfSecName::ARK_FUNCENTRY, {llvm::ELF::SHF_WRITE, llvm::ELF::SHF_ALLOC}}, {ElfSecName::ARK_STACKMAP, {llvm::ELF::SHF_WRITE, llvm::ELF::SHF_ALLOC}}, @@ -252,6 +233,7 @@ private: ElfSecFeature::VALID_AND_SEQUENTIAL, ElfSecFeature::VALID_AND_SEQUENTIAL, ElfSecFeature::VALID_AND_SEQUENTIAL, + ElfSecFeature::VALID_AND_SEQUENTIAL, ElfSecFeature::VALID_NOT_SEQUENTIAL, ElfSecFeature::VALID_NOT_SEQUENTIAL, }; diff --git a/ecmascript/platform/unix/map.cpp b/ecmascript/platform/unix/map.cpp index 262fafeac3..2072ba20c1 100644 --- a/ecmascript/platform/unix/map.cpp +++ b/ecmascript/platform/unix/map.cpp @@ -87,7 +87,10 @@ void PageClearTag(void *mem, size_t size) void PageProtect(void *mem, size_t size, int prot) { - mprotect(mem, size, prot); + int ret = mprotect(mem, size, prot); + if (ret != 0) { + LOG_ECMA(ERROR) << "PageProtect change to " << prot << " failed, error code is " << errno; + } } size_t PageSize()