How to stop variable symbols from being exported macOS

  c++, macos, unix, xcode

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', '{$content}' };

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

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

char hidden_var1[2] = { 'b', '{$content}' }; // dlsym will still find this
char hidden_var2[2] = { 'b', '{$content}' }; // 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.

Source: Windows Questions C++

LEAVE A COMMENT