[llvm] r331859 - [COFF] Improve correctness of def parsing for GNU features

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Wed May 9 02:21:53 PDT 2018


Author: mstorsjo
Date: Wed May  9 02:21:53 2018
New Revision: 331859

URL: http://llvm.org/viewvc/llvm-project?rev=331859&view=rev
Log:
[COFF] Improve correctness of def parsing for GNU features

The operator == used for exporting a function with a different
name in the DLL compared to the name in the import library
(which is useful for adding linker level aliases for function
in the import library) is a feature distinct and different from
the operator = used for exporting a function with a different
name (both in import library and DLL) than in the implementation
producing the DLL.

When creating an import library using dlltool, from a def file that
contains forwards (Func = OtherDll.Func), this shouldn't affect the
produced import library, which should still behave just as if it
was a normal exported function.

This clears a lot of confusion and subtle misunderstandings, and
avoids a parameter that was used to avoid creating weak aliases
when invoked from lld. (This parameter was added previously due to
the existing conflation of the two features.)

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

Modified:
    llvm/trunk/include/llvm/Object/COFFImportFile.h
    llvm/trunk/lib/Object/COFFImportFile.cpp
    llvm/trunk/lib/Object/COFFModuleDefinition.cpp
    llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
    llvm/trunk/test/tools/llvm-dlltool/coff-decorated.def
    llvm/trunk/test/tools/llvm-dlltool/coff-weak-exports.def

Modified: llvm/trunk/include/llvm/Object/COFFImportFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFFImportFile.h?rev=331859&r1=331858&r2=331859&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFFImportFile.h (original)
+++ llvm/trunk/include/llvm/Object/COFFImportFile.h Wed May  9 02:21:53 2018
@@ -74,6 +74,7 @@ struct COFFShortExport {
   std::string Name;
   std::string ExtName;
   std::string SymbolName;
+  std::string AliasTarget;
 
   uint16_t Ordinal = 0;
   bool Noname = false;
@@ -81,10 +82,6 @@ struct COFFShortExport {
   bool Private = false;
   bool Constant = false;
 
-  bool isWeak() {
-    return ExtName.size() && ExtName != Name;
-  }
-
   friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
     return L.Name == R.Name && L.ExtName == R.ExtName &&
             L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
@@ -98,8 +95,7 @@ struct COFFShortExport {
 
 Error writeImportLibrary(StringRef ImportName, StringRef Path,
                          ArrayRef<COFFShortExport> Exports,
-                         COFF::MachineTypes Machine, bool MakeWeakAliases,
-                         bool MinGW);
+                         COFF::MachineTypes Machine, bool MinGW);
 
 } // namespace object
 } // namespace llvm

