简体   繁体   中英

How to dynamic cast a class from another kext?

I have two kexts, kextA and kextB . In kextA , I'm trying to dynamic cast (using OSDynamicCast ) a pointer to a class which is in kextB .

$ sudo kextutil -n -t -d B.kext A.kext
Diagnostics for VD.kext:
...
kxld[com...]: The following symbols are unresolved for this kext:
kxld[com...]:   classInB::metaClass
Link failed (error code 5).
Check library declarations for your kext with kextlibs(8).

Is there a way to do this?

Linking classInB in A.kext will give an error at load time (symbol already exists).

You need to declare kextB as a dependency in kextA's Info.plist: in the OSBundleLibraries dictionary, add the reverse-DNS bundle identifier of kextB as the key, and its bundle version as the value. You will also need to place kextB in /SLE or /LE (10.9+), as those are the only locations that are searched for dependencies (regardless of whether kextB is already loaded).

This assumes that you won't just be casting, but actually calling methods specific to classInB . If all you need to do is test for a specific class, you can do that without linkage, too:

OSMetaClass* classInB_mc = OSMetaClass::getMetaClassWithName("classInB");
OSMetaClassBase* cast_ptr = object_ptr->metaCast(classInB_mc);
if (cast_ptr)
{
  // object_ptr can indeed be cast to classInB
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM