Gcc hide visibility of symbols coming from included static library
Asked Answered
P

2

9

I have:

  1. a shared library, say libShared.so, which contains a class Bar, with a method int Bar::do(int d) const
  2. a static library, say libStatic.a, which contains a class Foo, with a method int Foo::act(int a) const.

The code of Bar is something like this:

//Bar.h
class __attribute__ ((visibility ("default"))) Bar
{
  private: 
    __attribute__ ((visibility ("hidden"))) int privateMethod(int x) const;
  public:
    Bar() {}
    int do(int d) const;
}

//Bar.cpp
#include "Bar.h"
#include "Foo.h"

int Bar::do(int d) const {
   Foo foo;
   int result = foo.act(d) + this->privateMethod(d);
   return result;
}

libShared.so is compiled with flag -fvisibility=hidden.

The problem is the following: I execute Linux command nm -g -D -C --defined-only libShared.so, and it results that class Foo, along with its method, is visible outside libShared.so, despite having told to the compiler to hide everything except what is marked as "public" (infact, they are marked as "T" by nm).

How can I avoid this? I want libShared.so not to expose symbols coming from its dependencies.

Thanks

Popgun answered 28/7, 2015 at 8:21 Comment(0)
L
11

You need to compile libStatic.a also with flag -fvisibility=hidden.

Lightening answered 28/7, 2015 at 8:53 Comment(3)
sorry, but doing so won't prevent libShared.so to use Foo?Popgun
I answer myself. It works! I suppose this is because a static linkage doesn't support symbols hiding, so the hidden flag will take effect only when the objects in static library are used inside a shared one. Thanks a lot!Popgun
Please modify your answer with the cimments insight about hiding symbols in static and dynamic libs. Had discovered it just nowUmbilical
H
3

-fvisibility=hidden only affects the default visibility of symbols generated by the compiler, it does not modify the visibility of existing symbols such as those obtained from static libraries.

Fortunately the linker does have a flag to do exactly that: Use -Wl,--exclude-libs=ALL to change the visibility of global symbols from static libraries to "hidden", thus preventing them from being exported by your shared library.

Haff answered 4/3, 2021 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.