Skip to content

Commit 80e18b9

Browse files
authored
[llvm-dlltool] Remove the i386 underscore prefix from COFFImportFile::ImportName. NFC. (#98226)
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. This doesn't preserve the corner case of checking !isDecorated() before adding the prefix. This corner case isn't tested by any of our existing tests, and only would trigger for fastcall/vectorcall/MS C++ functions - while these kinds of renames primarily are used in mingw-w64-crt import libraries (which primarily handle cdecl and stdcall functions).
1 parent d01d9ab commit 80e18b9

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

llvm/include/llvm/Object/COFFImportFile.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ struct COFFShortExport {
135135
/// linking both ARM64EC and pure ARM64 objects, and the linker will pick only
136136
/// the exports relevant to the target platform. For non-hybrid targets,
137137
/// the NativeExports parameter should not be used.
138-
Error writeImportLibrary(
139-
StringRef ImportName, StringRef Path, ArrayRef<COFFShortExport> Exports,
140-
COFF::MachineTypes Machine, bool MinGW,
141-
ArrayRef<COFFShortExport> NativeExports = std::nullopt);
138+
Error writeImportLibrary(StringRef ImportName, StringRef Path,
139+
ArrayRef<COFFShortExport> Exports,
140+
COFF::MachineTypes Machine, bool MinGW,
141+
ArrayRef<COFFShortExport> NativeExports = std::nullopt,
142+
bool AddUnderscores = true);
142143

143144
} // namespace object
144145
} // namespace llvm

llvm/lib/Object/COFFImportFile.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
645645
Error writeImportLibrary(StringRef ImportName, StringRef Path,
646646
ArrayRef<COFFShortExport> Exports,
647647
MachineTypes Machine, bool MinGW,
648-
ArrayRef<COFFShortExport> NativeExports) {
648+
ArrayRef<COFFShortExport> NativeExports,
649+
bool AddUnderscores) {
649650

650651
MachineTypes NativeMachine = Machine;
651652
if (isArm64EC(Machine)) {
@@ -691,10 +692,15 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
691692
}
692693

693694
if (!E.ImportName.empty() && Name != E.ImportName) {
695+
StringRef Prefix = "";
696+
if (Machine == IMAGE_FILE_MACHINE_I386 && AddUnderscores)
697+
Prefix = "_";
698+
694699
if (ImportType == IMPORT_CODE)
695-
Members.push_back(
696-
OF.createWeakExternal(E.ImportName, Name, false, M));
697-
Members.push_back(OF.createWeakExternal(E.ImportName, Name, true, M));
700+
Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
701+
Name, false, M));
702+
Members.push_back(OF.createWeakExternal((Prefix + E.ImportName).str(),
703+
Name, true, M));
698704
continue;
699705
}
700706

llvm/lib/Object/COFFModuleDefinition.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,6 @@ class Parser {
282282
if (Tok.K == EqualEqual) {
283283
read();
284284
E.ImportName = std::string(Tok.Value);
285-
if (AddUnderscores && !isDecorated(E.ImportName, MingwDef))
286-
E.ImportName = std::string("_").append(E.ImportName);
287285
continue;
288286
}
289287
// EXPORTAS must be at the end of export definition

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
243243
}
244244

245245
std::string Path = std::string(Args.getLastArgValue(OPT_l));
246-
if (!Path.empty() && writeImportLibrary(OutputFile, Path, Exports, Machine,
247-
/*MinGW=*/true, NativeExports))
246+
if (!Path.empty() &&
247+
writeImportLibrary(OutputFile, Path, Exports, Machine,
248+
/*MinGW=*/true, NativeExports, AddUnderscores))
248249
return 1;
249250
return 0;
250251
}

0 commit comments

Comments
 (0)