Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1 | //===-- DynamicLoaderDarwin.cpp -----------------------------*- 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 | |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 9 | #include "DynamicLoaderDarwin.h" |
| 10 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 11 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Debugger.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
| 14 | #include "lldb/Core/ModuleSpec.h" |
| 15 | #include "lldb/Core/PluginManager.h" |
| 16 | #include "lldb/Core/Section.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 17 | #include "lldb/Expression/DiagnosticManager.h" |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 18 | #include "lldb/Host/FileSystem.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/ClangASTContext.h" |
| 20 | #include "lldb/Symbol/Function.h" |
| 21 | #include "lldb/Symbol/ObjectFile.h" |
| 22 | #include "lldb/Target/ABI.h" |
| 23 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 24 | #include "lldb/Target/RegisterContext.h" |
| 25 | #include "lldb/Target/StackFrame.h" |
| 26 | #include "lldb/Target/Target.h" |
| 27 | #include "lldb/Target/Thread.h" |
| 28 | #include "lldb/Target/ThreadPlanCallFunction.h" |
| 29 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/DataBuffer.h" |
| 31 | #include "lldb/Utility/DataBufferHeap.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 33 | #include "lldb/Utility/State.h" |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 34 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 35 | //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN |
| 36 | #ifdef ENABLE_DEBUG_PRINTF |
| 37 | #include <stdio.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 39 | #else |
| 40 | #define DEBUG_PRINTF(fmt, ...) |
| 41 | #endif |
| 42 | |
| 43 | #ifndef __APPLE__ |
| 44 | #include "Utility/UuidCompatibility.h" |
| 45 | #else |
| 46 | #include <uuid/uuid.h> |
| 47 | #endif |
| 48 | |
| 49 | using namespace lldb; |
| 50 | using namespace lldb_private; |
| 51 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 52 | //---------------------------------------------------------------------- |
| 53 | // Constructor |
| 54 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | DynamicLoaderDarwin::DynamicLoaderDarwin(Process *process) |
| 56 | : DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(), |
| 57 | m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(), |
| 58 | m_dyld_image_infos_stop_id(UINT32_MAX), m_dyld(), m_mutex() {} |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 59 | |
| 60 | //---------------------------------------------------------------------- |
| 61 | // Destructor |
| 62 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | DynamicLoaderDarwin::~DynamicLoaderDarwin() {} |
| 64 | |
| 65 | //------------------------------------------------------------------ |
| 66 | /// Called after attaching a process. |
| 67 | /// |
| 68 | /// Allow DynamicLoader plug-ins to execute some code after |
| 69 | /// attaching to a process. |
| 70 | //------------------------------------------------------------------ |
| 71 | void DynamicLoaderDarwin::DidAttach() { |
| 72 | PrivateInitialize(m_process); |
| 73 | DoInitialImageFetch(); |
| 74 | SetNotificationBreakpoint(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | //------------------------------------------------------------------ |
| 78 | /// Called after attaching a process. |
| 79 | /// |
| 80 | /// Allow DynamicLoader plug-ins to execute some code after |
| 81 | /// attaching to a process. |
| 82 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | void DynamicLoaderDarwin::DidLaunch() { |
| 84 | PrivateInitialize(m_process); |
| 85 | DoInitialImageFetch(); |
| 86 | SetNotificationBreakpoint(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 89 | //---------------------------------------------------------------------- |
| 90 | // Clear out the state of this class. |
| 91 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | void DynamicLoaderDarwin::Clear(bool clear_process) { |
| 93 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 94 | if (clear_process) |
| 95 | m_process = NULL; |
| 96 | m_dyld_image_infos.clear(); |
| 97 | m_dyld_image_infos_stop_id = UINT32_MAX; |
| 98 | m_dyld.Clear(false); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo( |
| 102 | ImageInfo &image_info, bool can_create, bool *did_create_ptr) { |
| 103 | if (did_create_ptr) |
| 104 | *did_create_ptr = false; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | Target &target = m_process->GetTarget(); |
| 107 | const ModuleList &target_images = target.GetImages(); |
| 108 | ModuleSpec module_spec(image_info.file_spec); |
| 109 | module_spec.GetUUID() = image_info.uuid; |
| 110 | ModuleSP module_sp(target_images.FindFirstModule(module_spec)); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | if (module_sp && !module_spec.GetUUID().IsValid() && |
| 113 | !module_sp->GetUUID().IsValid()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 114 | // No UUID, we must rely upon the cached module modification time and the |
| 115 | // modification time of the file on disk |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | if (module_sp->GetModificationTime() != |
Jonas Devlieghere | 4637696 | 2018-10-31 21:49:27 +0000 | [diff] [blame] | 117 | FileSystem::Instance().GetModificationTime(module_sp->GetFileSpec())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | module_sp.reset(); |
| 119 | } |
| 120 | |
| 121 | if (!module_sp) { |
| 122 | if (can_create) { |
| 123 | module_sp = target.GetSharedModule(module_spec); |
| 124 | if (!module_sp || module_sp->GetObjectFile() == NULL) |
| 125 | module_sp = m_process->ReadModuleFromMemory(image_info.file_spec, |
| 126 | image_info.address); |
| 127 | |
| 128 | if (did_create_ptr) |
| 129 | *did_create_ptr = (bool)module_sp; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 130 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | } |
| 132 | return module_sp; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | void DynamicLoaderDarwin::UnloadImages( |
| 136 | const std::vector<lldb::addr_t> &solib_addresses) { |
| 137 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 138 | if (m_process->GetStopID() == m_dyld_image_infos_stop_id) |
| 139 | return; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 142 | Target &target = m_process->GetTarget(); |
| 143 | if (log) |
| 144 | log->Printf("Removing %" PRId64 " modules.", |
| 145 | (uint64_t)solib_addresses.size()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | ModuleList unloaded_module_list; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | for (addr_t solib_addr : solib_addresses) { |
| 150 | Address header; |
| 151 | if (header.SetLoadAddress(solib_addr, &target)) { |
| 152 | if (header.GetOffset() == 0) { |
| 153 | ModuleSP module_to_remove(header.GetModule()); |
| 154 | if (module_to_remove.get()) { |
| 155 | if (log) |
| 156 | log->Printf("Removing module at address 0x%" PRIx64, solib_addr); |
| 157 | // remove the sections from the Target |
| 158 | UnloadSections(module_to_remove); |
| 159 | // add this to the list of modules to remove |
| 160 | unloaded_module_list.AppendIfNeeded(module_to_remove); |
| 161 | // remove the entry from the m_dyld_image_infos |
| 162 | ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end(); |
| 163 | for (pos = m_dyld_image_infos.begin(); pos != end; pos++) { |
| 164 | if (solib_addr == (*pos).address) { |
| 165 | m_dyld_image_infos.erase(pos); |
| 166 | break; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 167 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 169 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 171 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | if (unloaded_module_list.GetSize() > 0) { |
| 175 | if (log) { |
| 176 | log->PutCString("Unloaded:"); |
| 177 | unloaded_module_list.LogUUIDAndPaths( |
| 178 | log, "DynamicLoaderDarwin::UnloadModules"); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 179 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | m_process->GetTarget().GetImages().Remove(unloaded_module_list); |
| 181 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
| 182 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | void DynamicLoaderDarwin::UnloadAllImages() { |
| 186 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 187 | ModuleList unloaded_modules_list; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | Target &target = m_process->GetTarget(); |
| 190 | const ModuleList &target_modules = target.GetImages(); |
| 191 | std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | size_t num_modules = target_modules.GetSize(); |
| 194 | ModuleSP dyld_sp(GetDYLDModule()); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | for (size_t i = 0; i < num_modules; i++) { |
| 197 | ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | // Don't remove dyld - else we'll lose our breakpoint notifying us about |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 200 | // libraries being re-loaded... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | if (module_sp.get() != nullptr && module_sp.get() != dyld_sp.get()) { |
| 202 | UnloadSections(module_sp); |
| 203 | unloaded_modules_list.Append(module_sp); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 204 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | if (unloaded_modules_list.GetSize() != 0) { |
| 208 | if (log) { |
| 209 | log->PutCString("Unloaded:"); |
| 210 | unloaded_modules_list.LogUUIDAndPaths( |
| 211 | log, "DynamicLoaderDarwin::UnloadAllImages"); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 212 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | target.GetImages().Remove(unloaded_modules_list); |
| 214 | m_dyld_image_infos.clear(); |
| 215 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
| 216 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 219 | //---------------------------------------------------------------------- |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 220 | // Update the load addresses for all segments in MODULE using the updated INFO |
| 221 | // that is passed in. |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 222 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | bool DynamicLoaderDarwin::UpdateImageLoadAddress(Module *module, |
| 224 | ImageInfo &info) { |
| 225 | bool changed = false; |
| 226 | if (module) { |
| 227 | ObjectFile *image_object_file = module->GetObjectFile(); |
| 228 | if (image_object_file) { |
| 229 | SectionList *section_list = image_object_file->GetSectionList(); |
| 230 | if (section_list) { |
| 231 | std::vector<uint32_t> inaccessible_segment_indexes; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 232 | // We now know the slide amount, so go through all sections and update |
| 233 | // the load addresses with the correct values. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | const size_t num_segments = info.segments.size(); |
| 235 | for (size_t i = 0; i < num_segments; ++i) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 236 | // Only load a segment if it has protections. Things like __PAGEZERO |
| 237 | // don't have any protections, and they shouldn't be slid |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 238 | SectionSP section_sp( |
| 239 | section_list->FindSectionByName(info.segments[i].name)); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | if (info.segments[i].maxprot == 0) { |
| 242 | inaccessible_segment_indexes.push_back(i); |
| 243 | } else { |
| 244 | const addr_t new_section_load_addr = |
| 245 | info.segments[i].vmaddr + info.slide; |
| 246 | static ConstString g_section_name_LINKEDIT("__LINKEDIT"); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | if (section_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 249 | // __LINKEDIT sections from files in the shared cache can overlap |
| 250 | // so check to see what the segment name is and pass "false" so |
| 251 | // we don't warn of overlapping "Section" objects, and "true" for |
| 252 | // all other sections. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | const bool warn_multiple = |
| 254 | section_sp->GetName() != g_section_name_LINKEDIT; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 255 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | changed = m_process->GetTarget().SetSectionLoadAddress( |
| 257 | section_sp, new_section_load_addr, warn_multiple); |
| 258 | } else { |
| 259 | Host::SystemLog( |
| 260 | Host::eSystemLogWarning, |
| 261 | "warning: unable to find and load segment named '%s' at " |
| 262 | "0x%" PRIx64 " in '%s' in macosx dynamic loader plug-in.\n", |
| 263 | info.segments[i].name.AsCString("<invalid>"), |
| 264 | (uint64_t)new_section_load_addr, |
| 265 | image_object_file->GetFileSpec().GetPath().c_str()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 266 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 268 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 270 | // If the loaded the file (it changed) and we have segments that are |
| 271 | // not readable or writeable, add them to the invalid memory region |
| 272 | // cache for the process. This will typically only be the __PAGEZERO |
| 273 | // segment in the main executable. We might be able to apply this more |
| 274 | // generally to more sections that have no protections in the future, |
| 275 | // but for now we are going to just do __PAGEZERO. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 276 | if (changed && !inaccessible_segment_indexes.empty()) { |
| 277 | for (uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) { |
| 278 | const uint32_t seg_idx = inaccessible_segment_indexes[i]; |
| 279 | SectionSP section_sp( |
| 280 | section_list->FindSectionByName(info.segments[seg_idx].name)); |
| 281 | |
| 282 | if (section_sp) { |
| 283 | static ConstString g_pagezero_section_name("__PAGEZERO"); |
| 284 | if (g_pagezero_section_name == section_sp->GetName()) { |
| 285 | // __PAGEZERO never slides... |
| 286 | const lldb::addr_t vmaddr = info.segments[seg_idx].vmaddr; |
| 287 | const lldb::addr_t vmsize = info.segments[seg_idx].vmsize; |
| 288 | Process::LoadRange pagezero_range(vmaddr, vmsize); |
| 289 | m_process->AddInvalidMemoryRegion(pagezero_range); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 295 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | } |
| 297 | // We might have an in memory image that was loaded as soon as it was created |
| 298 | if (info.load_stop_id == m_process->GetStopID()) |
| 299 | changed = true; |
| 300 | else if (changed) { |
| 301 | // Update the stop ID when this library was updated |
| 302 | info.load_stop_id = m_process->GetStopID(); |
| 303 | } |
| 304 | return changed; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | //---------------------------------------------------------------------- |
| 308 | // Unload the segments in MODULE using the INFO that is passed in. |
| 309 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | bool DynamicLoaderDarwin::UnloadModuleSections(Module *module, |
| 311 | ImageInfo &info) { |
| 312 | bool changed = false; |
| 313 | if (module) { |
| 314 | ObjectFile *image_object_file = module->GetObjectFile(); |
| 315 | if (image_object_file) { |
| 316 | SectionList *section_list = image_object_file->GetSectionList(); |
| 317 | if (section_list) { |
| 318 | const size_t num_segments = info.segments.size(); |
| 319 | for (size_t i = 0; i < num_segments; ++i) { |
| 320 | SectionSP section_sp( |
| 321 | section_list->FindSectionByName(info.segments[i].name)); |
| 322 | if (section_sp) { |
| 323 | const addr_t old_section_load_addr = |
| 324 | info.segments[i].vmaddr + info.slide; |
| 325 | if (m_process->GetTarget().SetSectionUnloaded( |
| 326 | section_sp, old_section_load_addr)) |
| 327 | changed = true; |
| 328 | } else { |
| 329 | Host::SystemLog(Host::eSystemLogWarning, |
| 330 | "warning: unable to find and unload segment named " |
| 331 | "'%s' in '%s' in macosx dynamic loader plug-in.\n", |
| 332 | info.segments[i].name.AsCString("<invalid>"), |
| 333 | image_object_file->GetFileSpec().GetPath().c_str()); |
| 334 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 335 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 337 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | } |
| 339 | return changed; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | // Given a JSON dictionary (from debugserver, most likely) of binary images |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 343 | // loaded in the inferior process, add the images to the ImageInfo collection. |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 344 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo( |
| 346 | StructuredData::ObjectSP image_details, |
| 347 | ImageInfo::collection &image_infos) { |
| 348 | StructuredData::ObjectSP images_sp = |
| 349 | image_details->GetAsDictionary()->GetValueForKey("images"); |
| 350 | if (images_sp.get() == nullptr) |
| 351 | return false; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 352 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | image_infos.resize(images_sp->GetAsArray()->GetSize()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 354 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | for (size_t i = 0; i < image_infos.size(); i++) { |
| 356 | StructuredData::ObjectSP image_sp = |
| 357 | images_sp->GetAsArray()->GetItemAtIndex(i); |
| 358 | if (image_sp.get() == nullptr || image_sp->GetAsDictionary() == nullptr) |
| 359 | return false; |
| 360 | StructuredData::Dictionary *image = image_sp->GetAsDictionary(); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 361 | // clang-format off |
| 362 | if (!image->HasKey("load_address") || |
| 363 | !image->HasKey("pathname") || |
| 364 | !image->HasKey("mod_date") || |
| 365 | !image->HasKey("mach_header") || |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | image->GetValueForKey("mach_header")->GetAsDictionary() == nullptr || |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 367 | !image->HasKey("segments") || |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | image->GetValueForKey("segments")->GetAsArray() == nullptr || |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 369 | !image->HasKey("uuid")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 370 | return false; |
| 371 | } |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 372 | // clang-format on |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 373 | image_infos[i].address = |
| 374 | image->GetValueForKey("load_address")->GetAsInteger()->GetValue(); |
| 375 | image_infos[i].mod_date = |
| 376 | image->GetValueForKey("mod_date")->GetAsInteger()->GetValue(); |
| 377 | image_infos[i].file_spec.SetFile( |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 378 | image->GetValueForKey("pathname")->GetAsString()->GetValue(), |
Jonas Devlieghere | 937348c | 2018-06-13 22:08:14 +0000 | [diff] [blame] | 379 | FileSpec::Style::native); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 380 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | StructuredData::Dictionary *mh = |
| 382 | image->GetValueForKey("mach_header")->GetAsDictionary(); |
| 383 | image_infos[i].header.magic = |
| 384 | mh->GetValueForKey("magic")->GetAsInteger()->GetValue(); |
| 385 | image_infos[i].header.cputype = |
| 386 | mh->GetValueForKey("cputype")->GetAsInteger()->GetValue(); |
| 387 | image_infos[i].header.cpusubtype = |
| 388 | mh->GetValueForKey("cpusubtype")->GetAsInteger()->GetValue(); |
| 389 | image_infos[i].header.filetype = |
| 390 | mh->GetValueForKey("filetype")->GetAsInteger()->GetValue(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 391 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | if (image->HasKey("min_version_os_name")) { |
| 393 | std::string os_name = image->GetValueForKey("min_version_os_name") |
| 394 | ->GetAsString() |
| 395 | ->GetValue(); |
| 396 | if (os_name == "macosx") |
| 397 | image_infos[i].os_type = llvm::Triple::MacOSX; |
| 398 | else if (os_name == "ios" || os_name == "iphoneos") |
| 399 | image_infos[i].os_type = llvm::Triple::IOS; |
| 400 | else if (os_name == "tvos") |
| 401 | image_infos[i].os_type = llvm::Triple::TvOS; |
| 402 | else if (os_name == "watchos") |
| 403 | image_infos[i].os_type = llvm::Triple::WatchOS; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 404 | // NEED_BRIDGEOS_TRIPLE else if (os_name == "bridgeos") |
| 405 | // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type = llvm::Triple::BridgeOS; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 406 | } |
| 407 | if (image->HasKey("min_version_os_sdk")) { |
| 408 | image_infos[i].min_version_os_sdk = |
| 409 | image->GetValueForKey("min_version_os_sdk") |
| 410 | ->GetAsString() |
| 411 | ->GetValue(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 414 | // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 415 | // currently send them in the reply. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | |
| 417 | if (mh->HasKey("flags")) |
| 418 | image_infos[i].header.flags = |
| 419 | mh->GetValueForKey("flags")->GetAsInteger()->GetValue(); |
| 420 | else |
| 421 | image_infos[i].header.flags = 0; |
| 422 | |
| 423 | if (mh->HasKey("ncmds")) |
| 424 | image_infos[i].header.ncmds = |
| 425 | mh->GetValueForKey("ncmds")->GetAsInteger()->GetValue(); |
| 426 | else |
| 427 | image_infos[i].header.ncmds = 0; |
| 428 | |
| 429 | if (mh->HasKey("sizeofcmds")) |
| 430 | image_infos[i].header.sizeofcmds = |
| 431 | mh->GetValueForKey("sizeofcmds")->GetAsInteger()->GetValue(); |
| 432 | else |
| 433 | image_infos[i].header.sizeofcmds = 0; |
| 434 | |
| 435 | StructuredData::Array *segments = |
| 436 | image->GetValueForKey("segments")->GetAsArray(); |
| 437 | uint32_t segcount = segments->GetSize(); |
| 438 | for (size_t j = 0; j < segcount; j++) { |
| 439 | Segment segment; |
| 440 | StructuredData::Dictionary *seg = |
| 441 | segments->GetItemAtIndex(j)->GetAsDictionary(); |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 442 | segment.name = |
| 443 | ConstString(seg->GetValueForKey("name")->GetAsString()->GetValue()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | segment.vmaddr = |
| 445 | seg->GetValueForKey("vmaddr")->GetAsInteger()->GetValue(); |
| 446 | segment.vmsize = |
| 447 | seg->GetValueForKey("vmsize")->GetAsInteger()->GetValue(); |
| 448 | segment.fileoff = |
| 449 | seg->GetValueForKey("fileoff")->GetAsInteger()->GetValue(); |
| 450 | segment.filesize = |
| 451 | seg->GetValueForKey("filesize")->GetAsInteger()->GetValue(); |
| 452 | segment.maxprot = |
| 453 | seg->GetValueForKey("maxprot")->GetAsInteger()->GetValue(); |
| 454 | |
| 455 | // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 456 | // currently send them in the reply. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 457 | |
| 458 | if (seg->HasKey("initprot")) |
| 459 | segment.initprot = |
| 460 | seg->GetValueForKey("initprot")->GetAsInteger()->GetValue(); |
| 461 | else |
| 462 | segment.initprot = 0; |
| 463 | |
| 464 | if (seg->HasKey("flags")) |
| 465 | segment.flags = |
| 466 | seg->GetValueForKey("flags")->GetAsInteger()->GetValue(); |
| 467 | else |
| 468 | segment.flags = 0; |
| 469 | |
| 470 | if (seg->HasKey("nsects")) |
| 471 | segment.nsects = |
| 472 | seg->GetValueForKey("nsects")->GetAsInteger()->GetValue(); |
| 473 | else |
| 474 | segment.nsects = 0; |
| 475 | |
| 476 | image_infos[i].segments.push_back(segment); |
| 477 | } |
| 478 | |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 479 | image_infos[i].uuid.SetFromStringRef( |
| 480 | image->GetValueForKey("uuid")->GetAsString()->GetValue()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 482 | // All sections listed in the dyld image info structure will all either be |
| 483 | // fixed up already, or they will all be off by a single slide amount that |
| 484 | // is determined by finding the first segment that is at file offset zero |
| 485 | // which also has bytes (a file size that is greater than zero) in the |
| 486 | // object file. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 487 | |
| 488 | // Determine the slide amount (if any) |
| 489 | const size_t num_sections = image_infos[i].segments.size(); |
| 490 | for (size_t k = 0; k < num_sections; ++k) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 491 | // Iterate through the object file sections to find the first section |
| 492 | // that starts of file offset zero and that has bytes in the file... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | if ((image_infos[i].segments[k].fileoff == 0 && |
| 494 | image_infos[i].segments[k].filesize > 0) || |
| 495 | (image_infos[i].segments[k].name == ConstString("__TEXT"))) { |
| 496 | image_infos[i].slide = |
| 497 | image_infos[i].address - image_infos[i].segments[k].vmaddr; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 498 | // We have found the slide amount, so we can exit this for loop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return true; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 507 | void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos( |
| 508 | ImageInfo::collection &image_infos) { |
| 509 | uint32_t exe_idx = UINT32_MAX; |
| 510 | uint32_t dyld_idx = UINT32_MAX; |
| 511 | Target &target = m_process->GetTarget(); |
| 512 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 513 | ConstString g_dyld_sim_filename("dyld_sim"); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 514 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 515 | ArchSpec target_arch = target.GetArchitecture(); |
| 516 | const size_t image_infos_size = image_infos.size(); |
| 517 | for (size_t i = 0; i < image_infos_size; i++) { |
| 518 | if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) { |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 519 | // In a "simulator" process (an x86 process that is |
| 520 | // ios/tvos/watchos/bridgeos) we will have two dyld modules -- |
| 521 | // a "dyld" that we want to keep track of, and a "dyld_sim" which |
| 522 | // we don't need to keep track of here. If the target is an x86 |
| 523 | // system and the OS of the dyld binary is ios/tvos/watchos/bridgeos, |
| 524 | // then we are looking at dyld_sym. |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 525 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 526 | // debugserver has only recently (late 2016) started sending up the os |
| 527 | // type for each binary it sees -- so if we don't have an os type, use a |
| 528 | // filename check as our next best guess. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 529 | if (image_infos[i].os_type == llvm::Triple::OSType::UnknownOS) { |
| 530 | if (image_infos[i].file_spec.GetFilename() != g_dyld_sim_filename) { |
| 531 | dyld_idx = i; |
| 532 | } |
| 533 | } else if (target_arch.GetTriple().getArch() == llvm::Triple::x86 || |
| 534 | target_arch.GetTriple().getArch() == llvm::Triple::x86_64) { |
| 535 | if (image_infos[i].os_type != llvm::Triple::OSType::IOS && |
| 536 | image_infos[i].os_type != llvm::Triple::TvOS && |
| 537 | image_infos[i].os_type != llvm::Triple::WatchOS) { |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 538 | // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type != llvm::Triple::BridgeOS) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | dyld_idx = i; |
| 540 | } |
| 541 | } |
Jason Molenda | 777fcec | 2017-01-21 01:17:36 +0000 | [diff] [blame] | 542 | else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 543 | // catch-all for any other environment -- trust that dyld is actually |
| 544 | // dyld |
Jason Molenda | 777fcec | 2017-01-21 01:17:36 +0000 | [diff] [blame] | 545 | dyld_idx = i; |
| 546 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 547 | } else if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) { |
| 548 | exe_idx = i; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (exe_idx != UINT32_MAX) { |
| 553 | const bool can_create = true; |
| 554 | ModuleSP exe_module_sp( |
| 555 | FindTargetModuleForImageInfo(image_infos[exe_idx], can_create, NULL)); |
| 556 | if (exe_module_sp) { |
| 557 | if (log) |
| 558 | log->Printf("Found executable module: %s", |
| 559 | exe_module_sp->GetFileSpec().GetPath().c_str()); |
| 560 | target.GetImages().AppendIfNeeded(exe_module_sp); |
| 561 | UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]); |
| 562 | if (exe_module_sp.get() != target.GetExecutableModulePointer()) { |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 563 | target.SetExecutableModule(exe_module_sp, eLoadDependentsNo); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if (dyld_idx != UINT32_MAX) { |
| 569 | const bool can_create = true; |
| 570 | ModuleSP dyld_sp = |
| 571 | FindTargetModuleForImageInfo(image_infos[dyld_idx], can_create, NULL); |
| 572 | if (dyld_sp.get()) { |
| 573 | if (log) |
| 574 | log->Printf("Found dyld module: %s", |
| 575 | dyld_sp->GetFileSpec().GetPath().c_str()); |
| 576 | target.GetImages().AppendIfNeeded(dyld_sp); |
| 577 | UpdateImageLoadAddress(dyld_sp.get(), image_infos[dyld_idx]); |
| 578 | SetDYLDModule(dyld_sp); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo( |
| 584 | ImageInfo &image_info) { |
| 585 | if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) { |
| 586 | const bool can_create = true; |
| 587 | ModuleSP dyld_sp = |
| 588 | FindTargetModuleForImageInfo(image_info, can_create, NULL); |
| 589 | if (dyld_sp.get()) { |
| 590 | Target &target = m_process->GetTarget(); |
| 591 | target.GetImages().AppendIfNeeded(dyld_sp); |
| 592 | UpdateImageLoadAddress(dyld_sp.get(), image_info); |
| 593 | SetDYLDModule(dyld_sp); |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | void DynamicLoaderDarwin::SetDYLDModule(lldb::ModuleSP &dyld_module_sp) { |
| 599 | m_dyld_module_wp = dyld_module_sp; |
| 600 | } |
| 601 | |
| 602 | ModuleSP DynamicLoaderDarwin::GetDYLDModule() { |
| 603 | ModuleSP dyld_sp(m_dyld_module_wp.lock()); |
| 604 | return dyld_sp; |
| 605 | } |
| 606 | |
| 607 | bool DynamicLoaderDarwin::AddModulesUsingImageInfos( |
| 608 | ImageInfo::collection &image_infos) { |
| 609 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 610 | // Now add these images to the main list. |
| 611 | ModuleList loaded_module_list; |
| 612 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
| 613 | Target &target = m_process->GetTarget(); |
| 614 | ModuleList &target_images = target.GetImages(); |
| 615 | |
| 616 | for (uint32_t idx = 0; idx < image_infos.size(); ++idx) { |
| 617 | if (log) { |
| 618 | log->Printf("Adding new image at address=0x%16.16" PRIx64 ".", |
| 619 | image_infos[idx].address); |
| 620 | image_infos[idx].PutToLog(log); |
| 621 | } |
| 622 | |
| 623 | m_dyld_image_infos.push_back(image_infos[idx]); |
| 624 | |
| 625 | ModuleSP image_module_sp( |
| 626 | FindTargetModuleForImageInfo(image_infos[idx], true, NULL)); |
| 627 | |
| 628 | if (image_module_sp) { |
| 629 | ObjectFile *objfile = image_module_sp->GetObjectFile(); |
| 630 | if (objfile) { |
| 631 | SectionList *sections = objfile->GetSectionList(); |
| 632 | if (sections) { |
| 633 | ConstString commpage_dbstr("__commpage"); |
| 634 | Section *commpage_section = |
| 635 | sections->FindSectionByName(commpage_dbstr).get(); |
| 636 | if (commpage_section) { |
| 637 | ModuleSpec module_spec(objfile->GetFileSpec(), |
| 638 | image_infos[idx].GetArchitecture()); |
| 639 | module_spec.GetObjectName() = commpage_dbstr; |
| 640 | ModuleSP commpage_image_module_sp( |
| 641 | target_images.FindFirstModule(module_spec)); |
| 642 | if (!commpage_image_module_sp) { |
| 643 | module_spec.SetObjectOffset(objfile->GetFileOffset() + |
| 644 | commpage_section->GetFileOffset()); |
| 645 | module_spec.SetObjectSize(objfile->GetByteSize()); |
| 646 | commpage_image_module_sp = target.GetSharedModule(module_spec); |
| 647 | if (!commpage_image_module_sp || |
| 648 | commpage_image_module_sp->GetObjectFile() == NULL) { |
| 649 | commpage_image_module_sp = m_process->ReadModuleFromMemory( |
| 650 | image_infos[idx].file_spec, image_infos[idx].address); |
| 651 | // Always load a memory image right away in the target in case |
| 652 | // we end up trying to read the symbol table from memory... The |
| 653 | // __LINKEDIT will need to be mapped so we can figure out where |
| 654 | // the symbol table bits are... |
| 655 | bool changed = false; |
| 656 | UpdateImageLoadAddress(commpage_image_module_sp.get(), |
| 657 | image_infos[idx]); |
| 658 | target.GetImages().Append(commpage_image_module_sp); |
| 659 | if (changed) { |
| 660 | image_infos[idx].load_stop_id = m_process->GetStopID(); |
| 661 | loaded_module_list.AppendIfNeeded(commpage_image_module_sp); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 662 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 663 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 664 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 665 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 666 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 667 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 668 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 669 | // UpdateImageLoadAddress will return true if any segments change load |
| 670 | // address. We need to check this so we don't mention that all loaded |
| 671 | // shared libraries are newly loaded each time we hit out dyld breakpoint |
| 672 | // since dyld will list all shared libraries each time. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 673 | if (UpdateImageLoadAddress(image_module_sp.get(), image_infos[idx])) { |
| 674 | target_images.AppendIfNeeded(image_module_sp); |
| 675 | loaded_module_list.AppendIfNeeded(image_module_sp); |
| 676 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 677 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 678 | } |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 679 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 680 | if (loaded_module_list.GetSize() > 0) { |
| 681 | if (log) |
| 682 | loaded_module_list.LogUUIDAndPaths(log, |
| 683 | "DynamicLoaderDarwin::ModulesDidLoad"); |
| 684 | m_process->GetTarget().ModulesDidLoad(loaded_module_list); |
| 685 | } |
| 686 | return true; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 689 | //---------------------------------------------------------------------- |
| 690 | // On Mac OS X libobjc (the Objective-C runtime) has several critical dispatch |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 691 | // functions written in hand-written assembly, and also have hand-written |
| 692 | // unwind information in the eh_frame section. Normally we prefer analyzing |
| 693 | // the assembly instructions of a currently executing frame to unwind from that |
| 694 | // frame -- but on hand-written functions this profiling can fail. We should |
| 695 | // use the eh_frame instructions for these functions all the time. |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 696 | // |
| 697 | // As an aside, it would be better if the eh_frame entries had a flag (or were |
| 698 | // extensible so they could have an Apple-specific flag) which indicates that |
| 699 | // the instructions are asynchronous -- accurate at every instruction, instead |
| 700 | // of our normal default assumption that they are not. |
| 701 | //---------------------------------------------------------------------- |
| 702 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 703 | bool DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) { |
| 704 | ModuleSP module_sp; |
| 705 | if (sym_ctx.symbol) { |
| 706 | module_sp = sym_ctx.symbol->GetAddressRef().GetModule(); |
| 707 | } |
| 708 | if (module_sp.get() == NULL && sym_ctx.function) { |
| 709 | module_sp = |
| 710 | sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule(); |
| 711 | } |
| 712 | if (module_sp.get() == NULL) |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 713 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 714 | |
| 715 | ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime(); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 716 | return objc_runtime != NULL && objc_runtime->IsModuleObjCLibrary(module_sp); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 719 | //---------------------------------------------------------------------- |
| 720 | // Dump a Segment to the file handle provided. |
| 721 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 722 | void DynamicLoaderDarwin::Segment::PutToLog(Log *log, |
| 723 | lldb::addr_t slide) const { |
| 724 | if (log) { |
| 725 | if (slide == 0) |
| 726 | log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")", |
| 727 | name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize); |
| 728 | else |
| 729 | log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 |
| 730 | ") slide = 0x%" PRIx64, |
| 731 | name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize, |
| 732 | slide); |
| 733 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | const DynamicLoaderDarwin::Segment * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 737 | DynamicLoaderDarwin::ImageInfo::FindSegment(const ConstString &name) const { |
| 738 | const size_t num_segments = segments.size(); |
| 739 | for (size_t i = 0; i < num_segments; ++i) { |
| 740 | if (segments[i].name == name) |
| 741 | return &segments[i]; |
| 742 | } |
| 743 | return NULL; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 746 | //---------------------------------------------------------------------- |
| 747 | // Dump an image info structure to the file handle provided. |
| 748 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 749 | void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const { |
Pavel Labath | fbb1428 | 2018-06-20 20:13:04 +0000 | [diff] [blame] | 750 | if (!log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 751 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 752 | if (address == LLDB_INVALID_ADDRESS) { |
Pavel Labath | fbb1428 | 2018-06-20 20:13:04 +0000 | [diff] [blame] | 753 | LLDB_LOG(log, "modtime={0:x+8} uuid={1} path='{2}' (UNLOADED)", mod_date, |
| 754 | uuid.GetAsString(), file_spec.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 755 | } else { |
Pavel Labath | fbb1428 | 2018-06-20 20:13:04 +0000 | [diff] [blame] | 756 | LLDB_LOG(log, "address={0:x+16} modtime={1:x+8} uuid={2} path='{3}'", |
| 757 | address, mod_date, uuid.GetAsString(), file_spec.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | for (uint32_t i = 0; i < segments.size(); ++i) |
| 759 | segments[i].PutToLog(log, slide); |
| 760 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 763 | void DynamicLoaderDarwin::PrivateInitialize(Process *process) { |
| 764 | DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__, |
| 765 | StateAsCString(m_process->GetState())); |
| 766 | Clear(true); |
| 767 | m_process = process; |
| 768 | m_process->GetTarget().ClearAllLoadedSections(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | //---------------------------------------------------------------------- |
| 772 | // Member function that gets called when the process state changes. |
| 773 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 774 | void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process, |
| 775 | StateType state) { |
| 776 | DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__, |
| 777 | StateAsCString(state)); |
| 778 | switch (state) { |
| 779 | case eStateConnected: |
| 780 | case eStateAttaching: |
| 781 | case eStateLaunching: |
| 782 | case eStateInvalid: |
| 783 | case eStateUnloaded: |
| 784 | case eStateExited: |
| 785 | case eStateDetached: |
| 786 | Clear(false); |
| 787 | break; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 788 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 789 | case eStateStopped: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 790 | // Keep trying find dyld and set our notification breakpoint each time we |
| 791 | // stop until we succeed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 792 | if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) { |
| 793 | if (NeedToDoInitialImageFetch()) |
| 794 | DoInitialImageFetch(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 795 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 796 | SetNotificationBreakpoint(); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 797 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 798 | break; |
| 799 | |
| 800 | case eStateRunning: |
| 801 | case eStateStepping: |
| 802 | case eStateCrashed: |
| 803 | case eStateSuspended: |
| 804 | break; |
| 805 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | ThreadPlanSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 809 | DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread, |
| 810 | bool stop_others) { |
| 811 | ThreadPlanSP thread_plan_sp; |
| 812 | StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get(); |
| 813 | const SymbolContext ¤t_context = |
| 814 | current_frame->GetSymbolContext(eSymbolContextSymbol); |
| 815 | Symbol *current_symbol = current_context.symbol; |
| 816 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 817 | TargetSP target_sp(thread.CalculateTarget()); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 818 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 819 | if (current_symbol != NULL) { |
| 820 | std::vector<Address> addresses; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 821 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 822 | if (current_symbol->IsTrampoline()) { |
| 823 | const ConstString &trampoline_name = current_symbol->GetMangled().GetName( |
| 824 | current_symbol->GetLanguage(), Mangled::ePreferMangled); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 825 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 826 | if (trampoline_name) { |
| 827 | const ModuleList &images = target_sp->GetImages(); |
| 828 | |
| 829 | SymbolContextList code_symbols; |
| 830 | images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode, |
| 831 | code_symbols); |
| 832 | size_t num_code_symbols = code_symbols.GetSize(); |
| 833 | |
| 834 | if (num_code_symbols > 0) { |
| 835 | for (uint32_t i = 0; i < num_code_symbols; i++) { |
| 836 | SymbolContext context; |
| 837 | AddressRange addr_range; |
| 838 | if (code_symbols.GetContextAtIndex(i, context)) { |
| 839 | context.GetAddressRange(eSymbolContextEverything, 0, false, |
| 840 | addr_range); |
| 841 | addresses.push_back(addr_range.GetBaseAddress()); |
| 842 | if (log) { |
| 843 | addr_t load_addr = |
| 844 | addr_range.GetBaseAddress().GetLoadAddress(target_sp.get()); |
| 845 | |
| 846 | log->Printf("Found a trampoline target symbol at 0x%" PRIx64 |
| 847 | ".", |
| 848 | load_addr); |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | SymbolContextList reexported_symbols; |
| 855 | images.FindSymbolsWithNameAndType( |
| 856 | trampoline_name, eSymbolTypeReExported, reexported_symbols); |
| 857 | size_t num_reexported_symbols = reexported_symbols.GetSize(); |
| 858 | if (num_reexported_symbols > 0) { |
| 859 | for (uint32_t i = 0; i < num_reexported_symbols; i++) { |
| 860 | SymbolContext context; |
| 861 | if (reexported_symbols.GetContextAtIndex(i, context)) { |
| 862 | if (context.symbol) { |
| 863 | Symbol *actual_symbol = |
| 864 | context.symbol->ResolveReExportedSymbol(*target_sp.get()); |
| 865 | if (actual_symbol) { |
| 866 | const Address actual_symbol_addr = |
| 867 | actual_symbol->GetAddress(); |
| 868 | if (actual_symbol_addr.IsValid()) { |
| 869 | addresses.push_back(actual_symbol_addr); |
| 870 | if (log) { |
| 871 | lldb::addr_t load_addr = |
| 872 | actual_symbol_addr.GetLoadAddress(target_sp.get()); |
| 873 | log->Printf( |
| 874 | "Found a re-exported symbol: %s at 0x%" PRIx64 ".", |
| 875 | actual_symbol->GetName().GetCString(), load_addr); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 876 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 878 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 879 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 880 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 881 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 882 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 883 | |
| 884 | SymbolContextList indirect_symbols; |
| 885 | images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeResolver, |
| 886 | indirect_symbols); |
| 887 | size_t num_indirect_symbols = indirect_symbols.GetSize(); |
| 888 | if (num_indirect_symbols > 0) { |
| 889 | for (uint32_t i = 0; i < num_indirect_symbols; i++) { |
| 890 | SymbolContext context; |
| 891 | AddressRange addr_range; |
| 892 | if (indirect_symbols.GetContextAtIndex(i, context)) { |
| 893 | context.GetAddressRange(eSymbolContextEverything, 0, false, |
| 894 | addr_range); |
| 895 | addresses.push_back(addr_range.GetBaseAddress()); |
| 896 | if (log) { |
| 897 | addr_t load_addr = |
| 898 | addr_range.GetBaseAddress().GetLoadAddress(target_sp.get()); |
| 899 | |
| 900 | log->Printf("Found an indirect target symbol at 0x%" PRIx64 ".", |
| 901 | load_addr); |
| 902 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 903 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 904 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 905 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 906 | } |
| 907 | } else if (current_symbol->GetType() == eSymbolTypeReExported) { |
| 908 | // I am not sure we could ever end up stopped AT a re-exported symbol. |
| 909 | // But just in case: |
| 910 | |
| 911 | const Symbol *actual_symbol = |
| 912 | current_symbol->ResolveReExportedSymbol(*(target_sp.get())); |
| 913 | if (actual_symbol) { |
| 914 | Address target_addr(actual_symbol->GetAddress()); |
| 915 | if (target_addr.IsValid()) { |
| 916 | if (log) |
| 917 | log->Printf( |
| 918 | "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64 |
| 919 | ".", |
| 920 | current_symbol->GetName().GetCString(), |
| 921 | actual_symbol->GetName().GetCString(), |
| 922 | target_addr.GetLoadAddress(target_sp.get())); |
| 923 | addresses.push_back(target_addr.GetLoadAddress(target_sp.get())); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 924 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 925 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 928 | if (addresses.size() > 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 929 | // First check whether any of the addresses point to Indirect symbols, |
| 930 | // and if they do, resolve them: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 931 | std::vector<lldb::addr_t> load_addrs; |
| 932 | for (Address address : addresses) { |
| 933 | Symbol *symbol = address.CalculateSymbolContextSymbol(); |
| 934 | if (symbol && symbol->IsIndirect()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 935 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 936 | Address symbol_address = symbol->GetAddress(); |
| 937 | addr_t resolved_addr = thread.GetProcess()->ResolveIndirectFunction( |
| 938 | &symbol_address, error); |
| 939 | if (error.Success()) { |
| 940 | load_addrs.push_back(resolved_addr); |
| 941 | if (log) |
| 942 | log->Printf("ResolveIndirectFunction found resolved target for " |
| 943 | "%s at 0x%" PRIx64 ".", |
| 944 | symbol->GetName().GetCString(), resolved_addr); |
| 945 | } |
| 946 | } else { |
| 947 | load_addrs.push_back(address.GetLoadAddress(target_sp.get())); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 948 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 949 | } |
| 950 | thread_plan_sp.reset( |
| 951 | new ThreadPlanRunToAddress(thread, load_addrs, stop_others)); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 952 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 953 | } else { |
| 954 | if (log) |
| 955 | log->Printf("Could not find symbol for step through."); |
| 956 | } |
| 957 | |
| 958 | return thread_plan_sp; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 961 | size_t DynamicLoaderDarwin::FindEquivalentSymbols( |
| 962 | lldb_private::Symbol *original_symbol, lldb_private::ModuleList &images, |
| 963 | lldb_private::SymbolContextList &equivalent_symbols) { |
| 964 | const ConstString &trampoline_name = original_symbol->GetMangled().GetName( |
| 965 | original_symbol->GetLanguage(), Mangled::ePreferMangled); |
| 966 | if (!trampoline_name) |
| 967 | return 0; |
| 968 | |
| 969 | size_t initial_size = equivalent_symbols.GetSize(); |
| 970 | |
| 971 | static const char *resolver_name_regex = "(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$"; |
| 972 | std::string equivalent_regex_buf("^"); |
| 973 | equivalent_regex_buf.append(trampoline_name.GetCString()); |
| 974 | equivalent_regex_buf.append(resolver_name_regex); |
| 975 | |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 976 | RegularExpression equivalent_name_regex(equivalent_regex_buf); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 977 | const bool append = true; |
| 978 | images.FindSymbolsMatchingRegExAndType(equivalent_name_regex, eSymbolTypeCode, |
| 979 | equivalent_symbols, append); |
| 980 | |
| 981 | return equivalent_symbols.GetSize() - initial_size; |
| 982 | } |
| 983 | |
| 984 | lldb::ModuleSP DynamicLoaderDarwin::GetPThreadLibraryModule() { |
| 985 | ModuleSP module_sp = m_libpthread_module_wp.lock(); |
| 986 | if (!module_sp) { |
| 987 | SymbolContextList sc_list; |
| 988 | ModuleSpec module_spec; |
| 989 | module_spec.GetFileSpec().GetFilename().SetCString( |
| 990 | "libsystem_pthread.dylib"); |
| 991 | ModuleList module_list; |
| 992 | if (m_process->GetTarget().GetImages().FindModules(module_spec, |
| 993 | module_list)) { |
| 994 | if (module_list.GetSize() == 1) { |
| 995 | module_sp = module_list.GetModuleAtIndex(0); |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 996 | if (module_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 997 | m_libpthread_module_wp = module_sp; |
| 998 | } |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 999 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1000 | } |
| 1001 | return module_sp; |
| 1002 | } |
| 1003 | |
| 1004 | Address DynamicLoaderDarwin::GetPthreadSetSpecificAddress() { |
| 1005 | if (!m_pthread_getspecific_addr.IsValid()) { |
| 1006 | ModuleSP module_sp = GetPThreadLibraryModule(); |
| 1007 | if (module_sp) { |
| 1008 | lldb_private::SymbolContextList sc_list; |
| 1009 | module_sp->FindSymbolsWithNameAndType(ConstString("pthread_getspecific"), |
| 1010 | eSymbolTypeCode, sc_list); |
| 1011 | SymbolContext sc; |
| 1012 | if (sc_list.GetContextAtIndex(0, sc)) { |
| 1013 | if (sc.symbol) |
| 1014 | m_pthread_getspecific_addr = sc.symbol->GetAddress(); |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | return m_pthread_getspecific_addr; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | lldb::addr_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1022 | DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp, |
| 1023 | const lldb::ThreadSP thread_sp, |
| 1024 | lldb::addr_t tls_file_addr) { |
| 1025 | if (!thread_sp || !module_sp) |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1026 | return LLDB_INVALID_ADDRESS; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1027 | |
| 1028 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 1029 | |
| 1030 | const uint32_t addr_size = m_process->GetAddressByteSize(); |
| 1031 | uint8_t buf[sizeof(lldb::addr_t) * 3]; |
| 1032 | |
| 1033 | lldb_private::Address tls_addr; |
| 1034 | if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr)) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1035 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1036 | const size_t tsl_data_size = addr_size * 3; |
| 1037 | Target &target = m_process->GetTarget(); |
| 1038 | if (target.ReadMemory(tls_addr, false, buf, tsl_data_size, error) == |
| 1039 | tsl_data_size) { |
| 1040 | const ByteOrder byte_order = m_process->GetByteOrder(); |
| 1041 | DataExtractor data(buf, sizeof(buf), byte_order, addr_size); |
| 1042 | lldb::offset_t offset = addr_size; // Skip the first pointer |
| 1043 | const lldb::addr_t pthread_key = data.GetAddress(&offset); |
| 1044 | const lldb::addr_t tls_offset = data.GetAddress(&offset); |
| 1045 | if (pthread_key != 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1046 | // First check to see if we have already figured out the location of |
| 1047 | // TLS data for the pthread_key on a specific thread yet. If we have we |
| 1048 | // can re-use it since its location will not change unless the process |
| 1049 | // execs. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1050 | const tid_t tid = thread_sp->GetID(); |
| 1051 | auto tid_pos = m_tid_to_tls_map.find(tid); |
| 1052 | if (tid_pos != m_tid_to_tls_map.end()) { |
| 1053 | auto tls_pos = tid_pos->second.find(pthread_key); |
| 1054 | if (tls_pos != tid_pos->second.end()) { |
| 1055 | return tls_pos->second + tls_offset; |
| 1056 | } |
| 1057 | } |
| 1058 | StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0); |
| 1059 | if (frame_sp) { |
| 1060 | ClangASTContext *clang_ast_context = |
| 1061 | target.GetScratchClangASTContext(); |
| 1062 | |
| 1063 | if (!clang_ast_context) |
| 1064 | return LLDB_INVALID_ADDRESS; |
| 1065 | |
| 1066 | CompilerType clang_void_ptr_type = |
| 1067 | clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); |
| 1068 | Address pthread_getspecific_addr = GetPthreadSetSpecificAddress(); |
| 1069 | if (pthread_getspecific_addr.IsValid()) { |
| 1070 | EvaluateExpressionOptions options; |
| 1071 | |
| 1072 | lldb::ThreadPlanSP thread_plan_sp(new ThreadPlanCallFunction( |
| 1073 | *thread_sp, pthread_getspecific_addr, clang_void_ptr_type, |
| 1074 | llvm::ArrayRef<lldb::addr_t>(pthread_key), options)); |
| 1075 | |
| 1076 | DiagnosticManager execution_errors; |
| 1077 | ExecutionContext exe_ctx(thread_sp); |
| 1078 | lldb::ExpressionResults results = m_process->RunThreadPlan( |
| 1079 | exe_ctx, thread_plan_sp, options, execution_errors); |
| 1080 | |
| 1081 | if (results == lldb::eExpressionCompleted) { |
| 1082 | lldb::ValueObjectSP result_valobj_sp = |
| 1083 | thread_plan_sp->GetReturnValueObject(); |
| 1084 | if (result_valobj_sp) { |
| 1085 | const lldb::addr_t pthread_key_data = |
| 1086 | result_valobj_sp->GetValueAsUnsigned(0); |
| 1087 | if (pthread_key_data) { |
| 1088 | m_tid_to_tls_map[tid].insert( |
| 1089 | std::make_pair(pthread_key, pthread_key_data)); |
| 1090 | return pthread_key_data + tls_offset; |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | return LLDB_INVALID_ADDRESS; |
Jason Molenda | 5fe4d14 | 2016-07-17 21:27:32 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1102 | bool DynamicLoaderDarwin::UseDYLDSPI(Process *process) { |
| 1103 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1104 | bool use_new_spi_interface = false; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1105 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 1106 | llvm::VersionTuple version = process->GetHostOSVersion(); |
| 1107 | if (!version.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1108 | const llvm::Triple::OSType os_type = |
| 1109 | process->GetTarget().GetArchitecture().GetTriple().getOS(); |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1111 | // macOS 10.12 and newer |
| 1112 | if (os_type == llvm::Triple::MacOSX && |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 1113 | version >= llvm::VersionTuple(10, 12)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1114 | use_new_spi_interface = true; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1116 | // iOS 10 and newer |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 1117 | if (os_type == llvm::Triple::IOS && version >= llvm::VersionTuple(10)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1118 | use_new_spi_interface = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1119 | |
| 1120 | // tvOS 10 and newer |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 1121 | if (os_type == llvm::Triple::TvOS && version >= llvm::VersionTuple(10)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1122 | use_new_spi_interface = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1123 | |
| 1124 | // watchOS 3 and newer |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 1125 | if (os_type == llvm::Triple::WatchOS && version >= llvm::VersionTuple(3)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1126 | use_new_spi_interface = true; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 1127 | |
| 1128 | // NEED_BRIDGEOS_TRIPLE // Any BridgeOS |
| 1129 | // NEED_BRIDGEOS_TRIPLE if (os_type == llvm::Triple::BridgeOS) |
| 1130 | // NEED_BRIDGEOS_TRIPLE use_new_spi_interface = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1133 | if (log) { |
| 1134 | if (use_new_spi_interface) |
| 1135 | log->Printf( |
| 1136 | "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin"); |
| 1137 | else |
| 1138 | log->Printf( |
| 1139 | "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin"); |
| 1140 | } |
| 1141 | return use_new_spi_interface; |
Jason Molenda | 9ab5dc2 | 2016-07-21 08:30:55 +0000 | [diff] [blame] | 1142 | } |