[cfe-dev] Creating an alias to a static function in C++

Evan Driscoll via cfe-dev cfe-dev at lists.llvm.org
Tue May 1 21:43:09 PDT 2018


I'm not sure if this is a potential bug report (I could file something more
formally) or what, but I'm hitting something that's pretty weird to me.


G++ (random versions from GCC 4.6ish to 7.3) accepts the following code:

  extern "C" {
    static __attribute__((used)) void f1() {}
    void f() __attribute__ ((weak, alias("f1")));
  }

however Clang rejects it (interested in version 6 but earlier versions
reject as well), with

  <source>:3:35: error: alias must point to a defined variable or function

(sandbox: https://godbolt.org/g/38VVPS)

After playing around it for a bit, I notice something interesting. If I
drop the alias and static, I get an object file with the expected symbols:

  $ cat huh.cc
  extern "C" {
    __attribute__((used)) void f(int x) { }
  }
  $ clang -c huh.cc && nm huh.o
  0000000000000000 T f

but if I include the static, then I get something that is weird to me:

  $ cat huh.cc
  extern "C" {
    static __attribute__((used)) void dummy1(int x) { }
  }
  $ clang -c huh.cc && nm huh.o
  0000000000000000 t f
  0000000000000000 t _ZL1fi
  $ clang -c huh.cc && nm huh.o | c++filt
  0000000000000000 t f
  0000000000000000 t f(int)


So I tried using the mangled name in the alias, and that works:

  extern "C" {
    static __attribute__((used)) void f1 () {}
    void f () __attribute__ ((weak, alias("_ZL2f1v"))); // OK
  }

and of course then so does this

  static __attribute__((used)) void f1 () {}
  extern "C" {
    void f () __attribute__ ((weak, alias("_ZL2f1v")));
  }


Furthermore, if I browse the Itanium ABI page, I can't even figure out how
you get a "_ZL..." mangled symbol, but I'm sure I'm missing something (e.g.
c++filt demangles it fine...)


Anyway, I can easily work around this is my actual code, but how much of
this is expected behavior?

Evan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180501/42ab60da/attachment.html>


More information about the cfe-dev mailing list