Search code examples
kernelosx-mountain-liondarwinkernel-extension

Using custom Darwin kernel


I'm building a custom kernel for Mac OS (Mountain Lion, Darwin 2050.22.13). I can build the kernel just fine and I can add stuff to it but I'm missing something.

To keep things short, here is a sample of what I'm trying to do.

Let's say that I want to add a function [my_func(void*)] to say, bsd/kern/kern_fork.c I can add an include file and stick it into osfmk/bsd and change the Makefile so that the new .h file is now copied to BUILD/obj/RELEASE_X86_64/EXPORT_HDRS/

I also added the function name to config/BSDKernel.exports. I can see the function with its symbol in /mach_kernel so it would appear to be fine.

Now, here's the tricky part. It's not tricky per se but I can't figure it out. What I want to do is to be able to make a call to my function from a kext that I am also writing. Basically it would be some sort of private API for me.

My kext compiles fine but when I run kextload/kextutil it complains that it can't find the my_func symbol.

(kernel) kxld[com.blah.foo.kext]: The following symbols are unresolved by this kext (kernel) kxld[com.blah.foo.kext]: _my_func

kextlibs returns: For all architectures: com.apple.kpi.libkern = 12.3

for x86_64: 1 symbol not found in any library kext

So the question is how do I do to make my function(s) visible to my kext(s)? Thanks!


Solution

  • I believe the symbols in BSDKernel.exports end up in the com.apple.kpi.bsd KPI, so you need to add that to your OSBundleLibraries dictionary in the kext's info.plist. I'm not 100% sure that's all you need to do, so let us know if that doesn't work.

    Update:

    The built-in KPIs are actually represented by pseudo-kexts within the "Plugins" directory of System.kext. So for the BSDKernel KPI, that's /System/Library/Extensions/System.kext/PlugIns/BSDKernel.kext/ Perhaps that needs to be updated in kind?

    make all doesn't seem to automatically build a new System.kext. There is a large section dedicated to System.kext in config/Makefile, however, and it seems to be part of the install targets. make install might do the trick. Or, if you're wary of that (as I am), this seems to be the crucial line:

        install $(INSTALL_FLAGS) $(OBJROOT)/BSDKernel.symbolset    $(DSTROOT)/$(INSTALL_KEXT_DIR)/System.kext/PlugIns/BSDKernel.kext/BSDKernel;         \
    

    In other words, make a backup of your System.kext, then copy BUILD/obj/RELEASE_X86_64/BSDKernel.symbolset over /System/Library/Extensions/System.kext/PlugIns/BSDKernel.kext/BSDKernel and try booting.

    Best of luck!