Page 1 of 1

Custom Darwin Kernel

PostPosted: Thu Jul 18, 2013 4:51 pm
by Emmanuel
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

Part of my Info.plist:

<key>OSBundleLibraries</key>
<dict>
<key>com.apple.kpi.libkern</key>
<string>10.8</string>
<key>com.apple.kpi.bsd</key>
<string>10.8</string>
<key>com.apple.kpi.mach</key>
<string>10.8</string>
</dict>

The number (10.8) may be wrong as well, not sure.
So the question is how do I do to make my function(s) visible to my kext(s)? Thanks!

Re: Custom Darwin Kernel

PostPosted: Fri Jul 19, 2013 5:44 am
by Emmanuel
Took some effort but it seems to be good now.

The OSBundleLibraries now looks like this:

Code: Select all
<key>OSBundleLibraries</key>
    <dict>
        <key>com.apple.kpi.libkern</key>
        <string>12.3</string>
        <key>com.apple.kpi.bsd</key>
        <string>12.3</string>
     </dict>

The GCC command looks like this:

Code: Select all
cc -m64 -Xlinker -kext -static -c foo.c -o Contents/MacOS/foo -fno-builtin -nostdlib -lkmod -r -mlong-branch -I/System/Library/Frameworks/Kernel.Framework/Headers -Wall

In config/BSDKernel.exports: _my_func

In osfmk/bsd, I have bdsfoo.h [which has an extern int my_func(void) ]

To build the kernel:

make ARCH_CONFIGS=X86_64 KERNEL_CONFIGS=RELEASE VERBOSE=YES
Once mach_kernel is copied to /

cp BUILD/obj/RELEASE_X86_64/BSDKernel.symbolset /System/Library/Extensions/System.kext/PlugIns/BSDKernel.kext/BSDKernel

This was the missing link.

reboot. Build my kext and kextload, success!