Question

I have a small static library compiled by (linux) gcc 4.8.2 with -fvisibility=hidden which is linked to a shared library (I have two versions, gcc one with C code and ifort one with Fortran code). The static library consists of some internal functions, all prefixed by "ST_LIB_".

I want to be sure that the functions declared in the static library cannot be used by any executable/library that is linked to the shared library. What is the best command on Linux to check that functions with some prefix cannot be used by any external library?

I have tried:

nm --dynamic shared_lib | grep -i "ST_LIB_" | wc -l (outputs 0)

readelf -d shared_lib | grep -i "ST_LIB_" | wc -l (outputs 0)

nm -g shared_lib | grep -i "ST_LIB_" | wc -l (outputs 26 or 0 depending on share lib)

readelf -s shared_lib | grep -i "ST_LIB_" | wc -l (outputs 26 or 0 depending on share lib)

readelf -Ws shared_lib | grep -i "ST_LIB_" | grep -i "HIDDEN" | wc -l (outputs 26 or 0 depending on share lib)

Was it helpful?

Solution

nm --dynamic should be the option you look for, since it displays the symbols you can link against (from a shared library). readelf --dyn-syms should display the same information (different output).

When using nm, check for symbols that have the "T" attribute. From the man page:

The symbol type.  At least the following types are used; others are, as well, depending 
on the object file format.  If lowercase, the symbol is usually local; if uppercase, the
symbol is global (external).  There are however a few lowercase symbols that are shown
for special global symbols ("u", "v" and "w").
[...]
"T"
"t" The symbol is in the text (code) section.

If you want to be 100% sure, you can always write a test program that links against your shared library and attempts to use one of the ST_LIB_ symbols.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top