[llvm] r263722 - [COFF] Refactor section alignment calculation
David Majnemer via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 17 09:55:18 PDT 2016
Author: majnemer
Date: Thu Mar 17 11:55:18 2016
New Revision: 263722
URL: http://llvm.org/viewvc/llvm-project?rev=263722&view=rev
Log:
[COFF] Refactor section alignment calculation
Section alignment isn't completely trivial, let it live in one place so
that we may reuse it in LLVM.
Modified:
llvm/trunk/include/llvm/Object/COFF.h
llvm/trunk/lib/Object/COFFObjectFile.cpp
Modified: llvm/trunk/include/llvm/Object/COFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFF.h?rev=263722&r1=263721&r2=263722&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFF.h (original)
+++ llvm/trunk/include/llvm/Object/COFF.h Thu Mar 17 11:55:18 2016
@@ -414,6 +414,18 @@ struct coff_section {
return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) &&
NumberOfRelocations == UINT16_MAX;
}
+ uint32_t 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;
+
+ // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1.
+ uint32_t Shift = (Characteristics >> 20) & 0xF;
+ if (Shift > 0)
+ return 1U << (Shift - 1);
+ return 1;
+ }
};
struct coff_relocation {
@@ -494,8 +506,11 @@ struct coff_tls_directory {
support::ulittle32_t SizeOfZeroFill;
support::ulittle32_t Characteristics;
uint32_t getAlignment() const {
- uint32_t AlignBit = (Characteristics & 0x00F00000) >> 20;
- return AlignBit ? 1 << (AlignBit - 1) : 0;
+ // Bit [20:24] contains section alignment.
+ uint32_t Shift = (Characteristics & 0x00F00000) >> 20;
+ if (Shift > 0)
+ return 1U << (Shift - 1);
+ return 0;
}
};
Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=263722&r1=263721&r2=263722&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Thu Mar 17 11:55:18 2016
@@ -290,7 +290,7 @@ std::error_code COFFObjectFile::getSecti
uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
const coff_section *Sec = toSec(Ref);
- return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
+ return Sec->getAlignment();
}
bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
More information about the llvm-commits
mailing list