Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

c - In clang, how do you use per-function optimization attributes?

I'm trying to compile a specific function with no optimization using clang, in order to prevent certain security-related calls to memset() from being optimized away.

According to the documentation that can be found here, there exists an optnone attribute which allows this. Also, an example can be found here.

Unfortunately, (at least on the below version of clang, on OS X 10.9.5), this is causing compiler warnings, as can be seen in this example:

$ clang --version
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

$ cat optnone.c
#include <string.h>

__attribute__((optnone)) void*
always_memset(void *b, int c, size_t len)
{
    return memset(b, c, len);
}

$ clang -Wall -O3 -c -o optnone.o optnone.c
optnone.c:3:16: warning: unknown attribute 'optnone' ignored [-Wattributes]
__attribute__((optnone)) void*
               ^
1 warning generated.

I also tried using #pragma clang optimize off, but this caused an unknown pragma ignored warning.

Does anyone know why this isn't working? Did I miss a prerequisite for using this feature? (I also tried using various different -std= parameters, including c11, gnu11, c99, and gnu99, but nothing changed the behavior.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As clang documentation says,

Clang supports GCC’s gnu attribute namespace. All GCC attributes which are accepted with the __attribute__((foo)) syntax are also accepted as [[gnu::foo]]. This only extends to attributes which are specified by GCC (see the list of GCC function attributes, GCC variable attributes, and GCC type attributes). As with the GCC implementation, these attributes must appertain to the declarator-id in a declaration, which means they must go either at the start of the declaration or immediately after the name being declared.

Try

void* always_memset(void *b, int c, size_t len) [[gnu::optimize(0)]]

or

void* always_memset(void *b, int c, size_t len) __attribute__ ((optimize("0")));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...