Linkers, Loaders & Executables

Disassembling an object file shows that an instruction calling the external function printf has a placeholder target such as 0. When is the correct value filled in?

  1. AIt is a compiler bug, so it is never filled unless the source is fixed
  2. BThe CPU guesses the target at the moment it executes the instruction
  3. CAt link time (and partly at run time for dynamic linking), based on the relocation entries
Show answer and explanation

Answer: C. At link time (and partly at run time for dynamic linking), based on the relocation entries

At compile time printf's address is unknown, so the compiler leaves a placeholder and records a relocation entry saying "put printf's address in this field of this instruction". The linker reads it and writes the right value. With dynamic linking, the call is pointed at a PLT stub, and the dynamic linker places the real library function's address in the GOT at run time.

  • A. It is not a bug but a normal state awaiting relocation.
  • B. The CPU does not decide jump targets by guessing.
  • C. Correct. A placeholder is perfectly normal.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. What is the role of a linker script used in embedded development?
  2. On a microcontroller without an OS, what does the start-up code do right after power-on before calling main?
  3. What is the difference between "sections" and "segments" in an ELF file?
  4. Which attack does W^X (never allowing a page to be both writable and executable) make harder?
  5. On Linux, when is the version number in a shared library's soname, such as the ".1" in libfoo.so.1, generally increased?