Skip to content

Commit f641d0d

Browse files
committed
[COFF] Keep the underscore on exported decorated stdcall functions in MSVC mode
This (together with the corresponding LLD commit, that contains the testcase updates) fixes PR35733. Differential Revision: https://reviews.llvm.org/D41631 llvm-svn: 323035
1 parent 990eb1f commit f641d0d

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

llvm/include/llvm/Object/COFFImportFile.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ struct COFFShortExport {
9898

9999
Error writeImportLibrary(StringRef ImportName, StringRef Path,
100100
ArrayRef<COFFShortExport> Exports,
101-
COFF::MachineTypes Machine, bool MakeWeakAliases);
101+
COFF::MachineTypes Machine, bool MakeWeakAliases,
102+
bool MinGW);
102103

103104
} // namespace object
104105
} // namespace llvm

llvm/lib/Object/COFFImportFile.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,15 @@ static void writeStringTable(std::vector<uint8_t> &B,
9191
}
9292

9393
static ImportNameType getNameType(StringRef Sym, StringRef ExtName,
94-
MachineTypes Machine) {
94+
MachineTypes Machine, bool MinGW) {
95+
// A decorated stdcall function in MSVC is exported with the
96+
// type IMPORT_NAME, and the exported function name includes the
97+
// the leading underscore. In MinGW on the other hand, a decorated
98+
// stdcall function still omits the underscore (IMPORT_NAME_NOPREFIX).
99+
// See the comment in isDecorated in COFFModuleDefinition.cpp for more
100+
// details.
101+
if (ExtName.startswith("_") && ExtName.contains('@') && !MinGW)
102+
return IMPORT_NAME;
95103
if (Sym != ExtName)
96104
return IMPORT_NAME_UNDECORATE;
97105
if (Machine == IMAGE_FILE_MACHINE_I386 && Sym.startswith("_"))
@@ -558,7 +566,8 @@ NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
558566

559567
Error writeImportLibrary(StringRef ImportName, StringRef Path,
560568
ArrayRef<COFFShortExport> Exports,
561-
MachineTypes Machine, bool MakeWeakAliases) {
569+
MachineTypes Machine, bool MakeWeakAliases,
570+
bool MinGW) {
562571

563572
std::vector<NewArchiveMember> Members;
564573
ObjectFactory OF(llvm::sys::path::filename(ImportName), Machine);
@@ -589,7 +598,7 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
589598
ImportType = IMPORT_CONST;
590599

591600
StringRef SymbolName = E.SymbolName.empty() ? E.Name : E.SymbolName;
592-
ImportNameType NameType = getNameType(SymbolName, E.Name, Machine);
601+
ImportNameType NameType = getNameType(SymbolName, E.Name, Machine, MinGW);
593602
Expected<std::string> Name = E.ExtName.empty()
594603
? SymbolName
595604
: replace(SymbolName, E.Name, E.ExtName);

llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
173173
}
174174
}
175175

176-
if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine, true))
176+
if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine, true, true))
177177
return 1;
178178
return 0;
179179
}

0 commit comments

Comments
 (0)