Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Creating aliases to static functions doesn't work as expected #22601

Open Quuxplusone opened 9 years ago

Quuxplusone commented 9 years ago
Bugzilla Link PR22602
Status NEW
Importance P normal
Reported by torne@google.com
Reported on 2015-02-16 08:09:25 -0800
Last modified on 2015-02-23 12:50:40 -0800
Version 3.5
Hardware PC Linux
CC llvm-bugs@lists.llvm.org, rafael@espindo.la, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Function aliases don't appear to count as uses for the purposes of -Wunused-
function. The output binary is correct, only the warning is wrong.

alias.c:
  static void foo() {}
  void bar() __attribute__((alias("foo")));

$ clang -c -Wunused-function alias.c
/home/torne/alias.c:1:13: warning: unused function 'foo'
      [-Wunused-function]
static void foo() {}
            ^
1 warning generated.

$ clang --version
clang version 3.5
Target: x86_64-pc-linux-gnu
Thread model: posix
Quuxplusone commented 9 years ago
Actually, this is not just a bogus warning; it fails to compile entirely if you
compile it as C++:

alias.cc:
extern "C" {

static void foo() {}

void bar() __attribute__((alias("foo")))
Quuxplusone commented 9 years ago
Retitling, as this is not just a bogus warning; it fails to compile entirely if
you compile it as C++:

alias.cc:
extern "C" {

static void foo() {}

void bar() __attribute__((alias("foo")));

}

$ clang++ -c alias.cc -Wunused-function
alias.cc:5:13: warning: unused function 'foo' [-Wunused-function]
static void foo() {}
            ^
alias.cc:7:27: error: alias must point to a defined variable or function
void bar() __attribute__((alias("foo")));
                          ^
1 warning and 1 error generated.

It looks like this is due to a combination of extern "C" not applying to static
functions (and thus foo is having its name mangled), and alias() taking the
mangled symbol name as a parameter. Aliasing it to "_Z3foov" makes it compile
successfully, though the warning about foo being unused is still incorrectly
generated.

This same source works correctly in gcc.
Quuxplusone commented 9 years ago

gcc does not mangle the name of the extern "C" static function, which is presumably why it works there.

Quuxplusone commented 9 years ago

Yes, I believe it is unfortunate we implemented the language linkage rules as they are worded on the standard and not how they are used in practice.

This particular case might be solvable by creating the (foo -> mangled name) alias earlier.

Quuxplusone commented 9 years ago

Yeah, I am aware that the standard doesn't necessarily match what gcc is doing here, but since I believe attribute((alias)) is a gcc extension, cases that work in gcc should probably also work in clang for compatibility with existing code. :)

Quuxplusone commented 9 years ago
(In reply to comment #4)
> This particular case might be solvable by creating the (foo -> mangled name)
> alias earlier.

I think this is the right solution. There's also a fairly straightforward
workaround for the build failure:

  static void foo() __asm__("foo");
  static void foo() {}
  void bar() __attribute__((alias("foo")));

This does not help with the warning, though.