Is there a way to detect a null reference function pointer during assembly or linking ?

William Tambe tambewilliam@gmail.com
Sun Dec 1 12:45:00 GMT 2019


On Sat, Nov 30, 2019, 9:34 PM William Tambe <tambewilliam@gmail.com> wrote:

> 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 ?
>

I meant, could I have pointers in the compiler source that shows the use of
the GOT if -fPIC code for "if (func)" where func is a weak symbol ?


> >
> > 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