Skip to content

Commit

Permalink
Add new mechanism for function aliases - outside original source file
Browse files Browse the repository at this point in the history
For a long time, adding an alias to a function required us to use the
"weak_alias" macro in the *same* source file as the original function.
This caused us to modify some Musl files we didn't want to modify.

In this patch I add a new mechanism for creating an alias for functions
without modifying their original file. The original symbol's address is
only known at link time, not compile time, so we do this symbol copying
via a linker script - we have a new file libc/aliases.ld with a simple
list of symbol assignments.

To demonstrate the easiness and useful of this feature, we drop one
file which we had to change from musl - res_init.c - just because
we wanted to add an alias to it. With the new aliases.ld - we no
longer need to modify the original file.

In followup patches we can move a lot of the aliases we added in other
ways (weak_alias / alias in modified files, wrappers in libc/math/aliases.cc)
to the new aliases.ld.

Signed-off-by: Nadav Har'El <nyh@scylladb.com>
Message-Id: <20200803231809.1432323-1-nyh@scylladb.com>
  • Loading branch information
nyh authored and wkozaczuk committed Aug 4, 2020
1 parent 1ed4a9d commit 21e2bb9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ musl += network/getservbyport.o
libc += network/getifaddrs.o
libc += network/if_nameindex.o
musl += network/if_freenameindex.o
libc += network/res_init.o
musl += network/res_init.o

musl += prng/rand.o
musl += prng/rand_r.o
Expand Down
1 change: 1 addition & 0 deletions arch/aarch64/loader.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

INCLUDE "loader_options.ld"
INCLUDE "libc/aliases.ld"
SECTIONS
{
/* Set the initial program counter to one page beyond the minimal
Expand Down
1 change: 1 addition & 0 deletions arch/x64/loader.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

INCLUDE "loader_options.ld"
INCLUDE "libc/aliases.ld"
SECTIONS
{
/* Set the initial program counter to one page beyond the minimal
Expand Down
12 changes: 12 additions & 0 deletions libc/aliases.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* This file defines symbols as *aliases* to other symbols. The linker
* statically-linking the OSv kernel will set the alias's address to be
* the same one as the original symbol.
*
* This technique is more powerful than the C compiler's "alias(...)"
* attribute - the compiler-only technique is only usable when the alias
* and original symbol are defined in the same translation unit, because
* it is the compiler - not the linker - who need to copy the symbol's
* address.
*/

__res_init = res_init;
7 changes: 0 additions & 7 deletions libc/network/res_init.c

This file was deleted.

0 comments on commit 21e2bb9

Please sign in to comment.