[PATCH] D36548: [llvm-dlltool] Fix creating stdcall/fastcall import libraries for i386

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 10 01:51:01 PDT 2017


mstorsjo updated this revision to Diff 110530.
mstorsjo retitled this revision from "[llvm-dlltool] Fix creating stdcall import libraries for MinGW/i386" to "[llvm-dlltool] Fix creating stdcall/fastcall import libraries for i386".
mstorsjo edited the summary of this revision.
mstorsjo added a comment.

Changed the undecorate action into something that should work for all of cdecl/stdcall/fastcall/vectorcall. Vectorcall doesn't work here yet since the def parser always prepends an underscore though (and fixing that is a separate topic that involves quite a bit of lld as well), but at least this particular line should be mostly fine now.

Extended the testcase to test both stdcall and fastcall (mingw has def files containing both). In order to handle decorated fastcall symbols in the def file, the parser had to get a minor adjustment as well.


https://reviews.llvm.org/D36548

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


Index: test/DllTool/coff-decorated.def
===================================================================
--- /dev/null
+++ test/DllTool/coff-decorated.def
@@ -0,0 +1,18 @@
+; RUN: llvm-dlltool -k -m i386 --input-def %s --output-lib %t.a
+; RUN: llvm-readobj %t.a | FileCheck %s
+
+LIBRARY test.dll
+EXPORTS
+CdeclFunction
+StdcallFunction at 4
+ at FastcallFunction@4
+
+; 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
Index: lib/ToolDrivers/llvm-dlltool/Options.td
===================================================================
--- lib/ToolDrivers/llvm-dlltool/Options.td
+++ lib/ToolDrivers/llvm-dlltool/Options.td
@@ -12,13 +12,13 @@
 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>;
 
Index: lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
===================================================================
--- lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -155,6 +155,20 @@
   if (Path.empty())
     Path = getImplibPath(Def->OutputFile);
 
+  if (Machine == IMAGE_FILE_MACHINE_I386 && Args.getLastArg(OPT_k)) {
+    for (COFFShortExport& E : Def->Exports) {
+      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;
Index: lib/Object/COFFModuleDefinition.cpp
===================================================================
--- lib/Object/COFFModuleDefinition.cpp
+++ lib/Object/COFFModuleDefinition.cpp
@@ -232,7 +232,14 @@
     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();
+          break;
+        }
         read();
         if (Tok.K == KwNoname) {
           E.Noname = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36548.110530.patch
Type: text/x-patch
Size: 3484 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170810/663321da/attachment.bin>


More information about the llvm-commits mailing list