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 Exports, - COFF::MachineTypes Machine, bool MinGW, - ArrayRef NativeExports = std::nullopt); +Error writeImportLibrary(StringRef ImportName, StringRef Path, + ArrayRef Exports, + COFF::MachineTypes Machine, bool MinGW, + ArrayRef 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 Exports, MachineTypes Machine, bool MinGW, - ArrayRef NativeExports) { + ArrayRef 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 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; }