asmlinkage and GCC's Function attribute

原创 2010年05月23日 22:51:00

#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))

Function Attribute

Variable Attribute

Attribute Syntax

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.

在GNU C中,为了帮助你完善函数调用和更仔细地检查你的代码,你会为程序中的函数声明一些确定的东西。

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: noreturn, noinline, always_inline, pure, const, nothrow, sentinel, format, format_arg, no_instrument_function, section, constructor, destructor, used, unused, deprecated, weak, malloc, alias, warn_unused_result and nonnull. Several other attributes are defined for functions on particular target systems.

一般的函数定义的。noreturn, noinline, always_inline, pure, const, nothrow, sentinel, format, format_arg, no_instrument_function, section, constructor, destructor, used, unused, deprecated, weak, malloc, alias, warn_unused_result and nonnull. 还有几个其它的则是为特殊的函数机制定义的。

regparm (number)
    On the Intel 386, the regparm attribute causes the compiler to pass up to number integer arguments in registers EAX, EDX, and ECX instead of on the stack. Functions that take a variable number of arguments will continue to be passed all of their arguments on the stack.

    在Intel 386架构上,regparm属性使编译器从eax,edx,ecx这些寄存器而不是堆栈来传递整型参数。
    Beware that on some ELF systems this attribute is unsuitable for global functions in shared libraries with lazy binding (which is the default). Lazy binding will send the first call via resolving code in the loader, which might assume EAX, EDX and ECX can be clobbered, as per the standard calling conventions. Solaris 8 is affected by this. GNU systems with GLIBC 2.1 or higher, and FreeBSD, are believed to be safe since the loaders there save all registers. (Lazy binding can be disabled with the linker or the loader if desired, to avoid the problem.)

section ("section-name")
    Normally, the compiler places the code it generates in the text section. Sometimes, however, you need additional sections, or you need certain particular functions to appear in special sections. The section attribute specifies that a function lives in a particular section. For example, the declaration:

extern void foobar (void) __attribute__ ((section ("bar")));

    puts the function foobar in the bar section.

    Some file formats do not support arbitrary sections so the section attribute is not available on all platforms. If you need to map the entire contents of a module to a particular section, consider using the facilities of the linker instead.

This attribute, attached to a function, means that the function is meant to be possibly unused. GCC will not produce a warning for this function.

This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly.

A few standard library functions, such as abort and exit, cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact. For example,
void fatal () __attribute__ ((noreturn));

fatal (/* ... */)
/* ... */ /* Print error message. */ /* ... */
exit (1);

The noreturn keyword tells the compiler to assume that fatal cannot return. It can then optimize without regard to what would happen if fatal ever did return. This makes slightly better code. More importantly, it helps avoid spurious warnings of uninitialized variables.

The noreturn keyword does not affect the exceptional path when that applies: a noreturn-marked function may still return to the caller by throwing an exception or calling longjmp.

Do not assume that registers saved by the calling function are restored before calling the noreturn function.

It does not make sense for a noreturn function to have a return type other than void.

The attribute noreturn is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function does not return, which works in the current version and in some older versions, is as follows:

typedef void voidfn ();

volatile voidfn fatal;

This approach does not work in GNU C++.

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.


Some people object to the __attribute__ feature, suggesting that ISO C's #pragma should be used instead. At the time __attribute__ was designed, there were two reasons for not doing this.

1.It is impossible to generate #pragma commands from a macro.
2.There is no telling what the same #pragma might mean in another compiler.

These two reasons applied to almost any application that might have been proposed for #pragma. It was basically a mistake to use #pragma for anything.

The ISO C99 standard includes _Pragma, which now allows pragmas to be generated from macros. In addition, a #pragma GCC namespace is now in use for GCC-specific pragmas. However, it has been found convenient to use __attribute__ to achieve a natural attachment of attributes to their corresponding declarations, whereas #pragma GCC is of use for constructs that do not naturally form part of the grammar.


注:此文非原创,是博主在学习过程中网络摘抄整理而出。gcc的__attribute__的编绎属性__attribute__主要用于 改变所声明或定义的函数或数据的特性,它有很多子项,用于改变作用对象的...
  • itxiebo
  • itxiebo
  • 2016年03月16日 14:56
  • 2002

kernel-----内核宏问题asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__))

#define SYSCALL_DEFINEx(x, name, ...) \ asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__)) 系统调用的宏...
  • yf210yf
  • yf210yf
  • 2013年08月11日 08:27
  • 2427


相信大家在看linux的source code的时候,都会注意到asmlinkage这个宏,它是用来做什么的呢?The asmlinkage tag is one other thing that w...
  • zxj2008cn
  • zxj2008cn
  • 2006年05月11日 22:50
  • 3821

gcc –finstrument-functions特性的应用

1          gcc –finstrument-functions特性的应用       这几天看了一下nginx的源码,分析过程中发现nginx模块加载、初始化这部分利用了函数指针的方式挂...
  • hengshan
  • hengshan
  • 2012年08月15日 15:49
  • 4860


  • wavemcu
  • wavemcu
  • 2015年02月20日 21:18
  • 7077


  • liujiaoyage
  • liujiaoyage
  • 2014年06月17日 17:29
  • 3538

django error 'function' object has no attribute 'objects' 解决办法

view.py中函数和app的名字重复了。 请def一个和app名字不一样的函数名,问题就解决了。 参考:
  • gavinking0110
  • gavinking0110
  • 2016年11月12日 12:30
  • 1639

执行Python程序时,报AttributeError: 'function' object has no attribute 'name'

写Python程序时,经常会报AttributeError: 'function' object has no attribute 'name'错误,仔细检查了程序,发现代码并没有错误,比如我的一个蓝...
  • qq_25046261
  • qq_25046261
  • 2018年01月08日 10:01
  • 410

GCC的__attribute__ ((constructor))和__attribute__ ((destructor))

gcc允许为函数设置__attribute__ ((constructor))和__attribute__ ((destructor))两种属性,顾名思义,就是将被修饰的函数作为构造函数或析构函数。程...
  • jasonchen_gbd
  • jasonchen_gbd
  • 2015年03月08日 22:21
  • 1828

Python:AttributeError: 'function' object has no attribute '_doc_'

运行func_doc.py时,系统报错“”AttributeError: 'function' object has no attribute '_doc_'“:...
  • marysa
  • marysa
  • 2014年05月06日 16:38
  • 7934
您举报文章:asmlinkage and GCC's Function attribute