[AsmPrinter] Emit .weak directive for weak linkage on COFF for symbols without a...
authorMartin Storsjö <martin@martin.st>
Sat, 28 Mar 2020 15:37:59 +0000 (17:37 +0200)
committerMartin Storsjö <martin@martin.st>
Sat, 28 Mar 2020 16:48:58 +0000 (18:48 +0200)
commite6112a56dde6db13ea29721077cb9d294d01b54d
tree79d20d9b89392d0163b0491675f98d5bd5d5a955
parent6a4f8423ae681c4feb7481b9c7e222cc6cdb2c45
[AsmPrinter] Emit .weak directive for weak linkage on COFF for symbols without a comdat

MC already knows how to emulate the .weak directive (with its ELF
semantics; i.e., an undefined weak symbol resolves to 0, and a defined
weak symbol has lower link precedence than a strong symbol of the same
name) using COFF weak externals. Plumb this through the ASM printer too,
so that definitions marked with __attribute__((weak)) at the language
level (which gets translated to weak linkage at the IR level) have the
corresponding .weak directive emitted. Note that declarations marked
with __attribute__((weak)) at the language level (which translates to
extern_weak at the IR level) already have .weak directives emitted.

Weak*/linkonce* symbols without an associated comdat (in particular, ones
generated with __attribute__((weak)) in C/C++) were earlier emitted as
normal unique globals, as the comdat is required to provide the linkonce
semantics. This change makes sure they are emitted as .weak instead,
allowing other symbols to override them.

Rename the existing coff-weak.ll test to coff-linkonce.ll. I'm not
quite sure what that test covers, since the behavior being tested in it
(the emission of a one_only section) is just a result of passing
-function-sections to llc; the linkonce_odr makes no difference.

Add a new coff-weak.ll which tests the new directive emission.

Based on an previous patch by Shoaib Meenai.

Differential Revision: https://reviews.llvm.org/D44543
llvm/include/llvm/MC/MCAsmInfo.h
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/lib/MC/MCAsmInfoCOFF.cpp
llvm/test/CodeGen/X86/coff-linkonce.ll [new file with mode: 0644]
llvm/test/CodeGen/X86/coff-weak.ll
llvm/test/CodeGen/X86/dllexport-x86_64.ll
llvm/test/CodeGen/X86/dllexport.ll