[PATCH] D118328: [CodeGen] Emit COFF symbol type for function aliases

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 27 00:40:20 PST 2022


mstorsjo created this revision.
mstorsjo added a reviewer: rnk.
Herald added subscribers: jeroen.dobbelaere, pengfei, hiraditya.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

On the level of the generated object files, both symbols (both
original and alias) are generally indistinguishable - both are
regular defined symbols. But previously, only the original
function had the COFF ComplexType set to IMAGE_SYM_DTYPE_FUNCTION,
while the symbol created via an alias had the type set to
IMAGE_SYM_DTYPE_NULL.

This matches what GCC does, which emits directives for setting the
COFF symbol type for this kind of alias symbol too.

This makes a difference when GNU ld.bfd exports symbols without
dllexport directives or a def file - it seems to decide between
function or data exports based on the COFF symbol type. This means
that functions created via aliases, like some C++ constructors,
are exported as data symbols (missing the thunk for calling without
dllimport).

The hasnt been an issue when doing the same with LLD, as LLD decides
between function or data export based on the flags of the section
that the symbol points at.

This should fix the root cause of
https://github.com/msys2/MINGW-packages/issues/10547.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118328

Files:
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  llvm/test/CodeGen/X86/coff-alias-type.ll


Index: llvm/test/CodeGen/X86/coff-alias-type.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/coff-alias-type.ll
@@ -0,0 +1,24 @@
+; RUN: llc -mtriple=x86_64-windows-gnu -o - %s | FileCheck %s
+
+%struct.MyStruct = type { i8 }
+
+ at _ZN8MyStructC1Ev = dso_local alias void (%struct.MyStruct*), void (%struct.MyStruct*)* @_ZN8MyStructC2Ev
+
+define dso_local void @_ZN8MyStructC2Ev(%struct.MyStruct* %this) {
+entry:
+  ret void
+}
+
+; CHECK:      .def     _ZN8MyStructC2Ev
+; CHECK-NEXT: .scl     2
+; CHECK-NEXT: .type    32
+; CHECK-NEXT: .endef
+; CHECK-NEXT: .globl   _ZN8MyStructC2Ev
+; CHECK:      {{^}}_ZN8MyStructC2Ev:
+
+; CHECK:      .globl   _ZN8MyStructC1Ev
+; CHECK-NEXT: .def     _ZN8MyStructC1Ev
+; CHECK-NEXT: .scl     2
+; CHECK-NEXT: .type    32
+; CHECK-NEXT: .endef
+; CHECK-NEXT: .set     _ZN8MyStructC1Ev, _ZN8MyStructC2Ev
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1647,8 +1647,17 @@
 
   // Set the symbol type to function if the alias has a function type.
   // This affects codegen when the aliasee is not a function.
-  if (IsFunction)
+  if (IsFunction) {
     OutStreamer->emitSymbolAttribute(Name, MCSA_ELF_TypeFunction);
+    if (TM.getTargetTriple().isOSBinFormatCOFF()) {
+      OutStreamer->BeginCOFFSymbolDef(Name);
+      OutStreamer->EmitCOFFSymbolStorageClass(
+          GA.hasLocalLinkage() ? COFF::IMAGE_SYM_CLASS_STATIC : COFF::IMAGE_SYM_CLASS_EXTERNAL);
+      OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
+                                                 << COFF::SCT_COMPLEX_TYPE_SHIFT);
+      OutStreamer->EndCOFFSymbolDef();
+    }
+  }
 
   emitVisibility(Name, GA.getVisibility());
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118328.403531.patch
Type: text/x-patch
Size: 1886 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220127/63a1345b/attachment.bin>


More information about the llvm-commits mailing list