2

I'm not sure, whether the dynamic linker /usr/bin/ld is automatically invoked by the operating system, when the ELF file is loaded, or whether it's invoked by code embedded in the ELF file?

When I use r2 to debug an ELF file, it stops at first instruction to be executed, which should be dynamic linker code, but I don't know if this code is part of the ELF file.

  • How to you get to your gdb breakpoint? – Stephen Kitt Sep 6 at 11:50
  • Just by pressing "r". – Shuzheng Sep 6 at 11:55
  • OK, let me rephrase, how are you setting your gdb breakpoint? – Stephen Kitt Sep 6 at 11:57
  • I don't think that's the dynamic linker, it should be /usr/lib/ld-linux.so. – 炸鱼薯条德里克 Sep 6 at 11:57
  • What's the difference? – Shuzheng Sep 6 at 11:59
3

The kernel loads the dynamic loader (which isn’t /usr/bin/ld; see what are the executable ELF files respectively for static linker, dynamic linker, loader and dynamic loader?).

When you run an ELF binary, the kernel uses its specific ELF binary loader; for dynamically-linked binaries, this looks for the interpreter specified in the ELF headers, loads that and instructs it to run the target binary. The interpreter is the dynamic loader, which loads any required libraries, resolves the undefined symbols, and jumps to the programs start address. (See What types of executable files exist on Linux? for details of the binary loads in the kernel.)

LWN has an article which goes into the details, How programs get run: ELF binaries.

  • thank you. So, the dynamic linker is loaded into the process's address space, it loads the process, and as part of that links it dynamically to its (dynamic shared objects) dependencies; afterwards it transfers control to the user's program? – Shuzheng Sep 6 at 11:58
  • See my updated answer. – Stephen Kitt Sep 6 at 12:02
  • You should correct /usr/bin/ls :-) By "The kernel loads the dynamic linker..." - it sounds like the dynamic linker is loaded twice. – Shuzheng Sep 6 at 12:04
  • Done. I’ve rephrased in an attempt to make the explanation clearer. – Stephen Kitt Sep 6 at 12:09
  • In your other answer, you say: "static loading, dynamic linking: the linker is /usr/bin/ld again, but with shared libraries (.so); the loader is the binary’s interpreter". So, is the "specific ELF binary loader" the same for static-loading/static-linking and static-loading/dynamic-linking? It just changes its behavior depending on whether it finds an interpreter specified in the ELF headers? – Shuzheng Sep 6 at 12:13

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.