Modified: llvm/trunk/lib/Object/COFFImportFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFImportFile.cpp?rev=331859&r1=331858&r2=331859&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFImportFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFImportFile.cpp Wed May  9 02:21:53 2018
@@ -566,8 +566,7 @@ NewArchiveMember ObjectFactory::createWe
 
 Error writeImportLibrary(StringRef ImportName, StringRef Path,
                          ArrayRef<COFFShortExport> Exports,
-                         MachineTypes Machine, bool MakeWeakAliases,
-                         bool MinGW) {
+                         MachineTypes Machine, bool MinGW) {
 
   std::vector<NewArchiveMember> Members;
   ObjectFactory OF(llvm::sys::path::filename(ImportName), Machine);
@@ -585,12 +584,6 @@ Error writeImportLibrary(StringRef Impor
     if (E.Private)
       continue;
 
-    if (E.isWeak() && MakeWeakAliases) {
-      Members.push_back(OF.createWeakExternal(E.Name, E.ExtName, false));
-      Members.push_back(OF.createWeakExternal(E.Name, E.ExtName, true));
-      continue;
-    }
-
     ImportType ImportType = IMPORT_CODE;
     if (E.Data)
       ImportType = IMPORT_DATA;
@@ -606,6 +599,12 @@ Error writeImportLibrary(StringRef Impor
     if (!Name)
       return Name.takeError();
 
+    if (!E.AliasTarget.empty() && *Name != E.AliasTarget) {
+      Members.push_back(OF.createWeakExternal(E.AliasTarget, *Name, false));
+      Members.push_back(OF.createWeakExternal(E.AliasTarget, *Name, true));
+      continue;
+    }
+
     Members.push_back(
         OF.createShortImport(*Name, E.Ordinal, ImportType, NameType));
   }

Modified: llvm/trunk/lib/Object/COFFModuleDefinition.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFModuleDefinition.cpp?rev=331859&r1=331858&r2=331859&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFModuleDefinition.cpp (original)
+++ llvm/trunk/lib/Object/COFFModuleDefinition.cpp Wed May  9 02:21:53 2018
@@ -37,6 +37,7 @@ enum Kind {
   Identifier,
   Comma,
   Equal,
+  EqualEqual,
   KwBase,
   KwConstant,
   KwData,
@@ -104,9 +105,10 @@ public:
     }
     case '=':
       Buf = Buf.drop_front();
-      // GNU dlltool accepts both = and ==.
-      if (Buf.startswith("="))
+      if (Buf.startswith("=")) {
         Buf = Buf.drop_front();
+        return Token(EqualEqual, "==");
+      }
       return Token(Equal, "=");
     case ',':
       Buf = Buf.drop_front();
@@ -282,6 +284,13 @@ private:
         E.Private = true;
         continue;
       }
+      if (Tok.K == EqualEqual) {
+        read();
+        E.AliasTarget = Tok.Value;
+        if (Machine == IMAGE_FILE_MACHINE_I386 && !isDecorated(E.AliasTarget, MingwDef))
+          E.AliasTarget = std::string("_").append(E.AliasTarget);
+        continue;
+      }
       unget();
       Info.Exports.push_back(E);
       return Error::success();

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=331859&r1=331858&r2=331859&view=diff
==============================================================================
--- llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp (original)
+++ llvm/trunk/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp Wed May  9 02:21:53 2018
@@ -158,7 +158,7 @@ int llvm::dlltoolDriverMain(llvm::ArrayR
 
   if (Machine == IMAGE_FILE_MACHINE_I386 && Args.getLastArg(OPT_k)) {
     for (COFFShortExport& E : Def->Exports) {
-      if (E.isWeak() || (!E.Name.empty() && E.Name[0] == '?'))
+      if (!E.AliasTarget.empty() || (!E.Name.empty() && E.Name[0] == '?'))
         continue;
       E.SymbolName = E.Name;
       // Trim off the trailing decoration. Symbols will always have a
@@ -173,7 +173,7 @@ int llvm::dlltoolDriverMain(llvm::ArrayR
     }
   }
 
-  if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine, true, true))
+  if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine, true))
     return 1;
   return 0;
 }

Modified: llvm/trunk/test/tools/llvm-dlltool/coff-decorated.def
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dlltool/coff-decorated.def?rev=331859&r1=331858&r2=331859&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-dlltool/coff-decorated.def (original)
+++ llvm/trunk/test/tools/llvm-dlltool/coff-decorated.def Wed May  9 02:21:53 2018
@@ -7,7 +7,7 @@ EXPORTS
 CdeclFunction
 StdcallFunction at 4
 @FastcallFunction at 4
-StdcallAlias at 4=StdcallFunction at 4
+StdcallAlias at 4==StdcallFunction at 4
 ??_7exception@@6B@
 
 ; CHECK: Name type: noprefix

Modified: llvm/trunk/test/tools/llvm-dlltool/coff-weak-exports.def
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dlltool/coff-weak-exports.def?rev=331859&r1=331858&r2=331859&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-dlltool/coff-weak-exports.def (original)
+++ llvm/trunk/test/tools/llvm-dlltool/coff-weak-exports.def Wed May  9 02:21:53 2018
@@ -4,8 +4,25 @@
 LIBRARY test.dll
 EXPORTS
 TestFunction==AltTestFunction
+; When creating an import library, the DLL internal function name of
+; the implementation of a function isn't visible at all.
+ImpLibName = Implementation
+; A different import library name and implementation name can be mixed
+; with exposing it via a different name in the DLL than in code.
+ImpLibName2 = Implementation2 == AltTestFunction2
+; The fact that a DLL export entry is a forward to a different DLL doesn't
+; matter for the import library
+ImpLibName3 = kernel32.Sleep
 
 ; CHECK:      U AltTestFunction
 ; CHECK-NEXT: w TestFunction
 ; CHECK:      U __imp_AltTestFunction
 ; CHECK-NEXT: w __imp_TestFunction
+; CHECK:      T ImpLibName
+; CHECK-NEXT: T __imp_ImpLibName
+; CHECK:      U AltTestFunction2
+; CHECK-NEXT: w ImpLibName2
+; CHECK:      U __imp_AltTestFunction2
+; CHECK-NEXT: w __imp_ImpLibName2
+; CHECK:      T ImpLibName3
+; CHECK-NEXT: T __imp_ImpLibName3




More information about the llvm-commits mailing list