Home > database >  How to stop variable symbols from being exported macOS
How to stop variable symbols from being exported macOS

Time:11-07

So I've got my main program that uses dlsym to find the symbols of 2 functions and a variable from a dylib and I have a variable that I don't want exported.

My problem is that whether or not I use extern "C" for the variable dlsym will always find the variable Im not exporting, if I remove extern "C" from the functions they're not found by dlsym in the program but no matter what it'll always find the variables.

Code for the dylib :

export "C" char exported_var[2] = { 'a', '\0' };

extern "C" void dylib_quit()
{
    ...
}

extern "C" void dylib_main()
{
    ...
}

char hidden_var1[2] = { 'b', '\0' }; // dlsym will still find this
char hidden_var2[2] = { 'b', '\0' }; // dlsym will still find this

Code for the program :

// I want to find all these and I can
dylib_main_fn m_main_fn = (dylib_main_fn)dlsym(handle, "dylib_main");
dylib_quit_fn m_quit_fn = (dylib_quit_fn)dlsym(handle, "dylib_quit");
const char* exported_var = (const char*)dlsym(m_handle, "exported_var");

// And when I do this it will find the "hidden_var1" variable which I don't want happening
// same with "hidden_var2"
const char* hidden_var1 = (const char*)dlsym(m_handle, "hidden_var1");

So how am I able to hide the variable?
Im using Xcode 11.3.1 on MacOS 10.14.6.

CodePudding user response:

Hiding symbols using -fvisibility=hidden

Exporting

Code for the dylib :

#define EXPORT extern "C" __attribute__((visibility("default")))

EXPORT char exported_var[2] = { 'a', '\0' };

EXPORT void dylib_quit()
{
    ...
}

EXPORT void dylib_main()
{
    ...
}

char hidden_var1[2] = { 'b', '\0' };
char hidden_var2[2] = { 'b', '\0' };
  • Related