How do I force gcc to call a function directly in PIC code?

后端 未结 2 1916
走了就别回头了
走了就别回头了 2021-02-15 23:40

Consider the following function:

extern void test1(void);
extern void test2(void) {
    test1();
}

This is the code gcc generates without

2条回答
  •  别那么骄傲
    2021-02-15 23:46

    If you can't change the source code, you could use a big-hammer: -Bsymbolic linker flag:

    When creating a shared library, bind references to global symbols to the definition within the shared library, if any. Normally, it is possible for a program linked against a shared library to override the definition within the shared library. This option is only meaningful on ELF platforms which support shared libraries.

    But beware that it will break if some parts of the library rely on symbol interposition. I'd recommend to go with hiding functions that don't need to be exported (by annotating them with __attribute__((visibility("hidden")))) or calling them through hidden aliases (specifically designed to do PLT-less intra-library calls in a controlled fashion).

提交回复
热议问题