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