[PATCH] IFUNC: Update IFUNC resolver check with DT_TEXTREL
H.J. Lu
hjl.tools@gmail.com
Mon Jun 8 15:49:06 GMT 2020
On Mon, Jun 8, 2020 at 6:52 AM Alan Modra <amodra@gmail.com> wrote:
>
> On Mon, Jun 08, 2020 at 11:13:20PM +0930, Alan Modra wrote:
> > On Sun, Jun 07, 2020 at 07:10:47PM -0700, H.J. Lu wrote:
> > > On Sun, Jun 7, 2020 at 6:25 PM Alan Modra <amodra@gmail.com> wrote:
> > > >
> > > > On Sat, Jun 06, 2020 at 07:33:48AM -0700, H.J. Lu via Binutils wrote:
> > > > > All IFUNC targets can use readonly_dynrelocs_against_ifunc to warn
> > > > > dynamic IFUNC relocations in read-only segment.
> > > > >
> > > > > I'd like to check it into master if there are no objections.
> > > >
> > > > Do you actually need one of the dynamic ifunc relocations itself to be
> > > > in a read-only section for this problem to occur? I don't think so.
> > > >
> > > > The problem with IFUNC and TEXTREL occurs when the ifunc resolver
> > > > function is in a segment that needs to be made writable to apply
> > > > relocations. Making a memory region writable typically makes it
> > > > non-executable (but that might depend on architecture), resulting
> > > > in segfaults when attempting to run the ifunc resolver to apply
> > > > dynamic ifunc relocations. That has nothing to do with where the
> > > > ifunc dynamic relocation itself occurs.
> > >
> > > If there are no IFUNC dynamic relocations in the text segment,
> > > which may also contain IFUNC resolvers, we don't need to call
> > > the IFUNC resolver in the writable text segment. It is OK to
> > > have IFUNC relocations with DT_TEXTREL as long as these
> > > relocations aren't applied to the text segment, which means
> > > where the IFUNC relocations are matters.
> >
> > Nonsense.
> >
> > cat > ifunc.c <<\EOF
> > int foo (void);
> > void *foo_ifunc (void) __asm__ ("foo");
> > __asm__ (".type foo, %gnu_indirect_function");
> >
> > int (*fp) (void) = foo;
> >
> > static int
> > one (void)
> > {
> > return 1;
> > }
> >
> > void *
> > foo_ifunc (void)
> > {
> > return one;
> > }
> >
> > int
> > main (void)
> > {
> > return (*fp)() != foo();
> > }
> > EOF
> > cat > textrel.c <<\EOF
> > extern int main (void);
> > __attribute__ ((section (".text")))
> > int (*pmain) (void) = main;
> > EOF
> > gcc -c -O2 -fPIE ifunc.c
> > gcc -c -O2 -fPIE textrel.c
> > gcc -o ifunc -pie ifunc.o
> > ./ifunc
> > echo $?
> > gcc -o ifunct -pie ifunc.o textrel.o
> > ./ifunct
> >
> > ifunct will segfault for the reason I give above. Examine ifunct to
> > see no dynamic ifunc relocs in read-only sections, and no linker
> > warning about ifuncs.
>
> I suppose I should have said "will segfault with older glibc" since
> apparently from Fangrui Song's post newer glibc keeps PF_X set when
> making segments writable. The point stands though that the problem is
> not at all where the dynamic ifunc relocations are located but where
> the ifunc resolvers are located.
>
Here is the patch I am testing.
--
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-IFUNC-Update-IFUNC-resolver-check-with-DT_TEXTREL.patch
Type: application/x-patch
Size: 17035 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20200608/86789556/attachment-0001.bin>
More information about the Binutils
mailing list