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