[PATCH] D52602: [LLD] [COFF] Cope with GCC produced weak aliases referring to comdat functions

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 28 11:28:32 PDT 2018


mstorsjo added a comment.

In https://reviews.llvm.org/D52602#1249419, @ruiu wrote:

> The "proper" way of implementing a weak alias is to embed a "/alternatename" command line option to a .drectve section, so it is unfortunate that gcc took a different approach.
>
> Is there any way to distinguish a symbol explicitly annotated with `__attribute__((weak))` and an inline function?


Yes, they are completely different things. A normal inline function with GCC works just as it does with MSVC, you get a COMDAT symbol. A `__attribute__((weak)) func()` symbol gets you a COFF weak external named `func`, which points at `.weak.func.<somename>`, which LLD can handle just fine (MSVC import libraries use COFF weak externals occasionally as well).

There's just two minor extra details:

- As an "extra feature" maybe, GCC/binutils allows you to have multiple distinct weak implementations of a function. If there's no strong definition, the linker picks the first weak one it saw. This is handled by the other patch, https://reviews.llvm.org/D52601, and IMO is rather straightforward.
- As a bug(?) in GCC 5.x (and maybe older), certain special cases of inline templated functions in C++ can produce a weak symbol pointing to the COMDAT symbol. This is handled by this patch.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D52602





More information about the llvm-commits mailing list