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