Issue - kextload linking (Linking with .a file)

Rouki

I had an assembly file which needed to be linked with a kernel driver using Xcode. In order to that, I used 'ar rc' command to make an .a file and add it to my Xcode project @ 'Link binary with libraries'. The build succeeded. I have a global function there (In my assembly file) named f and i'm calling it from a cpp file which has a forward declaration of this function.

// bar.cpp
void f();
...
f();

The function's definition is located at the asm file mentioned earlier:

global _f
_f:
...
ret

When I try to execute the kext using 'kextload' I receive the following error :

(kernel) kxld[com.apple.dts.driver.SimpleDriver]: The following symbols are unresolved for this kext:
(kernel) kxld[com.apple.dts.driver.SimpleDriver]:   __Z9fy <---- that is probably the function f
(kernel) Can't load kext com.apple.dts.driver.SimpleDriver - link failed.
(kernel) Failed to load executable for kext com.apple.dts.driver.SimpleDriver.
(kernel) Kext com.apple.dts.driver.SimpleDriver failed to load (0xdc008016).
(kernel) Failed to load kext com.apple.dts.driver.SimpleDriver (error 0xdc008016).
Failed to load SimpleDriver.kext - (libkern/kext) link error.

When I use the 'nm' command on the .a file I created earlier I receive the following output:

..
00000000000002e9 t somefunction
0000000000000119 t somefunction
0000000000000242 t somefunction
0000000000000293 T f
0000000000000006 t somefunction
00000000000003cf t somefunction
..
  • the 'somefunction' term is used for functions which are not important for the question -

As you can see, the f function is here and marked with 'T' which means it is a global function. But the kextload still can't seem to resolve it.

Drew McGowen

This is due to Name Mangling, which causes the name f to be turned into __Z9fy. You have one of two choices:

  1. Prefix the C++ declaration with extern "C"
  2. Name the assembly function as __Z9fy

The first option is almost always the preferred route, as it's much more cross-platform (it doesn't rely on the specifics of C++ name mangling), and keeps the symbol names "clean".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related