johnno1962 / HotReloading

Hot reloading as a Swift Package

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Does it support C functions 、variable in other files?

qmkCamel opened this issue · comments

commented

I tried the C functions 、variable in one file, it does works. But when I test it in another file, it doesn`t work.

// when change the b_func() print, and call from main(), it doesn`t work.
// file A 
extern b_func();
int main()
{
   b_func();
   return 0;
}


// file B
void b_func() 
{
   printf("b_func");
}

You can't inject c functions across files though the Curren implementation will inject C++ functions. It could inject C functions I guess provided they are not static; No-one has ever asked before so I don't have any current plans to do this.

If this is actually important to you, try changing this line to be:

        filterImageSymbols(ST_LAST_IMAGE, .any, { _ in return true }) {

This means it will try to interpose all symbols which will only work for function calls. It may be much slower. This will of course also require -Xlinker -interposable.

If you're looking for more information on what "interposing" is up to read this where it is referred to as "indirection" in the section "linking".

commented

Got it, thanks~

commented

I am still a little confused with -interposable. I checked the man ld and found the interpretation as below

-interposable_list filename
             The specified filename contains a list of global symbol names that should always be accessed
             indirectly.  For instance, if libSystem.dylib is linked such that _malloc is interposable, then
             calls to malloc() from within libSystem will go through a dyld stub and could potentially
             indirected to an alternate malloc.  If libSystem.dylib were built without making _malloc
             interposable then if _malloc was interposed at runtime, calls to malloc from with libSystem
             would be missed (not interposed) because they would be direct calls.
-interposable
             Indirects access to all to exported symbols when creating a dynamic library. 

It looks like the -interposable only affect the dynamic library. But I test it in a executable binary, and found the follow info, the c_func_in_demovc is a c func in a .m file.

// objdump -m --lazy-bind /path/to/executable_binary
__DATA   __la_symbol_ptr    0x10000C058 this-image       _c_func_in_demovc

So is the man ld inaccurate, or I have a misunderstand with the interpretation?

I guess the ld man page is inaccurate, it seems to work for all images, not just dynamic libraries.

commented

Agree with you.