#include "DynamicLoaderMacOSXDYLD.h"
#include "DynamicLoaderDarwin.h"
#include "DynamicLoaderMacOS.h"
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/Breakpoint/StoppointCallbackContext.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Section.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadPlanRunToAddress.h"
#include "lldb/Utility/DataBuffer.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/State.h"
#ifdef ENABLE_DEBUG_PRINTF
#include <cstdio>
#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DEBUG_PRINTF(fmt, ...)
#endif
#ifndef __APPLE__
#include "lldb/Utility/AppleUuidCompatibility.h"
#else
#include <uuid/uuid.h>
#endif
using namespace lldb;
using namespace lldb_private;
LLDB_PLUGIN_DEFINE(DynamicLoaderMacOSXDYLD)
DynamicLoader *DynamicLoaderMacOSXDYLD::CreateInstance(Process *process,
bool force) {
bool create = force;
if (!create) {
create = true;
Module *exe_module = process->GetTarget().GetExecutableModulePointer();
if (exe_module) {
ObjectFile *object_file = exe_module->GetObjectFile();
if (object_file) {
create = (object_file->GetStrata() == ObjectFile::eStrataUser);
}
}
if (create) {
const llvm::Triple &triple_ref =
process->GetTarget().GetArchitecture().GetTriple();
switch (triple_ref.getOS()) {
case llvm::Triple::Darwin:
case llvm::Triple::MacOSX:
case llvm::Triple::IOS:
case llvm::Triple::TvOS:
case llvm::Triple::WatchOS:
case llvm::Triple::XROS:
case llvm::Triple::BridgeOS:
create = triple_ref.getVendor() == llvm::Triple::Apple;
break;
default:
create = false;
break;
}
}
}
if (UseDYLDSPI(process)) {
create = false;
}
if (create)
return new DynamicLoaderMacOSXDYLD(process);
return nullptr;
}
DynamicLoaderMacOSXDYLD::DynamicLoaderMacOSXDYLD(Process *process)
: DynamicLoaderDarwin(process),
m_dyld_all_image_infos_addr(LLDB_INVALID_ADDRESS),
m_dyld_all_image_infos(), m_dyld_all_image_infos_stop_id(UINT32_MAX),
m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
m_process_image_addr_is_all_images_infos(false) {}
DynamicLoaderMacOSXDYLD::~DynamicLoaderMacOSXDYLD() {
if (LLDB_BREAK_ID_IS_VALID(m_break_id))
m_process->GetTarget().RemoveBreakpointByID(m_break_id);
}
bool DynamicLoaderMacOSXDYLD::ProcessDidExec() {
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
bool did_exec = false;
if (m_process) {
if (m_process->GetThreadList().GetSize() == 1) {
const addr_t shlib_addr = m_process->GetImageInfoAddress();
if (m_process_image_addr_is_all_images_infos &&
shlib_addr != m_dyld_all_image_infos_addr) {
did_exec = true;
} else if (!m_process_image_addr_is_all_images_infos &&
shlib_addr == m_dyld.address) {
did_exec = true;
} else {
ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
if (thread_sp) {
lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
if (frame_sp) {
const Symbol *symbol =
frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
if (symbol) {
if (symbol->GetName() == "_dyld_start")
did_exec = true;
}
}
}
}
if (did_exec) {
m_libpthread_module_wp.reset();
m_pthread_getspecific_addr.Clear();
}
}
}
return did_exec;
}
void DynamicLoaderMacOSXDYLD::DoClear() {
std::lock_guard<std::recursive_mutex> guard(m_mutex);
if (LLDB_BREAK_ID_IS_VALID(m_break_id))
m_process->GetTarget().RemoveBreakpointByID(m_break_id);
m_dyld_all_image_infos_addr = LLDB_INVALID_ADDRESS;
m_dyld_all_image_infos.Clear();
m_break_id = LLDB_INVALID_BREAK_ID;
}
bool DynamicLoaderMacOSXDYLD::DidSetNotificationBreakpoint() {
return LLDB_BREAK_ID_IS_VALID(m_break_id);
}
void DynamicLoaderMacOSXDYLD::ClearNotificationBreakpoint() {
if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
m_process->GetTarget().RemoveBreakpointByID(m_break_id);
}
}
void DynamicLoaderMacOSXDYLD::DoInitialImageFetch() {
if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS) {
const addr_t shlib_addr = m_process->GetImageInfoAddress();
if (shlib_addr != LLDB_INVALID_ADDRESS) {
ByteOrder byte_order =
m_process->GetTarget().GetArchitecture().GetByteOrder();
uint8_t buf[4];
DataExtractor data(buf, sizeof(buf), byte_order, 4);
Status error;
if (m_process->ReadMemory(shlib_addr, buf, 4, error) == 4) {
lldb::offset_t offset = 0;
uint32_t magic = data.GetU32(&offset);
switch (magic) {
case llvm::MachO::MH_MAGIC:
case llvm::MachO::MH_MAGIC_64:
case llvm::MachO::MH_CIGAM:
case llvm::MachO::MH_CIGAM_64:
m_process_image_addr_is_all_images_infos = false;
ReadDYLDInfoFromMemoryAndSetNotificationCallback(shlib_addr);
return;
default:
break;
}
}
m_dyld_all_image_infos_addr = shlib_addr;
m_process_image_addr_is_all_images_infos = true;
}
}
if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) {
if (ReadAllImageInfosStructure()) {
if (m_dyld_all_image_infos.dyldImageLoadAddress != LLDB_INVALID_ADDRESS)
ReadDYLDInfoFromMemoryAndSetNotificationCallback(
m_dyld_all_image_infos.dyldImageLoadAddress);
else
ReadDYLDInfoFromMemoryAndSetNotificationCallback(
m_dyld_all_image_infos_addr & 0xfffffffffff00000ull);
return;
}
}
Module *executable = m_process->GetTarget().GetExecutableModulePointer();
if (executable) {
const ArchSpec &exe_arch = executable->GetArchitecture();
if (exe_arch.GetAddressByteSize() == 8) {
ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x7fff5fc00000ull);
} else if (exe_arch.GetMachine() == llvm::Triple::arm ||
exe_arch.GetMachine() == llvm::Triple::thumb ||
exe_arch.GetMachine() == llvm::Triple::aarch64 ||
exe_arch.GetMachine() == llvm::Triple::aarch64_32) {
ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x2fe00000);
} else {
ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x8fe00000);
}
}
}
bool DynamicLoaderMacOSXDYLD::ReadDYLDInfoFromMemoryAndSetNotificationCallback(
lldb::addr_t addr) {
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
DataExtractor data;
static ConstString g_dyld_all_image_infos("dyld_all_image_infos");
static ConstString g_new_dyld_all_image_infos("dyld4::dyld_all_image_infos");
if (ReadMachHeader(addr, &m_dyld.header, &data)) {
if (m_dyld.header.filetype == llvm::MachO::MH_DYLINKER) {
m_dyld.address = addr;
ModuleSP dyld_module_sp;
if (ParseLoadCommands(data, m_dyld, &m_dyld.file_spec)) {
if (m_dyld.file_spec) {
UpdateDYLDImageInfoFromNewImageInfo(m_dyld);
}
}
dyld_module_sp = GetDYLDModule();
Target &target = m_process->GetTarget();
if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS &&
dyld_module_sp.get()) {
const Symbol *symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
g_dyld_all_image_infos, eSymbolTypeData);
if (!symbol) {
symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
g_new_dyld_all_image_infos, eSymbolTypeData);
}
if (symbol)
m_dyld_all_image_infos_addr = symbol->GetLoadAddress(&target);
}
if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS) {
ConstString g_sect_name("__all_image_info");
SectionSP dyld_aii_section_sp =
dyld_module_sp->GetSectionList()->FindSectionByName(g_sect_name);
if (dyld_aii_section_sp) {
Address dyld_aii_addr(dyld_aii_section_sp, 0);
m_dyld_all_image_infos_addr = dyld_aii_addr.GetLoadAddress(&target);
}
}
InitializeFromAllImageInfos();
if (dyld_module_sp) {
target.GetImages().AppendIfNeeded(dyld_module_sp);
ModuleList modules;
modules.Append(dyld_module_sp);
target.ModulesDidLoad(modules);
SetDYLDModule(dyld_module_sp);
}
return true;
}
}
return false;
}
bool DynamicLoaderMacOSXDYLD::NeedToDoInitialImageFetch() {
return m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS;
}
bool DynamicLoaderMacOSXDYLD::NotifyBreakpointHit(
void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
lldb::user_id_t break_loc_id) {
DynamicLoaderMacOSXDYLD *dyld_instance = (DynamicLoaderMacOSXDYLD *)baton;
ExecutionContext exe_ctx(context->exe_ctx_ref);
Process *process = exe_ctx.GetProcessPtr();
if (process != dyld_instance->m_process)
return false;
if (dyld_instance->InitializeFromAllImageInfos())
return dyld_instance->GetStopWhenImagesChange();
const lldb::ABISP &abi = process->GetABI();
if (abi) {
TypeSystemClangSP scratch_ts_sp =
ScratchTypeSystemClang::GetForTarget(process->GetTarget());
if (!scratch_ts_sp)
return false;
ValueList argument_values;
Value input_value;
CompilerType clang_void_ptr_type =
scratch_ts_sp->GetBasicType(eBasicTypeVoid).GetPointerType();
CompilerType clang_uint32_type =
scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingUint,
32);
input_value.SetValueType(Value::ValueType::Scalar);
input_value.SetCompilerType(clang_uint32_type);
argument_values.PushValue(input_value);
argument_values.PushValue(input_value);
input_value.SetCompilerType(clang_void_ptr_type);
argument_values.PushValue(input_value);
if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
uint32_t dyld_mode =
argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
if (dyld_mode != static_cast<uint32_t>(-1)) {
uint32_t image_infos_count =
argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
if (image_infos_count != static_cast<uint32_t>(-1)) {
lldb::addr_t image_infos_addr =
argument_values.GetValueAtIndex(2)->GetScalar().ULongLong();
if (dyld_mode == 0) {
dyld_instance->AddModulesUsingImageInfosAddress(image_infos_addr,
image_infos_count);
} else {
dyld_instance->RemoveModulesUsingImageInfosAddress(
image_infos_addr, image_infos_count);
}
}
}
}
} else {
Target &target = process->GetTarget();
Debugger::ReportWarning(
"no ABI plugin located for triple " +
target.GetArchitecture().GetTriple().getTriple() +
": shared libraries will not be registered",
target.GetDebugger().GetID());
}
return dyld_instance->GetStopWhenImagesChange();
}
bool DynamicLoaderMacOSXDYLD::ReadAllImageInfosStructure() {
std::lock_guard<std::recursive_mutex> guard(m_mutex);
if (m_process->GetStopID() == m_dyld_all_image_infos_stop_id)
return true;
m_dyld_all_image_infos.Clear();
if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) {
ByteOrder byte_order =
m_process->GetTarget().GetArchitecture().GetByteOrder();
uint32_t addr_size =
m_process->GetTarget().GetArchitecture().GetAddressByteSize();
uint8_t buf[256];
DataExtractor data(buf, sizeof(buf), byte_order, addr_size);
lldb::offset_t offset = 0;
const size_t count_v2 = sizeof(uint32_t) +
sizeof(uint32_t) +
addr_size +
addr_size +
addr_size +
addr_size;
const size_t count_v11 = count_v2 + addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size +
addr_size;
const size_t count_v13 = count_v11 + addr_size +
sizeof(uuid_t);
UNUSED_IF_ASSERT_DISABLED(count_v13);
assert(sizeof(buf) >= count_v13);
Status error;
if (m_process->ReadMemory(m_dyld_all_image_infos_addr, buf, 4, error) ==
4) {
m_dyld_all_image_infos.version = data.GetU32(&offset);
if (m_dyld_all_image_infos.version & 0xff000000) {
if (byte_order == eByteOrderLittle)
byte_order = eByteOrderBig;
else
byte_order = eByteOrderLittle;
data.SetByteOrder(byte_order);
offset = 0;
m_dyld_all_image_infos.version = data.GetU32(&offset);
}
} else {
return false;
}
const size_t count =
(m_dyld_all_image_infos.version >= 11) ? count_v11 : count_v2;
const size_t bytes_read =
m_process->ReadMemory(m_dyld_all_image_infos_addr, buf, count, error);
if (bytes_read == count) {
offset = 0;
m_dyld_all_image_infos.version = data.GetU32(&offset);
m_dyld_all_image_infos.dylib_info_count = data.GetU32(&offset);
m_dyld_all_image_infos.dylib_info_addr = data.GetAddress(&offset);
m_dyld_all_image_infos.notification = data.GetAddress(&offset);
m_dyld_all_image_infos.processDetachedFromSharedRegion =
data.GetU8(&offset);
m_dyld_all_image_infos.libSystemInitialized = data.GetU8(&offset);
offset += addr_size - 2;
m_dyld_all_image_infos.dyldImageLoadAddress = data.GetAddress(&offset);
if (m_dyld_all_image_infos.version >= 11) {
offset += addr_size * 8;
uint64_t dyld_all_image_infos_addr = data.GetAddress(&offset);
if (dyld_all_image_infos_addr != m_dyld_all_image_infos_addr) {
uint64_t image_infos_offset =
dyld_all_image_infos_addr -
m_dyld_all_image_infos.dyldImageLoadAddress;
uint64_t notification_offset =
m_dyld_all_image_infos.notification -
m_dyld_all_image_infos.dyldImageLoadAddress;
m_dyld_all_image_infos.dyldImageLoadAddress =
m_dyld_all_image_infos_addr - image_infos_offset;
m_dyld_all_image_infos.notification =
m_dyld_all_image_infos.dyldImageLoadAddress + notification_offset;
}
}
m_dyld_all_image_infos_stop_id = m_process->GetStopID();
return true;
}
}
return false;
}
bool DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfosAddress(
lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
ImageInfo::collection image_infos;
Log *log = GetLog(LLDBLog::DynamicLoader);
LLDB_LOGF(log, "Adding %d modules.\n", image_infos_count);
std::lock_guard<std::recursive_mutex> guard(m_mutex);
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
return true;
StructuredData::ObjectSP image_infos_json_sp =
m_process->GetLoadedDynamicLibrariesInfos(image_infos_addr,
image_infos_count);
if (image_infos_json_sp.get() && image_infos_json_sp->GetAsDictionary() &&
image_infos_json_sp->GetAsDictionary()->HasKey("images") &&
image_infos_json_sp->GetAsDictionary()
->GetValueForKey("images")
->GetAsArray() &&
image_infos_json_sp->GetAsDictionary()
->GetValueForKey("images")
->GetAsArray()
->GetSize() == image_infos_count) {
bool return_value = false;
if (JSONImageInformationIntoImageInfo(image_infos_json_sp, image_infos)) {
UpdateSpecialBinariesFromNewImageInfos(image_infos);
return_value = AddModulesUsingImageInfos(image_infos);
}
m_dyld_image_infos_stop_id = m_process->GetStopID();
return return_value;
}
if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos))
return false;
UpdateImageInfosHeaderAndLoadCommands(image_infos, image_infos_count, false);
bool return_value = AddModulesUsingImageInfos(image_infos);
m_dyld_image_infos_stop_id = m_process->GetStopID();
return return_value;
}
bool DynamicLoaderMacOSXDYLD::RemoveModulesUsingImageInfosAddress(
lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
ImageInfo::collection image_infos;
Log *log = GetLog(LLDBLog::DynamicLoader);
std::lock_guard<std::recursive_mutex> guard(m_mutex);
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
return true;
if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) {
if (log)
log->PutCString("Failed reading image infos array.");
return false;
}
LLDB_LOGF(log, "Removing %d modules.", image_infos_count);
ModuleList unloaded_module_list;
for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
if (log) {
LLDB_LOGF(log, "Removing module at address=0x%16.16" PRIx64 ".",
image_infos[idx].address);
image_infos[idx].PutToLog(log);
}
bool found = false;
for (ImageInfo::collection::iterator pos = m_dyld_image_infos.begin();
pos != m_dyld_image_infos.end(); pos++) {
if (image_infos[idx].address == (*pos).address) {
image_infos[idx].uuid = (*pos).uuid;
ModuleSP unload_image_module_sp(
FindTargetModuleForImageInfo(image_infos[idx], false, nullptr));
if (unload_image_module_sp.get()) {
UnloadModuleSections(unload_image_module_sp.get(), *pos);
unloaded_module_list.AppendIfNeeded(unload_image_module_sp);
} else {
if (log) {
LLDB_LOGF(log, "Could not find module for unloading info entry:");
image_infos[idx].PutToLog(log);
}
}
m_dyld_image_infos.erase(pos);
found = true;
break;
}
}
if (!found) {
if (log) {
LLDB_LOGF(log, "Could not find image_info entry for unloading image:");
image_infos[idx].PutToLog(log);
}
}
}
if (unloaded_module_list.GetSize() > 0) {
if (log) {
log->PutCString("Unloaded:");
unloaded_module_list.LogUUIDAndPaths(
log, "DynamicLoaderMacOSXDYLD::ModulesDidUnload");
}
m_process->GetTarget().GetImages().Remove(unloaded_module_list);
}
m_dyld_image_infos_stop_id = m_process->GetStopID();
return true;
}
bool DynamicLoaderMacOSXDYLD::ReadImageInfos(
lldb::addr_t image_infos_addr, uint32_t image_infos_count,
ImageInfo::collection &image_infos) {
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
const ByteOrder endian = GetByteOrderFromMagic(m_dyld.header.magic);
const uint32_t addr_size = m_dyld.GetAddressByteSize();
image_infos.resize(image_infos_count);
const size_t count = image_infos.size() * 3 * addr_size;
DataBufferHeap info_data(count, 0);
Status error;
const size_t bytes_read = m_process->ReadMemory(
image_infos_addr, info_data.GetBytes(), info_data.GetByteSize(), error);
if (bytes_read == count) {
lldb::offset_t info_data_offset = 0;
DataExtractor info_data_ref(info_data.GetBytes(), info_data.GetByteSize(),
endian, addr_size);
for (size_t i = 0;
i < image_infos.size() && info_data_ref.ValidOffset(info_data_offset);
i++) {
image_infos[i].address = info_data_ref.GetAddress(&info_data_offset);
lldb::addr_t path_addr = info_data_ref.GetAddress(&info_data_offset);
info_data_ref.GetAddress(&info_data_offset);
char raw_path[PATH_MAX];
m_process->ReadCStringFromMemory(path_addr, raw_path, sizeof(raw_path),
error);
if (error.Success()) {
image_infos[i].file_spec.SetFile(raw_path, FileSpec::Style::native);
}
}
return true;
} else {
return false;
}
}
bool DynamicLoaderMacOSXDYLD::InitializeFromAllImageInfos() {
Log *log = GetLog(LLDBLog::DynamicLoader);
std::lock_guard<std::recursive_mutex> guard(m_mutex);
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
if (m_process->GetStopID() == m_dyld_image_infos_stop_id ||
m_dyld_image_infos.size() != 0)
return false;
if (ReadAllImageInfosStructure()) {
if (m_dyld_all_image_infos.dylib_info_count == 0)
return true;
if (m_dyld_all_image_infos.dylib_info_addr == 0) {
return false;
} else {
if (!AddModulesUsingImageInfosAddress(
m_dyld_all_image_infos.dylib_info_addr,
m_dyld_all_image_infos.dylib_info_count)) {
DEBUG_PRINTF("%s", "unable to read all data for all_dylib_infos.");
m_dyld_image_infos.clear();
}
}
Target &target = m_process->GetTarget();
ModuleList not_loaded_modules;
for (ModuleSP module_sp : target.GetImages().Modules()) {
if (!module_sp->IsLoadedInTarget(&target)) {
if (log) {
StreamString s;
module_sp->GetDescription(s.AsRawOstream());
LLDB_LOGF(log, "Unloading pre-run module: %s.", s.GetData());
}
not_loaded_modules.Append(module_sp);
}
}
if (not_loaded_modules.GetSize() != 0) {
target.GetImages().Remove(not_loaded_modules);
}
return true;
} else
return false;
}
bool DynamicLoaderMacOSXDYLD::ReadMachHeader(lldb::addr_t addr,
llvm::MachO::mach_header *header,
DataExtractor *load_command_data) {
DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0);
Status error;
size_t bytes_read = m_process->ReadMemory(addr, header_bytes.GetBytes(),
header_bytes.GetByteSize(), error);
if (bytes_read == sizeof(llvm::MachO::mach_header)) {
lldb::offset_t offset = 0;
::memset(header, 0, sizeof(llvm::MachO::mach_header));
DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(),
endian::InlHostByteOrder(), 4);
header->magic = data.GetU32(&offset);
lldb::addr_t load_cmd_addr = addr;
data.SetByteOrder(
DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(header->magic));
switch (header->magic) {
case llvm::MachO::MH_MAGIC:
case llvm::MachO::MH_CIGAM:
data.SetAddressByteSize(4);
load_cmd_addr += sizeof(llvm::MachO::mach_header);
break;
case llvm::MachO::MH_MAGIC_64:
case llvm::MachO::MH_CIGAM_64:
data.SetAddressByteSize(8);
load_cmd_addr += sizeof(llvm::MachO::mach_header_64);
break;
default:
return false;
}
if (data.GetU32(&offset, &header->cputype,
(sizeof(llvm::MachO::mach_header) / sizeof(uint32_t)) -
1)) {
if (load_command_data == nullptr)
return true;
WritableDataBufferSP load_cmd_data_sp(
new DataBufferHeap(header->sizeofcmds, 0));
size_t load_cmd_bytes_read =
m_process->ReadMemory(load_cmd_addr, load_cmd_data_sp->GetBytes(),
load_cmd_data_sp->GetByteSize(), error);
if (load_cmd_bytes_read == header->sizeofcmds) {
load_command_data->SetData(load_cmd_data_sp, 0, header->sizeofcmds);
load_command_data->SetByteOrder(data.GetByteOrder());
load_command_data->SetAddressByteSize(data.GetAddressByteSize());
return true;
}
return false;
}
}
return false;
}
uint32_t DynamicLoaderMacOSXDYLD::ParseLoadCommands(const DataExtractor &data,
ImageInfo &dylib_info,
FileSpec *lc_id_dylinker) {
lldb::offset_t offset = 0;
uint32_t cmd_idx;
Segment segment;
dylib_info.Clear(true);
for (cmd_idx = 0; cmd_idx < dylib_info.header.ncmds; cmd_idx++) {
if (data.ValidOffsetForDataOfSize(offset,
sizeof(llvm::MachO::load_command))) {
llvm::MachO::load_command load_cmd;
lldb::offset_t load_cmd_offset = offset;
load_cmd.cmd = data.GetU32(&offset);
load_cmd.cmdsize = data.GetU32(&offset);
switch (load_cmd.cmd) {
case llvm::MachO::LC_SEGMENT: {
segment.name.SetTrimmedCStringWithLength(
(const char *)data.GetData(&offset, 16), 16);
segment.vmaddr = data.GetU32(&offset);
segment.vmsize = data.GetU32(&offset);
segment.fileoff = data.GetU32(&offset);
segment.filesize = data.GetU32(&offset);
data.GetU32(&offset, &segment.maxprot, 4);
dylib_info.segments.push_back(segment);
} break;
case llvm::MachO::LC_SEGMENT_64: {
segment.name.SetTrimmedCStringWithLength(
(const char *)data.GetData(&offset, 16), 16);
data.GetU64(&offset, &segment.vmaddr, 4);
data.GetU32(&offset, &segment.maxprot, 4);
dylib_info.segments.push_back(segment);
} break;
case llvm::MachO::LC_ID_DYLINKER:
if (lc_id_dylinker) {
const lldb::offset_t name_offset =
load_cmd_offset + data.GetU32(&offset);
const char *path = data.PeekCStr(name_offset);
lc_id_dylinker->SetFile(path, FileSpec::Style::native);
FileSystem::Instance().Resolve(*lc_id_dylinker);
}
break;
case llvm::MachO::LC_UUID:
dylib_info.uuid = UUID(data.GetData(&offset, 16), 16);
break;
default:
break;
}
offset = load_cmd_offset + load_cmd.cmdsize;
}
}
const size_t num_sections = dylib_info.segments.size();
for (size_t i = 0; i < num_sections; ++i) {
if ((dylib_info.segments[i].fileoff == 0 &&
dylib_info.segments[i].filesize > 0) ||
(dylib_info.segments[i].name == "__TEXT")) {
dylib_info.slide = dylib_info.address - dylib_info.segments[i].vmaddr;
break;
}
}
return cmd_idx;
}
void DynamicLoaderMacOSXDYLD::UpdateImageInfosHeaderAndLoadCommands(
ImageInfo::collection &image_infos, uint32_t infos_count,
bool update_executable) {
uint32_t exe_idx = UINT32_MAX;
for (uint32_t i = 0; i < infos_count; i++) {
if (!image_infos[i].UUIDValid()) {
DataExtractor data;
if (!ReadMachHeader(image_infos[i].address, &image_infos[i].header,
&data))
continue;
ParseLoadCommands(data, image_infos[i], nullptr);
if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE)
exe_idx = i;
}
}
Target &target = m_process->GetTarget();
if (exe_idx < image_infos.size()) {
const bool can_create = true;
ModuleSP exe_module_sp(FindTargetModuleForImageInfo(image_infos[exe_idx],
can_create, nullptr));
if (exe_module_sp) {
UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]);
if (exe_module_sp.get() != target.GetExecutableModulePointer()) {
ModuleSP dyld_module_sp(GetDYLDModule());
m_process->GetTarget().SetExecutableModule(exe_module_sp,
eLoadDependentsNo);
if (dyld_module_sp) {
if (target.GetImages().AppendIfNeeded(dyld_module_sp)) {
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld);
}
}
}
}
}
}
void DynamicLoaderMacOSXDYLD::PutToLog(Log *log) const {
if (log == nullptr)
return;
std::lock_guard<std::recursive_mutex> guard(m_mutex);
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
LLDB_LOGF(log,
"dyld_all_image_infos = { version=%d, count=%d, addr=0x%8.8" PRIx64
", notify=0x%8.8" PRIx64 " }",
m_dyld_all_image_infos.version,
m_dyld_all_image_infos.dylib_info_count,
(uint64_t)m_dyld_all_image_infos.dylib_info_addr,
(uint64_t)m_dyld_all_image_infos.notification);
size_t i;
const size_t count = m_dyld_image_infos.size();
if (count > 0) {
log->PutCString("Loaded:");
for (i = 0; i < count; i++)
m_dyld_image_infos[i].PutToLog(log);
}
}
bool DynamicLoaderMacOSXDYLD::SetNotificationBreakpoint() {
DEBUG_PRINTF("DynamicLoaderMacOSXDYLD::%s() process state = %s\n",
__FUNCTION__, StateAsCString(m_process->GetState()));
if (m_break_id == LLDB_INVALID_BREAK_ID) {
if (m_dyld_all_image_infos.notification != LLDB_INVALID_ADDRESS) {
Address so_addr;
bool resolved = m_process->GetTarget().ResolveLoadAddress(
m_dyld_all_image_infos.notification, so_addr);
if (!resolved) {
ModuleSP dyld_module_sp = GetDYLDModule();
if (dyld_module_sp) {
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld);
resolved = m_process->GetTarget().ResolveLoadAddress(
m_dyld_all_image_infos.notification, so_addr);
}
}
if (resolved) {
Breakpoint *dyld_break =
m_process->GetTarget().CreateBreakpoint(so_addr, true, false).get();
dyld_break->SetCallback(DynamicLoaderMacOSXDYLD::NotifyBreakpointHit,
this, true);
dyld_break->SetBreakpointKind("shared-library-event");
m_break_id = dyld_break->GetID();
}
}
}
return m_break_id != LLDB_INVALID_BREAK_ID;
}
Status DynamicLoaderMacOSXDYLD::CanLoadImage() {
Status error;
if (ReadAllImageInfosStructure()) {
if (m_dyld_all_image_infos.dylib_info_addr != 0)
return error;
}
error.SetErrorString("unsafe to load or unload shared libraries");
return error;
}
bool DynamicLoaderMacOSXDYLD::GetSharedCacheInformation(
lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
LazyBool &private_shared_cache) {
base_address = LLDB_INVALID_ADDRESS;
uuid.Clear();
using_shared_cache = eLazyBoolCalculate;
private_shared_cache = eLazyBoolCalculate;
if (m_process) {
addr_t all_image_infos = m_process->GetImageInfoAddress();
Status err;
uint32_t version_or_magic =
m_process->ReadUnsignedIntegerFromMemory(all_image_infos, 4, -1, err);
if (version_or_magic != static_cast<uint32_t>(-1) &&
version_or_magic != llvm::MachO::MH_MAGIC &&
version_or_magic != llvm::MachO::MH_CIGAM &&
version_or_magic != llvm::MachO::MH_MAGIC_64 &&
version_or_magic != llvm::MachO::MH_CIGAM_64 &&
version_or_magic >= 13) {
addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
int wordsize = m_process->GetAddressByteSize();
if (wordsize == 8) {
sharedCacheUUID_address =
all_image_infos + 160;
}
if (wordsize == 4) {
sharedCacheUUID_address =
all_image_infos + 84;
}
if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS) {
uuid_t shared_cache_uuid;
if (m_process->ReadMemory(sharedCacheUUID_address, shared_cache_uuid,
sizeof(uuid_t), err) == sizeof(uuid_t)) {
uuid = UUID(shared_cache_uuid, 16);
if (uuid.IsValid()) {
using_shared_cache = eLazyBoolYes;
}
}
if (version_or_magic >= 15) {
addr_t sharedCacheBaseAddr_address = sharedCacheUUID_address + 16;
Status error;
base_address = m_process->ReadUnsignedIntegerFromMemory(
sharedCacheBaseAddr_address, wordsize, LLDB_INVALID_ADDRESS,
error);
if (error.Fail())
base_address = LLDB_INVALID_ADDRESS;
}
return true;
}
}
}
return false;
}
bool DynamicLoaderMacOSXDYLD::IsFullyInitialized() {
if (ReadAllImageInfosStructure())
return m_dyld_all_image_infos.libSystemInitialized;
return false;
}
void DynamicLoaderMacOSXDYLD::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance);
DynamicLoaderMacOS::Initialize();
}
void DynamicLoaderMacOSXDYLD::Terminate() {
DynamicLoaderMacOS::Terminate();
PluginManager::UnregisterPlugin(CreateInstance);
}
llvm::StringRef DynamicLoaderMacOSXDYLD::GetPluginDescriptionStatic() {
return "Dynamic loader plug-in that watches for shared library loads/unloads "
"in MacOSX user processes.";
}
uint32_t DynamicLoaderMacOSXDYLD::AddrByteSize() {
std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
switch (m_dyld.header.magic) {
case llvm::MachO::MH_MAGIC:
case llvm::MachO::MH_CIGAM:
return 4;
case llvm::MachO::MH_MAGIC_64:
case llvm::MachO::MH_CIGAM_64:
return 8;
default:
break;
}
return 0;
}
lldb::ByteOrder DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(uint32_t magic) {
switch (magic) {
case llvm::MachO::MH_MAGIC:
case llvm::MachO::MH_MAGIC_64:
return endian::InlHostByteOrder();
case llvm::MachO::MH_CIGAM:
case llvm::MachO::MH_CIGAM_64:
if (endian::InlHostByteOrder() == lldb::eByteOrderBig)
return lldb::eByteOrderLittle;
else
return lldb::eByteOrderBig;
default:
break;
}
return lldb::eByteOrderInvalid;
}