26 const uint64_t
Size =
sizeof(
T)) {
27 uintptr_t Addr = uintptr_t(Ptr);
30 return reinterpret_cast<const T *
>(Addr);
34 return reinterpret_cast<uintptr_t
>(
reinterpret_cast<const char *
>(Base) +
38 template <
typename T>
static const T *
viewAs(uintptr_t in) {
39 return reinterpret_cast<const T *
>(in);
45 return NulCharPtr ?
StringRef(Name, NulCharPtr - Name)
50 const T &DerivedXCOFFSectionHeader =
static_cast<const T &
>(*this);
55 const T &DerivedXCOFFSectionHeader =
static_cast<const T &
>(*this);
56 return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask;
61 return getSectionType() & SectionFlagsReservedMask;
78 void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr,
79 uintptr_t TableAddress)
const {
80 if (Addr < TableAddress)
83 uintptr_t
Offset = Addr - TableAddress;
84 if (Offset >= getSectionHeaderSize() * getNumberOfSections())
87 if (Offset % getSectionHeaderSize() != 0)
89 "Section header pointer does not point to a valid section header.");
94 assert(!
is64Bit() &&
"32-bit interface called on 64-bit object file.");
96 checkSectionAddress(Ref.
p, getSectionHeaderTableAddress());
98 return viewAs<XCOFFSectionHeader32>(Ref.
p);
102 XCOFFObjectFile::toSection64(
DataRefImpl Ref)
const {
103 assert(
is64Bit() &&
"64-bit interface called on a 32-bit object file.");
105 checkSectionAddress(Ref.
p, getSectionHeaderTableAddress());
107 return viewAs<XCOFFSectionHeader64>(Ref.
p);
111 assert(!
is64Bit() &&
"Symbol table support not implemented for 64-bit.");
112 assert(Ref.
p != 0 &&
"Symbol table pointer can not be nullptr!");
114 checkSymbolEntryPointer(Ref.
p);
116 auto SymEntPtr = viewAs<XCOFFSymbolEntry>(Ref.
p);
121 assert(!
is64Bit() &&
"32-bit interface called on 64-bit object file.");
126 assert(
is64Bit() &&
"64-bit interface called on a 32-bit object file.");
131 XCOFFObjectFile::sectionHeaderTable32()
const {
132 assert(!
is64Bit() &&
"32-bit interface called on 64-bit object file.");
137 XCOFFObjectFile::sectionHeaderTable64()
const {
138 assert(
is64Bit() &&
"64-bit interface called on a 32-bit object file.");
148 if (reinterpret_cast<uintptr_t>(SymEntPtr) != getEndOfSymbolTableAddress())
149 checkSymbolEntryPointer(reinterpret_cast<uintptr_t>(SymEntPtr));
151 Symb.
p =
reinterpret_cast<uintptr_t
>(SymEntPtr);
164 if (StringTable.Data !=
nullptr && StringTable.Size > Offset)
165 return (StringTable.Data + Offset);
167 return make_error<GenericBinaryError>(
"Bad offset for string table entry",
185 return StringRef(
"Unimplemented Debug Name");
194 assert(!
is64Bit() &&
"Symbol table support not implemented for 64-bit.");
195 return toSymbolEntry(Symb)->Value;
199 assert(!
is64Bit() &&
"Symbol table support not implemented for 64-bit.");
200 return toSymbolEntry(Symb)->Value;
221 return section_end();
231 const char *Ptr =
reinterpret_cast<const char *
>(Sec.
p);
232 Sec.
p =
reinterpret_cast<uintptr_t
>(Ptr + getSectionHeaderSize());
243 return toSection64(Sec)->VirtualAddress;
245 return toSection32(Sec)->VirtualAddress;
252 return toSection64(Sec) - sectionHeaderTable64() + 1;
254 return toSection32(Sec) - sectionHeaderTable32() + 1;
261 return toSection64(Sec)->SectionSize;
263 return toSection32(Sec)->SectionSize;
268 if (isSectionVirtual(Sec))
271 uint64_t OffsetToRaw;
273 OffsetToRaw = toSection64(Sec)->FileOffsetToRawData;
275 OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
277 const uint8_t * ContentStart = base() + OffsetToRaw;
279 if (checkOffset(
Data, uintptr_t(ContentStart), SectionSize))
280 return make_error<BinaryError>();
312 return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0
313 : toSection32(Sec)->FileOffsetToRawData == 0;
363 SymDRI.
p =
reinterpret_cast<uintptr_t
>(SymbolTblPtr);
370 SymDRI.
p =
reinterpret_cast<uintptr_t
>(
371 SymbolTblPtr + getLogicalNumberOfSymbolTableEntries32());
377 DRI.
p = getSectionHeaderTableAddress();
384 getNumberOfSections() * getSectionHeaderSize());
391 return is64Bit() ?
"aix5coff64-rs6000" :
"aixcoff-rs6000";
414 size_t XCOFFObjectFile::getFileHeaderSize()
const {
418 size_t XCOFFObjectFile::getSectionHeaderSize()
const {
428 return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic;
432 if (Num <= 0 || Num > getNumberOfSections())
437 getSectionHeaderSize() * (Num - 1));
443 assert(!
is64Bit() &&
"Symbol table support not implemented for 64-bit.");
446 switch (SectionNum) {
457 getSectionNameInternal(SecRef.
get()));
462 bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
463 return (SectionNumber <= 0 && SectionNumber >= -2);
467 return is64Bit() ? fileHeader64()->NumberOfSections
468 : fileHeader32()->NumberOfSections;
472 return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp;
476 return is64Bit() ? fileHeader64()->AuxHeaderSize
477 : fileHeader32()->AuxHeaderSize;
481 return fileHeader32()->SymbolTableOffset;
488 return fileHeader32()->NumberOfSymTableEntries;
492 return (fileHeader32()->NumberOfSymTableEntries >= 0
493 ? fileHeader32()->NumberOfSymTableEntries
498 return fileHeader64()->SymbolTableOffset;
502 return fileHeader64()->NumberOfSymTableEntries;
505 uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress()
const {
507 is64Bit() ? getNumberOfSymbolTableEntries64()
508 : getLogicalNumberOfSymbolTableEntries32();
509 return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr),
514 if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr))
517 if (SymbolEntPtr >= getEndOfSymbolTableAddress())
520 ptrdiff_t Offset =
reinterpret_cast<const char *
>(SymbolEntPtr) -
521 reinterpret_cast<const char *>(SymbolTblPtr);
525 "Symbol table entry position is not valid inside of symbol table.");
529 return (reinterpret_cast<const char *>(SymbolEntPtr) -
530 reinterpret_cast<const char *>(SymbolTblPtr)) /
539 if (Index >= getLogicalNumberOfSymbolTableEntries32())
543 SymDRI.
p =
reinterpret_cast<uintptr_t
>(getPointerToSymbolTable() +
Index);
548 return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags;
551 const char *XCOFFObjectFile::getSectionNameInternal(
DataRefImpl Sec)
const {
552 return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name;
555 uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress()
const {
556 return reinterpret_cast<uintptr_t
>(SectionHeaderTable);
560 return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags;
569 assert(
is64Bit() &&
"64-bit interface called for non 64-bit file.");
572 TablePtr + getNumberOfSections());
576 assert(!
is64Bit() &&
"32-bit interface called for non 32-bit file.");
579 TablePtr + getNumberOfSections());
589 uint16_t SectionIndex = &Sec - sectionHeaderTable32() + 1;
593 for (
const auto &Sec : sections32()) {
603 uintptr_t RelocAddr =
getWithOffset(reinterpret_cast<uintptr_t>(FileHeader),
605 auto NumRelocEntriesOrErr = getLogicalNumberOfRelocationEntries(Sec);
606 if (
Error E = NumRelocEntriesOrErr.takeError())
609 uint32_t NumRelocEntries = NumRelocEntriesOrErr.get();
611 auto RelocationOrErr =
612 getObject<XCOFFRelocation32>(
Data,
reinterpret_cast<void *
>(RelocAddr),
614 if (
Error E = RelocationOrErr.takeError())
623 XCOFFObjectFile::parseStringTable(
const XCOFFObjectFile *Obj, uint64_t Offset) {
638 auto StringTableOrErr =
640 if (
Error E = StringTableOrErr.takeError())
643 const char *StringTablePtr = StringTableOrErr.get();
644 if (StringTablePtr[Size - 1] !=
'\0')
653 std::unique_ptr<XCOFFObjectFile> Obj;
656 uint64_t CurOffset = 0;
657 const auto *
Base = Obj->base();
661 auto FileHeaderOrErr =
662 getObject<void>(Data,
Base + CurOffset, Obj->getFileHeaderSize());
663 if (
Error E = FileHeaderOrErr.takeError())
665 Obj->FileHeader = FileHeaderOrErr.get();
667 CurOffset += Obj->getFileHeaderSize();
670 CurOffset += Obj->getOptionalHeaderSize();
673 if (Obj->getNumberOfSections()) {
674 auto SecHeadersOrErr = getObject<void>(Data,
Base + CurOffset,
675 Obj->getNumberOfSections() *
676 Obj->getSectionHeaderSize());
677 if (
Error E = SecHeadersOrErr.takeError())
679 Obj->SectionHeaderTable = SecHeadersOrErr.get();
684 return std::move(Obj);
687 if (Obj->getLogicalNumberOfSymbolTableEntries32() == 0)
688 return std::move(Obj);
691 CurOffset = Obj->fileHeader32()->SymbolTableOffset;
693 Obj->getLogicalNumberOfSymbolTableEntries32();
695 getObject<XCOFFSymbolEntry>(Data,
Base + CurOffset, SymbolTableSize);
696 if (
Error E = SymTableOrErr.takeError())
698 Obj->SymbolTblPtr = SymTableOrErr.get();
699 CurOffset += SymbolTableSize;
703 parseStringTable(Obj.get(), CurOffset);
706 Obj->StringTable = StringTableOrErr.
get();
708 return std::move(Obj);
714 return XCOFFObjectFile::create(FileType, MemBufRef);
718 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->StorageClass;
722 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->NumberOfAuxEntries;
726 assert(!OwningObjectPtr->is64Bit() &&
727 "32-bit interface called on 64-bit object file.");
728 assert(hasCsectAuxEnt() &&
"No Csect Auxiliary Entry is found.");
736 OwningObjectPtr->checkSymbolEntryPointer(AuxAddr);
743 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SymbolType;
747 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SectionNumber;
757 if (OwningObjectPtr->is64Bit())
763 if (!hasCsectAuxEnt())
775 int16_t SectNum = getSectionNumber();
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
bool isFixupIndicated() const
bool hasCsectAuxEnt() const
Expected< StringRef > getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const
uint64_t getRelocationType(DataRefImpl Rel) const override
bool isSectionData(DataRefImpl Sec) const override
void moveRelocationNext(DataRefImpl &Rel) const override
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
static constexpr uint8_t XR_BIASED_LENGTH_MASK
uint8_t SymbolAlignmentAndType
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
uint8_t getRelocatedLength() const
basic_symbol_iterator symbol_end() const override
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
XCOFF::StorageMappingClass StorageMappingClass
This class is the base class for all object file types.
static uint32_t getSectionFlags(const MachOObjectFile &O, DataRefImpl Sec)
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const uint8_t * base() const
Error takeError()
Take ownership of the stored error.
Expected< uint64_t > getStartAddress() const override
static Expected< std::unique_ptr< ObjectFile > > createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType)
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
uint32_t getSymbolFlags(DataRefImpl Symb) const override
ArrayRef< XCOFFSectionHeader64 > sections64() const
void moveSectionNext(DataRefImpl &Sec) const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
uint32_t getLogicalNumberOfSymbolTableEntries32() const
uint32_t read32be(const void *P)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
const XCOFFSymbolEntry * toSymbolEntry(DataRefImpl Ref) const
static StringRef getSymbolName(SymbolKind SymKind)
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
This is a value type class that represents a single relocation in the list of relocations in the obje...
static Expected< const T * > getObject(MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
The access may reference the value stored in memory.
uint64_t getRelocationOffset(DataRefImpl Rel) const override
static constexpr uint8_t XR_FIXUP_INDICATOR_MASK
Tagged union holding either a T or a Error.
section_iterator section_begin() const override
bool isReservedSectionNumber(int32_t SectionNumber)
int32_t getRawNumberOfSymbolTableEntries32() const
uint16_t getMagic() const
content_iterator< SectionRef > section_iterator
char SymbolName[XCOFF::NameSize]
uint16_t getOptionalHeaderSize() const
int16_t getSectionNumber() const
Expected< uint32_t > getLogicalNumberOfRelocationEntries(const XCOFFSectionHeader32 &Sec) const
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
SubtargetFeatures getFeatures() const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
void moveSymbolNext(DataRefImpl &Symb) const override
uint16_t getFlags() const
The instances of the Type class are immutable: once they are created, they are never changed...
uint64_t getSectionAddress(DataRefImpl Sec) const override
uint8_t getNumberOfAuxEntries() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
int32_t getSectionFlags(DataRefImpl Sec) const
uint32_t getNumberOfSymbolTableEntries64() const
bool isSectionText(DataRefImpl Sec) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
void checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const
Triple::ArchType getArch() const override
XCOFF::StorageClass StorageClass
basic_symbol_iterator symbol_begin() const override
NameInStrTblType NameInStrTbl
static constexpr uint8_t XR_SIGN_INDICATOR_MASK
const XCOFFCsectAuxEnt32 * getXCOFFCsectAuxEnt32() const
static wasm::ValType getType(const TargetRegisterClass *RC)
static const T * viewAs(uintptr_t in)
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
support::big16_t SectionNumber
uint64_t getSectionSize(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
section_iterator section_end() const override
FileType
Defines the file type this file represents.
XCOFF::StorageClass getStorageClass() const
CHAIN = SC CHAIN, Imm128 - System call.
bool isSectionCompressed(DataRefImpl Sec) const override
reference get()
Returns a reference to the stored T value.
bool isSectionBSS(DataRefImpl Sec) const override
Expected< ArrayRef< XCOFFRelocation32 > > relocations(const XCOFFSectionHeader32 &) const
content_iterator< BasicSymbolRef > basic_symbol_iterator
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
Manages the enabling and disabling of subtarget specific features.
uint64_t getSymbolTableOffset64() const
This is a value type class that represents a single symbol in the list of symbols in the object file...
Expected< StringRef > getSymbolNameByIndex(uint32_t SymbolTableIndex) const
uint32_t getSymbolTableOffset32() const
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
uint8_t NumberOfAuxEntries
bool isRelocationSigned() const
Expected< DataRefImpl > getSectionByNum(int16_t Num) const
static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset)
uint64_t getSectionIndex(DataRefImpl Sec) const override
uint16_t getNumberOfSections() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef getFileFormatName() const override
int32_t getTimeStamp() const
Lightweight error class with error context and mandatory checking.
content_iterator< RelocationRef > relocation_iterator
StringRef - Represent a constant reference to a string, i.e.
Expected< StringRef > getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const
char Name[XCOFF::NameSize+XCOFF::FileNamePadSize]
uint32_t getSymbolIndex(uintptr_t SymEntPtr) const
static StringRef generateXCOFFFixedNameStringRef(const char *Name)
static bool is64Bit(const char *name)
bool isSectionVirtual(DataRefImpl Sec) const override
NameInStrTblType NameInStrTbl
ArrayRef< XCOFFSectionHeader32 > sections32() const
This is a value type class that represents a single section in the list of sections in the object fil...