点滴1@glibc

xiaoxiao2021-02-28  5

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: aligned, alloc_size, noreturn,returns_twice, noinline, always_inline, flatten, pure, const, nothrow, sentinel, format, format_arg, no_instrument_function, section, constructor, destructor, used, unused, deprecated, weak, malloc, alias, warn_unused_result, nonnull, gnu_inline, externally_visible, hot, cold, artificial, error 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.

A 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;
转载请注明原文地址: https://www.6miu.com/read-2399965.html

最新回复(0)