Bug 23058 - visibility attribute does not work for aliased symbols
Summary: visibility attribute does not work for aliased symbols
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.0.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: visibility
Depends on:
Reported: 2005-07-25 11:47 UTC by Bruno Haible
Modified: 2006-06-20 18:20 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Bruno Haible 2005-07-25 11:47:41 UTC
When a function or variable has its assembler name given through an 
__asm__, the __attribute__((__visibility__("default"))) present on the 
same declaration is ignored. 
$ gcc -v 
Using built-in specs. 
Target: i686-pc-linux-gnu 
Configured with: /freebsd/gnu/linuxarchive/gcc-4.0.1/configure 
--prefix=/packages/gnu-inst-gcc/4.0.1 --enable-shared 
--enable-version-specific-runtime-libs --enable-nls --enable-threads=posix 
--enable-__cxa_atexit --with-as=/packages/gnu/bin/as 
Thread model: posix 
gcc version 4.0.1 
$ cat bug.c 
       int hidden_var 
extern __attribute__((__visibility__("default"))) 
       int exported_var 
       int hidden_func (void) 
extern __attribute__((__visibility__("default"))) 
       int exported_func (void) 
int my_hidden_var = 0; 
int my_exported_var = 0; 
int my_hidden_func (void) { return 0; } 
int my_exported_func (void) { return 0; } 
$ gcc -c -fvisibility=hidden bug.c 
$ readelf --syms bug.o 
Symbol table '.symtab' contains 11 entries: 
   Num:    Value  Size Type    Bind   Vis      Ndx Name 
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND  
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS bug.c 
     2: 00000000     0 SECTION LOCAL  DEFAULT    1  
     3: 00000000     0 SECTION LOCAL  DEFAULT    2  
     4: 00000000     0 SECTION LOCAL  DEFAULT    3  
     5: 00000000     0 SECTION LOCAL  DEFAULT    5  
     6: 00000000     0 SECTION LOCAL  DEFAULT    4  
     7: 00000000     4 OBJECT  GLOBAL HIDDEN    3 my_hidden_var 
     8: 00000004     4 OBJECT  GLOBAL HIDDEN    3 my_exported_var 
     9: 00000000    10 FUNC    GLOBAL HIDDEN    1 my_hidden_func 
    10: 0000000a    10 FUNC    GLOBAL HIDDEN    1 my_exported_func 
$ gcc -shared -o bug.so bug.o && nm bug.so | grep my_ 
00000476 t my_exported_func 
000015e4 b my_exported_var 
0000046c t my_hidden_func 
000015e0 b my_hidden_var 
As you can see, the symbols my_exported_func, my_exported_var are not 
exported from bug.so, because their visibility was "HIDDEN" in bug.o. 
Known workaround: If I write "int exported_var = 0;" instead of 
"int my_exported_var = 0;", and "int exported_func (void) { return 0; }" 
instead of ""int my_exported_func (void) { return 0; }", the problem 
Comment 1 Jason Merrill 2006-06-20 18:20:55 UTC
You're telling the compiler that "exported_var" has default visibility, then defining a variable called "my_exported_var" which happens to have the same symbol.  The compiler doesn't unify multiple declarations with the same assembler name, and I don't think it should.

You should define the same declaration that you used to declare the visibility.