Strong_alias & weak_alias & _ attribute __

Source: Internet
Author: User

Http://www.cnblogs.com/justinyo/archive/2013/03/12/2956438.html

To view the implementation function of malloc in Linux, the glibc source code file is downloaded, but the implementation function cannot be found.

The file name should be in malloc/malloc. C.

 

We found that the implementation of _ libc_malloc is similar.

 

How does one change from malloc to _ libc_malloc?

 

The file contains a statement.

A little sign

 

Let's take a look at the implementation of strong_alias.

_ Typeof (name) aliasname indicates the alias definition. For more information, see the following description.

 

GNU CA major feature (but not known to beginners) is the _ attribute _ mechanism.

_ Attribute _ FUNCTION attribute, variable attribute, and type attribute can be set)

There are two underscores (_ attribute _) on the front and back, and a pair of original arc will be followed. The _ attribute _ parameter in the arc is

The syntax format of _ attribute _ is:

_ Attribute _ (Attribute-list ))

Function attribute helps developers add some features to function declaration, which makes the compiler more powerful in error checking.

The _ attribute _ mechanism is also easily compatible with non-GNU applications.

Gnu cc requires the use of-wall, which is a good way to control warning information.

 

This defines malloc as the alias of _ libc_malloc, __malloc is also the alias of _ libc_malloc.

__attribute__((alias)) Variable attribute

This variable attribute enables you to specify multiple aliases for variables.

Where a variable is defined in the current Translation Unit, the alias reference is replaced by a reference to the variable, and the alias is emitted alongside the original name. where a variable is not defined in the current Translation Unit, the alias
Reference is replaced by a reference to the real variable. where a variable is defined as static, the variable name is replaced by the alias name and the variable is declared external if the alias is declared external.

Note

Function names might also be aliased using the corresponding function attribute
__attribute__((alias)).

Syntax
type newname __attribute__((alias("oldname")));

Where:

oldname

Is the name of the variable to be aliased

newname

Is the new name of the aliased variable.

Example
#include <stdio.h>int oldname = 1;extern int newname __attribute__((alias("oldname"))); // declarationvoid foo(void){
    printf("newname = %d\n", newname); // prints 1}

 

There is also a concept of weak_alias. Compared with strong_alias, there are more _ attribute _ and more weak.

 

