[lld] [llvm] [llvm-dlltool] Fix renamed imports without a separate regular import entry (PR #98229)
Martin Storsjö via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 9 14:36:54 PDT 2024
https://github.com/mstorsjo created https://github.com/llvm/llvm-project/pull/98229
Normally, when doing renamed imports, we do this by providing a
weak alias, towards another regular import, for the symbol we
want to actually import. In a def file, this looks like this:
regularfunc
renamedfunc == regularfunc
However, if we want to link against a function in a DLL, where we
(intentionally) don't provide a regular import for that symbol
with the name in its DLL, doing the renamed import with a weak
alias doesn't work, as there's no symbol that the weak alias can
point towards.
We can't make up such an import either, as we may intentionally
not want to provide a regular import for that name.
This situation can either be resolved by using the "long" import
library format (as e.g. produced by GNU dlltool), or by using the
new short import library name type "export as".
This patch implements it by using the "export as" name type.
When producing a renamed import, defer emitting it until all regular
imports have been produced. If the renamed import refers to a
symbol that does exist as a regular import entry, produce a
weak alias, just as before. (This implementation also avoids needing
to know whether the symbol that the alias points towards actually
is prefixed or not, too.)
If the renamed import points at a symbol that isn't otherwise
available (or is available as a renamed symbol itself), generate
an "export as" import entry.
This name type is new, and is normally used in ARM64EC import
libraries, but can also be used for other architectures.
>From 40a59e2992d090b8b92b239ac6d102ead9cb3f80 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Tue, 9 Jul 2024 15:13:03 +0300
Subject: [PATCH 1/4] [llvm-dlltool] Respect the DATA flag when creating
aliases
---
llvm/lib/Object/COFFImportFile.cpp | 4 +++-
llvm/test/tools/llvm-dlltool/coff-weak-exports.def | 4 ++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp
index fd8aca393e90b..a9f150a965c35 100644
--- a/llvm/lib/Object/COFFImportFile.cpp
+++ b/llvm/lib/Object/COFFImportFile.cpp
@@ -691,7 +691,9 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
}
if (!E.ImportName.empty() && Name != E.ImportName) {
- Members.push_back(OF.createWeakExternal(E.ImportName, Name, false, M));
+ if (ImportType == IMPORT_CODE)
+ Members.push_back(
+ OF.createWeakExternal(E.ImportName, Name, false, M));
Members.push_back(OF.createWeakExternal(E.ImportName, Name, true, M));
continue;
}
diff --git a/llvm/test/tools/llvm-dlltool/coff-weak-exports.def b/llvm/test/tools/llvm-dlltool/coff-weak-exports.def
index dacc5f73530fd..67f0013bf170f 100644
--- a/llvm/test/tools/llvm-dlltool/coff-weak-exports.def
+++ b/llvm/test/tools/llvm-dlltool/coff-weak-exports.def
@@ -6,6 +6,7 @@
LIBRARY test.dll
EXPORTS
TestFunction==AltTestFunction
+TestData DATA == AltTestData
; When creating an import library, the DLL internal function name of
; the implementation of a function isn't visible at all.
ImpLibName = Implementation
@@ -20,6 +21,9 @@ ImpLibName3 = kernel32.Sleep
; CHECK-NEXT: W TestFunction
; CHECK: U __imp_AltTestFunction
; CHECK-NEXT: W __imp_TestFunction
+; CHECK-NOT: W TestData
+; CHECK: U __imp_AltTestData
+; CHECK-NEXT: W __imp_TestData
; CHECK: T ImpLibName
; CHECK-NEXT: T __imp_ImpLibName
; CHECK: U AltTestFunction2
>From 07437ecea9150075e9bf8660acbc40a4c30c17fc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Tue, 16 Apr 2024 14:27:44 +0300
Subject: [PATCH 2/4] [llvm-dlltool] Remove the i386 underscore prefix from
COFFImportFile::ImportName. NFC.
On i386, regular C level symbols are given an underscore prefix
in the symbols on the object file level. However, the exported
names from DLLs usually don't have this leading underscore.
When specified in a def file like "symbol == dllname", the "dllname"
is the name of the exported symbol from the DLL, which will be
linked against from an object file symbol named "_symbol"
(on i386).
The mechanism where one symbol is redirected to another one in
an import library is implemented with weak aliases. In that case,
we need to have the object file symbol level name for the target
of the import, as we make one object file symbol point at another
one. Therefore, we added an underscore to the ImportName field.
(This mechanism, with weak aliases, only works as long as the
target also is exported as is, in that form - this issue is
dealt with in a later commit.)
For clarity, for potentially handling the import renaming in
other ways, store the ImportName field unprefixed, containing
the actual name to import from the DLL.
When creating the aliases, add the prefix as needed. This requires
passing an extra AddUnderscores parameter to the writeImportLibrary
function; this is a temporary measure, until alias creation is
reworked in a later commit.
---
llvm/include/llvm/Object/COFFImportFile.h | 9 +++++----
llvm/lib/Object/COFFImportFile.cpp | 14 ++++++++++----
llvm/lib/Object/COFFModuleDefinition.cpp | 2 --
.../lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp | 5 +++--
4 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/llvm/include/llvm/Object/COFFImportFile.h b/llvm/include/llvm/Object/COFFImportFile.h
index 649fb4930934d..e91d5a9b3198a 100644
--- a/llvm/include/llvm/Object/COFFImportFile.h
+++ b/llvm/include/llvm/Object/COFFImportFile.h
@@ -135,10 +135,11 @@ struct COFFShortExport {
/// linking both ARM64EC and pure ARM64 objects, and the linker will pick only
/// the exports relevant to the target platform. For non-hybrid targets,
/// the NativeExports parameter should not be used.
-Error writeImportLibrary(
- StringRef ImportName, StringRef Path, ArrayRef<COFFShortExport> Exports,
- COFF::MachineTypes Machine, bool MinGW,
- ArrayRef<COFFShortExport> NativeExports = std::nullopt);
+Error writeImportLibrary(StringRef ImportName, StringRef Path,
+ ArrayRef<COFFShortExport> Exports,
+ COFF::MachineTypes Machine, bool MinGW,
+ ArrayRef<COFFShortExport> NativeExports = std::nullopt,
+ bool AddUnderscores = true);
} // namespace object
} // namespace llvm
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp
index a9f150a965c35..03af4921ddbca 100644
--- a/llvm/lib/Object/COFFImportFile.cpp
+++ b/llvm/lib/Object/COFFImportFile.cpp
@@ -645,7 +645,8 @@ NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
Error writeImportLibrary(StringRef ImportName, StringRef Path,
ArrayRef<COFFShortExport> Exports,
MachineTypes Machine, bool MinGW,
- ArrayRef<COFFShortExport> NativeExports) {
+ ArrayRef<COFFShortExport> NativeExports,
+ bool AddUnderscores) {
MachineTypes NativeMachine = Machine;
if (isArm64EC(Machine)) {
@@ -691,10 +692,15 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
}
if (!E.ImportName.empty() && Name != E.ImportName) {
+ StringRef Prefix = "";
+ if (Machine == IMAGE_FILE_MACHINE_I386 && AddUnderscores)
+ Prefix = "_";
+
if (ImportType == IMPORT_CODE)
- Members.push_back(
- OF.createWeakExternal(E.ImportName, Name, false, M));
- Members.push_back(OF.createWeakExternal(E.ImportName, Name, true, M));
+ Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
+ Name, false, M));
+ Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
+ Name, true, M));
continue;
}
diff --git a/llvm/lib/Object/COFFModuleDefinition.cpp b/llvm/lib/Object/COFFModuleDefinition.cpp
index 0c0bef1319e44..82c18539658e8 100644
--- a/llvm/lib/Object/COFFModuleDefinition.cpp
+++ b/llvm/lib/Object/COFFModuleDefinition.cpp
@@ -282,8 +282,6 @@ class Parser {
if (Tok.K == EqualEqual) {
read();
E.ImportName = std::string(Tok.Value);
- if (AddUnderscores && !isDecorated(E.ImportName, MingwDef))
- E.ImportName = std::string("_").append(E.ImportName);
continue;
}
// EXPORTAS must be at the end of export definition
diff --git a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
index 15e4cac08cd4e..012ad246888f9 100644
--- a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -243,8 +243,9 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
}
std::string Path = std::string(Args.getLastArgValue(OPT_l));
- if (!Path.empty() && writeImportLibrary(OutputFile, Path, Exports, Machine,
- /*MinGW=*/true, NativeExports))
+ if (!Path.empty() &&
+ writeImportLibrary(OutputFile, Path, Exports, Machine,
+ /*MinGW=*/true, NativeExports, AddUnderscores))
return 1;
return 0;
}
>From 7f1ecac165e7de8bea27b3ba8d1e2212ef8403c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Tue, 16 Apr 2024 15:22:37 +0300
Subject: [PATCH 3/4] [llvm-dlltool] Handle import renaming using other name
types, when possible
This avoids needing to use weak aliases for these cases. (Weak
aliases only work if there's another, regular import entry that
provide the desired symbol from the DLL.)
---
llvm/lib/Object/COFFImportFile.cpp | 62 ++++++++++++-------
.../tools/llvm-dlltool/coff-decorated.def | 16 +++++
2 files changed, 57 insertions(+), 21 deletions(-)
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp
index 03af4921ddbca..2736179b65c7c 100644
--- a/llvm/lib/Object/COFFImportFile.cpp
+++ b/llvm/lib/Object/COFFImportFile.cpp
@@ -52,18 +52,12 @@ StringRef COFFImportFile::getFileFormatName() const {
}
}
-StringRef COFFImportFile::getExportName() const {
- const coff_import_header *hdr = getCOFFImportHeader();
- StringRef name = Data.getBuffer().substr(sizeof(*hdr)).split('\0').first;
-
+static StringRef applyNameType(ImportNameType Type, StringRef name) {
auto ltrim1 = [](StringRef s, StringRef chars) {
return !s.empty() && chars.contains(s[0]) ? s.substr(1) : s;
};
- switch (hdr->getNameType()) {
- case IMPORT_ORDINAL:
- name = "";
- break;
+ switch (Type) {
case IMPORT_NAME_NOPREFIX:
name = ltrim1(name, "?@_");
break;
@@ -71,6 +65,24 @@ StringRef COFFImportFile::getExportName() const {
name = ltrim1(name, "?@_");
name = name.substr(0, name.find('@'));
break;
+ default:
+ break;
+ }
+ return name;
+}
+
+StringRef COFFImportFile::getExportName() const {
+ const coff_import_header *hdr = getCOFFImportHeader();
+ StringRef name = Data.getBuffer().substr(sizeof(*hdr)).split('\0').first;
+
+ switch (hdr->getNameType()) {
+ case IMPORT_ORDINAL:
+ name = "";
+ break;
+ case IMPORT_NAME_NOPREFIX:
+ case IMPORT_NAME_UNDECORATE:
+ name = applyNameType(static_cast<ImportNameType>(hdr->getNameType()), name);
+ break;
case IMPORT_NAME_EXPORTAS: {
// Skip DLL name
name = Data.getBuffer().substr(sizeof(*hdr) + name.size() + 1);
@@ -691,19 +703,6 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
Name.swap(*ReplacedName);
}
- if (!E.ImportName.empty() && Name != E.ImportName) {
- StringRef Prefix = "";
- if (Machine == IMAGE_FILE_MACHINE_I386 && AddUnderscores)
- Prefix = "_";
-
- if (ImportType == IMPORT_CODE)
- Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
- Name, false, M));
- Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
- Name, true, M));
- continue;
- }
-
ImportNameType NameType;
std::string ExportName;
if (E.Noname) {
@@ -711,6 +710,27 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
} else if (!E.ExportAs.empty()) {
NameType = IMPORT_NAME_EXPORTAS;
ExportName = E.ExportAs;
+ } else if (!E.ImportName.empty()) {
+ if (Machine == IMAGE_FILE_MACHINE_I386 &&
+ applyNameType(IMPORT_NAME_UNDECORATE, Name) == E.ImportName)
+ NameType = IMPORT_NAME_UNDECORATE;
+ else if (Machine == IMAGE_FILE_MACHINE_I386 &&
+ applyNameType(IMPORT_NAME_NOPREFIX, Name) == E.ImportName)
+ NameType = IMPORT_NAME_NOPREFIX;
+ else if (Name == E.ImportName)
+ NameType = IMPORT_NAME;
+ else {
+ StringRef Prefix = "";
+ if (Machine == IMAGE_FILE_MACHINE_I386 && AddUnderscores)
+ Prefix = "_";
+
+ if (ImportType == IMPORT_CODE)
+ Members.push_back(OF.createWeakExternal(
+ (Prefix + E.ImportName).str(), Name, false, M));
+ Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
+ Name, true, M));
+ continue;
+ }
} else {
NameType = getNameType(SymbolName, E.Name, M, MinGW);
}
diff --git a/llvm/test/tools/llvm-dlltool/coff-decorated.def b/llvm/test/tools/llvm-dlltool/coff-decorated.def
index fc81f23d09d6c..773e3762cc3d7 100644
--- a/llvm/test/tools/llvm-dlltool/coff-decorated.def
+++ b/llvm/test/tools/llvm-dlltool/coff-decorated.def
@@ -13,6 +13,10 @@ StdcallExportName at 4=StdcallInternalFunction at 4
OtherStdcallExportName at 4=CdeclInternalFunction
CdeclExportName=StdcallInternalFunction at 4
+NoprefixStdcall at 4 == NoprefixStdcall at 4
+DecoratedStdcall at 4 == _DecoratedStdcall at 4
+UndecoratedStdcall at 4 == UndecoratedStdcall
+
; CHECK: Name type: noprefix
; CHECK-NEXT: Export name: CdeclFunction
; CHECK-NEXT: Symbol: __imp__CdeclFunction
@@ -43,3 +47,15 @@ CdeclExportName=StdcallInternalFunction at 4
; CHECK-NEXT: Export name: CdeclExportName
; CHECK-NEXT: Symbol: __imp__CdeclExportName
; CHECK-NEXT: Symbol: _CdeclExportName
+; CHECK: Name type: noprefix
+; CHECK-NEXT: Export name: NoprefixStdcall at 4
+; CHECK-NEXT: Symbol: __imp__NoprefixStdcall at 4
+; CHECK-NEXT: Symbol: _NoprefixStdcall at 4
+; CHECK: Name type: name
+; CHECK-NEXT: Export name: _DecoratedStdcall at 4
+; CHECK-NEXT: Symbol: __imp__DecoratedStdcall at 4
+; CHECK-NEXT: Symbol: _DecoratedStdcall at 4
+; CHECK: Name type: undecorate
+; CHECK-NEXT: Export name: UndecoratedStdcall
+; CHECK-NEXT: Symbol: __imp__UndecoratedStdcall at 4
+; CHECK-NEXT: Symbol: _UndecoratedStdcall at 4
>From 1cd035b0b08a2be150bd0a7e340b6eb8aecf79ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Tue, 9 Jul 2024 00:41:19 +0300
Subject: [PATCH 4/4] [llvm-dlltool] Fix renamed imports without a separate
regular import entry
Normally, when doing renamed imports, we do this by providing a
weak alias, towards another regular import, for the symbol we
want to actually import. In a def file, this looks like this:
regularfunc
renamedfunc == regularfunc
However, if we want to link against a function in a DLL, where we
(intentionally) don't provide a regular import for that symbol
with the name in its DLL, doing the renamed import with a weak
alias doesn't work, as there's no symbol that the weak alias can
point towards.
We can't make up such an import either, as we may intentionally
not want to provide a regular import for that name.
This situation can either be resolved by using the "long" import
library format (as e.g. produced by GNU dlltool), or by using the
new short import library name type "export as".
This patch implements it by using the "export as" name type.
When producing a renamed import, defer emitting it until all regular
imports have been produced. If the renamed import refers to a
symbol that does exist as a regular import entry, produce a
weak alias, just as before. (This implementation also avoids needing
to know whether the symbol that the alias points towards actually
is prefixed or not, too.)
If the renamed import points at a symbol that isn't otherwise
available (or is available as a renamed symbol itself), generate
an "export as" import entry.
This name type is new, and is normally used in ARM64EC import
libraries, but can also be used for other architectures.
---
lld/test/COFF/lib.test | 16 +++----
llvm/include/llvm/Object/COFFImportFile.h | 9 ++--
llvm/lib/Object/COFFImportFile.cpp | 42 ++++++++++++++-----
.../llvm-dlltool/DlltoolDriver.cpp | 5 +--
.../tools/llvm-dlltool/coff-decorated.def | 2 +-
.../tools/llvm-dlltool/coff-weak-exports.def | 15 +++++--
llvm/test/tools/llvm-dlltool/renaming.def | 39 +++++++++++++++++
7 files changed, 96 insertions(+), 32 deletions(-)
create mode 100644 llvm/test/tools/llvm-dlltool/renaming.def
diff --git a/lld/test/COFF/lib.test b/lld/test/COFF/lib.test
index 7525ef4226cda..82abca6ec9307 100644
--- a/lld/test/COFF/lib.test
+++ b/lld/test/COFF/lib.test
@@ -1,6 +1,14 @@
# RUN: lld-link /machine:x64 /def:%S/Inputs/library.def /out:%t.lib
# RUN: llvm-nm %t.lib | FileCheck %s
+CHECK: 00000000 R __imp_constant
+CHECK: 00000000 R constant
+
+CHECK: 00000000 D __imp_data
+
+CHECK: 00000000 T __imp_function
+CHECK: 00000000 T function
+
CHECK: 00000000 a @comp.id
CHECK: 00000000 a @feat.00
CHECK: 00000000 W alias
@@ -11,11 +19,3 @@ CHECK: 00000000 a @feat.00
CHECK: 00000000 W __imp_alias
CHECK: U __imp_function
-CHECK: 00000000 R __imp_constant
-CHECK: 00000000 R constant
-
-CHECK: 00000000 D __imp_data
-
-CHECK: 00000000 T __imp_function
-CHECK: 00000000 T function
-
diff --git a/llvm/include/llvm/Object/COFFImportFile.h b/llvm/include/llvm/Object/COFFImportFile.h
index e91d5a9b3198a..649fb4930934d 100644
--- a/llvm/include/llvm/Object/COFFImportFile.h
+++ b/llvm/include/llvm/Object/COFFImportFile.h
@@ -135,11 +135,10 @@ struct COFFShortExport {
/// linking both ARM64EC and pure ARM64 objects, and the linker will pick only
/// the exports relevant to the target platform. For non-hybrid targets,
/// the NativeExports parameter should not be used.
-Error writeImportLibrary(StringRef ImportName, StringRef Path,
- ArrayRef<COFFShortExport> Exports,
- COFF::MachineTypes Machine, bool MinGW,
- ArrayRef<COFFShortExport> NativeExports = std::nullopt,
- bool AddUnderscores = true);
+Error writeImportLibrary(
+ StringRef ImportName, StringRef Path, ArrayRef<COFFShortExport> Exports,
+ COFF::MachineTypes Machine, bool MinGW,
+ ArrayRef<COFFShortExport> NativeExports = std::nullopt);
} // namespace object
} // namespace llvm
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp
index 2736179b65c7c..6922ac6994469 100644
--- a/llvm/lib/Object/COFFImportFile.cpp
+++ b/llvm/lib/Object/COFFImportFile.cpp
@@ -12,6 +12,8 @@
#include "llvm/Object/COFFImportFile.h"
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ArchiveWriter.h"
@@ -657,8 +659,7 @@ NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
Error writeImportLibrary(StringRef ImportName, StringRef Path,
ArrayRef<COFFShortExport> Exports,
MachineTypes Machine, bool MinGW,
- ArrayRef<COFFShortExport> NativeExports,
- bool AddUnderscores) {
+ ArrayRef<COFFShortExport> NativeExports) {
MachineTypes NativeMachine = Machine;
if (isArm64EC(Machine)) {
@@ -680,6 +681,13 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
auto addExports = [&](ArrayRef<COFFShortExport> Exp,
MachineTypes M) -> Error {
+ StringMap<std::string> RegularImports;
+ struct Deferred {
+ std::string Name;
+ ImportType ImportType;
+ const COFFShortExport *Export;
+ };
+ SmallVector<Deferred, 0> Renames;
for (const COFFShortExport &E : Exp) {
if (E.Private)
continue;
@@ -720,15 +728,11 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
else if (Name == E.ImportName)
NameType = IMPORT_NAME;
else {
- StringRef Prefix = "";
- if (Machine == IMAGE_FILE_MACHINE_I386 && AddUnderscores)
- Prefix = "_";
-
- if (ImportType == IMPORT_CODE)
- Members.push_back(OF.createWeakExternal(
- (Prefix + E.ImportName).str(), Name, false, M));
- Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
- Name, true, M));
+ Deferred D;
+ D.Name = Name;
+ D.ImportType = ImportType;
+ D.Export = &E;
+ Renames.push_back(D);
continue;
}
} else {
@@ -750,9 +754,25 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
}
}
+ RegularImports[applyNameType(NameType, Name)] = Name;
Members.push_back(OF.createShortImport(Name, E.Ordinal, ImportType,
NameType, ExportName, M));
}
+ for (const auto &D : Renames) {
+ auto It = RegularImports.find(D.Export->ImportName);
+ if (It != RegularImports.end()) {
+ // We have a regular import entry for a symbol with the name we
+ // want to reference; produce an alias pointing at that.
+ StringRef Symbol = It->second;
+ if (D.ImportType == IMPORT_CODE)
+ Members.push_back(OF.createWeakExternal(Symbol, D.Name, false, M));
+ Members.push_back(OF.createWeakExternal(Symbol, D.Name, true, M));
+ } else {
+ Members.push_back(OF.createShortImport(
+ D.Name, D.Export->Ordinal, D.ImportType, IMPORT_NAME_EXPORTAS,
+ D.Export->ImportName, M));
+ }
+ }
return Error::success();
};
diff --git a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
index 012ad246888f9..15e4cac08cd4e 100644
--- a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -243,9 +243,8 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
}
std::string Path = std::string(Args.getLastArgValue(OPT_l));
- if (!Path.empty() &&
- writeImportLibrary(OutputFile, Path, Exports, Machine,
- /*MinGW=*/true, NativeExports, AddUnderscores))
+ if (!Path.empty() && writeImportLibrary(OutputFile, Path, Exports, Machine,
+ /*MinGW=*/true, NativeExports))
return 1;
return 0;
}
diff --git a/llvm/test/tools/llvm-dlltool/coff-decorated.def b/llvm/test/tools/llvm-dlltool/coff-decorated.def
index 773e3762cc3d7..f5685fb1cf0c6 100644
--- a/llvm/test/tools/llvm-dlltool/coff-decorated.def
+++ b/llvm/test/tools/llvm-dlltool/coff-decorated.def
@@ -7,7 +7,7 @@ EXPORTS
CdeclFunction
StdcallFunction at 4
@FastcallFunction at 4
-StdcallAlias at 4==StdcallFunction at 4
+StdcallAlias at 4==StdcallFunction
??_7exception@@6B@
StdcallExportName at 4=StdcallInternalFunction at 4
OtherStdcallExportName at 4=CdeclInternalFunction
diff --git a/llvm/test/tools/llvm-dlltool/coff-weak-exports.def b/llvm/test/tools/llvm-dlltool/coff-weak-exports.def
index 67f0013bf170f..b08040e29fa42 100644
--- a/llvm/test/tools/llvm-dlltool/coff-weak-exports.def
+++ b/llvm/test/tools/llvm-dlltool/coff-weak-exports.def
@@ -5,6 +5,9 @@
LIBRARY test.dll
EXPORTS
+AltTestFunction
+AltTestFunction2
+AltTestData
TestFunction==AltTestFunction
TestData DATA == AltTestData
; When creating an import library, the DLL internal function name of
@@ -17,6 +20,14 @@ ImpLibName2 = Implementation2 == AltTestFunction2
; matter for the import library
ImpLibName3 = kernel32.Sleep
+; CHECK: T AltTestFunction
+; CHECK-NEXT: T __imp_AltTestFunction
+; CHECK: T AltTestFunction2
+; CHECK-NEXT: T __imp_AltTestFunction2
+; CHECK: T ImpLibName
+; CHECK-NEXT: T __imp_ImpLibName
+; CHECK: T ImpLibName3
+; CHECK-NEXT: T __imp_ImpLibName3
; CHECK: U AltTestFunction
; CHECK-NEXT: W TestFunction
; CHECK: U __imp_AltTestFunction
@@ -24,14 +35,10 @@ ImpLibName3 = kernel32.Sleep
; CHECK-NOT: W TestData
; CHECK: U __imp_AltTestData
; CHECK-NEXT: W __imp_TestData
-; 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
; ARCH-NOT: unknown arch
diff --git a/llvm/test/tools/llvm-dlltool/renaming.def b/llvm/test/tools/llvm-dlltool/renaming.def
new file mode 100644
index 0000000000000..57fd472aa37cf
--- /dev/null
+++ b/llvm/test/tools/llvm-dlltool/renaming.def
@@ -0,0 +1,39 @@
+; 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
+
+symbolname == actualimport
+
+dataname DATA == actualdata
+
+_wcstok == wcstok
+wcstok == wcstok_s
+
+; CHECK-NM-NOT: actualimport
+; CHECK-NM-NOT: actualdata
+
+; CHECK: Type: code
+; CHECK-NEXT: Name type: export as
+; CHECK-NEXT: Export name: actualimport
+; CHECK-NEXT: Symbol: __imp__symbolname
+; CHECK-NEXT: Symbol: _symbolname
+
+; CHECK: Type: data
+; CHECK-NEXT: Name type: export as
+; CHECK-NEXT: Export name: actualdata
+; CHECK-NEXT: Symbol: __imp__dataname
+
+; CHECK: Type: code
+; CHECK-NEXT: Name type: export as
+; CHECK-NEXT: Export name: wcstok
+; CHECK-NEXT: Symbol: __imp___wcstok
+; CHECK-NEXT: Symbol: __wcstok
+
+; CHECK: Type: code
+; CHECK-NEXT: Name type: export as
+; CHECK-NEXT: Export name: wcstok_s
+; CHECK-NEXT: Symbol: __imp__wcstok
+; CHECK-NEXT: Symbol: _wcstok
More information about the llvm-commits
mailing list