Linkers, Loaders & Executables

How does a linker treat a weak symbol?

  1. AA weak symbol always takes priority over other definitions
  2. BIf an ordinary (strong) definition with the same name exists, it is used; otherwise the weak definition is used
  3. CFiles containing weak symbols cannot be linked
Show answer and explanation

Answer: B. If an ordinary (strong) definition with the same name exists, it is used; otherwise the weak definition is used

A weak symbol is a fallback definition: "use this if there is no other". Embedded software often supplies default interrupt handlers as weak symbols so that a user-written function with the same name replaces them. Two strong definitions collide with a multiple-definition error, but overlapping with a weak definition is not an error.

  • A. On the contrary, it loses to a strong definition.
  • B. Correct. It provides a default definition that can be overridden.
  • C. They link normally.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. What is the main purpose of name mangling, in which a C++ compiler encodes argument types and other information into symbol names?
  2. What is the most suitable use of Linux's dlopen function (the counterpart of Windows LoadLibrary)?
  3. When 100 processes use the same shared library, how is physical memory used?
  4. 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?
  5. What is the role of a linker script used in embedded development?