1 //===-- ObjectFileMachO.h ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_ObjectFileMachO_h_
11 #define liblldb_ObjectFileMachO_h_
12 
13 #include "llvm/Support/MachO.h"
14 
15 #include "lldb/Core/Address.h"
16 #include "lldb/Core/RangeMap.h"
17 #include "lldb/Host/FileSpec.h"
18 #include "lldb/Host/Mutex.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 
21 //----------------------------------------------------------------------
22 // This class needs to be hidden as eventually belongs in a plugin that
23 // will export the ObjectFile protocol
24 //----------------------------------------------------------------------
25 class ObjectFileMachO :
26     public lldb_private::ObjectFile
27 {
28 public:
29     //------------------------------------------------------------------
30     // Static Functions
31     //------------------------------------------------------------------
32     static void
33     Initialize();
34 
35     static void
36     Terminate();
37 
38     static const char *
39     GetPluginNameStatic();
40 
41     static const char *
42     GetPluginDescriptionStatic();
43 
44     static lldb_private::ObjectFile *
45     CreateInstance (const lldb::ModuleSP &module_sp,
46                     lldb::DataBufferSP& dataSP,
47                     const lldb_private::FileSpec* file,
48                     lldb::addr_t offset,
49                     lldb::addr_t length);
50 
51     static lldb_private::ObjectFile *
52     CreateMemoryInstance (const lldb::ModuleSP &module_sp,
53                           lldb::DataBufferSP& data_sp,
54                           const lldb::ProcessSP &process_sp,
55                           lldb::addr_t header_addr);
56 
57     static bool
58     MagicBytesMatch (lldb::DataBufferSP& dataSP,
59                      lldb::addr_t offset,
60                      lldb::addr_t length);
61 
62     //------------------------------------------------------------------
63     // Member Functions
64     //------------------------------------------------------------------
65     ObjectFileMachO (const lldb::ModuleSP &module_sp,
66                      lldb::DataBufferSP& dataSP,
67                      const lldb_private::FileSpec* file,
68                      lldb::addr_t offset,
69                      lldb::addr_t length);
70 
71     ObjectFileMachO (const lldb::ModuleSP &module_sp,
72                      lldb::DataBufferSP& dataSP,
73                      const lldb::ProcessSP &process_sp,
74                      lldb::addr_t header_addr);
75 
76     virtual
77     ~ObjectFileMachO();
78 
79     virtual bool
80     ParseHeader ();
81 
82     virtual lldb::ByteOrder
83     GetByteOrder () const;
84 
85     virtual bool
86     IsExecutable () const;
87 
88     virtual size_t
89     GetAddressByteSize ()  const;
90 
91     virtual lldb::AddressClass
92     GetAddressClass (lldb::addr_t file_addr);
93 
94     virtual lldb_private::Symtab *
95     GetSymtab();
96 
97     virtual lldb_private::SectionList *
98     GetSectionList();
99 
100     virtual void
101     Dump (lldb_private::Stream *s);
102 
103     virtual bool
104     GetArchitecture (lldb_private::ArchSpec &arch);
105 
106     virtual bool
107     GetUUID (lldb_private::UUID* uuid);
108 
109     virtual uint32_t
110     GetDependentModules (lldb_private::FileSpecList& files);
111 
112     //------------------------------------------------------------------
113     // PluginInterface protocol
114     //------------------------------------------------------------------
115     virtual const char *
116     GetPluginName();
117 
118     virtual const char *
119     GetShortPluginName();
120 
121     virtual uint32_t
122     GetPluginVersion();
123 
124     virtual lldb_private::Address
125     GetEntryPointAddress ();
126 
127     virtual lldb_private::Address
128     GetHeaderAddress ();
129 
130     virtual uint32_t
131     GetNumThreadContexts ();
132 
133     virtual lldb::RegisterContextSP
134     GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread);
135 
136     virtual ObjectFile::Type
137     CalculateType();
138 
139     virtual ObjectFile::Strata
140     CalculateStrata();
141 
142     virtual uint32_t
143     GetVersion (uint32_t *versions, uint32_t num_versions);
144 
145 protected:
146     llvm::MachO::mach_header m_header;
147     mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap;
148     mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap;
149     static const lldb_private::ConstString &GetSegmentNameTEXT();
150     static const lldb_private::ConstString &GetSegmentNameDATA();
151     static const lldb_private::ConstString &GetSegmentNameOBJC();
152     static const lldb_private::ConstString &GetSegmentNameLINKEDIT();
153     static const lldb_private::ConstString &GetSectionNameEHFrame();
154 
155     llvm::MachO::dysymtab_command m_dysymtab;
156     std::vector<llvm::MachO::segment_command_64> m_mach_segments;
157     std::vector<llvm::MachO::section_64> m_mach_sections;
158     typedef lldb_private::RangeArray<uint32_t, uint32_t, 1> FileRangeArray;
159     lldb_private::Address  m_entry_point_address;
160     FileRangeArray m_thread_context_offsets;
161     bool m_thread_context_offsets_valid;
162 
163     size_t
164     ParseSections ();
165 
166     size_t
167     ParseSymtab (bool minimize);
168 
169 };
170 
171 #endif  // liblldb_ObjectFileMachO_h_
172