39 #ifdef ENABLE_DEBUG_PRINTF
41 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
43 #define DEBUG_PRINTF(fmt, ...)
49 #include <uuid/uuid.h>
58 DynamicLoaderDarwin::DynamicLoaderDarwin(
Process *process)
59 :
DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(),
60 m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(),
61 m_dyld_image_infos_stop_id(
UINT32_MAX), m_dyld(), m_mutex() {}
88 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
97 ImageInfo &image_info,
bool can_create,
bool *did_create_ptr) {
99 *did_create_ptr =
false;
109 if (target_triple.getOS() == llvm::Triple::IOS &&
110 target_triple.getEnvironment() == llvm::Triple::MacABI) {
120 !module_sp->GetUUID().IsValid()) {
123 if (module_sp->GetModificationTime() !=
128 if (module_sp || !can_create)
131 if (HostInfo::GetArchitecture().IsCompatibleMatch(target.
GetArchitecture())) {
142 if (image_info.
uuid &&
154 if (!module_sp || module_sp->GetObjectFile() ==
nullptr)
159 *did_create_ptr = (bool)module_sp;
165 const std::vector<lldb::addr_t> &solib_addresses) {
166 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
172 LLDB_LOGF(log,
"Removing %" PRId64
" modules.",
173 (uint64_t)solib_addresses.size());
177 for (
addr_t solib_addr : solib_addresses) {
181 ModuleSP module_to_remove(header.
GetModule());
182 if (module_to_remove.get()) {
183 LLDB_LOGF(log,
"Removing module at address 0x%" PRIx64, solib_addr);
191 if (solib_addr == (*pos).address) {
201 if (unloaded_module_list.
GetSize() > 0) {
205 log,
"DynamicLoaderDarwin::UnloadModules");
218 std::lock_guard<std::recursive_mutex> guard(target_modules.
GetMutex());
221 for (ModuleSP module_sp : target_modules.
Modules()) {
224 if (module_sp && module_sp != dyld_sp) {
226 unloaded_modules_list.
Append(module_sp);
230 if (unloaded_modules_list.
GetSize() != 0) {
234 log,
"DynamicLoaderDarwin::UnloadAllImages");
246 bool changed =
false;
249 if (image_object_file) {
252 std::vector<uint32_t> inaccessible_segment_indexes;
255 const size_t num_segments = info.
segments.size();
256 for (
size_t i = 0; i < num_segments; ++i) {
259 SectionSP section_sp(
262 if (info.
segments[i].maxprot == 0) {
263 inaccessible_segment_indexes.push_back(i);
265 const addr_t new_section_load_addr =
267 static ConstString g_section_name_LINKEDIT(
"__LINKEDIT");
274 const bool warn_multiple =
275 section_sp->GetName() != g_section_name_LINKEDIT;
278 section_sp, new_section_load_addr, warn_multiple);
289 if (changed && !inaccessible_segment_indexes.empty()) {
290 for (
uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) {
291 const uint32_t seg_idx = inaccessible_segment_indexes[i];
292 SectionSP section_sp(
296 static ConstString g_pagezero_section_name(
"__PAGEZERO");
297 if (g_pagezero_section_name == section_sp->GetName()) {
323 bool changed =
false;
326 if (image_object_file) {
329 const size_t num_segments = info.
segments.size();
330 for (
size_t i = 0; i < num_segments; ++i) {
331 SectionSP section_sp(
334 const addr_t old_section_load_addr =
337 section_sp, old_section_load_addr))
341 llvm::formatv(
"unable to find and unload segment named "
342 "'{0}' in '{1}' in macosx dynamic loader plug-in",
343 info.
segments[i].name.AsCString(
"<invalid>"),
360 image_details->GetAsDictionary()->GetValueForKey(
"images");
361 if (images_sp.get() ==
nullptr)
364 image_infos.resize(images_sp->GetAsArray()->GetSize());
366 for (
size_t i = 0; i < image_infos.size(); i++) {
368 images_sp->GetAsArray()->GetItemAtIndex(i);
369 if (image_sp.get() ==
nullptr || image_sp->GetAsDictionary() ==
nullptr)
373 if (!image->
HasKey(
"load_address") ||
374 !image->
HasKey(
"pathname") ||
375 !image->
HasKey(
"mod_date") ||
376 !image->
HasKey(
"mach_header") ||
377 image->
GetValueForKey(
"mach_header")->GetAsDictionary() ==
nullptr ||
378 !image->
HasKey(
"segments") ||
384 image_infos[i].address =
385 image->
GetValueForKey(
"load_address")->GetAsInteger()->GetValue();
386 image_infos[i].mod_date =
388 image_infos[i].file_spec.SetFile(
390 FileSpec::Style::native);
394 image_infos[i].header.magic =
396 image_infos[i].header.cputype =
398 image_infos[i].header.cpusubtype =
400 image_infos[i].header.filetype =
403 if (image->
HasKey(
"min_version_os_name")) {
408 if (os_name ==
"macosx")
409 image_infos[i].os_type = llvm::Triple::MacOSX;
410 else if (os_name ==
"ios" || os_name ==
"iphoneos")
411 image_infos[i].os_type = llvm::Triple::IOS;
412 else if (os_name ==
"tvos")
413 image_infos[i].os_type = llvm::Triple::TvOS;
414 else if (os_name ==
"watchos")
415 image_infos[i].os_type = llvm::Triple::WatchOS;
418 else if (os_name ==
"maccatalyst") {
419 image_infos[i].os_type = llvm::Triple::IOS;
420 image_infos[i].os_env = llvm::Triple::MacABI;
421 }
else if (os_name ==
"iossimulator") {
422 image_infos[i].os_type = llvm::Triple::IOS;
423 image_infos[i].os_env = llvm::Triple::Simulator;
424 }
else if (os_name ==
"tvossimulator") {
425 image_infos[i].os_type = llvm::Triple::TvOS;
426 image_infos[i].os_env = llvm::Triple::Simulator;
427 }
else if (os_name ==
"watchossimulator") {
428 image_infos[i].os_type = llvm::Triple::WatchOS;
429 image_infos[i].os_env = llvm::Triple::Simulator;
432 if (image->
HasKey(
"min_version_os_sdk")) {
433 image_infos[i].min_version_os_sdk =
443 image_infos[i].header.flags =
446 image_infos[i].header.flags = 0;
449 image_infos[i].header.ncmds =
452 image_infos[i].header.ncmds = 0;
454 if (mh->
HasKey(
"sizeofcmds"))
455 image_infos[i].header.sizeofcmds =
458 image_infos[i].header.sizeofcmds = 0;
463 for (
size_t j = 0; j < segcount; j++) {
483 if (seg->
HasKey(
"initprot"))
495 if (seg->
HasKey(
"nsects"))
501 image_infos[i].segments.push_back(segment);
504 image_infos[i].uuid.SetFromOptionalStringRef(
514 const size_t num_sections = image_infos[i].segments.size();
515 for (
size_t k = 0; k < num_sections; ++k) {
518 if ((image_infos[i].segments[k].fileoff == 0 &&
519 image_infos[i].segments[k].filesize > 0) ||
520 (image_infos[i].segments[k].name ==
"__TEXT")) {
521 image_infos[i].slide =
522 image_infos[i].address - image_infos[i].segments[k].vmaddr;
541 const size_t image_infos_size = image_infos.size();
542 for (
size_t i = 0; i < image_infos_size; i++) {
543 if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) {
548 if (target_arch.
GetTriple().getEnvironment() == llvm::Triple::Simulator &&
549 image_infos[i].os_type != llvm::Triple::OSType::MacOSX) {
555 if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) {
562 const bool can_create =
true;
564 can_create,
nullptr));
566 LLDB_LOGF(log,
"Found executable module: %s",
567 exe_module_sp->GetFileSpec().GetPath().c_str());
577 const bool can_create =
true;
579 can_create,
nullptr);
582 dyld_sp->GetFileSpec().GetPath().c_str());
592 if (image_info.
header.filetype == llvm::MachO::MH_DYLINKER) {
593 const bool can_create =
true;
618 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
625 for (
uint32_t idx = 0; idx < image_infos.size(); ++idx) {
627 LLDB_LOGF(log,
"Adding new image at address=0x%16.16" PRIx64
".",
628 image_infos[idx].address);
629 image_infos[idx].PutToLog(log);
634 ModuleSP image_module_sp(
637 if (image_module_sp) {
638 ObjectFile *objfile = image_module_sp->GetObjectFile();
645 if (commpage_section) {
647 image_infos[idx].GetArchitecture());
649 ModuleSP commpage_image_module_sp(
651 if (!commpage_image_module_sp) {
657 if (!commpage_image_module_sp ||
658 commpage_image_module_sp->GetObjectFile() ==
nullptr) {
660 image_infos[idx].file_spec, image_infos[idx].address);
665 bool changed =
false;
690 ArchSpec dyld_spec = image_infos[idx].GetArchitecture();
691 auto &dyld_triple = dyld_spec.
GetTriple();
692 if ((dyld_triple.getEnvironment() == llvm::Triple::MacABI &&
693 dyld_triple.getOS() == llvm::Triple::IOS) ||
694 (dyld_triple.getEnvironment() == llvm::Triple::Simulator &&
695 (dyld_triple.getOS() == llvm::Triple::IOS ||
696 dyld_triple.getOS() == llvm::Triple::TvOS ||
697 dyld_triple.getOS() == llvm::Triple::WatchOS)))
698 image_module_sp->MergeArchitecture(dyld_spec);
702 if (loaded_module_list.
GetSize() > 0) {
705 "DynamicLoaderDarwin::ModulesDidLoad");
728 if (module_sp.get() ==
nullptr && sym_ctx.
function) {
732 if (module_sp.get() ==
nullptr)
736 return objc_runtime !=
nullptr &&
745 LLDB_LOGF(log,
"\t\t%16s [0x%16.16" PRIx64
" - 0x%16.16" PRIx64
")",
749 "\t\t%16s [0x%16.16" PRIx64
" - 0x%16.16" PRIx64
750 ") slide = 0x%" PRIx64,
760 if (os_env == llvm::Triple::MacABI && os_type == llvm::Triple::IOS) {
762 "-apple-ios" + min_version_os_sdk +
"-macabi");
767 if (os_env == llvm::Triple::Simulator &&
768 (os_type == llvm::Triple::IOS || os_type == llvm::Triple::TvOS ||
769 os_type == llvm::Triple::WatchOS)) {
771 "-apple-" + llvm::Triple::getOSTypeName(os_type) +
772 min_version_os_sdk +
"-simulator");
782 const size_t num_segments = segments.size();
783 for (
size_t i = 0; i < num_segments; ++i) {
784 if (segments[i].name == name)
795 LLDB_LOG(log,
"modtime={0:x+8} uuid={1} path='{2}' (UNLOADED)", mod_date,
796 uuid.GetAsString(), file_spec.GetPath());
798 LLDB_LOG(log,
"address={0:x+16} modtime={1:x+8} uuid={2} path='{3}'",
799 address, mod_date, uuid.GetAsString(), file_spec.GetPath());
800 for (
uint32_t i = 0; i < segments.size(); ++i)
801 segments[i].PutToLog(log, slide);
806 DEBUG_PRINTF(
"DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__,
816 DEBUG_PRINTF(
"DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__,
851 ThreadPlanSP thread_plan_sp;
859 if (current_symbol !=
nullptr) {
860 std::vector<Address> addresses;
864 current_symbol->
GetMangled().GetName(Mangled::ePreferMangled);
866 if (trampoline_name) {
867 const ModuleList &images = target_sp->GetImages();
872 size_t num_code_symbols = code_symbols.
GetSize();
874 if (num_code_symbols > 0) {
875 for (
uint32_t i = 0; i < num_code_symbols; i++) {
887 "Found a trampoline target symbol at 0x%" PRIx64
".",
897 size_t num_reexported_symbols = reexported_symbols.
GetSize();
898 if (num_reexported_symbols > 0) {
899 for (
uint32_t i = 0; i < num_reexported_symbols; i++) {
906 const Address actual_symbol_addr =
908 if (actual_symbol_addr.
IsValid()) {
909 addresses.push_back(actual_symbol_addr);
915 "Found a re-exported symbol: %s at 0x%" PRIx64
".",
928 size_t num_indirect_symbols = indirect_symbols.
GetSize();
929 if (num_indirect_symbols > 0) {
930 for (
uint32_t i = 0; i < num_indirect_symbols; i++) {
942 "Found an indirect target symbol at 0x%" PRIx64
".",
953 const Symbol *actual_symbol =
960 "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64
970 if (addresses.size() > 0) {
973 std::vector<lldb::addr_t> load_addrs;
974 for (
Address address : addresses) {
980 &symbol_address,
error);
981 if (
error.Success()) {
982 load_addrs.push_back(resolved_addr);
984 "ResolveIndirectFunction found resolved target for "
985 "%s at 0x%" PRIx64
".",
989 load_addrs.push_back(address.GetLoadAddress(target_sp.get()));
992 thread_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
993 thread, load_addrs, stop_others);
996 LLDB_LOGF(log,
"Could not find symbol for step through.");
999 return thread_plan_sp;
1006 original_symbol->
GetMangled().GetName(Mangled::ePreferMangled);
1007 if (!trampoline_name)
1010 static const char *resolver_name_regex =
"(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$";
1012 equivalent_regex_buf.append(trampoline_name.
GetCString());
1013 equivalent_regex_buf.append(resolver_name_regex);
1017 equivalent_symbols);
1027 "libsystem_pthread.dylib");
1031 if (module_list.
GetSize() == 1) {
1046 module_sp->FindSymbolsWithNameAndType(
ConstString(
"pthread_getspecific"),
1060 const lldb::ThreadSP thread_sp,
1062 if (!thread_sp || !module_sp)
1065 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
1071 if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr)) {
1073 const size_t tsl_data_size = addr_size * 3;
1078 DataExtractor data(buf,
sizeof(buf), byte_order, addr_size);
1082 if (pthread_key != 0) {
1087 const tid_t tid = thread_sp->GetID();
1090 auto tls_pos = tid_pos->second.find(pthread_key);
1091 if (tls_pos != tid_pos->second.end()) {
1092 return tls_pos->second + tls_offset;
1095 StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0);
1100 if (!clang_ast_context)
1106 if (pthread_getspecific_addr.
IsValid()) {
1110 *thread_sp, pthread_getspecific_addr, clang_void_ptr_type,
1111 llvm::ArrayRef<lldb::addr_t>(pthread_key), options));
1116 exe_ctx, thread_plan_sp, options, execution_errors);
1119 lldb::ValueObjectSP result_valobj_sp =
1120 thread_plan_sp->GetReturnValueObject();
1121 if (result_valobj_sp) {
1123 result_valobj_sp->GetValueAsUnsigned(0);
1124 if (pthread_key_data) {
1126 std::make_pair(pthread_key, pthread_key_data));
1127 return pthread_key_data + tls_offset;
1141 bool use_new_spi_interface =
false;
1144 if (!version.empty()) {
1145 const llvm::Triple::OSType os_type =
1149 if (os_type == llvm::Triple::MacOSX &&
1150 version >= llvm::VersionTuple(10, 12))
1151 use_new_spi_interface =
true;
1154 if (os_type == llvm::Triple::IOS && version >= llvm::VersionTuple(10))
1155 use_new_spi_interface =
true;
1158 if (os_type == llvm::Triple::TvOS && version >= llvm::VersionTuple(10))
1159 use_new_spi_interface =
true;
1162 if (os_type == llvm::Triple::WatchOS && version >= llvm::VersionTuple(3))
1163 use_new_spi_interface =
true;
1171 if (use_new_spi_interface)
1173 log,
"DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin");
1176 log,
"DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin");
1178 return use_new_spi_interface;