Hide exported Symbols in .so

Started by
3 comments, last by Bregma 15 years, 10 months ago
Hi, I've ported a DLL from Win32 to Linux (.so). Basically, the DLL contains a bunch of classes which are derived from pure virtual interfaces. The DLL only exports 1 function (getFactory) which returns a pointer to one of those pure virtual interfaces. The aren't any other symbols exported. Now, when I take a look at my .so using "nm -D" almost all of my classes are visible. I don't want anyone to see my classes, function, etc. but the one I have exported in my Windows DLL (namely getFactory). Is there any way to hide those symbols? Thanks, Metron
----------------------------------------http://www.sidema.be----------------------------------------
Advertisement
I believe there are ways of doing this using some Gcc attributes, though off hand I can't remember the exact syntax of attributes.
I never tried it with shared libs, but maybe using strip is worth a shot.
Use a configure script to see if the g++ compiler supports the visibility attribute and use macros just as you would do so for windows (import/export).

#ifdef WIN32#	ifdef EXPORT...#	else....#	endif#	define DLL_LOCAL #else#	ifdef __GNU__#		if (GCC_SUPPORTS_VISABLE == 1) /*defined by configure*/#			ifdef EXPORT#				define DLL_API __attribute__ ((visibility("default")))#			else#				define DLL_API __attribute__ ((visibility("default")))#			endif#			define DLL_LOCAL __attribute__ ((visibility("hidden")))#		else#			define DLL_API#			define DLL_LOCAL#		endif#	endif#endif
Quote:Original post by Metron
Is there any way to hide those symbols?

As with Windows, there are two different ways.

First, there is the GCC visibility attribute as pointed out by dmail. You use it just lke the Windows import/export declspec (it has a different meaning, but the defined symbol goes in the same place so you just need to define a macro in a header file as appropriate). Problem with this method is it only works in GCC 4.2 or later: the attribute was available in 4.1 but cuase dhorrible things to go wrong with standard library symbols.

Second, you can create a version list file and feed it to the linker (like the old Windows .def files). This is not the preferred way, but is the only way if you're using a pre-4.2 toolchain.

Finally, the end effect of using the version list or visibility attribute is that it marks some symbols as local, and others as global. It doesn't remove those symbols. For that, you will want to run the strip -x command. Most packaging systems do this automatically as they package your library.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement