[llvm-dev] [lld] avoid emitting PLT entries for ifuncs

Mark Johnston via llvm-dev llvm-dev at lists.llvm.org
Tue Aug 21 16:56:11 PDT 2018

On Tue, Aug 21, 2018 at 05:47:59PM +0100, Peter Smith wrote:
> Hello Mark,
> On 21 August 2018 at 14:47, Mark Johnston via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
> > Hello,
> >
> > We've recently started using ifuncs in the x86(_64) FreeBSD kernel.
> > Currently lld will emit a PLT entry for each ifunc, so ifunc calls are
> > more expensive that those of regular functions.  In our kernel, this
> > overhead isn't really necessary: if lld instead emits PC-relative
> > relocations for each ifunc call site, where each relocation references
> > a symbol of type GNU_IFUNC, then during boot we can resolve each
> > call site and apply the relocation before mapping the kernel text
> > read-only.  Then, ifunc calls have the same overhead as regular function
> > calls.
> >
> > To implement this optimization, I wrote an lld patch to add
> > "-z ifunc-noplt".  When this option is specified, lld does not create
> > PLT entries for ifuncs and instead passes the existing PC-relative
> > relocation through to the output file.  The patch is below; I tested it
> > with lld 7.0 and the patch applied without modifications to the sources
> > in trunk.
> >
> > I'm wondering if such an option would be acceptable in upstream lld, and
> > whether anyone had comments on my implementation.  The patch is lacking
> > tests, and I had some questions:
> I'm not the LLD maintainer so this is just a personal opinion. If I
> understand the optimisation correctly, if it used on some program then
> either the loader for the program or the program itself is responsible
> for running the ifunc resolver and resolving the callsites. I think it
> would have to come with a big health warning in at least the help and
> documentation that platform/OS support is needed to run the program.

That's a good point.  For FreeBSD I had documented the option in the man
page, and will amend it as you suggest.

> > - How should "-z ifunc-noplt" interact with "-z text"? Should the
> >   invoker be required to additionally specify "-z notext"?
> I think it could it either be -z text -z ifunc-noplt = error, with -z
> ifunc-noplt implying -z notext; or -ifunc-noplt is an error without -z
> notext.

I think the latter option is preferable for such a rarely used option,
since it's more explicit.

More information about the llvm-dev mailing list