How does libc provide functions with two names?

后端 未结 2 682
广开言路
广开言路 2020-12-21 02:17

Before the advent of direct binding (-B direct) libc provided many functions with two names. For example, getpwent() and _getpwent().

2条回答
  •  眼角桃花
    2020-12-21 02:58

    It's done via weak aliases a "nonstandard" linker trick that's been around since early unices and that's supported by all unix compilers/linkers I know of. It's basically done as:

    void __foo(void);
    void foo(void) __attribute__((weak, alias("__foo")));
    

    often with macros to abstract it a little bit. This makes it so the symbol foo will have the same address and type as the symbol __foo by default, but allows it to be overridden by a "strong" definition somewhere else.

提交回复
热议问题