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 @@ -456,18 +456,16 @@ NumberOfRelocations == UINT16_MAX; } - uint32_t getAlignment() const { + Align getAlignment() const { // The IMAGE_SCN_TYPE_NO_PAD bit is a legacy way of getting to // IMAGE_SCN_ALIGN_1BYTES. if (Characteristics & COFF::IMAGE_SCN_TYPE_NO_PAD) - return 1; + return Align(1); // Bit [20:24] contains section alignment. 0 means use a default alignment // of 16. - uint32_t Shift = (Characteristics >> 20) & 0xF; - if (Shift > 0) - return 1U << (Shift - 1); - return 16; + uint32_t EncodedLogValue = (Characteristics >> 20) & 0xF; + return decodeMaybeAlign(EncodedLogValue).value_or(Align(16)); } }; @@ -946,7 +944,7 @@ uint64_t getSectionSize(DataRefImpl Sec) const override; Expected> getSectionContents(DataRefImpl Sec) const override; - uint64_t getSectionAlignment(DataRefImpl Sec) const override; + Align getSectionAlignment(DataRefImpl Sec) const override; bool isSectionCompressed(DataRefImpl Sec) const override; bool isSectionText(DataRefImpl Sec) const override; bool isSectionData(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 @@ -294,7 +294,7 @@ uint64_t getSectionSize(DataRefImpl Sec) const override; Expected> getSectionContents(DataRefImpl Sec) const override; - uint64_t getSectionAlignment(DataRefImpl Sec) const override; + Align getSectionAlignment(DataRefImpl Sec) const override; bool isSectionCompressed(DataRefImpl Sec) const override; bool isSectionText(DataRefImpl Sec) const override; bool isSectionData(DataRefImpl Sec) const override; @@ -870,8 +870,10 @@ } template -uint64_t ELFObjectFile::getSectionAlignment(DataRefImpl Sec) const { - return getSection(Sec)->sh_addralign; +Align ELFObjectFile::getSectionAlignment(DataRefImpl Sec) const { + // The value 0 or 1 means that the section has no alignment constraints. + // https://man7.org/linux/man-pages/man5/elf.5.html + return MaybeAlign(getSection(Sec)->sh_addralign).valueOrOne(); } template 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 @@ -446,7 +446,7 @@ ArrayRef getSectionContents(uint32_t Offset, uint64_t Size) const; Expected> getSectionContents(DataRefImpl Sec) const override; - uint64_t getSectionAlignment(DataRefImpl Sec) const override; + Align getSectionAlignment(DataRefImpl Sec) const override; Expected getSection(unsigned SectionIndex) const; Expected getSection(StringRef SectionName) const; bool isSectionCompressed(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 @@ -266,7 +266,7 @@ virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0; virtual Expected> getSectionContents(DataRefImpl Sec) const = 0; - virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0; + virtual Align getSectionAlignment(DataRefImpl Sec) const = 0; virtual bool isSectionCompressed(DataRefImpl Sec) const = 0; virtual bool isSectionText(DataRefImpl Sec) const = 0; virtual bool isSectionData(DataRefImpl Sec) const = 0; @@ -480,7 +480,7 @@ } inline uint64_t SectionRef::getAlignment() const { - return OwningObject->getSectionAlignment(SectionPimpl); + return OwningObject->getSectionAlignment(SectionPimpl).value(); } inline bool SectionRef::isCompressed() const { 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 @@ -181,7 +181,7 @@ uint64_t getSectionSize(DataRefImpl Sec) const override; Expected> getSectionContents(DataRefImpl Sec) const override; - uint64_t getSectionAlignment(DataRefImpl Sec) const override; + Align getSectionAlignment(DataRefImpl Sec) const override; bool isSectionCompressed(DataRefImpl Sec) const override; bool isSectionText(DataRefImpl Sec) const override; bool isSectionData(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 @@ -581,7 +581,7 @@ uint64_t getSectionSize(DataRefImpl Sec) const override; Expected> getSectionContents(DataRefImpl Sec) const override; - uint64_t getSectionAlignment(DataRefImpl Sec) const override; + Align getSectionAlignment(DataRefImpl Sec) const override; bool isSectionCompressed(DataRefImpl Sec) const override; bool isSectionText(DataRefImpl Sec) const override; bool isSectionData(DataRefImpl Sec) const override; diff --git a/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp --- a/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp @@ -162,7 +162,7 @@ B = &G->createZeroFillBlock( *GraphSec, getSectionSize(Obj, *Sec), orc::ExecutorAddr(getSectionAddress(Obj, *Sec)), - (*Sec)->getAlignment(), 0); + (*Sec)->getAlignment().value(), 0); else { ArrayRef Data; if (auto Err = Obj.getSectionContents(*Sec, Data)) @@ -178,7 +178,7 @@ B = &G->createContentBlock( *GraphSec, CharData, orc::ExecutorAddr(getSectionAddress(Obj, *Sec)), - (*Sec)->getAlignment(), 0); + (*Sec)->getAlignment().value(), 0); } setGraphBlock(SecIndex, B); 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 @@ -299,9 +299,9 @@ return Res; } -uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const { +Align COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const { const coff_section *Sec = toSec(Ref); - return Sec->getAlignment(); + return Align(Sec->getAlignment()); } bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { 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 @@ -1990,17 +1990,10 @@ return getSectionContents(Offset, Size); } -uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const { - uint32_t Align; - if (is64Bit()) { - MachO::section_64 Sect = getSection64(Sec); - Align = Sect.align; - } else { - MachO::section Sect = getSection(Sec); - Align = Sect.align; - } +Align MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const { - return uint64_t(1) << Align; + return is64Bit() ? Align(1ULL << getSection64(Sec).align) + : Align(1ULL << getSection(Sec).align); } Expected MachOObjectFile::getSection(unsigned SectionIndex) const { 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 @@ -1735,8 +1735,8 @@ return S.Content; } -uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { - return 1; +Align WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { + return Align(1); } bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { 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 @@ -417,10 +417,9 @@ return makeArrayRef(ContentStart,SectionSize); } -uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { - uint64_t Result = 0; +Align XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { llvm_unreachable("Not yet implemented!"); - return Result; + return {}; } uint64_t XCOFFObjectFile::getSectionFileOffsetToRawData(DataRefImpl Sec) const {