On Mon, May 29, 2017 at 9:14 AM, Alexander Monakov <amona...@ispras.ru> wrote:
> Hi,
>
> On Mon, 29 May 2017, Yuri Gribov wrote:
>
>> Hi all,
>>
>> As discussed in
>> https://sourceware.org/ml/libc-alpha/2017-01/msg00455.html , some
>> libdl functions rely on return address to figure out the calling
>> DSO and then use this information in computation (e.g. output of dlsym
>> depends on which library called it).
>>
>> As reported in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826 this
>> may break under tailcall optimization i.e. in cases like
>>
>>   return dlsym(...);
>>
>> Carlos confirmed that they would prefer to have GCC attribute to
>> prevent tailcalls
>> (https://sourceware.org/ml/libc-alpha/2017-01/msg00502.html) so there
>> you go.
>
> A few comments:
>
> - the new attribute will need documentation

Right, completely forgot...

> - as mentioned earlier, calls to dlsym via a function pointer may still lead 
> to
>   the same issue (so the documentation should mention that)

Yes but compiler will emit an error on cast to function pointer that
lacks an attribute so hopefully we can catch situations like this.

> - this suppresses tailcalls for all dlsym calls, although only those with
>   RTLD_NEXT are magic and need such suppression

Note that other Glibc functions need no_tail_call as well e.g. dlinfo
and dlmopen (grep for RETURN_ADDRESS in dlfcn/ for full list).

> Are there any other possible uses for this attribute?  Given the issue of
> calls-via-pointers, I don't understand why Glibc needs it, because for direct
> calls Jakub pointed out a simpler solution that works with existing compilers:
>
> #define dlsym(h, s) \
>   ({ \
>   void *__r = dlsym (h, s); \
>   asm ("" : "+r" (__r)); \
>   __r; })

True, perhaps they were worried that inline asm may have performance
implications.

-Y

Reply via email to