[llvm-commits] [llvm] r120203 - in /llvm/trunk: include/llvm/Object/MachOFormat.h include/llvm/Object/MachOObject.h lib/Object/MachOObject.cpp tools/macho-dump/macho-dump.cpp
Daniel Dunbar
daniel at zuster.org
Sat Nov 27 00:22:29 PST 2010
Author: ddunbar
Date: Sat Nov 27 02:22:29 2010
New Revision: 120203
URL: http://llvm.org/viewvc/llvm-project?rev=120203&view=rev
Log:
macho-dump: Add support for dumping segment load commands.
Modified:
llvm/trunk/include/llvm/Object/MachOFormat.h
llvm/trunk/include/llvm/Object/MachOObject.h
llvm/trunk/lib/Object/MachOObject.cpp
llvm/trunk/tools/macho-dump/macho-dump.cpp
Modified: llvm/trunk/include/llvm/Object/MachOFormat.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachOFormat.h?rev=120203&r1=120202&r2=120203&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachOFormat.h (original)
+++ llvm/trunk/include/llvm/Object/MachOFormat.h Sat Nov 27 02:22:29 2010
@@ -146,6 +146,39 @@
uint32_t Size;
};
+ /// @name Load Command Structures
+ /// @{
+
+ struct SegmentLoadCommand {
+ uint32_t Type;
+ uint32_t Size;
+ char Name[16];
+ uint32_t VMAddress;
+ uint32_t VMSize;
+ uint32_t FileOffset;
+ uint32_t FileSize;
+ uint32_t MaxVMProtection;
+ uint32_t InitialVMProtection;
+ uint32_t NumSections;
+ uint32_t Flags;
+ };
+
+ struct Segment64LoadCommand {
+ uint32_t Type;
+ uint32_t Size;
+ char Name[16];
+ uint64_t VMAddress;
+ uint64_t VMSize;
+ uint64_t FileOffset;
+ uint64_t FileSize;
+ uint32_t MaxVMProtection;
+ uint32_t InitialVMProtection;
+ uint32_t NumSections;
+ uint32_t Flags;
+ };
+
+ /// @}
+
// See <mach-o/nlist.h>.
enum SymbolTypeType {
STT_Undefined = 0x00,
Modified: llvm/trunk/include/llvm/Object/MachOObject.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachOObject.h?rev=120203&r1=120202&r2=120203&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachOObject.h (original)
+++ llvm/trunk/include/llvm/Object/MachOObject.h Sat Nov 27 02:22:29 2010
@@ -11,6 +11,7 @@
#define LLVM_OBJECT_MACHOOBJECT_H
#include <string>
+#include "llvm/ADT/InMemoryStruct.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Object/MachOFormat.h"
@@ -88,6 +89,7 @@
/// @{
bool isLittleEndian() const { return IsLittleEndian; }
+ bool isSwappedEndian() const { return IsSwappedEndian; }
bool is64Bit() const { return Is64Bit; }
unsigned getHeaderSize() const {
@@ -111,6 +113,13 @@
/// \brief Retrieve the information for the given load command.
const LoadCommandInfo &getLoadCommandInfo(unsigned Index) const;
+ void ReadSegmentLoadCommand(
+ const LoadCommandInfo &LCI,
+ InMemoryStruct<macho::SegmentLoadCommand> &Res) const;
+ void ReadSegment64LoadCommand(
+ const LoadCommandInfo &LCI,
+ InMemoryStruct<macho::Segment64LoadCommand> &Res) const;
+
/// @}
};
Modified: llvm/trunk/lib/Object/MachOObject.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObject.cpp?rev=120203&r1=120202&r2=120203&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObject.cpp (original)
+++ llvm/trunk/lib/Object/MachOObject.cpp Sat Nov 27 02:22:29 2010
@@ -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 @@
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);
+}
Modified: llvm/trunk/tools/macho-dump/macho-dump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/macho-dump/macho-dump.cpp?rev=120203&r1=120202&r2=120203&view=diff
==============================================================================
--- llvm/trunk/tools/macho-dump/macho-dump.cpp (original)
+++ llvm/trunk/tools/macho-dump/macho-dump.cpp Sat Nov 27 02:22:29 2010
@@ -65,20 +65,73 @@
return 0;
}
+static void DumpSegmentCommandData(StringRef Name,
+ uint64_t VMAddr, uint64_t VMSize,
+ uint64_t FileOffset, uint64_t FileSize,
+ uint32_t MaxProt, uint32_t InitProt,
+ uint32_t NumSections, uint32_t Flags) {
+ outs() << " ('segment_name', '";
+ outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
+ outs() << " ('vm_addr', " << VMAddr << ")\n";
+ outs() << " ('vm_size', " << VMSize << ")\n";
+ outs() << " ('file_offset', " << FileOffset << ")\n";
+ outs() << " ('file_size', " << FileSize << ")\n";
+ outs() << " ('maxprot', " << MaxProt << ")\n";
+ outs() << " ('initprot', " << InitProt << ")\n";
+ outs() << " ('num_sections', " << NumSections << ")\n";
+ outs() << " ('flags', " << Flags << ")\n";
+}
+
+static int DumpSegmentCommand(MachOObject &Obj,
+ const MachOObject::LoadCommandInfo &LCI) {
+ InMemoryStruct<macho::SegmentLoadCommand> SLC;
+ Obj.ReadSegmentLoadCommand(LCI, SLC);
+ if (!SLC)
+ return Error("unable to read segment load command");
+
+ DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
+ SLC->FileOffset, SLC->FileSize,
+ SLC->MaxVMProtection, SLC->InitialVMProtection,
+ SLC->NumSections, SLC->Flags);
+
+ return 0;
+}
+static int DumpSegment64Command(MachOObject &Obj,
+ const MachOObject::LoadCommandInfo &LCI) {
+ InMemoryStruct<macho::Segment64LoadCommand> SLC;
+ Obj.ReadSegment64LoadCommand(LCI, SLC);
+ if (!SLC)
+ return Error("unable to read segment load command");
+
+ DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
+ SLC->FileOffset, SLC->FileSize,
+ SLC->MaxVMProtection, SLC->InitialVMProtection,
+ SLC->NumSections, SLC->Flags);
+
+ return 0;
+}
+
static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
+ int Res = 0;
outs() << " # Load Command " << Index << "\n"
<< " (('command', " << LCI.Command.Type << ")\n"
<< " ('size', " << LCI.Command.Size << ")\n";
switch (LCI.Command.Type) {
+ case macho::LCT_Segment:
+ Res = DumpSegmentCommand(Obj, LCI);
+ break;
+ case macho::LCT_Segment64:
+ Res = DumpSegment64Command(Obj, LCI);
+ break;
default:
Warning("unknown load command: " + Twine(LCI.Command.Type));
break;
}
outs() << " ),\n";
- return 0;
+ return Res;
}
int main(int argc, char **argv) {
@@ -104,10 +157,12 @@
return Res;
// Print the load commands.
+ int Res = 0;
outs() << "('load_commands', [\n";
for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
- DumpLoadCommand(*InputObject, i);
+ if ((Res = DumpLoadCommand(*InputObject, i)))
+ break;
outs() << "])\n";
- return 0;
+ return Res;
}
More information about the llvm-commits
mailing list