This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/81310] New: Is __attribute__ alias on variables valid?


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81310

            Bug ID: 81310
           Summary: Is __attribute__ alias on variables valid?
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: palves at redhat dot com
  Target Milestone: ---

I'm improving GDB's support for finding symbols resulting from __attribute__
alias, and while writing a test noticed something that I'd like clarification
on.

While __attribute__ alias is documented as a function attribute:

  https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

I noticed that GCC lets you use it with variables too.  That seems odd to me,
since that makes it possible to have two different objects with the same
address:

 $ cat symbol-alias.c
 extern int global1;
 extern int global2;

 int
 main (void)
 {
   if (&global1 == &global2)
     __builtin_abort ();
   return 0;
 }

 $ cat symbol-alias1.c
 int global1 = 1;

 extern int global2 __attribute__ ((alias ("global1")));

 $ /opt/gcc/bin/gcc symbol-alias.c symbol-alias1.c -O2 -Wall

 $ nm ./a.out | grep " global"
 0000000000601040 D global1
 0000000000601040 D global2

 $ ./a.out 
 Aborted (core dumped)

I also noticed that clang optimizes out the abort above, even at -O0, most
likely assuming that the objects can't have both the same address:

 $ clang symbol-alias.c symbol-alias1.c -O0 -Wall
 $ nm ./a.out | grep " global"
 000000000060102c D global1
 000000000060102c D global2
 $ ./a.out 
 $  (no abort)

 $ objdump -d a.out
 ...
 0000000000400500 <main>:
   400500:       55                      push   %rbp
   400501:       48 89 e5                mov    %rsp,%rbp
   400504:       31 c0                   xor    %eax,%eax
   400506:       c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%rbp)
   40050d:       5d                      pop    %rbp
   40050e:       c3                      retq   
   40050f:       90                      nop
 ...


So, hence the questions:

- Is __attribute__ alias valid on variables?
- If valid, ISTM the documentation needs improvement.
- If invalid, shouldn't GCC reject it?
- And if invalid, is "(&global1 == &global2) => true" a missed optimization?

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]