diff --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h --- a/llvm/include/llvm/Object/COFF.h +++ b/llvm/include/llvm/Object/COFF.h @@ -959,6 +959,7 @@ bool isSectionData(DataRefImpl Sec) const override; bool isSectionBSS(DataRefImpl Sec) const override; bool isSectionVirtual(DataRefImpl Sec) const override; + bool isSectionReadOnly(DataRefImpl Sec) const override; bool isDebugSection(StringRef SectionName) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override; diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -287,6 +287,7 @@ bool isSectionData(DataRefImpl Sec) const override; bool isSectionBSS(DataRefImpl Sec) const override; bool isSectionVirtual(DataRefImpl Sec) const override; + bool isSectionReadOnly(DataRefImpl Sec) const override; bool isBerkeleyText(DataRefImpl Sec) const override; bool isBerkeleyData(DataRefImpl Sec) const override; bool isDebugSection(StringRef SectionName) const override; @@ -933,6 +934,14 @@ SectionName.startswith(".zdebug") || SectionName == ".gdb_index"; } +template +bool ELFObjectFile::isSectionReadOnly(DataRefImpl Sec) const { + const Elf_Shdr *EShdr = getSection(Sec); + return EShdr->sh_flags & ELF::SHF_ALLOC && + !(EShdr->sh_flags & ELF::SHF_WRITE) && + EShdr->sh_type == ELF::SHT_PROGBITS; +} + template relocation_iterator ELFObjectFile::section_rel_begin(DataRefImpl Sec) const { diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h --- a/llvm/include/llvm/Object/MachO.h +++ b/llvm/include/llvm/Object/MachO.h @@ -319,6 +319,7 @@ /// to reading the number of bytes specified in the load command, starting /// from offset 0 (i.e. the Mach-O header at the beginning of the file). bool isSectionStripped(DataRefImpl Sec) const override; + bool isSectionReadOnly(DataRefImpl Sec) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override; diff --git a/llvm/include/llvm/Object/ObjectFile.h b/llvm/include/llvm/Object/ObjectFile.h --- a/llvm/include/llvm/Object/ObjectFile.h +++ b/llvm/include/llvm/Object/ObjectFile.h @@ -112,6 +112,7 @@ bool isVirtual() const; bool isBitcode() const; bool isStripped() const; + bool isReadOnly() const; /// Whether this section will be placed in the text segment, according to the /// Berkeley size format. This is true if the section is allocatable, and @@ -272,6 +273,7 @@ virtual bool isSectionVirtual(DataRefImpl Sec) const = 0; virtual bool isSectionBitcode(DataRefImpl Sec) const; virtual bool isSectionStripped(DataRefImpl Sec) const; + virtual bool isSectionReadOnly(DataRefImpl Sec) const = 0; virtual bool isBerkeleyText(DataRefImpl Sec) const; virtual bool isBerkeleyData(DataRefImpl Sec) const; virtual bool isDebugSection(StringRef SectionName) const; @@ -508,6 +510,10 @@ return OwningObject->isDebugSection(SectionName); } +inline bool SectionRef::isReadOnly() const { + return OwningObject->isSectionReadOnly(SectionPimpl); +} + inline relocation_iterator SectionRef::relocation_begin() const { return OwningObject->section_rel_begin(SectionPimpl); } diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h --- a/llvm/include/llvm/Object/Wasm.h +++ b/llvm/include/llvm/Object/Wasm.h @@ -190,6 +190,7 @@ bool isSectionData(DataRefImpl Sec) const override; bool isSectionBSS(DataRefImpl Sec) const override; bool isSectionVirtual(DataRefImpl Sec) const override; + bool isSectionReadOnly(DataRefImpl Sec) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override; diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h --- a/llvm/include/llvm/Object/XCOFFObjectFile.h +++ b/llvm/include/llvm/Object/XCOFFObjectFile.h @@ -296,6 +296,7 @@ bool isSectionBSS(DataRefImpl Sec) const override; bool isSectionVirtual(DataRefImpl Sec) const override; + bool isSectionReadOnly(DataRefImpl Sec) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override; diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -346,6 +346,11 @@ return Sec->PointerToRawData == 0; } +bool COFFObjectFile::isSectionReadOnly(DataRefImpl Ref) const { + llvm_unreachable("not implemented"); + return false; +} + static uint32_t getNumberOfRelocations(const coff_section *Sec, MemoryBufferRef M, const uint8_t *base) { // The field for the number of relocations in COFF section table is only diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -2066,6 +2066,11 @@ return getSection(Sec).offset == 0; } +bool MachOObjectFile::isSectionReadOnly(DataRefImpl Sec) const { + llvm_unreachable("not implemented"); + return false; +} + relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { DataRefImpl Ret; Ret.d.a = Sec.d.a; diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1673,6 +1673,8 @@ bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } +bool WasmObjectFile::isSectionReadOnly(DataRefImpl Sec) const { return false; } + relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { DataRefImpl RelocRef; RelocRef.d.a = Ref.d.a; diff --git a/llvm/lib/Object/XCOFFObjectFile.cpp b/llvm/lib/Object/XCOFFObjectFile.cpp --- a/llvm/lib/Object/XCOFFObjectFile.cpp +++ b/llvm/lib/Object/XCOFFObjectFile.cpp @@ -319,6 +319,10 @@ : toSection32(Sec)->FileOffsetToRawData == 0; } +bool XCOFFObjectFile::isSectionReadOnly(DataRefImpl Sec) const { + report_fatal_error("isSectionReadOnly not implemented yet"); +} + relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const { if (is64Bit()) report_fatal_error("64-bit support not implemented yet");