[lld] r316317 - [COFF] Fix exporting of functions starting with underscores, etc

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 02:08:24 PDT 2017


Author: mstorsjo
Date: Mon Oct 23 02:08:24 2017
New Revision: 316317

URL: http://llvm.org/viewvc/llvm-project?rev=316317&view=rev
Log:
[COFF] Fix exporting of functions starting with underscores, etc

This fixes exporting functions in the following cases:
- functions starting with an underscore in def files
- functions starting with an underscore, via dllexport attributes, for mingw
- fastcall and vectorcall functions when declared undecorated in def files
- vectorcall functions when declared decorated in def files
- stdcall functions when declared decorated in def files for mingw

This still exports the stdcall functions with the wrong name
in the normal msvc/link.exe mode, if declared with decoration in
the def file though (this is not a regression though). Exporting
functions via def files including decoration is not something I
believe is routinely done though, but is tested to try to match
link.exe's behaviour as far as easily possible.

Differential Revision: https://reviews.llvm.org/D39170

Modified:
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/SymbolTable.cpp
    lld/trunk/test/COFF/def-export-stdcall.s
    lld/trunk/test/COFF/dllexport-mingw.s

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=316317&r1=316316&r2=316317&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Mon Oct 23 02:08:24 2017
@@ -212,7 +212,8 @@ void LinkerDriver::enqueueArchiveMember(
 }
 
 static bool isDecorated(StringRef Sym) {
-  return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
+  return Sym.startswith("@") || Sym.contains("@@") || Sym.startswith("?") ||
+         (!Config->MinGW && Sym.contains('@'));
 }
 
 // Parses .drectve section contents and returns a list of files
