Object: add getSectionAlignment.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Mon, 10 Oct 2011 21:55:43 +0000 (21:55 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Mon, 10 Oct 2011 21:55:43 +0000 (21:55 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141581 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/COFF.h
include/llvm/Object/ObjectFile.h
lib/Object/COFFObjectFile.cpp
lib/Object/ELFObjectFile.cpp
lib/Object/MachOObjectFile.cpp

index 9d83e287e48cbb83ec63fd81a41b98ef5540cc52..067bcd471ae98800722c739b434d41eb7289c5ca 100644 (file)
@@ -107,6 +107,7 @@ protected:
   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
+  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
index 1390a7ed1e024be0cfb52a3f52f00922a5fa09d5..84ef8632cd449e030b31e848d20a0895992c5d0e 100644 (file)
@@ -181,6 +181,9 @@ public:
   error_code getSize(uint64_t &Result) const;
   error_code getContents(StringRef &Result) const;
 
+  /// @brief Get the alignment of this section as the actual value (not log 2).
+  error_code getAlignment(uint64_t &Result) const;
+
   // FIXME: Move to the normalization layer when it's created.
   error_code isText(bool &Result) const;
   error_code isData(bool &Result) const;
@@ -236,6 +239,7 @@ protected:
   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
+  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0;
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
@@ -372,6 +376,10 @@ inline error_code SectionRef::getContents(StringRef &Result) const {
   return OwningObject->getSectionContents(SectionPimpl, Result);
 }
 
+inline error_code SectionRef::getAlignment(uint64_t &Result) const {
+  return OwningObject->getSectionAlignment(SectionPimpl, Result);
+}
+
 inline error_code SectionRef::isText(bool &Result) const {
   return OwningObject->isSectionText(SectionPimpl, Result);
 }
index 91492db2f0d6726be7443a774ce33af14ae6b63b..e84eb914fc749a20588f90bf7c55d5d0787ac3b0 100644 (file)
@@ -336,6 +336,15 @@ error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
   return object_error::success;
 }
 
+error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
+                                               uint64_t &Res) const {
+  const coff_section *sec = toSec(Sec);
+  if (!sec)
+    return object_error::parse_failed;
+  Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
+  return object_error::success;
+}
+
 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
                                          bool &Result) const {
   const coff_section *sec = toSec(Sec);
index 6a6870a4e5c7990cb4462ac7d4056512c2e005ba..16e9d14ce607e9d1c9eb4a17add99316f9b80a86 100644 (file)
@@ -337,6 +337,7 @@ protected:
   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
+  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
@@ -671,6 +672,15 @@ error_code ELFObjectFile<target_endianness, is64Bits>
   return object_error::success;
 }
 
+template<support::endianness target_endianness, bool is64Bits>
+error_code ELFObjectFile<target_endianness, is64Bits>
+                        ::getSectionAlignment(DataRefImpl Sec,
+                                              uint64_t &Result) const {
+  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
+  Result = sec->sh_addralign;
+  return object_error::success;
+}
+
 template<support::endianness target_endianness, bool is64Bits>
 error_code ELFObjectFile<target_endianness, is64Bits>
                         ::isSectionText(DataRefImpl Sec,
index 73fa8275c30f732726cd4099de5c653b82c0c75e..52b27b6b753e55013434c1aa6cf9cd29686b92a0 100644 (file)
@@ -60,6 +60,7 @@ protected:
   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
+  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
@@ -481,6 +482,20 @@ error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
   return object_error::success;
 }
 
+error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
+                                                uint64_t &Result) const {
+  if (is64BitLoadCommand(MachOObj, DRI)) {
+    InMemoryStruct<macho::Section64> Sect;
+    getSection64(DRI, Sect);
+    Result = 1 << Sect->Align;
+  } else {
+    InMemoryStruct<macho::Section> Sect;
+    getSection(DRI, Sect);
+    Result = 1 << Sect->Align;
+  }
+  return object_error::success;
+}
+
 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
                                           bool &Result) const {
   if (is64BitLoadCommand(MachOObj, DRI)) {