diff --git a/include/llvm/Object/XCOFFObjectFile.h b/include/llvm/Object/XCOFFObjectFile.h index 84073ce5f6c..4f80297b04c 100644 --- a/include/llvm/Object/XCOFFObjectFile.h +++ b/include/llvm/Object/XCOFFObjectFile.h @@ -47,7 +47,20 @@ struct XCOFFFileHeader64 { support::ubig32_t NumberOfSymTableEntries; }; -struct XCOFFSectionHeader32 { +template struct XCOFFSectionHeader { + // Least significant 3 bits are reserved. + static constexpr unsigned SectionFlagsReservedMask = 0x7; + + // The low order 16 bits of section flags denotes the section type. + static constexpr unsigned SectionFlagsTypeMask = 0xffffu; + +public: + StringRef getName() const; + uint16_t getSectionType() const; + bool isReservedSectionType() const; +}; + +struct XCOFFSectionHeader32 : XCOFFSectionHeader { char Name[XCOFF::NameSize]; support::ubig32_t PhysicalAddress; support::ubig32_t VirtualAddress; @@ -58,11 +71,9 @@ struct XCOFFSectionHeader32 { support::ubig16_t NumberOfRelocations; support::ubig16_t NumberOfLineNumbers; support::big32_t Flags; - - StringRef getName() const; }; -struct XCOFFSectionHeader64 { +struct XCOFFSectionHeader64 : XCOFFSectionHeader { char Name[XCOFF::NameSize]; support::ubig64_t PhysicalAddress; support::ubig64_t VirtualAddress; @@ -74,8 +85,6 @@ struct XCOFFSectionHeader64 { support::ubig32_t NumberOfLineNumbers; support::big32_t Flags; char Padding[4]; - - StringRef getName() const; }; struct XCOFFSymbolEntry { diff --git a/lib/Object/XCOFFObjectFile.cpp b/lib/Object/XCOFFObjectFile.cpp index 98782c2701c..6f1d999bc20 100644 --- a/lib/Object/XCOFFObjectFile.cpp +++ b/lib/Object/XCOFFObjectFile.cpp @@ -46,6 +46,25 @@ static StringRef generateXCOFFFixedNameStringRef(const char *Name) { : StringRef(Name, XCOFF::NameSize); } +// Explictly instantiate template classes. +template struct XCOFFSectionHeader; +template struct XCOFFSectionHeader; + +template StringRef XCOFFSectionHeader::getName() const { + const T &DerivedXCOFFSectionHeader = static_cast(*this); + return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name); +} + +template uint16_t XCOFFSectionHeader::getSectionType() const { + const T &DerivedXCOFFSectionHeader = static_cast(*this); + return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask; +} + +template +bool XCOFFSectionHeader::isReservedSectionType() const { + return getSectionType() & SectionFlagsReservedMask; +} + bool XCOFFRelocation32::isRelocationSigned() const { return Info & XR_SIGN_INDICATOR_MASK; } @@ -688,14 +707,6 @@ ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef, return XCOFFObjectFile::create(FileType, MemBufRef); } -StringRef XCOFFSectionHeader32::getName() const { - return generateXCOFFFixedNameStringRef(Name); -} - -StringRef XCOFFSectionHeader64::getName() const { - return generateXCOFFFixedNameStringRef(Name); -} - XCOFF::StorageClass XCOFFSymbolRef::getStorageClass() const { return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->StorageClass; } diff --git a/tools/llvm-readobj/XCOFFDumper.cpp b/tools/llvm-readobj/XCOFFDumper.cpp index fe95b6d1b49..1f940366559 100644 --- a/tools/llvm-readobj/XCOFFDumper.cpp +++ b/tools/llvm-readobj/XCOFFDumper.cpp @@ -49,13 +49,6 @@ private: void printCsectAuxEnt32(const XCOFFCsectAuxEnt32 *AuxEntPtr); void printSectAuxEntForStat(const XCOFFSectAuxEntForStat *AuxEntPtr); void printSymbol(const SymbolRef &); - - // Least significant 3 bits are reserved. - static constexpr unsigned SectionFlagsReservedMask = 0x7; - - // The low order 16 bits of section flags denotes the section type. - static constexpr unsigned SectionFlagsTypeMask = 0xffffu; - void printRelocations(ArrayRef Sections); const XCOFFObjectFile &Obj; }; @@ -496,8 +489,7 @@ void XCOFFDumper::printSectionHeaders(ArrayRef Sections) { DictScope SecDS(W, "Section"); W.printNumber("Index", Index++); - - uint16_t SectionType = Sec.Flags & SectionFlagsTypeMask; + uint16_t SectionType = Sec.getSectionType(); switch (SectionType) { case XCOFF::STYP_OVRFLO: printOverflowSectionHeader(Sec); @@ -513,8 +505,7 @@ void XCOFFDumper::printSectionHeaders(ArrayRef Sections) { printGenericSectionHeader(Sec); break; } - // For now we just dump the section type portion of the flags. - if (SectionType & SectionFlagsReservedMask) + if (Sec.isReservedSectionType()) W.printHex("Flags", "Reserved", SectionType); else W.printEnum("Type", SectionType, makeArrayRef(SectionTypeFlagsNames));