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: William Tambe <tambewilliam at gmail dot com>
- To: Alan Modra <amodra at gmail dot com>
- Cc: binutils at sourceware dot org
- Date: Sat, 30 Nov 2019 22:34:34 -0500
- 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> <20191201023115.GA28726@bubble.grove.modra.org>
On Sat, Nov 30, 2019 at 9:31 PM Alan Modra <amodra@gmail.com> wrote:
>
> 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.
Could I please have pointers to examples of code that implement "if
(func)" when -fPIC ?
>
> 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