In clang, how do you use optimization attributes for each function?

I am trying to compile a specific function without optimization using clang to prevent the optimization of certain security related calls from memset() .

According to the documentation that can be found here , there is an optnone attribute that allows this. In addition, an example can be found here .

Unfortunately (at least in the clang version below, in OS X 10.9.5), this causes compiler warnings, as shown 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 triggered an unknown pragma ignored .

Does anyone know why this is not working? Did I miss the prerequisite for using this function? (I also tried using various different -std= options, including c11 , gnu11 , c99 and gnu99 , but nothing changed the behavior.)

+6
source share
2 answers

As the clang documentation says,

Clang supports the gnu GCC attribute namespace. All GCC attributes accepted with __attribute__((foo)) syntax are also accepted as [[gnu::foo]] . This applies only to attributes specified by GCC (see List of GCC Function Attributes, GCC Variable Attributes, and GCC Type Attributes). As with the GCC implementation, these attributes must match the identifier of the ad in the ad, which means they must go either at the beginning of the ad or immediately after the name is 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"))); 
+4
source

As @dulacc said in his comment, __attribute__ ((optnone)) working on clang 9.0.0 on Mac High Sierra.

0
source

Source: https://habr.com/ru/post/976457/


All Articles