This question is in continuation of How does compiler lay out code in memory, which is posted at stack-overflow.

I have few questions with respect to ld (GNU) utility available in Linux.

Whenever a program is run in the shell, say ./a.out, the shell uses ld to load the program represented by a.out.

  1. How does the shell know it has to use ld to load a.out. Does it scan the a.out to check if it is in the ELF format and if so, uses ld? It certainly can't use the file name extension, since there is no rule to name executable's in a certain format.

  2. Can ld utility load programs represented in any other executable formats other than ELF?

  3. Suppose I come up with my own executable format, say "xyz" and I write my own loader abc which handles such executables. Then, is there any shell command to configure: "use loader abc to load program compiled in a particular executable format "xyz"?

  • You might be interested in binfmt – Torin May 4 '19 at 14:24
  1. The shell doesn’t know, the kernel does. See What types of executable files exist on Linux? and the linked articles for details. The kernel loader loads the binary, and if necessary, its interpreter (which is ld.so for dynamic binaries).

  2. Each implementation of ld.so is format-specific.

  3. Yes, either by adding a binary loader to the kernel, or by using binfmt_misc. See How is Mono magical? for details.

  • Why the dynamic linker is named "ld.so"? I always thought ld used for linking shared libraries at load time is an executable which is used by the kernel. But going by its name, seems like it's also a shared library. – kartik trivikram May 5 '19 at 7:37
  • ld is used at build time, ld.so is used at run time. ld.so is both a shared library and an executable. – Stephen Kitt May 7 '19 at 9:00

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.