0

Are dynamic linkers interchangeable?

Let's say I compile a program, for example with gcc, linking in various dynamic libraries, with the program to be used at runtime with the gnu linker for resolving the dynamic library symbols. And the instead of using the gnu linker, I have another linker that I need or want to use.

Will that work? Why/why not?

2 Answers 2

1

I’m assuming that by “dynamic linker”, you’re referring to the dynamic linker/loader, typically known as ld.so, rather than the linker used when building programs, ld.

The dynamic linker/loader is tightly coupled with the C library it is associated with, or even part of. On Linux as typically discussed here, the usual loader is the GNU C library’s loader; if you look at its symbols, you’ll see that it pulls in symbols from the C library:

$ nm -D /lib/ld-linux.so.2 | grep -E ' A|D '
00028898 D _dl_argv
00000000 A GLIBC_2.0
00000000 A GLIBC_2.1
00000000 A GLIBC_2.3
00000000 A GLIBC_2.4
00000000 A GLIBC_PRIVATE
00028f28 D __libc_enable_secure
00028f24 D __libc_stack_end
00029040 D _rtld_global
000288a0 D _rtld_global_ro

See How can the dynamic linker/loader itself be dynamically linked as reported by `file`? for details of how this works.

Alternative C libraries provide their own dynamic loaders; for example, programs built with musl on x86_64 specify their “interpreter” as /lib/ld-musl-x86_64.so.1, which is a link to the musl C library itself:

$ ls -l /lib/ld-musl-x86_64.so.1
lrwxrwxrwx 1 root root 25 Jan 23  2019 /lib/ld-musl-x86_64.so.1 -> x86_64-linux-musl/libc.so

Loaders rely on features provided by their associated C libraries, and vice versa. It would technically be possible to replace a given loader with a compatible implementation, but you’d effectively be rewriting the existing loader, at a minimum. While the basic specification of a dynamic loader is well defined and bounded — loading an ELF binary (on Linux), associated dynamic libraries, and performing the necessary relocation etc. — and one might therefore imagine replacing any given loader with another, the implementation details are all significant.

Note that as long as each dynamic loader has a different canonical name, it is possible for a single system to have different loaders, used by programs as appropriate. This is how it’s possible for a GNU C library-based system to also be used to build and run musl binaries, and opens the way to efforts such as llvm-libc, or even historically, the switch from libc.so.5 to libc.so.6.

0

They must be interchangeable.

A good discussion about e.g. ld vs gold could be found here: https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience

Also check this: https://stackoverflow.com/questions/29361801/is-the-lld-linker-a-drop-in-replacement-for-ld-and-gold

There's also LLD from the LLVM project which promises to be the fastest of them all.

5
  • 1
    I understood the question as being about the dynamic linker/loader, ld.so, not the ld linker... Commented Jun 11, 2021 at 11:01
  • @StephenKitt I know ld, gold, lld, yet I've never heard of different loaders akin to ld.so being used. Most UNIX systems have just one and I'm not even sure what's the point of changing it. Commented Jun 11, 2021 at 11:09
  • musl for example has its own dynamic loader. Binaries built with musl use that instead of the GNU C library ld.so on Linux. Commented Jun 11, 2021 at 11:11
  • Last time I heard it's for embedded systems where space is a concern. And I'm not sure it's a drop-in replacement for ld.so unless you rebuild your entire system based on it. Commented Jun 11, 2021 at 11:14
  • Sorry, I didn’t mean to imply it’s a drop-in replacement; it’s an example of a different loader which is used on Linux systems (not just embedded systems). I’m pretty certain dynamic loaders aren’t interchangeable, but I don’t have a factual answer — if I did, I’d write it ;-). Commented Jun 11, 2021 at 11:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.