[llvm] 10c4746 - [Alignment] Use Align for ObjectFile::getSectionAlignment

Guillaume Chatelet via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 9 01:35:12 PST 2022


Author: Guillaume Chatelet
Date: 2022-12-09T09:34:43Z
New Revision: 10c47465e2505ddfee4e62a2ab2e535abea3ec56

URL: https://github.com/llvm/llvm-project/commit/10c47465e2505ddfee4e62a2ab2e535abea3ec56
DIFF: https://github.com/llvm/llvm-project/commit/10c47465e2505ddfee4e62a2ab2e535abea3ec56.diff

LOG: [Alignment] Use Align for ObjectFile::getSectionAlignment

Differential Revision: https://reviews.llvm.org/D139098

Added: 
    

Modified: 
    llvm/include/llvm/Object/COFF.h
    llvm/include/llvm/Object/ELFObjectFile.h
    llvm/include/llvm/Object/MachO.h
    llvm/include/llvm/Object/ObjectFile.h
    llvm/include/llvm/Object/Wasm.h
    llvm/include/llvm/Object/XCOFFObjectFile.h
    llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
    llvm/lib/Object/COFFObjectFile.cpp
    llvm/lib/Object/MachOObjectFile.cpp
    llvm/lib/Object/WasmObjectFile.cpp
    llvm/lib/Object/XCOFFObjectFile.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h
index 8ad065d89c2a1..20faa54ec031c 100644
--- a/llvm/include/llvm/Object/COFF.h
+++ b/llvm/include/llvm/Object/COFF.h
@@ -456,18 +456,16 @@ struct coff_section {
            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 @@ class COFFObjectFile : public ObjectFile {
   uint64_t getSectionSize(DataRefImpl Sec) const override;
   Expected<ArrayRef<uint8_t>>
   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
index cbbec13f35c6c..5e90dc59c3caf 100644
--- a/llvm/include/llvm/Object/ELFObjectFile.h
+++ b/llvm/include/llvm/Object/ELFObjectFile.h
@@ -294,7 +294,7 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
   uint64_t getSectionSize(DataRefImpl Sec) const override;
   Expected<ArrayRef<uint8_t>>
   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 @@ ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
 }
 
 template <class ELFT>
-uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
-  return getSection(Sec)->sh_addralign;
+Align ELFObjectFile<ELFT>::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 <class ELFT>

diff  --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h
index 00e2fc67013e5..14dd8c21b0c53 100644
--- a/llvm/include/llvm/Object/MachO.h
+++ b/llvm/include/llvm/Object/MachO.h
@@ -446,7 +446,7 @@ class MachOObjectFile : public ObjectFile {
   ArrayRef<uint8_t> getSectionContents(uint32_t Offset, uint64_t Size) const;
   Expected<ArrayRef<uint8_t>>
   getSectionContents(DataRefImpl Sec) const override;
-  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
+  Align getSectionAlignment(DataRefImpl Sec) const override;
   Expected<SectionRef> getSection(unsigned SectionIndex) const;
   Expected<SectionRef> 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
index c936ff8bf13d1..3921776895c98 100644
--- a/llvm/include/llvm/Object/ObjectFile.h
+++ b/llvm/include/llvm/Object/ObjectFile.h
@@ -266,7 +266,7 @@ class ObjectFile : public SymbolicFile {
   virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
   virtual Expected<ArrayRef<uint8_t>>
   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;
@@ -482,7 +482,7 @@ inline Expected<StringRef> SectionRef::getContents() const {
 }
 
 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
index a26f703aeb43d..faf94ed513a83 100644
--- a/llvm/include/llvm/Object/Wasm.h
+++ b/llvm/include/llvm/Object/Wasm.h
@@ -181,7 +181,7 @@ class WasmObjectFile : public ObjectFile {
   uint64_t getSectionSize(DataRefImpl Sec) const override;
   Expected<ArrayRef<uint8_t>>
   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
index 9ef94f4420b5a..4efda9e361834 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -581,7 +581,7 @@ class XCOFFObjectFile : public ObjectFile {
   uint64_t getSectionSize(DataRefImpl Sec) const override;
   Expected<ArrayRef<uint8_t>>
   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
index 782928c260842..23ab82e17257e 100644
--- a/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
@@ -162,7 +162,7 @@ Error COFFLinkGraphBuilder::graphifySections() {
       B = &G->createZeroFillBlock(
           *GraphSec, getSectionSize(Obj, *Sec),
           orc::ExecutorAddr(getSectionAddress(Obj, *Sec)),
-          (*Sec)->getAlignment(), 0);
+          (*Sec)->getAlignment().value(), 0);
     else {
       ArrayRef<uint8_t> Data;
       if (auto Err = Obj.getSectionContents(*Sec, Data))
@@ -178,7 +178,7 @@ Error COFFLinkGraphBuilder::graphifySections() {
 
       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
index d0ca50e3506b3..a74ff39986cb7 100644
--- a/llvm/lib/Object/COFFObjectFile.cpp
+++ b/llvm/lib/Object/COFFObjectFile.cpp
@@ -299,9 +299,9 @@ COFFObjectFile::getSectionContents(DataRefImpl Ref) const {
   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
index 39e84fffcc4c6..7f90284c8747c 100644
--- a/llvm/lib/Object/MachOObjectFile.cpp
+++ b/llvm/lib/Object/MachOObjectFile.cpp
@@ -1990,17 +1990,10 @@ MachOObjectFile::getSectionContents(DataRefImpl Sec) const {
   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<SectionRef> MachOObjectFile::getSection(unsigned SectionIndex) const {

diff  --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp
index 0e24ac94bf70c..6657102bf4797 100644
--- a/llvm/lib/Object/WasmObjectFile.cpp
+++ b/llvm/lib/Object/WasmObjectFile.cpp
@@ -1735,8 +1735,8 @@ WasmObjectFile::getSectionContents(DataRefImpl Sec) const {
   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
index 061c47d3eb56d..c6a571bcd8607 100644
--- a/llvm/lib/Object/XCOFFObjectFile.cpp
+++ b/llvm/lib/Object/XCOFFObjectFile.cpp
@@ -417,10 +417,9 @@ XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
   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 {


        


More information about the llvm-commits mailing list