4

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)

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Peter Petrik
  • 9,701
  • 5
  • 41
  • 65
  • 1
    Why is `nm -g` giving you 26 results? `-g` (or `--extern-only`) should give you only externally visible symbols (exactly what you are asking for). So probably you have 26 `ST_LIB_*` functions that are not static. – Shahbaz Mar 11 '14 at 14:12
  • static library is compiled by gcc, but i have a shared library compiled by gcc and a shared library compiled by ifort. The nm -g outputs 0 for gcc's linked shared lib, but 26 for ifort's linked one. That is why I am asking for command that really shows what linker reads – Peter Petrik Mar 11 '14 at 14:17
  • I wasn't talking about static vs. shared library. I was talking about the `static` keyword of C. If a function is static, it should be externally invisible to the linker. If gcc correctly tells you that there are no externally visible functions (with `ST_LIB_` prefix), but the ifort linker makes those functions externally visible, then perhaps there is something wrong with ifort, or something wrong with the way you call it. – Shahbaz Mar 11 '14 at 14:46
  • the functions in the static library are not prefixed with static keyword, then they would be unusable in the shared library at all. I want to use the ST_LIB_ functions in the shared library, but I do not want the shared library to expose them further – Peter Petrik Mar 11 '14 at 14:48
  • Got it. Then perhaps you are looking for [this](http://stackoverflow.com/q/9909528/912144)? – Shahbaz Mar 11 '14 at 15:13
  • I am already using fvisibility option. I just want to test, if it works as expected (e.g. wrong gcc version can be used during compilation and I want to write an unit test that checkes the symbols in my shared library). – Peter Petrik Mar 11 '14 at 16:11

1 Answers1

3

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.

Unknown
  • 5,722
  • 5
  • 43
  • 64