@@ -510,8 +511,8 @@ static void createImportLibrary(bool AsL
 static void parseModuleDefs(StringRef Path) {
   std::unique_ptr<MemoryBuffer> MB = check(
     MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path);
-  COFFModuleDefinition M =
-      check(parseCOFFModuleDefinition(MB->getMemBufferRef(), Config->Machine));
+  COFFModuleDefinition M = check(parseCOFFModuleDefinition(
+      MB->getMemBufferRef(), Config->Machine, Config->MinGW));
 
   if (Config->OutputFile.empty())
     Config->OutputFile = Saver.save(M.OutputFile);

Modified: lld/trunk/COFF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/SymbolTable.cpp?rev=316317&r1=316316&r2=316317&view=diff
==============================================================================
--- lld/trunk/COFF/SymbolTable.cpp (original)
+++ lld/trunk/COFF/SymbolTable.cpp Mon Oct 23 02:08:24 2017
@@ -351,10 +351,18 @@ StringRef SymbolTable::findMangle(String
     return findByPrefix(("?" + Name + "@@Y").str());
   if (!Name.startswith("_"))
     return "";
-  // Search for x86 C function.
+  // Search for x86 stdcall function.
   StringRef S = findByPrefix((Name + "@").str());
   if (!S.empty())
     return S;
+  // Search for x86 fastcall function.
+  S = findByPrefix(("@" + Name.substr(1) + "@").str());
+  if (!S.empty())
+    return S;
+  // Search for x86 vectorcall function.
+  S = findByPrefix((Name.substr(1) + "@@").str());
+  if (!S.empty())
+    return S;
   // Search for x86 C++ non-member function.
   return findByPrefix(("?" + Name.substr(1) + "@@Y").str());
 }
@@ -364,8 +372,10 @@ void SymbolTable::mangleMaybe(SymbolBody
   if (!U || U->WeakAlias)
     return;
   StringRef Alias = findMangle(U->getName());
-  if (!Alias.empty())
+  if (!Alias.empty()) {
+    log(U->getName() + " aliased to " + Alias);
     U->WeakAlias = addUndefined(Alias);
+  }
 }
 
 SymbolBody *SymbolTable::addUndefined(StringRef Name) {

Modified: lld/trunk/test/COFF/def-export-stdcall.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/def-export-stdcall.s?rev=316317&r1=316316&r2=316317&view=diff
==============================================================================
--- lld/trunk/test/COFF/def-export-stdcall.s (original)
+++ lld/trunk/test/COFF/def-export-stdcall.s Mon Oct 23 02:08:24 2017
@@ -1,21 +1,89 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=i686-windows-msvc %s -o %t.obj
-# RUN: echo -e "LIBRARY foo\nEXPORTS\n  stdcall" > %t.def
+# RUN: echo -e "LIBRARY foo\nEXPORTS\n  stdcall\n  fastcall\n  vectorcall\n  _underscored" > %t.def
 # RUN: lld-link -entry:dllmain -dll -def:%t.def %t.obj -out:%t.dll -implib:%t.lib
-# RUN: llvm-readobj %t.lib | FileCheck %s
-# CHECK: Name type: undecorate
-# CHECK: __imp__stdcall at 8
-# CHECK: _stdcall at 8
+# RUN: llvm-readobj %t.lib | FileCheck -check-prefix UNDECORATED-IMPLIB %s
+# RUN: llvm-readobj -coff-exports %t.dll | FileCheck -check-prefix UNDECORATED-EXPORTS %s
+
+# UNDECORATED-IMPLIB: Name type: noprefix
+# UNDECORATED-IMPLIB-NEXT: __imp___underscored
+# UNDECORATED-IMPLIB-NEXT: __underscored
+# UNDECORATED-IMPLIB: Name type: undecorate
+# UNDECORATED-IMPLIB-NEXT: __imp_ at fastcall@8
+# UNDECORATED-IMPLIB-NEXT: fastcall at 8
+# UNDECORATED-IMPLIB: Name type: undecorate
+# UNDECORATED-IMPLIB-NEXT: __imp__stdcall at 8
+# UNDECORATED-IMPLIB-NEXT: _stdcall at 8
+# UNDECORATED-IMPLIB: Name type: undecorate
+# UNDECORATED-IMPLIB-NEXT: __imp_vectorcall@@8
+# UNDECORATED-IMPLIB-NEXT: vectorcall@@8
+
+# UNDECORATED-EXPORTS: Name: _underscored
+# UNDECORATED-EXPORTS: Name: fastcall
+# UNDECORATED-EXPORTS: Name: stdcall
+# UNDECORATED-EXPORTS: Name: vectorcall
+
+
+# RUN: echo -e "LIBRARY foo\nEXPORTS\n  _stdcall at 8\n  @fastcall at 8\n  vectorcall@@8" > %t.def
+# RUN: lld-link -entry:dllmain -dll -def:%t.def %t.obj -out:%t.dll -implib:%t.lib
+# RUN: llvm-readobj %t.lib | FileCheck -check-prefix DECORATED-IMPLIB %s
+# RUN: llvm-readobj -coff-exports %t.dll | FileCheck -check-prefix DECORATED-EXPORTS %s
+
+# DECORATED-IMPLIB: Name type: name
+# DECORATED-IMPLIB-NEXT: __imp_ at fastcall@8
+# DECORATED-IMPLIB-NEXT: @fastcall at 8
+# TODO: To match link.exe, this one should also be Name type: name.
+# DECORATED-IMPLIB: Name type: noprefix
+# DECORATED-IMPLIB-NEXT: __imp__stdcall at 8
+# DECORATED-IMPLIB-NEXT: _stdcall at 8
+# DECORATED-IMPLIB: Name type: name
+# DECORATED-IMPLIB-NEXT: __imp_vectorcall@@8
+# DECORATED-IMPLIB-NEXT: vectorcall@@8
+
+# DECORATED-EXPORTS: Name: @fastcall at 8
+# TODO: To match link.exe, this one should actually be _stdcall at 8
+# DECORATED-EXPORTS: Name: stdcall at 8
+# DECORATED-EXPORTS: Name: vectorcall@@8
+
+
+# RUN: echo -e "LIBRARY foo\nEXPORTS\n  stdcall at 8\n  @fastcall at 8" > %t.def
+# RUN: lld-link -lldmingw -entry:dllmain -dll -def:%t.def %t.obj -out:%t.dll -implib:%t.lib
+# RUN: llvm-readobj %t.lib | FileCheck -check-prefix DECORATED-MINGW-IMPLIB %s
+# RUN: llvm-readobj -coff-exports %t.dll | FileCheck -check-prefix DECORATED-MINGW-EXPORTS %s
+
+# DECORATED-MINGW-IMPLIB: Name type: name
+# DECORATED-MINGW-IMPLIB-NEXT: __imp_ at fastcall@8
+# DECORATED-MINGW-IMPLIB-NEXT: fastcall at 8
+# DECORATED-MINGW-IMPLIB: Name type: noprefix
+# DECORATED-MINGW-IMPLIB-NEXT: __imp__stdcall at 8
+# DECORATED-MINGW-IMPLIB-NEXT: _stdcall at 8
+
+# DECORATED-MINGW-EXPORTS: Name: @fastcall at 8
+# DECORATED-MINGW-EXPORTS: Name: stdcall at 8
+
 
         .def     _stdcall at 8;
         .scl    2;
         .type   32;
         .endef
         .globl  _stdcall at 8
+        .globl  @fastcall at 8
+        .globl  vectorcall@@8
+        .globl  __underscored
 _stdcall at 8:
         movl    8(%esp), %eax
         addl    4(%esp), %eax
         retl    $8
+ at fastcall@8:
+        movl    8(%esp), %eax
+        addl    4(%esp), %eax
+        retl    $8
+vectorcall@@8:
+        movl    8(%esp), %eax
+        addl    4(%esp), %eax
+        retl    $8
+__underscored:
+        ret
 
         .def     _dllmain;
         .scl    2;

Modified: lld/trunk/test/COFF/dllexport-mingw.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/dllexport-mingw.s?rev=316317&r1=316316&r2=316317&view=diff
==============================================================================
--- lld/trunk/test/COFF/dllexport-mingw.s (original)
+++ lld/trunk/test/COFF/dllexport-mingw.s Mon Oct 23 02:08:24 2017
@@ -5,15 +5,20 @@
 # RUN: lld-link -lldmingw -dll -out:%t.dll -entry:main %t.obj -implib:%t.lib
 # RUN: llvm-readobj %t.lib | FileCheck %s
 
+# CHECK: Symbol: __imp___underscoredFunc
+# CHECK: Symbol: __underscoredFunc
 # CHECK: Symbol: __imp__func
 # CHECK: Symbol: _func
 
 .global _main
 .global _func
+.global __underscoredFunc
 .text
 _main:
   ret
 _func:
   ret
+__underscoredFunc:
+  ret
 .section .drectve
-.ascii "-export:func"
+.ascii "-export:func -export:_underscoredFunc"




More information about the llvm-commits mailing list