Alias: the alias of the current symbol (target. For example:

?
123 void
__f () {
/* Do something. */; }
  void
f () __attribute__ ((weak, alias (
"__f")));

 

Alias modifies the symbol F, and the specified target symbol is _ F, that is, the symbol F is the alias of the symbol _ F.

Weak: indicates that the current symbol is a weak symbol (weak symbol), rather than a global symbol. Example:
1. First, prepare a library file (taking static library as an example, I will mention why dynamic library is not used as an example ):

[root@localhost weak_test1]# cat /etc/issue

CentOS release 6.2 (Final)

Kernel \r on an \m

[root@localhost weak_test1]# uname -a

Linux localhost.localdomain 2.6.32-220.el6.i686 #1 SMP Tue Dec 6 16:15:40 GMT 2011 i686 i686 i386 GNU/Linux

[root@localhost weak_test1]# vi mylib.c

[root@localhost weak_test1]# cat !$

cat mylib.c

#include <stdio.h>

void foo()

{

printf("lib test\n");

}

void foo() __attribute__ ((weak));

[root@localhost weak_test1]# <span class="wp_keywordlink_affiliate"><a href="http://lenky.info/tag/gcc/"
title="View all articles in GCC">gcc</a></span> -c mylib.c

[root@localhost weak_test1]# ar crv libmylib.a mylib.o

a - mylib.o

We can see that the libmylib. A library defines a function Foo (), which is a weak type.

2. Compile an application (consisting of two source files MyApp. C and myfoo. C ):

[root@localhost weak_test1]# vi myapp.c

[root@localhost weak_test1]# cat !$

cat myapp.c

#include <stdio.h>

int main()

{

foo();

return 0;

}

[root@localhost weak_test1]# vi myfoo.c

[root@localhost weak_test1]# cat !$

cat myfoo.c

#include <stdio.h>

void foo()

{

printf("app test\n");

}

The Foo () function called by the source file myfoo. C can be from the libmylib. A library, or another source file of the application, connected and executed:

[root@localhost weak_test1]# gcc myapp.c myfoo.c libmylib.a -o myapp_weak

[root@localhost weak_test1]# ./myapp_weak

app test

The print shows app test, that is, the function Foo () of the application is called ().

3. If the application itself does not provide the function Foo (), what is the situation:

[root@localhost weak_test1]# gcc myapp.c libmylib.a -o myapp

[root@localhost weak_test1]# ./myapp

lib test

The printed display is lib test, which calls the weak type function Foo () in libmylib. A library ().

4. Try removing the weak modifier from the Library:

[root@localhost weak_test1]# vi mylib.c

[root@localhost weak_test1]# cat mylib.c

#include <stdio.h>

void foo()

{

printf("lib test\n");

}

//void foo() __attribute__ ((weak));

[root@localhost weak_test1]# gcc -c mylib.c;ar crv libmylib.a mylib.o

r - mylib.o

[root@localhost weak_test1]# gcc myapp.c myfoo.c libmylib.a -o myapp

[root@localhost weak_test1]# ./myapp

app test

Is it okay after weak modification is removed? So what is the use of the weak attribute? Try to put libmylib. A above:

[root@localhost weak_test1]# gcc myapp.c libmylib.a myfoo.c -o myapp

/tmp/ccIjvazY.o: In function `foo':

myfoo.c:(.text+0x0): multiple definition of `foo'

libmylib.a(mylib.o):mylib.c:(.text+0x0): first defined here

collect2: ld returned 1 exit status

[root@localhost weak_test1]# gcc libmylib.a myapp.c myfoo.c -o myapp

[root@localhost weak_test1]# gcc libmylib.a myfoo.c myapp.c -o myapp

[root@localhost weak_test1]# gcc myfoo.c libmylib.a myapp.c -o myapp

[root@localhost weak_test1]# gcc myfoo.c myapp.c libmylib.a -o myapp

For comparison, add the weak modifier:

[root@localhost weak_test1]# vi mylib.c

[root@localhost weak_test1]# cat mylib.c

#include <stdio.h>

void foo()

{

printf("lib test\n");

}

void foo() __attribute__ ((weak));

[root@localhost weak_test1]# gcc -c mylib.c;ar crv libmylib.a mylib.o

r - mylib.o

[root@localhost weak_test1]# gcc myapp.c libmylib.a myfoo.c -o myapp

[root@localhost weak_test1]# gcc libmylib.a myapp.c myfoo.c -o myapp

[root@localhost weak_test1]# gcc libmylib.a myfoo.c myapp.c -o myapp

[root@localhost weak_test1]# gcc myfoo.c libmylib.a myapp.c -o myapp

[root@localhost weak_test1]# gcc myfoo.c myapp.c libmylib.a -o myapp

[root@localhost weak_test1]# ./myapp

app test

After weak modification is removed, an error is reported for only one cup. Is it related to the location where the library file is discharged? You can use the source code without using library functions:

[root@localhost weak_test1]# vi mylib.c

[root@localhost weak_test1]# cat mylib.c

#include <stdio.h>

void foo()

{

printf("lib test\n");

}

//void foo() __attribute__ ((weak));

[root@localhost weak_test1]# gcc myapp.c myfoo.c mylib.c -o myapp

/tmp/ccbKVCYU.o: In function `foo':

mylib.c:(.text+0x0): multiple definition of `foo'

/tmp/cc3DKEyS.o:myfoo.c:(.text+0x0): first defined here

collect2: ld returned 1 exit status

[root@localhost weak_test1]# gcc myapp.c mylib.c myfoo.c -o myapp

/tmp/ccosX9JJ.o: In function `foo':

myfoo.c:(.text+0x0): multiple definition of `foo'

/tmp/ccyyg6us.o:mylib.c:(.text+0x0): first defined here

collect2: ld returned 1 exit status

Error (inevitable), plus:

[root@localhost weak_test1]# vi mylib.c

[root@localhost weak_test1]# cat mylib.c

#include <stdio.h>

void foo()

{

printf("lib test\n");

}

void foo() __attribute__ ((weak));

[root@localhost weak_test1]# gcc myapp.c myfoo.c mylib.c -o myapp

[root@localhost weak_test1]# gcc myapp.c mylib.c myfoo.c -o myapp

What is the conclusion?It seems that the original meaning of the weak modifier is that a weak function can be overwritten by other functions with the same name (that is, no conflict occurs). If no other functions with the same name exist, use the weak function, similar to the default function;

However, due to the special influence of the "library", the weak type becomes "unreliable" to the library, and according to weak definitions aren't so weak, for the new version of glibc, the weak modifier only affects static libraries.

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.