xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1 //===-- ObjectFileMachO.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
11 
12 #include "lldb/Core/Address.h"
13 #include "lldb/Core/FileSpecList.h"
14 #include "lldb/Host/SafeMachO.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/RangeMap.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/Utility/UUID.h"
20 #include <optional>
21 
22 // This class needs to be hidden as eventually belongs in a plugin that
23 // will export the ObjectFile protocol
24 class ObjectFileMachO : public lldb_private::ObjectFile {
25 public:
26   ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
27                   lldb::offset_t data_offset,
28                   const lldb_private::FileSpec *file, lldb::offset_t offset,
29                   lldb::offset_t length);
30 
31   ObjectFileMachO(const lldb::ModuleSP &module_sp,
32                   lldb::WritableDataBufferSP data_sp,
33                   const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
34 
35   ~ObjectFileMachO() override = default;
36 
37   // Static Functions
38   static void Initialize();
39 
40   static void Terminate();
41 
GetPluginNameStatic()42   static llvm::StringRef GetPluginNameStatic() { return "mach-o"; }
43 
GetPluginDescriptionStatic()44   static llvm::StringRef GetPluginDescriptionStatic() {
45     return "Mach-o object file reader (32 and 64 bit)";
46   }
47 
48   static lldb_private::ObjectFile *
49   CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
50                  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
51                  lldb::offset_t file_offset, lldb::offset_t length);
52 
53   static lldb_private::ObjectFile *CreateMemoryInstance(
54       const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
55       const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
56 
57   static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
58                                         lldb::DataBufferSP &data_sp,
59                                         lldb::offset_t data_offset,
60                                         lldb::offset_t file_offset,
61                                         lldb::offset_t length,
62                                         lldb_private::ModuleSpecList &specs);
63 
64   static bool SaveCore(const lldb::ProcessSP &process_sp,
65                        const lldb_private::FileSpec &outfile,
66                        lldb::SaveCoreStyle &core_style,
67                        lldb_private::Status &error);
68 
69   static bool MagicBytesMatch(lldb::DataBufferSP data_sp, lldb::addr_t offset,
70                               lldb::addr_t length);
71 
72   // LLVM RTTI support
73   static char ID;
isA(const void * ClassID)74   bool isA(const void *ClassID) const override {
75     return ClassID == &ID || ObjectFile::isA(ClassID);
76   }
classof(const ObjectFile * obj)77   static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
78 
79   // Member Functions
80   bool ParseHeader() override;
81 
82   bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
83                       bool value_is_offset) override;
84 
85   lldb::ByteOrder GetByteOrder() const override;
86 
87   bool IsExecutable() const override;
88 
89   bool IsDynamicLoader() const;
90 
91   bool IsSharedCacheBinary() const;
92 
93   uint32_t GetAddressByteSize() const override;
94 
95   lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
96 
97   void ParseSymtab(lldb_private::Symtab &symtab) override;
98 
99   bool IsStripped() override;
100 
101   void CreateSections(lldb_private::SectionList &unified_section_list) override;
102 
103   void Dump(lldb_private::Stream *s) override;
104 
105   lldb_private::ArchSpec GetArchitecture() override;
106 
107   lldb_private::UUID GetUUID() override;
108 
109   uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
110 
GetReExportedLibraries()111   lldb_private::FileSpecList GetReExportedLibraries() override {
112     return m_reexported_dylibs;
113   }
114 
115   lldb_private::Address GetEntryPointAddress() override;
116 
117   lldb_private::Address GetBaseAddress() override;
118 
119   uint32_t GetNumThreadContexts() override;
120 
121   std::string GetIdentifierString() override;
122 
123   lldb::addr_t GetAddressMask() override;
124 
125   bool GetCorefileMainBinaryInfo(lldb::addr_t &value, bool &value_is_offset,
126                                  lldb_private::UUID &uuid,
127                                  ObjectFile::BinaryType &type) override;
128 
129   bool LoadCoreFileImages(lldb_private::Process &process) override;
130 
131   lldb::RegisterContextSP
132   GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override;
133 
134   ObjectFile::Type CalculateType() override;
135 
136   ObjectFile::Strata CalculateStrata() override;
137 
138   llvm::VersionTuple GetVersion() override;
139 
140   llvm::VersionTuple GetMinimumOSVersion() override;
141 
142   llvm::VersionTuple GetSDKVersion() override;
143 
144   bool GetIsDynamicLinkEditor() override;
145 
146   bool CanTrustAddressRanges() override;
147 
148   static bool ParseHeader(lldb_private::DataExtractor &data,
149                           lldb::offset_t *data_offset_ptr,
150                           llvm::MachO::mach_header &header);
151 
152   bool AllowAssemblyEmulationUnwindPlans() override;
153 
154   // PluginInterface protocol
GetPluginName()155   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
156 
157 protected:
158   static lldb_private::UUID
159   GetUUID(const llvm::MachO::mach_header &header,
160           const lldb_private::DataExtractor &data,
161           lldb::offset_t lc_offset); // Offset to the first load command
162 
163   static lldb_private::ArchSpec GetArchitecture(
164       lldb::ModuleSP module_sp, const llvm::MachO::mach_header &header,
165       const lldb_private::DataExtractor &data, lldb::offset_t lc_offset);
166 
167   /// Enumerate all ArchSpecs supported by this Mach-O file.
168   ///
169   /// On macOS one Mach-O slice can contain multiple load commands:
170   /// One load command for being loaded into a macOS process and one
171   /// load command for being loaded into a macCatalyst process. In
172   /// contrast to ObjectContainerUniversalMachO, this is the same
173   /// binary that can be loaded into different contexts.
174   static void GetAllArchSpecs(const llvm::MachO::mach_header &header,
175                               const lldb_private::DataExtractor &data,
176                               lldb::offset_t lc_offset,
177                               lldb_private::ModuleSpec &base_spec,
178                               lldb_private::ModuleSpecList &all_specs);
179 
180   /// Intended for same-host arm device debugging where lldb needs to
181   /// detect libraries in the shared cache and augment the nlist entries
182   /// with an on-disk dyld_shared_cache file.  The process will record
183   /// the shared cache UUID so the on-disk cache can be matched or rejected
184   /// correctly.
185   void GetProcessSharedCacheUUID(lldb_private::Process *,
186                                  lldb::addr_t &base_addr,
187                                  lldb_private::UUID &uuid);
188 
189   /// Intended for same-host arm device debugging where lldb will read
190   /// shared cache libraries out of its own memory instead of the remote
191   /// process' memory as an optimization.  If lldb's shared cache UUID
192   /// does not match the process' shared cache UUID, this optimization
193   /// should not be used.
194   void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid);
195 
196   lldb_private::Section *GetMachHeaderSection();
197 
198   lldb::addr_t CalculateSectionLoadAddressForMemoryImage(
199       lldb::addr_t mach_header_load_address,
200       const lldb_private::Section *mach_header_section,
201       const lldb_private::Section *section);
202 
203   lldb_private::UUID
204   GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache,
205                      const lldb::ByteOrder byte_order,
206                      const uint32_t addr_byte_size);
207 
208   size_t ParseSymtab();
209 
210   typedef lldb_private::RangeVector<uint32_t, uint32_t, 8> EncryptedFileRanges;
211   EncryptedFileRanges GetEncryptedFileRanges();
212 
213   struct SegmentParsingContext;
214   void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd,
215                               lldb::offset_t offset);
216   void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd,
217                              lldb::offset_t offset, uint32_t cmd_idx,
218                              SegmentParsingContext &context);
219   void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd,
220                               uint32_t cmd_idx);
221 
222   bool SectionIsLoadable(const lldb_private::Section *section);
223 
224   /// A corefile may include metadata about all of the binaries that were
225   /// present in the process when the corefile was taken.  This is only
226   /// implemented for Mach-O files for now; we'll generalize it when we
227   /// have other systems that can include the same.
228   struct MachOCorefileImageEntry {
229     std::string filename;
230     lldb_private::UUID uuid;
231     lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
232     lldb::addr_t slide = 0;
233     bool currently_executing = false;
234     std::vector<std::tuple<lldb_private::ConstString, lldb::addr_t>>
235         segment_load_addresses;
236   };
237 
238   struct LCNoteEntry {
LCNoteEntryLCNoteEntry239     LCNoteEntry(uint32_t addr_byte_size, lldb::ByteOrder byte_order)
240         : payload(lldb_private::Stream::eBinary, addr_byte_size, byte_order) {}
241 
242     std::string name;
243     lldb::addr_t payload_file_offset = 0;
244     lldb_private::StreamString payload;
245   };
246 
247   struct MachOCorefileAllImageInfos {
248     std::vector<MachOCorefileImageEntry> all_image_infos;
IsValidMachOCorefileAllImageInfos249     bool IsValid() { return all_image_infos.size() > 0; }
250   };
251 
252   /// Get the list of binary images that were present in the process
253   /// when the corefile was produced.
254   /// \return
255   ///     The MachOCorefileAllImageInfos object returned will have
256   ///     IsValid() == false if the information is unavailable.
257   MachOCorefileAllImageInfos GetCorefileAllImageInfos();
258 
259   llvm::MachO::mach_header m_header;
260   static lldb_private::ConstString GetSegmentNameTEXT();
261   static lldb_private::ConstString GetSegmentNameDATA();
262   static lldb_private::ConstString GetSegmentNameDATA_DIRTY();
263   static lldb_private::ConstString GetSegmentNameDATA_CONST();
264   static lldb_private::ConstString GetSegmentNameOBJC();
265   static lldb_private::ConstString GetSegmentNameLINKEDIT();
266   static lldb_private::ConstString GetSegmentNameDWARF();
267   static lldb_private::ConstString GetSectionNameEHFrame();
268 
269   llvm::MachO::dysymtab_command m_dysymtab;
270   std::vector<llvm::MachO::segment_command_64> m_mach_segments;
271   std::vector<llvm::MachO::section_64> m_mach_sections;
272   std::optional<llvm::VersionTuple> m_min_os_version;
273   std::optional<llvm::VersionTuple> m_sdk_versions;
274   typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray;
275   lldb_private::Address m_entry_point_address;
276   FileRangeArray m_thread_context_offsets;
277   lldb::offset_t m_linkedit_original_offset = 0;
278   lldb::addr_t m_text_address = LLDB_INVALID_ADDRESS;
279   bool m_thread_context_offsets_valid;
280   lldb_private::FileSpecList m_reexported_dylibs;
281   bool m_allow_assembly_emulation_unwind_plans;
282 };
283 
284 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
285