Page 1 of 1

Converting an executable into a shared library?

PostPosted: Thu Aug 31, 2017 2:44 am
by johncoates
Before I go off and try writing a converter, I was wondering if anyone here has experience or guidance in this?
As far as I can tell all you would need to do would need to do is update the header to MH_DYLIB, then strip out the LC_MAIN load command and add in LC_ID_DYLIB. Anything I'm missing here?

Re: Converting an executable into a shared library?

PostPosted: Thu Aug 31, 2017 2:46 am
by Siguza
You should probably also remove __PAGEZERO...

Re: Converting an executable into a shared library?

PostPosted: Thu Aug 31, 2017 2:58 am
by johncoates
Gotcha, had to look up what that segment contains. Hope I'm not missing anything and it's as simple as that.

Re: Converting an executable into a shared library?

PostPosted: Thu Aug 31, 2017 5:56 am
by johncoates
It's kind of working!
When I try and use a class it errors out with dyld: Symbol not found: _OBJC_CLASS_$_example
However if I use it dynamically like so: [NSClassFromString(@"example") print]; it works.

So this is what I've done so far:
- renaming __PAGEZERO to nullify it
- remove LC_MAIN
- remove LC_LOAD_DYLINKER
- add LC_ID_DYLIB
- Change _OBJC_CLASS_$_example & _OBJC_METACLASS_$_example from N_PEXT to N_EXT

I'm thinking that LC_DYLD_INFO_ONLY's exports will need to be filled in with references to the class and meta class.

Re: Converting an executable into a shared library?

PostPosted: Tue Sep 05, 2017 12:05 pm
by scknight
Very cool. Keep us updated with what all you need to add/change to get it to work.