How does libc provide two-name functions?

Prior to the advent of direct-linking (-B direct), libc provided many functions with two names. For example, getpwent () and _getpwent (). These two names refer to exactly the same function in libc.

How does libc create two function names for the same implementation?

I think it should not be as simple as writing the same code twice.

+4
source share
2 answers

This is done with the help of weak aliases of a "non-standard" linker, which has been around since early versions and was supported by all the unix compilers / linkers that I know of. This is mainly done as:

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

often with macros to abstract it a bit. This makes the foo character have the same address and print as the __foo character by default, but allows it to override the β€œstrong” definition somewhere else.

+9
source

The getpwent () function simply calls _getpwent () simply. The reason for this is to hide some functions from function calls and to avoid what is called namespace pollution. This way you can create a kind of abstraction that allows you to hide things from the user. In addition, underlining and double underlining are reserved by the system and are backed up to ensure that you do not override anything, for example, in macro definitions.

+1
source

All Articles