[llvm] r310990 - [llvm-dlltool] Fix creating stdcall/fastcall import libraries for i386

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 15 22:18:36 PDT 2017


Author: mstorsjo
Date: Tue Aug 15 22:18:36 2017
New Revision: 310990

URL: http://llvm.org/viewvc/llvm-project?rev=310990&view=rev
Log:
[llvm-dlltool] Fix creating stdcall/fastcall import libraries for i386

Hook up the -k option (that in the original GNU dlltool removes the
@n suffix from the symbol that the final executable ends up linked to).

In llvm-dlltool, make sure that functions end up with the undecorate
name type if this option is set and they are decorated. In mingw, when
creating import libraries from def files instead of creating an import
library as a side effect of linking a DLL, the symbol names in the def
contain the stdcall/fastcall decoration (but no leading underscore).

By setting the undecorate name type, a linker linking to the import
library will omit the decoration from the DLL import entry.

With this in place, mingw-w64 for i386 built with llvm-dlltool/clang
produces import libraries that actually work.

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

Added:
    llvm/trunk/test/DllTool/coff-decorated.def
Modified:
    llvm/trunk/lib/Object/COFFModuleDefinition.cpp
    llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
    llvm/trunk/lib/ToolDrivers/llvm-dlltool/Options.td

Modified: llvm/trunk/lib/Object/COFFModuleDefinition.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFModuleDefinition.cpp?rev=310990&r1=310989&r2=310990&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFModuleDefinition.cpp (original)
+++ llvm/trunk/lib/Object/COFFModuleDefinition.cpp Tue Aug 15 22:18:36 2017
@@ -232,7 +232,13 @@ private:
     for (;;) {
       read();
       if (Tok.K == Identifier && Tok.Value[0] == '@') {
-        Tok.Value.drop_front().getAsInteger(10, E.Ordinal);
+        if (Tok.Value.drop_front().getAsInteger(10, E.Ordinal)) {
+          // Not an ordinal modifier at all, but the next export (fastcall
+          // decorated) - complete the current one.
+          unget();
+          Info.Exports.push_back(E);
+          return Error::success();
+        }
         read();
         if (Tok.K == KwNoname) {
           E.Noname = true;

Modified: llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp?rev=310990&r1=310989&r2=310990&view=diff
==============================================================================
--- llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp (original)
+++ llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp Tue Aug 15 22:18:36 2017
@@ -155,6 +155,22 @@ int llvm::dlltoolDriverMain(llvm::ArrayR
   if (Path.empty())
     Path = getImplibPath(Def->OutputFile);
 
+  if (Machine == IMAGE_FILE_MACHINE_I386 && Args.getLastArg(OPT_k)) {
+    for (COFFShortExport& E : Def->Exports) {
+      if (E.isWeak() || (!E.Name.empty() && E.Name[0] == '?'))
+        continue;
+      E.SymbolName = E.Name;
+      // Trim off the trailing decoration. Symbols will always have a
+      // starting prefix here (either _ for cdecl/stdcall, @ for fastcall
+      // or ? for C++ functions). (Vectorcall functions also will end up having
+      // a prefix here, even if they shouldn't.)
+      E.Name = E.Name.substr(0, E.Name.find('@', 1));
+      // By making sure E.SymbolName != E.Name for decorated symbols,
+      // writeImportLibrary writes these symbols with the type
+      // IMPORT_NAME_UNDECORATE.
+    }
+  }
+
   if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine))
     return 1;
   return 0;

Modified: llvm/trunk/lib/ToolDrivers/llvm-dlltool/Options.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ToolDrivers/llvm-dlltool/Options.td?rev=310990&r1=310989&r2=310990&view=diff
==============================================================================
--- llvm/trunk/lib/ToolDrivers/llvm-dlltool/Options.td (original)
+++ llvm/trunk/lib/ToolDrivers/llvm-dlltool/Options.td Tue Aug 15 22:18:36 2017
@@ -12,13 +12,13 @@ def D_long : JoinedOrSeparate<["--"], "d
 def d: JoinedOrSeparate<["-"], "d">, HelpText<"Input .def File">;
 def d_long : JoinedOrSeparate<["--"], "input-def">, Alias<d>;
 
+def k: Flag<["-"], "k">, HelpText<"Kill @n Symbol from export">;
+def k_alias: Flag<["--"], "kill-at">, Alias<k>;
+
 //==============================================================================
 // The flags below do nothing. They are defined only for dlltool compatibility.
 //==============================================================================
 
-def k: Flag<["-"], "k">, HelpText<"Kill @n Symbol from export">;
-def k_alias: Flag<["--"], "kill-at">, Alias<k>;
-
 def S: JoinedOrSeparate<["-"], "S">, HelpText<"Assembler">;
 def S_alias: JoinedOrSeparate<["--"], "as">, Alias<S>;
 

Added: llvm/trunk/test/DllTool/coff-decorated.def
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/DllTool/coff-decorated.def?rev=310990&view=auto
==============================================================================
--- llvm/trunk/test/DllTool/coff-decorated.def (added)
+++ llvm/trunk/test/DllTool/coff-decorated.def Tue Aug 15 22:18:36 2017
@@ -0,0 +1,26 @@
+; RUN: llvm-dlltool -k -m i386 --input-def %s --output-lib %t.a
+; RUN: llvm-readobj %t.a | FileCheck %s
+; RUN: llvm-nm %t.a | FileCheck %s -check-prefix=CHECK-NM
+
+LIBRARY test.dll
+EXPORTS
+CdeclFunction
+StdcallFunction at 4
+ at FastcallFunction@4
+StdcallAlias at 4=StdcallFunction at 4
+??_7exception@@6B@
+
+; CHECK: Name type: noprefix
+; CHECK: Symbol: __imp__CdeclFunction
+; CHECK: Symbol: _CdeclFunction
+; CHECK: Name type: undecorate
+; CHECK: Symbol: __imp__StdcallFunction at 4
+; CHECK: Symbol: _StdcallFunction at 4
+; CHECK: Name type: undecorate
+; CHECK: Symbol: __imp_ at FastcallFunction@4
+; CHECK: Symbol: @FastcallFunction at 4
+; CHECK: Name type: name
+; CHECK: Symbol: __imp_??_7exception@@6B@
+; CHECK: Symbol: ??_7exception@@6B@
+; CHECK-NM: w _StdcallAlias at 4
+; CHECK-NM: U _StdcallFunction at 4




More information about the llvm-commits mailing list