2010-11-27 05:38:50 +00:00
|
|
|
//===- MachOObject.h - Mach-O Object File Wrapper ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_OBJECT_MACHOOBJECT_H
|
|
|
|
#define LLVM_OBJECT_MACHOOBJECT_H
|
|
|
|
|
|
|
|
#include <string>
|
2010-11-27 08:22:29 +00:00
|
|
|
#include "llvm/ADT/InMemoryStruct.h"
|
2010-11-27 05:38:50 +00:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2010-11-27 13:46:11 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2010-11-27 07:19:41 +00:00
|
|
|
#include "llvm/Object/MachOFormat.h"
|
2010-11-27 05:38:50 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class MemoryBuffer;
|
|
|
|
|
|
|
|
namespace object {
|
|
|
|
|
|
|
|
/// \brief Wrapper object for manipulating Mach-O object files.
|
|
|
|
///
|
|
|
|
/// This class is designed to implement a full-featured, efficient, portable,
|
|
|
|
/// and robust Mach-O interface to Mach-O object files. It does not attempt to
|
|
|
|
/// smooth over rough edges in the Mach-O format or generalize access to object
|
|
|
|
/// independent features.
|
|
|
|
///
|
|
|
|
/// The class is designed around accessing the Mach-O object which is expected
|
|
|
|
/// to be fully loaded into memory.
|
|
|
|
///
|
|
|
|
/// This class is *not* suitable for concurrent use. For efficient operation,
|
|
|
|
/// the class uses APIs which rely on the ability to cache the results of
|
|
|
|
/// certain calls in internal objects which are not safe for concurrent
|
|
|
|
/// access. This allows the API to be zero-copy on the common paths.
|
|
|
|
//
|
|
|
|
// FIXME: It would be cool if we supported a "paged" MemoryBuffer
|
|
|
|
// implementation. This would allow us to implement a more sensible version of
|
|
|
|
// MemoryObject which can work like a MemoryBuffer, but be more efficient for
|
|
|
|
// objects which are in the current address space.
|
|
|
|
class MachOObject {
|
|
|
|
public:
|
2010-11-27 07:19:41 +00:00
|
|
|
struct LoadCommandInfo {
|
|
|
|
/// The load command information.
|
|
|
|
macho::LoadCommand Command;
|
|
|
|
|
|
|
|
/// The offset to the start of the load command in memory.
|
|
|
|
uint64_t Offset;
|
|
|
|
};
|
2010-11-27 05:38:50 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
OwningPtr<MemoryBuffer> Buffer;
|
|
|
|
|
2010-11-27 06:39:22 +00:00
|
|
|
/// Whether the object is little endian.
|
|
|
|
bool IsLittleEndian;
|
|
|
|
/// Whether the object is 64-bit.
|
|
|
|
bool Is64Bit;
|
2010-11-27 07:19:41 +00:00
|
|
|
/// Whether the object is swapped endianness from the host.
|
|
|
|
bool IsSwappedEndian;
|
2010-11-27 13:46:11 +00:00
|
|
|
/// Whether the string table has been registered.
|
|
|
|
bool HasStringTable;
|
2010-11-27 07:19:41 +00:00
|
|
|
|
|
|
|
/// The cached information on the load commands.
|
|
|
|
LoadCommandInfo *LoadCommands;
|
|
|
|
mutable unsigned NumLoadedCommands;
|
|
|
|
|
|
|
|
/// The cached copy of the header.
|
|
|
|
macho::Header Header;
|
|
|
|
macho::Header64Ext Header64Ext;
|
2010-11-27 06:39:22 +00:00
|
|
|
|
2010-11-27 13:46:11 +00:00
|
|
|
/// Cache string table information.
|
|
|
|
StringRef StringTable;
|
|
|
|
|
2010-11-27 06:39:22 +00:00
|
|
|
private:
|
|
|
|
MachOObject(MemoryBuffer *Buffer, bool IsLittleEndian, bool Is64Bit);
|
|
|
|
|
|
|
|
public:
|
2010-11-27 07:19:41 +00:00
|
|
|
~MachOObject();
|
|
|
|
|
2010-11-27 05:38:50 +00:00
|
|
|
/// \brief Load a Mach-O object from a MemoryBuffer object.
|
|
|
|
///
|
|
|
|
/// \param Buffer - The buffer to load the object from. This routine takes
|
|
|
|
/// exclusive ownership of the buffer (which is passed to the returned object
|
|
|
|
/// on success).
|
|
|
|
/// \param ErrorStr [out] - If given, will be set to a user readable error
|
|
|
|
/// message on failure.
|
|
|
|
/// \returns The loaded object, or null on error.
|
|
|
|
static MachOObject *LoadFromBuffer(MemoryBuffer *Buffer,
|
|
|
|
std::string *ErrorStr = 0);
|
|
|
|
|
2010-11-27 07:19:41 +00:00
|
|
|
/// @name File Information
|
2010-11-27 05:38:50 +00:00
|
|
|
/// @{
|
2010-11-27 07:19:41 +00:00
|
|
|
|
|
|
|
bool isLittleEndian() const { return IsLittleEndian; }
|
2010-11-27 08:22:29 +00:00
|
|
|
bool isSwappedEndian() const { return IsSwappedEndian; }
|
2010-11-27 07:19:41 +00:00
|
|
|
bool is64Bit() const { return Is64Bit; }
|
|
|
|
|
|
|
|
unsigned getHeaderSize() const {
|
|
|
|
return Is64Bit ? macho::Header64Size : macho::Header32Size;
|
|
|
|
}
|
|
|
|
|
2010-11-27 13:58:16 +00:00
|
|
|
StringRef getData(size_t Offset, size_t Size) const;
|
|
|
|
|
2010-11-27 13:46:11 +00:00
|
|
|
/// @}
|
|
|
|
/// @name String Table Data
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
StringRef getStringTableData() const {
|
|
|
|
assert(HasStringTable && "String table has not been registered!");
|
|
|
|
return StringTable;
|
|
|
|
}
|
|
|
|
|
2010-11-27 13:52:53 +00:00
|
|
|
StringRef getStringAtIndex(unsigned Index) const {
|
|
|
|
size_t End = getStringTableData().find('\0', Index);
|
|
|
|
return getStringTableData().slice(Index, End);
|
|
|
|
}
|
|
|
|
|
2010-11-27 13:46:11 +00:00
|
|
|
void RegisterStringTable(macho::SymtabLoadCommand &SLC);
|
|
|
|
|
2010-11-27 07:19:41 +00:00
|
|
|
/// @}
|
|
|
|
/// @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;
|
|
|
|
|
2010-11-27 08:22:29 +00:00
|
|
|
void ReadSegmentLoadCommand(
|
|
|
|
const LoadCommandInfo &LCI,
|
|
|
|
InMemoryStruct<macho::SegmentLoadCommand> &Res) const;
|
|
|
|
void ReadSegment64LoadCommand(
|
|
|
|
const LoadCommandInfo &LCI,
|
|
|
|
InMemoryStruct<macho::Segment64LoadCommand> &Res) const;
|
2010-11-27 08:33:44 +00:00
|
|
|
void ReadSymtabLoadCommand(
|
|
|
|
const LoadCommandInfo &LCI,
|
|
|
|
InMemoryStruct<macho::SymtabLoadCommand> &Res) const;
|
|
|
|
void ReadDysymtabLoadCommand(
|
|
|
|
const LoadCommandInfo &LCI,
|
|
|
|
InMemoryStruct<macho::DysymtabLoadCommand> &Res) const;
|
2010-11-27 13:26:12 +00:00
|
|
|
void ReadIndirectSymbolTableEntry(
|
|
|
|
const macho::DysymtabLoadCommand &DLC,
|
|
|
|
unsigned Index,
|
|
|
|
InMemoryStruct<macho::IndirectSymbolTableEntry> &Res) const;
|
2010-11-27 13:33:15 +00:00
|
|
|
void ReadSection(
|
|
|
|
const LoadCommandInfo &LCI,
|
|
|
|
unsigned Index,
|
|
|
|
InMemoryStruct<macho::Section> &Res) const;
|
|
|
|
void ReadSection64(
|
|
|
|
const LoadCommandInfo &LCI,
|
|
|
|
unsigned Index,
|
|
|
|
InMemoryStruct<macho::Section64> &Res) const;
|
2010-11-27 13:39:48 +00:00
|
|
|
void ReadRelocationEntry(
|
|
|
|
uint64_t RelocationTableOffset, unsigned Index,
|
|
|
|
InMemoryStruct<macho::RelocationEntry> &Res) const;
|
2010-11-27 13:52:53 +00:00
|
|
|
void ReadSymbolTableEntry(
|
|
|
|
uint64_t SymbolTableOffset, unsigned Index,
|
|
|
|
InMemoryStruct<macho::SymbolTableEntry> &Res) const;
|
|
|
|
void ReadSymbol64TableEntry(
|
|
|
|
uint64_t SymbolTableOffset, unsigned Index,
|
|
|
|
InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
|
2010-11-27 08:22:29 +00:00
|
|
|
|
2010-11-27 05:38:50 +00:00
|
|
|
/// @}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace object
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|