What's the difference between the -symbolic and -shared GCC flags?

前端 未结 1 366
深忆病人
深忆病人 2021-02-03 11:57

From the documentation\'s description, they seem to do the same thing except that \"not all systems\" support shared and \"only some systems\" support symbolic (it\'s unclear if

相关标签:
1条回答
  • 2021-02-03 12:49

    Summary: -symbolic prevents intra-shared object function interposition

    Linking with shared objects allows for a feature called symbol interposition. The idea is that you can 'interpose' a new definition of a global symbol so that it is called rather then the 'regular' definition.

    One classic example is malloc(). In the most common case, malloc() is defined within libc. But you can interpose your own version of malloc by loading a library that defines that symbol before you load libc (most runtime linkers allow you to use LD_PRELOAD to specific libraries to load before the executable).

    By default, any function within a shared object that is not static is a global symbol. Because of that, any functions within the shared object can be interposed on. Consider a scenario where a shared object has function high_level() and low_level() and high_level() calls low_level() as part of it's implementation and neither high_level() nor low_level() are static functions.

    It's possible to interpose low_level() such that high_level() is calling a low_level() from a different shared object.

    This is where -symbolic comes in. When creating your shared object, the linker will see that low_level() is defined in the same shared object as high_level() and bind the call such that it can't be interposed on. This way, you know that any calls from one function in your shared object to another in the same shared object will never be interposed on.

    0 讨论(0)
提交回复
热议问题