点滴1@glibc

void (*signal (int sig, void (*handler)(int)))(int)  复杂 -> 拆开看

glibc/signal/signal.h

/* Type of a signal handler.  */
typedef void (*__sighandler_t) (int);

glibc/signal/signal.c

/* Set the handler for the signal SIG to HANDLER,
   returning the old handler, or SIG_ERR on error.  */
__sighandler_t
signal (sig, handler)
     int sig;
     __sighandler_t handler;
{
  __set_errno (ENOSYS);
  return SIG_ERR;
}

weak_alias (signal, ssignal)

别名机制

glibc/include/libc-symbols.h

/* Define ALIASNAME as a weak alias for NAME.
   If weak aliases are not available, this defines a strong alias.  */
# define weak_alias(name, aliasname) _weak_alias (name, aliasname)
# define _weak_alias(name, aliasname) \
  extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));

Declaring Attributes of Functions

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_sizenoreturn,returns_twicenoinlinealways_inlineflattenpureconstnothrowsentinelformatformat_argno_instrument_functionsectionconstructordestructorusedunuseddeprecatedweakmallocaliaswarn_unused_resultnonnullgnu_inlineexternally_visiblehotcoldartificialerror and warning. Several other attributes are defined for functions on particular target systems. Other attributes, including section are supported for variables declarations (see Variable Attributes) and for types (see Type 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.

See Attribute Syntax, for details of the exact syntax for using attributes.

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 attribute. 

Assembler (GAS) 别名机制

#  ifdef HAVE_ASM_GLOBAL_DOT_NAME
#   define strong_alias(original, alias)				\
  ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP		\
  .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original) ASM_LINE_SEP	\
  ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP		\
  .set C_SYMBOL_DOT_NAME (alias),C_SYMBOL_DOT_NAME (original)
#   define strong_data_alias(original, alias)				\
  ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP		\
  .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
#  else
#   define strong_alias(original, alias)				\
  ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP		\
  .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
#   define strong_data_alias(original, alias) strong_alias(original, alias)
#  endif

.set 参考下文,https://www.ece.cmu.edu/~ee349/f-2012/lab2/gas-tips.pdf

2.1 Determining the Length of Strings in AssemblyThe “.” symbol refers to the current assembling address, and the “.set” directive assigns a value to asymbol. Combined, one may determine the size of a section of assembly code by subtracting the currentaddress from a label and assigning the result to a symbol. For example, the following code sets the symbolhello size to the size of the string hello str:

hello_str:

.ascii "Hello world!\n"

.set hello_size, .-hello_str

With this code, modifications to the hello str string will automatically change the value of hello sizeaccordingly.

typeof or __typeof

If you are writing a header file that must work when included in ISO C programs, write __typeof__ instead of typeof. See Alternate Keywords.

typeof-construct can be used anywhere a typedef name could be used. For example, you can use it in a declaration, in a cast, or inside of sizeof or typeof.

     #define max(a,b) \
       ({ typeof (a) _a = (a); \
           typeof (b) _b = (b); \
         _a > _b ? _a : _b; })
This declares  y  as an array of pointers to characters:
          typeof (typeof (char *)[4]) y;     

It is equivalent to the following traditional C declaration:

          char *y[4];     

To see the meaning of the declaration using typeof, and why it might be a useful way to write, rewrite it with these macros:

          #define pointer(T)  typeof(T *)
          #define array(T, N) typeof(T [N])     

Now the declaration can be rewritten this way:

          array (pointer (char), 4) y;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值