diff options
author | Daniel Dunbar <daniel@zuster.org> | 2010-11-27 06:39:22 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2010-11-27 06:39:22 +0000 |
commit | 95369163f58f06a6494ca9623184a8849ecf85f3 (patch) | |
tree | d4df3a0e8fcdb303818d09279421990cb2717383 | |
parent | 2a0e97431ecef2aa6a24a16ced207d5b53fcfc2d (diff) | |
download | external_llvm-95369163f58f06a6494ca9623184a8849ecf85f3.zip external_llvm-95369163f58f06a6494ca9623184a8849ecf85f3.tar.gz external_llvm-95369163f58f06a6494ca9623184a8849ecf85f3.tar.bz2 |
Object/Mach-O: Validate Mach-O magic and initialize format info.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120195 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/Object/MachOObject.h | 12 | ||||
-rw-r--r-- | lib/Object/MachOObject.cpp | 26 |
2 files changed, 33 insertions, 5 deletions
diff --git a/include/llvm/Object/MachOObject.h b/include/llvm/Object/MachOObject.h index 74339cb..71164ac 100644 --- a/include/llvm/Object/MachOObject.h +++ b/include/llvm/Object/MachOObject.h @@ -43,10 +43,16 @@ public: 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 diff --git a/lib/Object/MachOObject.cpp b/lib/Object/MachOObject.cpp index 5946863..de87605 100644 --- a/lib/Object/MachOObject.cpp +++ b/lib/Object/MachOObject.cpp @@ -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(); } |