This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH i386] PR65753: allow PIC tail calls via function pointers

> In the i386 backend, tailcalls are incorrectly disallowed in PIC mode for
> calls via function pointers on the basis that indirect calls, like direct
> calls, would go via PLT and thus require %ebx to point to GOT -- but that is
> not true.  Quoting Rich Felker who reported the bug,
>   "For PLT slots in the non-PIE main executable, %ebx is not required at all.
>   PLT slots in PIE or shared libraries need %ebx, but a function pointer can
>   never evaluate to such a PLT slot; it always evaluates to the nominal address
>   of the function which is the same in all DSOs and therefore fundamentally
>   cannot depend on the address of the GOT in the calling DSO"
> As far as I can see it's simply a mistake that was there from day 1 (comment 4
> in points to original patch).
> Bootstrapped and regtested on 32-bit x86, OK for trunk?
> (the comment before the condition will need to be adjusted too, i.e.
> s/optimize any indirect call, or a direct call/optimize any direct call/ )
> 	PR target/65753
> 	* config/i386/i386.c (ix86_function_ok_for_sibcall): Allow PIC sibcalls
> 	via function pointers.
> diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
> index 3263656..f29e053 100644
> --- a/gcc/config/i386/i386.c
> +++ b/gcc/config/i386/i386.c
> @@ -5448,13 +5448,13 @@ ix86_function_ok_for_sibcall (tree decl, tree exp)
>    /* If we are generating position-independent code, we cannot sibcall
>       optimize any indirect call, or a direct call to a global function,
>       as the PLT requires %ebx be live. (Darwin does not have a PLT.)  */
>    if (!TARGET_MACHO
>        && !TARGET_64BIT
>        && flag_pic
> -      && (!decl || !targetm.binds_local_p (decl)))
> +      && (decl && !targetm.binds_local_p (decl)))

You probably need to update comment here. I wonder what happens when we optimize
indirect call to direct call to global function at RTL level? I suppose we are
safe here, because at RTL level we explicitly represent if we refer to PLT entry
or the functionaddress itself and we never optimize one to the other?

Patch is OK if you make sure that this works and update the comment.

>      return false;
>    /* If we need to align the outgoing stack, then sibcalling would
>       unalign the stack, which may break the called function.  */
>    if (ix86_minimum_incoming_stack_boundary (true)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]