diff options
author | Martin Storsjö <martin@martin.st> | 2024-07-16 23:19:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-16 23:19:39 +0300 |
commit | f2d6d74cd2a8dccc5ec47bb5debb003e06249db3 (patch) | |
tree | f21031d4cca77c18de8de2e5f8a24d7cda6a650c /llvm/lib/Object | |
parent | 446965070149d53374ee4d3010789673083fb11c (diff) | |
download | llvm-f2d6d74cd2a8dccc5ec47bb5debb003e06249db3.zip llvm-f2d6d74cd2a8dccc5ec47bb5debb003e06249db3.tar.gz llvm-f2d6d74cd2a8dccc5ec47bb5debb003e06249db3.tar.bz2 |
[llvm-dlltool] Fix renamed imports without a separate regular import entry (#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.
Diffstat (limited to 'llvm/lib/Object')
-rw-r--r-- | llvm/lib/Object/COFFImportFile.cpp | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp index 1ddc5d9..2458a53 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 ImpType; + const COFFShortExport *Export; + }; + SmallVector<Deferred, 0> Renames; for (const COFFShortExport &E : Exp) { if (E.Private) continue; @@ -724,15 +732,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.ImpType = ImportType; + D.Export = &E; + Renames.push_back(D); continue; } } else { @@ -754,9 +758,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.ImpType == 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.ImpType, IMPORT_NAME_EXPORTAS, + D.Export->ImportName, M)); + } + } return Error::success(); }; |