Menu Content/Inhalt
Home arrow Computers arrow Development arrow Mach-o and CFM Wizardry

Online

No Users Online

Login

Syndicate

feed image

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /home/cuttterp/public_html/mambots/content/geshibot/geshi.class.php on line 2285
Mach-o and CFM Wizardry PDF Print E-mail
Written by Dave Thorup   
Thursday, 20 April 2006
CFM is dead.  It died a long time ago, replaced by the Mach-o binary format.  Even though it is dead, it still likes to poke its head out every now and then just to anoy the crap out of you.
 
Anyway, in order to call code in a CFM library from a Mach-o application you need to implement some magical pointer glue so that the different environments know how to take to each other. But hasn't this been documented in the past?  Well, sure for some cases.  In particular, Apple has some sample code, CFM_MachO_CFM, that shows you how to pass a CFM callback function to a Mach-o library from within a CFM application.  That is, the code path is CFM -> Mach-o -> CFM.  What I'll be detailing is how to do the opposite of that - Mach-o -> CFM -> Mach-o.

Lucky for us, the CFBundle APIs will take care of most of the heavy lifting.  The basic steps in the process are as follows:

  1. Put your CFM shared library into a Bundle.
  2. Use the CFBundle APIs to load your library and get pointers to its functions.
  3. Implement some pointer glue for any Callback functions you might have.

Bundlize your Library

In order to use the CFBundle APIs you need to put your CFM shared library into a Bundle structure.  The easiest way to do this is to use a nice little application called MakeBundle which you can find here.  With this application all you need to do is drag and drop your library onto MakeBundle and it will be bundlized.

The bundle that is created ends with a .cfm extension and I'd suggest you change this to .bundle.  This isn't required, but it makes things easier in Xcode because it knows what to do with .bundles but not .cfms.

Add your new bundlized library to Xcode and now we're ready for some coding.

CFBundle is our Friend

While you can certainly implement your own pointer glue and other wizardry to load the CFM function pointers, it's much easier to use the CFBundle APIs.  All you need to do is 1) load your bundle using CFBundleLoadExecutable, 2) then load the function pointers you need using CFBundleGetFunctionPointerForName.  Here's some code that shows how to do that:

  1.  
  2. #define kCFMBundleName "CFMLibrary.bundle"
  3. typedef int (*Function_Proc)( int nFoo, int nBar );
  4. Function_Proc pCFMFunctionPtr = NULL;
  5.  
  6. bool LoadCFMFunction(void)
  7. {
  8. CFBundleRef newBundle;
  9. CFURLRef bundleDirURL;
  10. CFURLRef bundleURL;
  11. // Obtain a URL to the Resources directory inside our application.
  12. bundleDirURL = CFBundleCopyResourcesDirectoryURL( CFBundleGetMainBundle() );
  13. // We want to load the CFMLibrary Bundle, so append its name to the URL.
  14. bundleURL = CFURLCreateCopyAppendingPathComponent( NULL,
  15. bundleDirURL,
  16. CFSTR( kCFMBundleName ),
  17. FALSE );
  18. // Create a CFBundle using the URL. Note that the bundle's code is not
  19. // actually loaded at this stage unless the bundle is using dynamic
  20. // registration.
  21. newBundle = CFBundleCreate( NULL, bundleURL );
  22. CFRelease( bundleDirURL );
  23. CFRelease( bundleURL );
  24. // The plug-in was located. Now locate the interface.
  25. if( newBundle )
  26. {
  27. if (CFBundleLoadExecutable(newBundle))
  28. {
  29. pCFMFunctionPtr = (Function_Proc)CFBundleGetFunctionPointerForName(
  30. newBundle,
  31. CFSTR("CFMFunction"));
  32. if ( pCFMFunctionPtr != NULL )
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
 

Note that this code isn't very good. It uses a global variable to store the function pointer among other things.

We still need some Glue

For most situations this is all you need to do.  But, if you need to pass a Mach-o callback function to the CFM library then you need to perform a little magic.  What you need to do is create a fake TVector structure (thank George Warner for this).

  1.  
  2. typedef struct TVector_struct
  3. {
  4. ProcPtr fProcPtr;
  5. void* fTOC;
  6. } TVector_rec, *TVector_ptr;
  7.  
  8. void * NewCFMFuncPointerFromMachOFuncPointer(void * pMachOFuncPtr)
  9. {
  10. TVector_rec * pCFMFuncPtr = (TVector_rec *)malloc(sizeof(TVector_rec));
  11. if ( pCFMFuncPtr != NULL )
  12. {
  13. // the Mach-o fuction pointer
  14. pCFMFuncPtr->fProcPtr = (ProcPtr)pMachOFuncPtr;
  15. // this is ignored
  16. pCFMFuncPtr->fTOC = 0;
  17. }
  18. return (void *)pCFMFuncPtr;
  19. }
  20.  
  21. void DeleteCFMFuncPointer(void * pCFMFuncPtr)
  22. {
  23. if ( pCFMFuncPtr != NULL )
  24. free( pCFMFuncPtr );
  25. }
 

Now, all you need to do is call NewCFMFuncPointerFromMachOFuncPointer with your Mach-o function pointer as an argument and you'll get back a function pointer that you can pass to the CFM library.  Be sure to delete the CFM pointer when you're done with it.

Well, that's it for now.  I might update this in the future so that there is a working example.  But CFM is so long dead that I doubt this will be very helpful anybody in the future.

References:

CFM_MachO_CFM
How to pass callback from mach-o to CFM (1)
How to pass callback from mach-o to CFM (2)


Related Items:

Nothing related
Add as favourites (67) | Quote this article on your site | Views: 2148

Be first to comment this article
RSS comments

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
E-mail
Homepage
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Code:* Code
I wish to be contacted by email regarding additional comments

Powered by AkoComment Tweaked Special Edition v.1.4.3

Last Updated ( Wednesday, 30 August 2006 )
 
< Prev
designed by www.madeyourweb.com | joomla templates