linuxgcclinkeraliasabi

How do I find out if my linux platform supports strong aliases?


The GCC manual describes -fabi-compat-version=n, which is used to handle variations in C++ name mangling in the slightly variant C++ ABIs of GCC 3.4 to 9.2, and probably later. It has an important caveat:

On targets that support strong aliases, G++ works around mangling changes by creating an alias with the correct mangled name when defining a symbol with an incorrect mangled name. This switch specifies which ABI version to use for the alias.

However, it's not immediately obvious how you find out if your platform does support strong aliases.


Solution

  • The easy way to find out is to write a small program that uses __attribute__ to create an alias, and then use nm to see if the alias exists. Here's sample code, which is a variant on the classic "Hello, World" program:

    /* strong_alias.c 
       compile with 'gcc strong_alias.c'
       run ./a.out to check it works, and then run 'nm ./a.out'
       to check that strong_alias() has the same address as main() */
    
    #include <stdio.h>
    
    int main( int argc, char *argv[])
    {
      printf( "Hello, World\n");
      return 0;
    }
    
    int strong_alias( int argc, char *argv[]) __attribute__ (( alias ("main")));
    

    Compile it and check that it runs, then use nm a.out to look at its symbol table. Here's a version that was compiled on CentOS 7 for x86-64:

    nm ./a.out | egrep ' (main|strong_alias)'
    000000000040052d T main
    000000000040052d T strong_alias
    

    We can see that main and strong_alias have the same address, and are thus aliases.