[llvm-commits] [llvm] r120195 - in /llvm/trunk: include/llvm/Object/MachOObject.h lib/Object/MachOObject.cpp

Daniel Dunbar daniel at zuster.org
Fri Nov 26 22:39:22 PST 2010


Author: ddunbar
Date: Sat Nov 27 00:39:22 2010
New Revision: 120195

URL: http://llvm.org/viewvc/llvm-project?rev=120195&view=rev
Log:
Object/Mach-O: Validate Mach-O magic and initialize format info.

Modified:
    llvm/trunk/include/llvm/Object/MachOObject.h
    llvm/trunk/lib/Object/MachOObject.cpp

Modified: llvm/trunk/include/llvm/Object/MachOObject.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachOObject.h?rev=120195&r1=120194&r2=120195&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachOObject.h (original)
+++ llvm/trunk/include/llvm/Object/MachOObject.h Sat Nov 27 00:39:22 2010
@@ -43,10 +43,16 @@
 
 private:
   OwningPtr<MemoryBuffer> Buffer;
-  
-public:
-  MachOObject(MemoryBuffer *Buffer);
 
+  /// Whether the object is little endian.
+  bool IsLittleEndian;
+  /// Whether the object is 64-bit.
+  bool Is64Bit;
+
+private:
+  MachOObject(MemoryBuffer *Buffer, bool IsLittleEndian, bool Is64Bit);
+
+public:
   /// \brief Load a Mach-O object from a MemoryBuffer object.
   ///
   /// \param Buffer - The buffer to load the object from. This routine takes

Modified: llvm/trunk/lib/Object/MachOObject.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObject.cpp?rev=120195&r1=120194&r2=120195&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObject.cpp (original)
+++ llvm/trunk/lib/Object/MachOObject.cpp Sat Nov 27 00:39:22 2010
@@ -8,16 +8,38 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Object/MachOObject.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/MemoryBuffer.h"
 
 using namespace llvm;
 using namespace object;
 
-MachOObject::MachOObject(MemoryBuffer *Buffer_) : Buffer(Buffer_) {
+MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
+                         bool Is64Bit_)
+  : Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_) {
 }
 
 MachOObject *MachOObject::LoadFromBuffer(MemoryBuffer *Buffer,
                                          std::string *ErrorStr) {
+  // First, check the magic value and initialize the basic object info.
+  bool IsLittleEndian = false, Is64Bit = false;
+  StringRef Magic = Buffer->getBuffer().slice(0, 4);
+  if (Magic == "\xFE\xED\xFA\xCE") {
+  }  else if (Magic == "\xCE\xFA\xED\xFE") {
+    IsLittleEndian = true;
+  } else if (Magic == "\xFE\xED\xFA\xCF") {
+    Is64Bit = true;
+  } else if (Magic == "\xCF\xFA\xED\xFE") {
+    IsLittleEndian = true;
+    Is64Bit = true;
+  } else {
+    *ErrorStr = "not a Mach object file";
+    return 0;
+  }
+
+  OwningPtr<MachOObject> Object(new MachOObject(Buffer, IsLittleEndian,
+                                                Is64Bit));
+
   if (ErrorStr) *ErrorStr = "";
-  return new MachOObject(Buffer);
+  return Object.take();
 }





More information about the llvm-commits mailing list