Skip to content

Commit

Permalink
[llvm-dlltool] Remove the i386 underscore prefix from COFFImportFile:…
Browse files Browse the repository at this point in the history
…: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).
  • Loading branch information
mstorsjo authored Jul 16, 2024
1 parent d01d9ab commit 80e18b9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
9 changes: 5 additions & 4 deletions llvm/include/llvm/Object/COFFImportFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions llvm/lib/Object/COFFImportFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions llvm/lib/Object/COFFModuleDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 80e18b9

Please sign in to comment.