PE-COFF: Prefer weak external with defined fallback over null fallback
The linker now prefers weak external symbols with defined fallbacks over those with null fallbacks to avoid runtime crashes.
When linking PE COFF objects, the linker can encounter multiple weak external symbols for the same name. This patch modifies the linker to prefer a weak external with a non-null fallback (i.e., a function body) over one with a null fallback. This prevents runtime crashes that occur when the symbol resolves to address 0.
- proposer
Modifies the linker to prefer weak externals with defined symbols as fallbacks, preventing crashes when a weak declaration is linked before a weak definition.
“Fix this by comparing the fallback targets when two weak externals meet: if the incoming weak external's fallback resolves to a defined symbol and the existing one does not (or points to NULL), update the aux record to use the better fallback.”
In Details
In bfd/cofflink.c, when two PE COFF weak externals meet, the patch checks if the incoming weak external's fallback resolves to a defined symbol and the existing one does not (or points to NULL) and updates the aux record to use the better fallback.
For Context
In PE COFF linking, 'weak externals' are symbols that can optionally fall back to a default definition if no strong definition is found. This patch addresses a scenario where multiple weak externals exist for the same symbol. It ensures that if one weak external has a valid fallback (a function), and another has a NULL fallback (meaning it resolves to zero if not defined), the linker will choose the valid fallback, preventing a crash when the function is called. This is especially relevant for C++ features like operator new and personality routines.