Skip to content

Commit

Permalink
feat: MachO files may now be retrieved from the current process.
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Mar 7, 2024
1 parent 58f6dd4 commit 8ce4d4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib-macho/include/MachO/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
37 changes: 37 additions & 0 deletions lib-macho/source/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
#include <MachO/LoadCommands/UUID.hpp>
#include <MachO/LoadCommands/VersionMin.hpp>

#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif

namespace MachO
{
class File::IMPL
Expand All @@ -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 ) )
{}
Expand Down

0 comments on commit 8ce4d4a

Please sign in to comment.