#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/StringRef.h"
#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
#include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h"
#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Progress.h"
#include "lldb/Core/Section.h"
#include "lldb/Host/Host.h"
#include "lldb/Symbol/DWARFCallFrameInfo.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Target/DynamicLoader.h"
#include "lldb/Target/MemoryRegionInfo.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadList.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/DataBuffer.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/FileSpecList.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/RangeMap.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/Timer.h"
#include "lldb/Utility/UUID.h"
#include "lldb/Host/SafeMachO.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "ObjectFileMachO.h"
#if defined(__APPLE__)
#include <TargetConditionals.h>
#include <dlfcn.h>
#include <mach/mach_init.h>
#include <mach/vm_map.h>
#include <lldb/Host/SafeMachO.h>
#endif
#ifndef __APPLE__
#include "lldb/Utility/AppleUuidCompatibility.h"
#else
#include <uuid/uuid.h>
#endif
#include <bitset>
#include <memory>
#include <optional>
#ifdef CPU_TYPE_ARM
#undef CPU_TYPE_ARM
#endif
#ifdef CPU_TYPE_ARM64
#undef CPU_TYPE_ARM64
#endif
#ifdef CPU_TYPE_ARM64_32
#undef CPU_TYPE_ARM64_32
#endif
#ifdef CPU_TYPE_I386
#undef CPU_TYPE_I386
#endif
#ifdef CPU_TYPE_X86_64
#undef CPU_TYPE_X86_64
#endif
#ifdef MH_DYLINKER
#undef MH_DYLINKER
#endif
#ifdef MH_OBJECT
#undef MH_OBJECT
#endif
#ifdef LC_VERSION_MIN_MACOSX
#undef LC_VERSION_MIN_MACOSX
#endif
#ifdef LC_VERSION_MIN_IPHONEOS
#undef LC_VERSION_MIN_IPHONEOS
#endif
#ifdef LC_VERSION_MIN_TVOS
#undef LC_VERSION_MIN_TVOS
#endif
#ifdef LC_VERSION_MIN_WATCHOS
#undef LC_VERSION_MIN_WATCHOS
#endif
#ifdef LC_BUILD_VERSION
#undef LC_BUILD_VERSION
#endif
#ifdef PLATFORM_MACOS
#undef PLATFORM_MACOS
#endif
#ifdef PLATFORM_MACCATALYST
#undef PLATFORM_MACCATALYST
#endif
#ifdef PLATFORM_IOS
#undef PLATFORM_IOS
#endif
#ifdef PLATFORM_IOSSIMULATOR
#undef PLATFORM_IOSSIMULATOR
#endif
#ifdef PLATFORM_TVOS
#undef PLATFORM_TVOS
#endif
#ifdef PLATFORM_TVOSSIMULATOR
#undef PLATFORM_TVOSSIMULATOR
#endif
#ifdef PLATFORM_WATCHOS
#undef PLATFORM_WATCHOS
#endif
#ifdef PLATFORM_WATCHOSSIMULATOR
#undef PLATFORM_WATCHOSSIMULATOR
#endif
#define THUMB_ADDRESS_BIT_MASK …
usingnamespacelldb;
usingnamespacelldb_private;
usingnamespacellvm::MachO;
static constexpr llvm::StringLiteral g_loader_path = …;
static constexpr llvm::StringLiteral g_executable_path = …;
LLDB_PLUGIN_DEFINE(…)
static void PrintRegisterValue(RegisterContext *reg_ctx, const char *name,
const char *alt_name, size_t reg_byte_size,
Stream &data) { … }
class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 { … };
class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386 { … };
class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { … };
class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 { … };
static uint32_t MachHeaderSizeFromMagic(uint32_t magic) { … }
#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB …
char ObjectFileMachO::ID;
void ObjectFileMachO::Initialize() { … }
void ObjectFileMachO::Terminate() { … }
ObjectFile *ObjectFileMachO::CreateInstance(const lldb::ModuleSP &module_sp,
DataBufferSP data_sp,
lldb::offset_t data_offset,
const FileSpec *file,
lldb::offset_t file_offset,
lldb::offset_t length) { … }
ObjectFile *ObjectFileMachO::CreateMemoryInstance(
const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp,
const ProcessSP &process_sp, lldb::addr_t header_addr) { … }
size_t ObjectFileMachO::GetModuleSpecifications(
const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
lldb::offset_t data_offset, lldb::offset_t file_offset,
lldb::offset_t length, lldb_private::ModuleSpecList &specs) { … }
ConstString ObjectFileMachO::GetSegmentNameTEXT() { … }
ConstString ObjectFileMachO::GetSegmentNameDATA() { … }
ConstString ObjectFileMachO::GetSegmentNameDATA_DIRTY() { … }
ConstString ObjectFileMachO::GetSegmentNameDATA_CONST() { … }
ConstString ObjectFileMachO::GetSegmentNameOBJC() { … }
ConstString ObjectFileMachO::GetSegmentNameLINKEDIT() { … }
ConstString ObjectFileMachO::GetSegmentNameDWARF() { … }
ConstString ObjectFileMachO::GetSegmentNameLLVM_COV() { … }
ConstString ObjectFileMachO::GetSectionNameEHFrame() { … }
bool ObjectFileMachO::MagicBytesMatch(DataBufferSP data_sp,
lldb::addr_t data_offset,
lldb::addr_t data_length) { … }
ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
DataBufferSP data_sp,
lldb::offset_t data_offset,
const FileSpec *file,
lldb::offset_t file_offset,
lldb::offset_t length)
: … { … }
ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
lldb::WritableDataBufferSP header_data_sp,
const lldb::ProcessSP &process_sp,
lldb::addr_t header_addr)
: … { … }
bool ObjectFileMachO::ParseHeader(DataExtractor &data,
lldb::offset_t *data_offset_ptr,
llvm::MachO::mach_header &header) { … }
bool ObjectFileMachO::ParseHeader() { … }
ByteOrder ObjectFileMachO::GetByteOrder() const { … }
bool ObjectFileMachO::IsExecutable() const { … }
bool ObjectFileMachO::IsDynamicLoader() const { … }
bool ObjectFileMachO::IsSharedCacheBinary() const { … }
bool ObjectFileMachO::IsKext() const { … }
uint32_t ObjectFileMachO::GetAddressByteSize() const { … }
AddressClass ObjectFileMachO::GetAddressClass(lldb::addr_t file_addr) { … }
bool ObjectFileMachO::IsStripped() { … }
ObjectFileMachO::EncryptedFileRanges ObjectFileMachO::GetEncryptedFileRanges() { … }
void ObjectFileMachO::SanitizeSegmentCommand(
llvm::MachO::segment_command_64 &seg_cmd, uint32_t cmd_idx) { … }
static uint32_t
GetSegmentPermissions(const llvm::MachO::segment_command_64 &seg_cmd) { … }
static lldb::SectionType GetSectionType(uint32_t flags,
ConstString section_name) { … }
struct ObjectFileMachO::SegmentParsingContext { … };
void ObjectFileMachO::ProcessSegmentCommand(
const llvm::MachO::load_command &load_cmd_, lldb::offset_t offset,
uint32_t cmd_idx, SegmentParsingContext &context) { … }
void ObjectFileMachO::ProcessDysymtabCommand(
const llvm::MachO::load_command &load_cmd, lldb::offset_t offset) { … }
void ObjectFileMachO::CreateSections(SectionList &unified_section_list) { … }
class MachSymtabSectionInfo { … };
#define TRIE_SYMBOL_IS_THUMB …
struct TrieEntry { … };
struct TrieEntryWithOffset { … };
static bool ParseTrieEntries(DataExtractor &data, lldb::offset_t offset,
const bool is_arm, addr_t text_seg_base_addr,
std::vector<llvm::StringRef> &nameSlices,
std::set<lldb::addr_t> &resolver_addresses,
std::vector<TrieEntryWithOffset> &reexports,
std::vector<TrieEntryWithOffset> &ext_symbols) { … }
static SymbolType GetSymbolType(const char *&symbol_name,
bool &demangled_is_synthesized,
const SectionSP &text_section_sp,
const SectionSP &data_section_sp,
const SectionSP &data_dirty_section_sp,
const SectionSP &data_const_section_sp,
const SectionSP &symbol_section) { … }
static std::optional<struct nlist_64>
ParseNList(DataExtractor &nlist_data, lldb::offset_t &nlist_data_offset,
size_t nlist_byte_size) { … }
enum { … };
void ObjectFileMachO::ParseSymtab(Symtab &symtab) { … }
void ObjectFileMachO::Dump(Stream *s) { … }
UUID ObjectFileMachO::GetUUID(const llvm::MachO::mach_header &header,
const lldb_private::DataExtractor &data,
lldb::offset_t lc_offset) { … }
static llvm::StringRef GetOSName(uint32_t cmd) { … }
namespace {
struct OSEnv { … };
struct MinOS { … };
}
void ObjectFileMachO::GetAllArchSpecs(const llvm::MachO::mach_header &header,
const lldb_private::DataExtractor &data,
lldb::offset_t lc_offset,
ModuleSpec &base_spec,
lldb_private::ModuleSpecList &all_specs) { … }
ArchSpec ObjectFileMachO::GetArchitecture(
ModuleSP module_sp, const llvm::MachO::mach_header &header,
const lldb_private::DataExtractor &data, lldb::offset_t lc_offset) { … }
UUID ObjectFileMachO::GetUUID() { … }
uint32_t ObjectFileMachO::GetDependentModules(FileSpecList &files) { … }
lldb_private::Address ObjectFileMachO::GetEntryPointAddress() { … }
lldb_private::Address ObjectFileMachO::GetBaseAddress() { … }
uint32_t ObjectFileMachO::GetNumThreadContexts() { … }
std::vector<std::tuple<offset_t, offset_t>>
ObjectFileMachO::FindLC_NOTEByName(std::string name) { … }
std::string ObjectFileMachO::GetIdentifierString() { … }
AddressableBits ObjectFileMachO::GetAddressableBits() { … }
bool ObjectFileMachO::GetCorefileMainBinaryInfo(addr_t &value,
bool &value_is_offset,
UUID &uuid,
ObjectFile::BinaryType &type) { … }
bool ObjectFileMachO::GetCorefileThreadExtraInfos(
std::vector<lldb::tid_t> &tids) { … }
lldb::RegisterContextSP
ObjectFileMachO::GetThreadContextAtIndex(uint32_t idx,
lldb_private::Thread &thread) { … }
ObjectFile::Type ObjectFileMachO::CalculateType() { … }
ObjectFile::Strata ObjectFileMachO::CalculateStrata() { … }
llvm::VersionTuple ObjectFileMachO::GetVersion() { … }
ArchSpec ObjectFileMachO::GetArchitecture() { … }
void ObjectFileMachO::GetProcessSharedCacheUUID(Process *process,
addr_t &base_addr, UUID &uuid) { … }
dyld_process_info;
struct lldb_copy__dyld_process_cache_info { … };
extern "C" unsigned int mach_task_self();
void ObjectFileMachO::GetLLDBSharedCacheUUID(addr_t &base_addr, UUID &uuid) { … }
static llvm::VersionTuple FindMinimumVersionInfo(DataExtractor &data,
lldb::offset_t offset,
size_t ncmds) { … }
llvm::VersionTuple ObjectFileMachO::GetMinimumOSVersion() { … }
llvm::VersionTuple ObjectFileMachO::GetSDKVersion() { … }
bool ObjectFileMachO::GetIsDynamicLinkEditor() { … }
bool ObjectFileMachO::CanTrustAddressRanges() { … }
bool ObjectFileMachO::AllowAssemblyEmulationUnwindPlans() { … }
Section *ObjectFileMachO::GetMachHeaderSection() { … }
bool ObjectFileMachO::SectionIsLoadable(const Section *section) { … }
lldb::addr_t ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage(
lldb::addr_t header_load_address, const Section *header_section,
const Section *section) { … }
bool ObjectFileMachO::SetLoadAddress(Target &target, lldb::addr_t value,
bool value_is_offset) { … }
struct all_image_infos_header { … };
struct image_entry { … };
struct segment_vmaddr { … };
static offset_t
CreateAllImageInfosPayload(const lldb::ProcessSP &process_sp,
offset_t initial_file_offset,
StreamString &all_image_infos_payload,
lldb_private::SaveCoreOptions &options) { … }
struct page_object { … };
bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp,
lldb_private::SaveCoreOptions &options,
Status &error) { … }
ObjectFileMachO::MachOCorefileAllImageInfos
ObjectFileMachO::GetCorefileAllImageInfos() { … }
bool ObjectFileMachO::LoadCoreFileImages(lldb_private::Process &process) { … }