Is there a way to detect a null reference function pointer during assembly or linking ?
Alan Modra
amodra@gmail.com
Sun Dec 1 02:31:00 GMT 2019
On Sat, Nov 30, 2019 at 08:59:29AM -0500, William Tambe wrote:
> To supplement my previous question, a null reference function pointer
> can occur when using a weak function for which there is no definition;
> ie:
>
> extern void func (void) __attribute__((weak));
>
> int main () {
> if (func)
> func();
> }
>
> In the above example, func will be null if there is no default implementation.
>
> The issue I am running into is that the value of a null reference
> function pointer is not null when the executable is not loaded at the
> address specified in the ELF header, because that value is computed by
> offsetting a constant value from the
> start of the section .text.
"if (func)" when -fPIC is generally implemented as a load of an
address from the GOT or similar, and a comparison of that address with
zero. You have a compiler problem if -fPIC code for "if (func)" uses
a relative offset for the address of func.
You have a linker problem if ld is emitting dynamic relocs, or
resolving symbols and/or editing code so that what wasn't a relative
offset in the relocatable object file becomes one in the final
executable.
--
Alan Modra
Australia Development Lab, IBM
More information about the Binutils
mailing list