Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSLinkModule in place of dlopen for frameworks



Hi,

I am porting one Linux library.
This library loads other shared libraries(.so) using dlopen() , dlclose(),
dlsym().
So I have
1. loader-Lib.so
2. tobe-loaded-lib.so
written in C.

I am referring to tech note:
http://developer.apple.com/technotes/tn2002/tn2071.html

I want to convert both
1. loader-Lib.so
2. tobe-loaded-lib.so
to cocoa frameworks.

Number of tobe-loaded-lib.so libraries are not fixed. their paths are
supplied in one list.conf file.
I converted .so to Cocoa Frameworks.

loader-lib.framework contains the code I shown below.

As per tn2071.html dlopen() /dlclose()/ dlsym() should not be used.
So I am using NSModule.
But I am not able to load/link the tobe-loaded-lib.framework 's Mach-o
dynamic lib using this NSModule.
NSCreateObjectFileImageFromFile() returns -
NSObjectFileImageInappropriateFile.

I look in the <mach-o/dyld.h> for function
NSCreateObjectFileImageFromFile().
there is says this function is supported for only MH_BUNDLE type of Mach-o
files.

Limitation to me is I don't know how may frameworks are there to be loaded
at compile time.
I have to take path of frameworks from list.conf file.
How can I load the framework's shared library at runtime using C program and
execute its code?
Do I have to use some other APIs?

Here is the code i tried.

int
LoadLibrary()
{
	/* 1 */
	const char *filename =
"/Users/user1/Library/Frameworks/SDMLib.framework/Versions/Current/SDMLib";
	/* 2 */
	//const char *filename =
"/Users/user1/project/qlogic/test_fw/NSM/UseSDM/build/test.o";
	/* I compile a test c file by gcc -bundle -o test.o test.c ,
         this test.o gets loaded NSCreateObjectFileImageFromFile()
successfully.
       */

	NSObjectFileImage *fileImage;
	NSModule handle;
	NSObjectFileImageReturnCode returnCode;
	int result;
	NSSymbol nssym;
	void (*address)();
	printf("Entered - %s()\n",__func__);
	printf("File name = %s\n",filename);

	returnCode = NSCreateObjectFileImageFromFile(filename, &fileImage);

	switch (returnCode) {
		case NSObjectFileImageSuccess:
			printf("Return code = NSObjectFileImageSuccess\n");
			break;
		case NSObjectFileImageFailure:
			printf("Return code = NSObjectFileImageFailure\n");
			break;
		case NSObjectFileImageInappropriateFile:
			printf("Return code = NSObjectFileImageInappropriateFile\n");
			break;
		case NSObjectFileImageArch:
			printf("Return code = NSObjectFileImageArch\n");
			break;
		case NSObjectFileImageFormat:
			printf("Return code = NSObjectFileImageFormat\n");
			break;
		case NSObjectFileImageAccess:
			printf("Return code = NSObjectFileImageAccess\n");
			break;
		default:
			printf("Unkown return status\n");
			break;
	};
	if(returnCode == NSObjectFileImageSuccess)
	{
		handle = NSLinkModule(fileImage,filename,
NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE);
		if (handle) {
			printf("Valid Handle - \n");
			nssym = NSLookupSymbolInModule(handle, "test_display");
			/* this doesn't work event for test.o */
			address = NSAddressOfSymbol(nssym);
			printf("Address = %p\n",address);
			(*address)();
		} else {
			printf("INVALID HANDLE\n");
		}
		NSDestroyObjectFileImage(fileImage);
		result = NSUnLinkModule(handle, 0);
  }
  printf("Leaving - %s()\n",__func__);
  return 0;
}

Am I posting this message to correct mailing list?
If not please direct me to correct mailing list.

Regards,
Parav Pandit

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Unix-porting mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/unix-porting/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.