Default Handlers in C: weak_alias function tells the linker that new is to be a weak alias for  old. That is, this definition of new is a weak symbol. If there is no other definition of a symbol called new, this old definition stands.

Might seems alien to you first, so go through a below example & read again.

  • Definition of weak_alias is as follows :
1
2
#define weak_alias(old, new) \
        extern __typeof(old) new __attribute__((weak, alias(#old)))
  • If there is another (non-weak) definition of new then that non-weak(i.e. strong) definition stands and the weak definition is ignored.

Let’s understand default handlers in C: weak_alias by example

oldDef.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#define weak_alias(old, new) \
        extern __typeof(old) new __attribute__((weak, alias(#old)))


void DefaultHandler()
{
        puts("Default Handler");
}

weak_alias( DefaultHandler, Feature1);

weak.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>

/*
void Feature1()
{
        puts("Feature 1");
}
*/

int main()
{
        Feature1();
        DefaultHandler();

        return 0;
}

Compilation

1
2
$ gcc weak.c oldDef.c -o weak
$ ./weak
  • If you run the above program as it is, it will print
1
2
Default Handler
Default Handler
  • But if you uncomment Feature1()then it will print
1
2
Default Handler
Feature 1
  • Why so? It’s due to way linker understand symbols. When you first run without Feature1()function linker does not found strong Feature1()symbol so it links to DefaultHandler() And in the second case,  when we introduce Feature1()linker finds a strong symbol & links it to Feature1()