[Object]
authorDavid Meyer <pdox@google.com>
Fri, 9 Mar 2012 20:41:57 +0000 (20:41 +0000)
committerDavid Meyer <pdox@google.com>
Fri, 9 Mar 2012 20:41:57 +0000 (20:41 +0000)
Make Binary::TypeID more granular, to distinguish between ELF 32/64 little/big

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152435 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/Archive.h
include/llvm/Object/Binary.h
include/llvm/Object/COFF.h
include/llvm/Object/ELF.h
include/llvm/Object/MachO.h
include/llvm/Object/ObjectFile.h
lib/Object/Archive.cpp
lib/Object/COFFObjectFile.cpp
lib/Object/MachOObjectFile.cpp

index e6df85679d4e487d27c9efe5091335e533d58d47..358b27a416cdf9adf6a7e82338bafd9651dbd5dd 100644 (file)
@@ -131,7 +131,7 @@ public:
   // Cast methods.
   static inline bool classof(Archive const *v) { return true; }
   static inline bool classof(Binary const *v) {
-    return v->getType() == Binary::isArchive;
+    return v->isArchive();
   }
 
 private:
index cd092fd8e485924d8ab9c6b02d06bffe7cb8ceec..81cdd32a73e3d32b2f8bf09b397dc4214e6766f2 100644 (file)
@@ -37,16 +37,25 @@ protected:
   Binary(unsigned int Type, MemoryBuffer *Source);
 
   enum {
-    isArchive,
-
+    ID_Archive,
     // Object and children.
-    isObject,
-    isCOFF,
-    isELF,
-    isMachO,
-    lastObject
+    ID_StartObjects,
+    ID_COFF,
+    ID_ELF32L, // ELF 32-bit, little endian
+    ID_ELF32B, // ELF 32-bit, big endian
+    ID_ELF64L, // ELF 64-bit, little endian
+    ID_ELF64B, // ELF 64-bit, big endian
+    ID_MachO,
+    ID_EndObjects
   };
 
+  static inline unsigned int getELFType(bool isLittleEndian, bool is64Bits) {
+    if (isLittleEndian)
+      return is64Bits ? ID_ELF64L : ID_ELF32L;
+    else
+      return is64Bits ? ID_ELF64B : ID_ELF32B;
+  }
+
 public:
   virtual ~Binary();
 
@@ -56,6 +65,27 @@ public:
   // Cast methods.
   unsigned int getType() const { return TypeID; }
   static inline bool classof(const Binary *v) { return true; }
+
+  // Convenience methods
+  bool isObject() const {
+    return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
+  }
+
+  bool isArchive() const {
+    return TypeID == ID_Archive;
+  }
+
+  bool isELF() const {
+    return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
+  }
+
+  bool isMachO() const {
+    return TypeID == ID_MachO;
+  }
+
+  bool isCOFF() const {
+    return TypeID == ID_COFF;
+  }
 };
 
 error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
index 4f90187895722a7d574443985c80a40fbf4cb19f..fc33ab1035827fe47fbab75a9139f6cc76ea442a 100644 (file)
@@ -179,7 +179,7 @@ public:
   error_code getSymbolName(const coff_symbol *symbol, StringRef &Res) const;
 
   static inline bool classof(const Binary *v) {
-    return v->getType() == isCOFF;
+    return v->isCOFF();
   }
   static inline bool classof(const COFFObjectFile *v) { return true; }
 };
index e746b0aaa55d2edb12a232dbd883b69fbe878756..e27a23edeb656aacdaad2fc23dce27c955b71e6d 100644 (file)
@@ -484,7 +484,8 @@ public:
   // Methods for type inquiry through isa, cast, and dyn_cast
   bool isDyldType() const { return isDyldELFObject; }
   static inline bool classof(const Binary *v) {
-    return v->getType() == Binary::isELF;
+    return v->getType() == getELFType(target_endianness == support::little,
+                                      is64Bits);
   }
   static inline bool classof(const ELFObjectFile *v) { return true; }
 };
@@ -1257,7 +1258,8 @@ void ELFObjectFile<target_endianness, is64Bits>
 template<support::endianness target_endianness, bool is64Bits>
 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
                                                           , error_code &ec)
-  : ObjectFile(Binary::isELF, Object, ec)
+  : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
+               Object, ec)
   , isDyldELFObject(false)
   , SectionHeaderTable(0)
   , dot_shstrtab_sec(0)
index 1aae85ab36c3c659252586e680d089638fbad6da..1e409b28568c87d89336d8e2d9e3ac2117f8f04f 100644 (file)
@@ -47,7 +47,7 @@ public:
   MachOObject *getObject() { return MachOObj; }
 
   static inline bool classof(const Binary *v) {
-    return v->getType() == isMachO;
+    return v->isMachO();
   }
   static inline bool classof(const MachOObjectFile *v) { return true; }
 
index 1e9d89549d631d4727eb5a41a5ef66ef06022550..09eb7fc75e65e58b1b0e1d15fd0b4c6ca563fe88 100644 (file)
@@ -372,8 +372,7 @@ public:
   static ObjectFile *createObjectFile(MemoryBuffer *Object);
 
   static inline bool classof(const Binary *v) {
-    return v->getType() >= isObject &&
-           v->getType() < lastObject;
+    return v->isObject();
   }
   static inline bool classof(const ObjectFile *v) { return true; }
 
index b67377c63416450b207a662cab1716f9626bd00e..c5f15bafcfba4048cc63478c88164e78d590776e 100644 (file)
@@ -174,7 +174,7 @@ error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
 }
 
 Archive::Archive(MemoryBuffer *source, error_code &ec)
-  : Binary(Binary::isArchive, source) {
+  : Binary(Binary::ID_Archive, source) {
   // Check for sufficient magic.
   if (!source || source->getBufferSize()
                  < (8 + sizeof(ArchiveMemberHeader) + 2) // Smallest archive.
index a3fdd5bb6a7eb6630fa2944de8ee087a39f48729..fe22242538286b640b5137624513a5d6abacf7e5 100644 (file)
@@ -421,7 +421,7 @@ relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
 }
 
 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
-  : ObjectFile(Binary::isCOFF, Object, ec)
+  : ObjectFile(Binary::ID_COFF, Object, ec)
   , Header(0)
   , SectionTable(0)
   , SymbolTable(0)
index 655c40aeda563374330c97937c29eddfc0056056..819409e3a1458e593a6d35f47cb66bccddb83ab7 100644 (file)
@@ -30,7 +30,7 @@ namespace object {
 
 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
                                  error_code &ec)
-    : ObjectFile(Binary::isMachO, Object, ec),
+    : ObjectFile(Binary::ID_MachO, Object, ec),
       MachOObj(MOO),
       RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
   DataRefImpl DRI;