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?
- AIt is a compiler bug, so it is never filled unless the source is fixed
- BThe CPU guesses the target at the moment it executes the instruction
- 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.