Function attribute for optimization level

Dimitry Andric dim at FreeBSD.org
Mon Jun 5 11:57:16 UTC 2017


On 5 Jun 2017, at 13:09, Konstantin Belousov <kostikbel at gmail.com> wrote:
> 
> On Mon, Jun 05, 2017 at 01:03:24PM +0300, Konstantin Belousov wrote:
>> I think that toolchain@ is more suitable list for the discussion.
>> 
>> On Sun, Jun 04, 2017 at 05:44:31PM -0500, Eric van Gyzen wrote:
>>> _thr_rtld_init() calls memcpy() for the sole purpose of resolving its
>>> PLT entry. With clang 4.0 and the current code, compiler optimization
>>> defeats this attempt by completely eliding the call. Other compilers
>>> or code might emit inline instructions instead of the library call,
>>> also defeating the purpose.
>> After looking more closely at the whole situation, I have a question
>> that we probably must answer first.  Is clang -ffreestanding mode
>> broken ?  memcpy(3) is not included into the set of the environment
>> features required for a C11 freestanding implementation,  and clang
>> pretending that it knows the semantic of the call sounds broken.
> Ok, I realized that I only added -ffreestanding to the rtld Makefile.
> So clang is optimizing correctly there.  Should we compile both libc
> and libthr in the freestanding environment as well ?
> 
> I am sure that there are a lot of similar assumptions that libc and libthr
> code calls into itself and not into the arbitrary re-implementation of
> the same code as generated by modern compilers.
> 
> Then hopefully the __no_optimization hack is not needed.

Like that, or use -fno-builtin:

$ cat test-memcpy.c
#include <string.h>

void foo(void)
{
	long dummy = -1;
	memcpy(&dummy, &dummy, sizeof(dummy));
}

$ cc -O2 -fno-builtin -fomit-frame-pointer -S test-memcpy.c -o -
	.text
	.file	"test-memcpy.c"
	.globl	foo                     # -- Begin function foo
	.p2align	4, 0x90
	.type	foo, at function
foo:                                    # @foo
	.cfi_startproc
# BB#0:                                 # %entry
	pushq	%rax
.Lcfi0:
	.cfi_def_cfa_offset 16
	movq	$-1, (%rsp)
	movq	%rsp, %rdi
	movl	$8, %edx
	movq	%rdi, %rsi
	callq	memcpy
	popq	%rax
	retq
.Lfunc_end0:
	.size	foo, .Lfunc_end0-foo
	.cfi_endproc
                                        # -- End function

	.ident	"FreeBSD clang version 5.0.0 (trunk 304659) (based on LLVM 5.0.0svn)"
	.section	".note.GNU-stack","", at progbits

-Dimitry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 194 bytes
Desc: Message signed with OpenPGP
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20170605/3f9d5384/attachment.sig>


More information about the freebsd-hackers mailing list