Gnu c-using GNU gcc _ attribute _ mechanic 01 function attributegnu C-a unique hellowo

Source: Internet
Author: User

Recently, when I read the glibc source code, I encountered a lot of questions about _ attribute _. I simply found the relevant materials and learned about it.

If this problem is not solved, sometimes it is really difficult to start. For glibc, use xcscope to search POSIX pthread.

Function: pthread_create. The result is as follows:

If you follow up the function all the way, you will find a macro definition like this:

 

The following are comments made by GNU glibc developers on these macros, which seem straightforward:

 

Now, we only know that pthread_create will be linked to _ pthread_create_2.1/_ pthread_create_2.0 during the link.


The above shows the commonly used programming skills in GNU glibc. This is not the focus of this article. I have not studied this point. if you know me, you can leave me a message. hope to give you more advice.

 

Next, the alias mechanism is used in many parts of glibc, which is related to GNU gcc's _ attribute _ mechanic. The GCC document contains the attribute syntax chapter,

Here, we will explain _ attribute _ mechanisms. The reference links are as follows:

Attribute Syntax: http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Attribute-Syntax.html#Attribute-Syntax

 

Attribute syntax is divided into three parts:

Function attribute: function attribute Semantics

Variable attribute: Variable attribute Semantics

Type attribute: Type attribute Semantics

 

 

The following describes the first part of each selection.:

I. Function attribute:

1. _ attribute _ (alias): declares an alias for a symbol.

 

 
Return-type newname ([arguments-list]) _ attribute _ (alias ("oldname ")))

 

Oldname:Original Function Name

Newname: alias of the original function

Let's take a look at the implementation of the _ strong_alias macro in glibc:

 

In the last sentence, _ typeof (name) is the return type of the original function, and aliasname is the alias. You do not need to explain it later...

See belowProgramRight:

 

 
# Include <stdio. h> # include <stdlib. h> void Foo () {printf ("\ ninside % s \ n" ,__ function _);} void _ Foo () _ attribute _ (alias ("foo"); // @ Author: forestint main (INT ARGs, char ** argv) {_ Foo (); return exit_success ;}

 

The output is very simple, that is, the original function is called through the alias.

 

2. _ attribute _ (const): The attribute const is added after gcc2.5. It is unavailable in earlier versions. const attribute can only be used for functions with numeric parameter types.

Because the return values of functions with numeric parameters are the same, when multiple calls are performed, the compiler only needs to perform the optimization once to obtain the execution result.

Const attribute is used to optimize the compiler.

 
Int evaluate (int B) _ attribute _ (const); int function_attribute_const_0 (int B) {int alocal = 0; alocal + = function_attribute_const_0 (B ); alocal + = function_attribute_const_0 (B); Return alocal ;}

The "function_attribute_const_0 (INT)" function will be executed once.

 


3. _ attribute _ (constructor | destructor (priority): As mentioned in the previous blog:Gnu c-a special helloworld program extended to: _ attribute _ (constructor) | (destructor) (priority ))

 

4. _ attribute _ (Deprecated): deprecated, obsolete. If the deprecated attribute function is used anywhere in the source file, the compiler will issue a warning.

_ Attribute _ (deprecated (MSG): MSG, which will be output in the compiler warnning.

Below is a small example of rewriting:

 

 
# Include <stdio. h> # include <stdlib. h >__ attribute _ (deprecated ("foo function has been discarded") void Foo () {printf ("\ ninside % s \ n ", __function _);} void _ Foo () _ attribute _ (alias ("foo"); // @ Author: forestint main (INT ARGs, char ** argv) {_ Foo (); Return exit_success ;}

 

Output:

It can be seen that in the above example, the declaration of Foo () is discarded, and the calling in main () will throw the warnning during compilation. However, the calling using the alias will not output

Warnning. There is no relevant introduction to this on the official website. Please refer to the practice.

 

5. _ attribute _ (format (archetype, string-index, first-to-check): Format attribute provides functions of the printf, scanf, strftime, strfmon type.

Parameters and the corresponding format type check ~)

There are many usage of format attribute in glibc:

 

Archtype: determines how format string will be interpreted. the interpreted type should be printf, scanf, strftime, gnu_printf, gnu_scanf, gnu_strftime .. (You can also use _ printf __, like glibc __,

_ Scanf __,__ strftime... the start and end of the line below ).


String-index: the function parameter increments from left to right with the serial number 1, 2, and 3. String-index is the serial number of FMT.

 

First-to-check: the first parameter in the parameter list to be compared with the FMT Format.

 

 
# Include <stdio. h> # include <stdlib. h> void Foo (const char * FMT ,...) \__ attribute _ (_ format _ (_ printf __, 1, 2); // @ Author: forestint main (INT ARGs, char ** argv) {// @ testfoo ("\ n % d", "test_a", "test_ B"); Return exit_success;} void Foo (const char * FMT ,...) {/* do nothing */}

 

The output result is as follows:

 

 

 

6. _ attribute _ (weak): weak symbol, weak symbol. if two identical global symbols exist, a redefinition error is thrown. if weak attribute is used

When both weak symbol and non-weak symbol exist, linker uses non-weak symbol.

If yes, only weak symbol is used.

Glibc has a large number of instances:

 

 

Supplement: the attribute fields after _ attribute _ can also be used together.

 

The first part is complete. If you have a rough look at this, you will not find the function implementation problem after reading some source code libraries.

Smooth all obstacles ~

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.