9

I have application that has a function f1 void f1 ()

In addition, I have a library that I load using LD_PRELOAD.

The library has several code files and several header file, and it compiled to .so file.

On of the header files also uses a function named f1 with same signature as above. The latest f1 is used only in the library. (I can not change it to static method)

The problem is that when I load the library (using LD_PRELOAD) f1 from the library overrides f1 of the application.

Is there a way to configure f1 of the library to be visible only to the library?

nneonneo
  • 171,345
  • 36
  • 312
  • 383
dk7
  • 323
  • 3
  • 14
  • 2
    Is there a reason why you can't just rename one of the function declarations? I'm guessing this is in an existing codebase which might be why you can't – Bojangles Mar 10 '13 at 12:27

2 Answers2

6

If you can modify the header files at all, make the function static to make it visible only in that compilation unit, or mark it with __attribute__ ((visibility ("hidden"))) (GCC only) to make it visible only in that library:

__attribute__ ((visibility ("hidden"))) void f1();
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Is `__attribute__ ((visibility ("hidden")))` macro that replace with static **?** or how does it works? – Grijesh Chauhan Mar 10 '13 at 13:09
  • 1
    @GrijeshChauhan: No, that attribute marks a symbol as having hidden visibility. It is visible only *within that shared library* (kind of like the opposite of Win32 `DLLEXPORT`). The hidden visibility attribute is passed to and understood by the linker (as opposed to `static`, which makes a symbol basically *invisible* to the linker). – nneonneo Mar 10 '13 at 13:11
3

You could also compile your library with -fvisibility=hidden and use explicit __attribute__ ((visibility ("default"))) for the few functions of your library which needs to be visible.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547