Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does libc provide functions with two names?

Tags:

c

libc

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

How does libc make two function names point to the same implementation?

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

like image 840
Je Rog Avatar asked Oct 31 '25 16:10

Je Rog


1 Answers

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.

like image 71
R.. GitHub STOP HELPING ICE Avatar answered Nov 02 '25 06:11

R.. GitHub STOP HELPING ICE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!