Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1 | //===-- DynamicLoaderDarwin.h -------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef liblldb_DynamicLoaderDarwin_h_ |
| 10 | #define liblldb_DynamicLoaderDarwin_h_ |
| 11 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 12 | #include <map> |
| 13 | #include <mutex> |
| 14 | #include <vector> |
| 15 | |
Pavel Labath | 7704473 | 2018-09-12 12:26:05 +0000 | [diff] [blame] | 16 | #include "lldb/Host/SafeMachO.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include "lldb/Target/DynamicLoader.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 18 | #include "lldb/Target/Process.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/FileSpec.h" |
Pavel Labath | f2a8bcc | 2017-06-27 10:45:31 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/StructuredData.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 21 | #include "lldb/Utility/UUID.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 22 | |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Triple.h" |
| 24 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 25 | namespace lldb_private { |
| 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | class DynamicLoaderDarwin : public lldb_private::DynamicLoader { |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 28 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | DynamicLoaderDarwin(lldb_private::Process *process); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | virtual ~DynamicLoaderDarwin() override; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | //------------------------------------------------------------------ |
| 34 | /// Called after attaching a process. |
| 35 | /// |
| 36 | /// Allow DynamicLoader plug-ins to execute some code after |
| 37 | /// attaching to a process. |
| 38 | //------------------------------------------------------------------ |
| 39 | void DidAttach() override; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | void DidLaunch() override; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, |
| 44 | bool stop_others) override; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | size_t FindEquivalentSymbols( |
| 47 | lldb_private::Symbol *original_symbol, |
| 48 | lldb_private::ModuleList &module_list, |
| 49 | lldb_private::SymbolContextList &equivalent_symbols) override; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module, |
| 52 | const lldb::ThreadSP thread, |
| 53 | lldb::addr_t tls_file_addr) override; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | bool AlwaysRelyOnEHUnwindInfo(lldb_private::SymbolContext &sym_ctx) override; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | virtual void DoInitialImageFetch() = 0; |
| 58 | |
| 59 | virtual bool NeedToDoInitialImageFetch() = 0; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 60 | |
| 61 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | void PrivateInitialize(lldb_private::Process *process); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | void PrivateProcessStateChanged(lldb_private::Process *process, |
| 65 | lldb::StateType state); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | void Clear(bool clear_process); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | // Clear method for classes derived from this one |
| 70 | virtual void DoClear() = 0; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | void SetDYLDModule(lldb::ModuleSP &dyld_module_sp); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | lldb::ModuleSP GetDYLDModule(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | class Segment { |
| 77 | public: |
| 78 | Segment() |
| 79 | : name(), vmaddr(LLDB_INVALID_ADDRESS), vmsize(0), fileoff(0), |
| 80 | filesize(0), maxprot(0), initprot(0), nsects(0), flags(0) {} |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | lldb_private::ConstString name; |
| 83 | lldb::addr_t vmaddr; |
| 84 | lldb::addr_t vmsize; |
| 85 | lldb::addr_t fileoff; |
| 86 | lldb::addr_t filesize; |
| 87 | uint32_t maxprot; |
| 88 | uint32_t initprot; |
| 89 | uint32_t nsects; |
| 90 | uint32_t flags; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | bool operator==(const Segment &rhs) const { |
| 93 | return name == rhs.name && vmaddr == rhs.vmaddr && vmsize == rhs.vmsize; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | void PutToLog(lldb_private::Log *log, lldb::addr_t slide) const; |
| 97 | }; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | struct ImageInfo { |
| 100 | lldb::addr_t address; // Address of mach header for this dylib |
| 101 | lldb::addr_t slide; // The amount to slide all segments by if there is a |
| 102 | // global slide. |
| 103 | lldb::addr_t mod_date; // Modification date for this dylib |
| 104 | lldb_private::FileSpec file_spec; // Resolved path for this dylib |
| 105 | lldb_private::UUID |
| 106 | uuid; // UUID for this dylib if it has one, else all zeros |
| 107 | llvm::MachO::mach_header header; // The mach header for this image |
| 108 | std::vector<Segment> segments; // All segment vmaddr and vmsize pairs for |
| 109 | // this executable (from memory of inferior) |
| 110 | uint32_t load_stop_id; // The process stop ID that the sections for this |
| 111 | // image were loaded |
| 112 | llvm::Triple::OSType os_type; // LC_VERSION_MIN_... load command os type |
| 113 | std::string min_version_os_sdk; // LC_VERSION_MIN_... sdk value |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | ImageInfo() |
| 116 | : address(LLDB_INVALID_ADDRESS), slide(0), mod_date(0), file_spec(), |
| 117 | uuid(), header(), segments(), load_stop_id(0), |
| 118 | os_type(llvm::Triple::OSType::UnknownOS), min_version_os_sdk() {} |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | void Clear(bool load_cmd_data_only) { |
| 121 | if (!load_cmd_data_only) { |
| 122 | address = LLDB_INVALID_ADDRESS; |
| 123 | slide = 0; |
| 124 | mod_date = 0; |
| 125 | file_spec.Clear(); |
| 126 | ::memset(&header, 0, sizeof(header)); |
| 127 | } |
| 128 | uuid.Clear(); |
| 129 | segments.clear(); |
| 130 | load_stop_id = 0; |
| 131 | os_type = llvm::Triple::OSType::UnknownOS; |
| 132 | min_version_os_sdk.clear(); |
| 133 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | bool operator==(const ImageInfo &rhs) const { |
| 136 | return address == rhs.address && slide == rhs.slide && |
| 137 | mod_date == rhs.mod_date && file_spec == rhs.file_spec && |
| 138 | uuid == rhs.uuid && |
| 139 | memcmp(&header, &rhs.header, sizeof(header)) == 0 && |
| 140 | segments == rhs.segments && os_type == rhs.os_type; |
| 141 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | bool UUIDValid() const { return uuid.IsValid(); } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | uint32_t GetAddressByteSize() { |
| 146 | if (header.cputype) { |
| 147 | if (header.cputype & llvm::MachO::CPU_ARCH_ABI64) |
| 148 | return 8; |
| 149 | else |
| 150 | return 4; |
| 151 | } |
| 152 | return 0; |
| 153 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | lldb_private::ArchSpec GetArchitecture() const { |
| 156 | return lldb_private::ArchSpec(lldb_private::eArchTypeMachO, |
| 157 | header.cputype, header.cpusubtype); |
| 158 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 159 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame^] | 160 | const Segment *FindSegment(lldb_private::ConstString name) const; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | |
| 162 | void PutToLog(lldb_private::Log *log) const; |
| 163 | |
| 164 | typedef std::vector<ImageInfo> collection; |
| 165 | typedef collection::iterator iterator; |
| 166 | typedef collection::const_iterator const_iterator; |
| 167 | }; |
| 168 | |
| 169 | bool UpdateImageLoadAddress(lldb_private::Module *module, ImageInfo &info); |
| 170 | |
| 171 | bool UnloadModuleSections(lldb_private::Module *module, ImageInfo &info); |
| 172 | |
| 173 | lldb::ModuleSP FindTargetModuleForImageInfo(ImageInfo &image_info, |
| 174 | bool can_create, |
| 175 | bool *did_create_ptr); |
| 176 | |
| 177 | void UnloadImages(const std::vector<lldb::addr_t> &solib_addresses); |
| 178 | |
| 179 | void UnloadAllImages(); |
| 180 | |
| 181 | virtual bool SetNotificationBreakpoint() = 0; |
| 182 | |
| 183 | virtual void ClearNotificationBreakpoint() = 0; |
| 184 | |
| 185 | virtual bool DidSetNotificationBreakpoint() = 0; |
| 186 | |
| 187 | typedef std::map<uint64_t, lldb::addr_t> PthreadKeyToTLSMap; |
| 188 | typedef std::map<lldb::user_id_t, PthreadKeyToTLSMap> ThreadIDToTLSMap; |
| 189 | |
| 190 | std::recursive_mutex &GetMutex() const { return m_mutex; } |
| 191 | |
| 192 | lldb::ModuleSP GetPThreadLibraryModule(); |
| 193 | |
| 194 | lldb_private::Address GetPthreadSetSpecificAddress(); |
| 195 | |
| 196 | bool JSONImageInformationIntoImageInfo( |
| 197 | lldb_private::StructuredData::ObjectSP image_details, |
| 198 | ImageInfo::collection &image_infos); |
| 199 | |
| 200 | // If image_infos contains / may contain dyld or executable image, call this |
| 201 | // method |
| 202 | // to keep our internal record keeping of the special binaries up-to-date. |
| 203 | void |
| 204 | UpdateSpecialBinariesFromNewImageInfos(ImageInfo::collection &image_infos); |
| 205 | |
| 206 | // if image_info is a dyld binary, call this method |
| 207 | void UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info); |
| 208 | |
| 209 | // If image_infos contains / may contain executable image, call this method |
| 210 | // to keep our internal record keeping of the special dyld binary up-to-date. |
| 211 | void AddExecutableModuleIfInImageInfos(ImageInfo::collection &image_infos); |
| 212 | |
| 213 | bool AddModulesUsingImageInfos(ImageInfo::collection &image_infos); |
| 214 | |
| 215 | // Whether we should use the new dyld SPI to get shared library information, |
| 216 | // or read |
| 217 | // it directly out of the dyld_all_image_infos. Whether we use the (newer) |
| 218 | // DynamicLoaderMacOS |
| 219 | // plugin or the (older) DynamicLoaderMacOSX plugin. |
| 220 | static bool UseDYLDSPI(lldb_private::Process *process); |
| 221 | |
| 222 | lldb::ModuleWP m_dyld_module_wp; // the dyld whose file type (mac, ios, etc) |
| 223 | // matches the process |
| 224 | lldb::ModuleWP m_libpthread_module_wp; |
| 225 | lldb_private::Address m_pthread_getspecific_addr; |
| 226 | ThreadIDToTLSMap m_tid_to_tls_map; |
| 227 | ImageInfo::collection |
| 228 | m_dyld_image_infos; // Current shared libraries information |
| 229 | uint32_t m_dyld_image_infos_stop_id; // The process stop ID that |
| 230 | // "m_dyld_image_infos" is valid for |
| 231 | ImageInfo m_dyld; |
| 232 | mutable std::recursive_mutex m_mutex; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 233 | |
| 234 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | DISALLOW_COPY_AND_ASSIGN(DynamicLoaderDarwin); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | } // namespace lldb_private |
| 239 | |
| 240 | #endif // liblldb_DynamicLoaderDarwin_h_ |