summaryrefslogtreecommitdiff
path: root/include/llvm/Object/MachOObject.h
blob: 49007686812f533eff691f9bcbf0d602355aae9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===- 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>
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Object/MachOFormat.h"

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:
  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;

  /// Whether the object is little endian.
  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
  /// 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);

  /// @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;

  /// @}
};

} // end namespace object
} // end namespace llvm

#endif