GCC _attribute__ weak weakref

来源:互联网 发布:淘宝评论看不见 编辑:程序博客网 时间:2024/05/22 03:18
#include <stdio.h> /*void test(){    printf("the test for weak refrence!\n");    }*/ static __attribute__ ((weakref("test"))) void foo(); int main(){    if(foo) foo();  }

如果test函数有定义,执行main程序时将执行test函数;

如果test函数没有定义,执行main函数时foo地址为0,不执行foo函数。??


In GNU C, you declare certain things about functions called in your program which help the compiler optimize function calls and check your code more carefully.

The keyword __attribute__ allows you to specify special attributes when making a declaration. This keyword is followed by an attribute specification inside double parentheses.

The following attributes are currently defined for functions on  all targets:                                                                                                                                                                  alignedalloc_sizenoreturnreturns_twicenoinline,noclonealways_inlineflattenpureconstnothrowsentinelformatformat_argno_instrument_function,

no_split_stacksectionconstructordestructorusedunuseddeprecatedweakmallocaliasifunc,warn_unused_resultnonnullgnu_inlineexternally_visiblehot,

coldartificialerror and warning.


Several other attributes are defined for functions on particular target systems. Other attributes, including sectionare supported for variables declarations (see Variable Attributes) and for types (see Type Attributes).

GCC plugins may provide their own attributes.

You may also specify attributes with `__' preceding and following each keyword. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use __noreturn__ instead of noreturn.

alias ("target")
The  alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified. For instance,

          void __f () { /* Do something. */; }          void f () __attribute__ ((weak, alias ("__f")));

defines `f' to be a weak alias for `__f'. In C++, the mangled name for the target must be used. It is an error if `__f' is not defined in the same translation unit.

Not all target machines support this 

......

.....


weak
The  weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.


weakref
weakref ("target ")
The  weakref attribute marks a declaration as a weak reference. Without arguments, it should be accompanied by an alias attribute naming the target symbol. Optionally, the target may be given as an argument to weakref itself. In either case,  weakref implicitly marks the declaration as weak. Without a target, given as an argument to weakref or to aliasweakref is equivalent to weak.
          static int x() __attribute__ ((weakref ("y")));          /* is equivalent to... */          static int x() __attribute__ ((weak, weakref, alias ("y")));          /* and to... */          static int x() __attribute__ ((weakref));          static int x() __attribute__ ((alias ("y")));

A weak reference is an alias that does not by itself require a definition to be given for the target symbol. If the target symbol is only referenced through weak references, then it becomes aweak undefined symbol. If it is directly referenced, however, then such strong references prevail, and a definition will be required for the symbol, not necessarily in the same translation unit.

The effect is equivalent to moving all references to the alias to a separate translation unit, renaming the alias to the aliased symbol, declaring it as weak, compiling the two separate translation units and performing a reloadable link on them.



0 0