summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Object/MachOObject.cpp
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2010-11-27 08:22:29 +0000
committerDaniel Dunbar <daniel@zuster.org>2010-11-27 08:22:29 +0000
commita8070e02845f549083a7cdad64c054a228d7f26a (patch)
treed1f667a967c932e39d42bdb9988ed62c779d2c59 /llvm/lib/Object/MachOObject.cpp
parent8ba5f39f70ac4fd59ec56fddee592d17542550e3 (diff)
downloadbcm5719-llvm-a8070e02845f549083a7cdad64c054a228d7f26a.tar.gz
bcm5719-llvm-a8070e02845f549083a7cdad64c054a228d7f26a.zip
macho-dump: Add support for dumping segment load commands.
llvm-svn: 120203
Diffstat (limited to 'llvm/lib/Object/MachOObject.cpp')
-rw-r--r--llvm/lib/Object/MachOObject.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/llvm/lib/Object/MachOObject.cpp b/llvm/lib/Object/MachOObject.cpp
index 5c17dfcfe9c..ce817d6cac2 100644
--- a/llvm/lib/Object/MachOObject.cpp
+++ b/llvm/lib/Object/MachOObject.cpp
@@ -16,11 +16,43 @@
using namespace llvm;
using namespace llvm::object;
+/* Translation Utilities */
+
template<typename T>
static void SwapValue(T &Value) {
Value = sys::SwapByteOrder(Value);
}
+template<typename T>
+static void SwapStruct(T &Value);
+
+template<typename T>
+static void ReadInMemoryStruct(const MachOObject &MOO,
+ StringRef Buffer, uint64_t Base,
+ InMemoryStruct<T> &Res) {
+ typedef T struct_type;
+ uint64_t Size = sizeof(struct_type);
+
+ // Check that the buffer contains the expected data.
+ if (Base + Size > Buffer.size()) {
+ Res = 0;
+ return;
+ }
+
+ // Check whether we can return a direct pointer.
+ struct_type *Ptr = (struct_type *) (Buffer.data() + Base);
+ if (!MOO.isSwappedEndian()) {
+ Res = Ptr;
+ return;
+ }
+
+ // Otherwise, copy the struct and translate the values.
+ Res = *Ptr;
+ SwapStruct(*Res);
+}
+
+/* *** */
+
MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
bool Is64Bit_)
: Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_),
@@ -120,3 +152,39 @@ MachOObject::getLoadCommandInfo(unsigned Index) const {
return LoadCommands[Index];
}
+
+template<>
+static void SwapStruct(macho::SegmentLoadCommand &Value) {
+ SwapValue(Value.Type);
+ SwapValue(Value.Size);
+ SwapValue(Value.VMAddress);
+ SwapValue(Value.VMSize);
+ SwapValue(Value.FileOffset);
+ SwapValue(Value.FileSize);
+ SwapValue(Value.MaxVMProtection);
+ SwapValue(Value.InitialVMProtection);
+ SwapValue(Value.NumSections);
+ SwapValue(Value.Flags);
+}
+void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI,
+ InMemoryStruct<macho::SegmentLoadCommand> &Res) const {
+ ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
+}
+
+template<>
+static void SwapStruct(macho::Segment64LoadCommand &Value) {
+ SwapValue(Value.Type);
+ SwapValue(Value.Size);
+ SwapValue(Value.VMAddress);
+ SwapValue(Value.VMSize);
+ SwapValue(Value.FileOffset);
+ SwapValue(Value.FileSize);
+ SwapValue(Value.MaxVMProtection);
+ SwapValue(Value.InitialVMProtection);
+ SwapValue(Value.NumSections);
+ SwapValue(Value.Flags);
+}
+void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI,
+ InMemoryStruct<macho::Segment64LoadCommand> &Res) const {
+ ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
+}
OpenPOWER on IntegriCloud