__attribute__ Properties of Weak,alias

Source: Internet
Author: User
Tags error code socket

Weak Alias has nothing to do with Weak Reference, but I thought of it when I saw Weak Reference.

Weak Alias is something in the GCC extension, which is actually the property of the function. This thing may often be used in the implementation of the library, such as glibc inside the use of a lot. Transcribe a section of the GCC Handbook and explain what the function properties are doing,

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

First on the code, see how weak alias is written. The first file dummy.c content,

#include int __foo () {puts ("I do no thing.");} int foo () __attribute__ ((Weak,alias ("__foo"));

Weak and alias are two attributes respectively. weak makes Foo the symbol in the target file as weak symbol instead of global symbol. Viewing the target file generated by the compilation dummy.c with the NM command you can see that foo is a weak symbol, and that the previous tag is W. When you add a weak property to a function, the function can be compiled successfully even if the function is not defined.

00000000 T __foo 00000000 W foo U puts

while alias makes Foo a __foo alias , __foo and Foo must be defined in the same compilation unit, or a compilation error will occur.

So the use of this thing is.

Look at the second file, FUNC.C,

#include int foo () {puts ("I do something.");}

Here is a function named Foo. If we compile func.c and dummy.c to get two target files, when we link with FUNC.O and dummy.o and other target files simultaneously, if the other target file references the symbol Foo, the final use is the function defined in FUNC.C, not __foo, Although it has an alias Foo. In other words, the function we end up using is the one that "actually does things". Of course, using the DUMMY.O link alone is the "Do not work" function. if Foo in dummy.o is not weak symbol, there will be a conflict when linking, which is why we want to use weak .

The implementation of GLIBC often uses weak alias. For example, its socket function, in the C file you will see a __socket function, it almost nothing to do, just set up some error code, return something. In the same C file, a __socket weak alias socket will be declared again. The actual completion of the code through the Assembly to achieve, in the other assembly file will have set the system call number, execute sysenter or int and other actions to request system calls. Before look glibc inside system call implementation of time depressed too long, is that time only then know weak alias this thing.

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.