Object/Mach-O: Add header and load command information.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120198 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar
2010-11-27 07:19:41 +00:00
parent bce55776af
commit a956d8b717
4 changed files with 228 additions and 24 deletions

View File

@@ -22,6 +22,8 @@
#ifndef LLVM_OBJECT_MACHOFORMAT_H
#define LLVM_OBJECT_MACHOFORMAT_H
#include "llvm/System/DataTypes.h"
namespace llvm {
namespace object {
@@ -83,13 +85,6 @@ namespace mach {
/// Format information for Mach object files.
namespace macho {
/// \brief Constants for header magic field.
enum HeaderMagic {
HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
HM_Universal = 0xCAFEBABE ///< Universal object file
};
/// \brief Constants for structure sizes.
enum StructureSizes {
Header32Size = 28,
@@ -105,6 +100,29 @@ namespace macho {
RelocationInfoSize = 8
};
/// \brief Constants for header magic field.
enum HeaderMagic {
HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
HM_Universal = 0xCAFEBABE ///< Universal object file
};
/// \brief Header common to all Mach object files.
struct Header {
uint32_t Magic;
uint32_t CPUType;
uint32_t CPUSubtype;
uint32_t FileType;
uint32_t NumLoadCommands;
uint32_t SizeOfLoadCommands;
uint32_t Flags;
};
/// \brief Extended header for 64-bit object files.
struct Header64Ext {
uint32_t Reserved;
};
// See <mach-o/loader.h>.
enum HeaderFileType {
HFT_Object = 0x1
@@ -118,7 +136,14 @@ namespace macho {
LCT_Segment = 0x1,
LCT_Symtab = 0x2,
LCT_Dysymtab = 0xb,
LCT_Segment64 = 0x19
LCT_Segment64 = 0x19,
LCT_UUID = 0x1b
};
/// \brief Load command structure.
struct LoadCommand {
uint32_t Type;
uint32_t Size;
};
// See <mach-o/nlist.h>.

View File

@@ -12,6 +12,7 @@
#include <string>
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Object/MachOFormat.h"
namespace llvm {
@@ -40,6 +41,13 @@ namespace object {
// objects which are in the current address space.
class MachOObject {
public:
struct LoadCommandInfo {
/// The load command information.
macho::LoadCommand Command;
/// The offset to the start of the load command in memory.
uint64_t Offset;
};
private:
OwningPtr<MemoryBuffer> Buffer;
@@ -48,11 +56,23 @@ private:
bool IsLittleEndian;
/// Whether the object is 64-bit.
bool Is64Bit;
/// Whether the object is swapped endianness from the host.
bool IsSwappedEndian;
/// The cached information on the load commands.
LoadCommandInfo *LoadCommands;
mutable unsigned NumLoadedCommands;
/// The cached copy of the header.
macho::Header Header;
macho::Header64Ext Header64Ext;
private:
MachOObject(MemoryBuffer *Buffer, bool IsLittleEndian, bool Is64Bit);
public:
~MachOObject();
/// \brief Load a Mach-O object from a MemoryBuffer object.
///
/// \param Buffer - The buffer to load the object from. This routine takes
@@ -64,8 +84,33 @@ public:
static MachOObject *LoadFromBuffer(MemoryBuffer *Buffer,
std::string *ErrorStr = 0);
/// @name Object Header Information
/// @name File Information
/// @{
bool isLittleEndian() const { return IsLittleEndian; }
bool is64Bit() const { return Is64Bit; }
unsigned getHeaderSize() const {
return Is64Bit ? macho::Header64Size : macho::Header32Size;
}
/// @}
/// @name Object Header Access
/// @{
const macho::Header &getHeader() const { return Header; }
const macho::Header64Ext &getHeader64Ext() const {
assert(is64Bit() && "Invalid access!");
return Header64Ext;
}
/// @}
/// @name Object Structure Access
/// @{
/// \brief Retrieve the information for the given load command.
const LoadCommandInfo &getLoadCommandInfo(unsigned Index) const;
/// @}
};