This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Is there a way to detect a null reference function pointer during assembly or linking ?
- From: Alan Modra <amodra at gmail dot com>
- To: William Tambe <tambewilliam at gmail dot com>
- Cc: binutils at sourceware dot org
- Date: Sun, 1 Dec 2019 13:01:15 +1030
- Subject: Re: Is there a way to detect a null reference function pointer during assembly or linking ?
- References: <CAF8i9mPL59876dxgHAM81AV7P8T4dBuQ1tOj2N=sPK8FWE3GLQ@mail.gmail.com> <CAF8i9mMSq1CLEasVz6kVMCRKrYhsyDSpe3_R++TGm6w-+e5cAg@mail.gmail.com>
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