-1

In Linux, what are the executable ELF files respectively for

  • static linker
  • dynamic linker
  • loader
  • dynamic loader?

Which of them are invoked

  • by execve()
  • by dlopen()?

How are they invoked by execve() or dlopen(), to solve the chicken-egg problem that execve() or dlopen() are used for invoking executable files?

Thanks.

  • grammar understand I not make sense random words. – ctrl-alt-delor Oct 20 '18 at 19:25
  • What doesn't make sense? @c – Tim Oct 20 '18 at 19:36
5

I take it you’re using Jeff Darcy’s definitions:

  • static linking involves resolving symbols at compile time, whereas dynamic linking involves resolving them at run time;
  • static loading involves mapping executables and libraries at load time, whereas dynamic loading involves mapping libraries after the process has started.

It doesn’t really make sense to try to split everything up; instead, you should consider the pairs of features:

  • static loading, static linking: the linker is /usr/bin/ld typically, along with static libraries (.a); the loader is the kernel;
  • static loading, dynamic linking: the linker is /usr/bin/ld again, but with shared libraries (.so); the loader is the binary’s interpreter, e.g. /lib64/ld-linux-x86-64.so.2 (which maps to /lib/x86_64-linux-gnu/ld-2.24.so currently) on Debian 9 on 64-bit x86, itself loaded by the kernel, which also loads the main executable;
  • dynamic loading, static linking: this isn’t used on Linux as far as I’m aware;
  • dynamic loading, dynamic linking: the loader is a library such as libdl; the linker is split between the library and the program calling libdl.

Perhaps some more explanation is called for in relation to the last point. When a program uses libdl to load a library, it calls dlopen, which loads the named library, along with any of its dependencies which aren’t already available in the process, and links the named library with its dependencies. Thus libdl performs both loading (of the necessary libraries) and linking (of the libraries only, not the calling program). When the calling program needs to use a symbol from the named library, it calls dlsym which returns the address of the requested symbol (if it can find it). Thus the calling program performs part of the linking.

There is no chicken-and-egg problem with regards to execve or dlopen. Programs compiled to use execve are compiled with the appropriate references to libc (which contains the wrapper most programs actually use) and the execve symbol (strictly speaking, the aforementioned wrapper). Those references are resolved either at compile-time (for statically-linked programs) or at load-time (for dynamically-linked programs); after that, the symbol is available for use and the program can call execve. The same goes for dlopen: programs which want to use dlopen are compiled with the appropriate references to libdl and dlopen. The dynamic linker itself is a static binary, so the kernel can load it without the help of the dynamic linker. You’ll find lots more detail on this topic in How programs get run and How programs get run: ELF binaries.

(Note that in all cases of dynamic linking, if lazy binding is enabled, linking can happen just-in-time, and not necessarily at load time.)

  • Thanks. "the loader is the binary’s interpreter", do you mean the loader that loads a shared library by "the loader", and the loader for the executable is always the kernel? What is "the binary", and is it the shared library? Does a shared library need to be interpreted, and I think only scripts or executable need to be interpreted. – Tim Oct 20 '18 at 19:35
  • The “binary” is the program being run; it is loaded by the kernel, but it is linked by the dynamic linker (the program’s interpreter), which also loads (and links) any required libraries. The “loader” for the libraries is thus the dynamic linker (this is getting confusing). Shared libraries typically don’t have an interpreter, but they can. – Stephen Kitt Oct 20 '18 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.