From 8ce4d4af54ab9913573f0f51a2fc1d61b802360c Mon Sep 17 00:00:00 2001 From: macmade Date: Fri, 8 Mar 2024 00:22:25 +0100 Subject: [PATCH] feat: MachO files may now be retrieved from the current process. --- Submodules/STDXS | 2 +- lib-macho/include/MachO/File.hpp | 6 +++++- lib-macho/source/File.cpp | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Submodules/STDXS b/Submodules/STDXS index c3bb5cc..5541d28 160000 --- a/Submodules/STDXS +++ b/Submodules/STDXS @@ -1 +1 @@ -Subproject commit c3bb5cc6820637ebf9f789c2cc481f2cca69db13 +Subproject commit 5541d28073491bacaa9f1f0ad126e801fb6b6822 diff --git a/lib-macho/include/MachO/File.hpp b/lib-macho/include/MachO/File.hpp index b29d11b..87e1544 100644 --- a/lib-macho/include/MachO/File.hpp +++ b/lib-macho/include/MachO/File.hpp @@ -60,7 +60,11 @@ namespace MachO LittleEndian, BigEndian }; - + + #ifdef __APPLE__ + static std::optional< std::tuple< File, const void * > > fromCurrentProcess( const std::string & path ); + #endif + File( const std::string & path ); File( XS::IO::BinaryStream & stream ); File( const File & o ); diff --git a/lib-macho/source/File.cpp b/lib-macho/source/File.cpp index 2d3b9b0..3dd2381 100644 --- a/lib-macho/source/File.cpp +++ b/lib-macho/source/File.cpp @@ -68,6 +68,10 @@ #include #include +#ifdef __APPLE__ +#include +#endif + namespace MachO { class File::IMPL @@ -92,6 +96,39 @@ namespace MachO std::vector< std::shared_ptr< LoadCommand > > _loadCommands; }; + #ifdef __APPLE__ + std::optional< std::tuple< File, const void * > > File::fromCurrentProcess( const std::string & path ) + { + std::optional< uint32_t > index; + + for( uint32_t i = 0; i < _dyld_image_count(); i++ ) + { + std::string image( _dyld_get_image_name( i ) ); + + if( image == path ) + { + index = i; + } + } + + if( index.has_value() == false ) + { + return {}; + } + + const void * header = _dyld_get_image_header( index.value() ); + + if( header == nullptr ) + { + return {}; + } + + XS::IO::BinaryMemoryStream stream( static_cast< const uint8_t * >( header ) ); + + return { { stream, header } }; + } + #endif + File::File( const std::string & path ): impl( std::make_unique< IMPL >( path ) ) {}