0

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"?

Kusalananda
  • 333,661

1 Answers1

3
  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.

Stephen Kitt
  • 434,908