[llvm] [llvm-lib][llvm-dlltool] Fix handling of invalid ARM64EC function names (PR #116250)
Jacek Caban via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 14 08:03:23 PST 2024
https://github.com/cjacek created https://github.com/llvm/llvm-project/pull/116250
This is a follow-up to #115567. Emit an error for invalid function names, similar to MSVC's `lib.exe` behavior.
Returning an error from `writeImportLibrary` exposed bugs in error handling by its callers, which have been addressed in this patch.
>From 8493a9da2d0d19c56959fdf15c1b2e2f1ce7791a Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Sun, 10 Nov 2024 15:37:10 +0100
Subject: [PATCH] [llvm-lib][llvm-dlltool] Fix handling of invalid ARM64EC
function names
This is a follow-up to #115567. Emit an error for invalid function names, similar to MSVC's `lib.exe` behavior.
Returning an error from `writeImportLibrary` exposed bugs in error handling by its callers, which have been addressed in this patch.
---
llvm/lib/Object/COFFImportFile.cpp | 9 ++++++++-
llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp | 12 +++++++++---
llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp | 13 +++++++++----
.../tools/llvm-dlltool/arm64ec-invalid-name.test | 6 ++++++
llvm/test/tools/llvm-lib/arm64ec-invalid-name.test | 6 ++++++
5 files changed, 38 insertions(+), 8 deletions(-)
create mode 100644 llvm/test/tools/llvm-dlltool/arm64ec-invalid-name.test
create mode 100644 llvm/test/tools/llvm-lib/arm64ec-invalid-name.test
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp
index cc0a5da7e0d16a..ff3dcf9e13ffaf 100644
--- a/llvm/lib/Object/COFFImportFile.cpp
+++ b/llvm/lib/Object/COFFImportFile.cpp
@@ -756,8 +756,15 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
}
Name = std::move(*MangledName);
} else if (!E.Noname && ExportName.empty()) {
+ std::optional<std::string> DemangledName =
+ getArm64ECDemangledFunctionName(Name);
+ if (!DemangledName)
+ return make_error<StringError>(
+ StringRef(Twine("Invalid ARM64EC function name '" + Name + "'")
+ .str()),
+ object_error::parse_failed);
NameType = IMPORT_NAME_EXPORTAS;
- ExportName = std::move(*getArm64ECDemangledFunctionName(Name));
+ ExportName = std::move(*DemangledName);
}
}
diff --git a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
index 15e4cac08cd4ed..b3dcc0f9866842 100644
--- a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -243,8 +243,14 @@ 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))
- return 1;
+ if (!Path.empty()) {
+ if (Error E = writeImportLibrary(OutputFile, Path, Exports, Machine,
+ /*MinGW=*/true, NativeExports)) {
+ handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
+ llvm::errs() << EI.message() << "\n";
+ });
+ return 1;
+ }
+ }
return 0;
}
diff --git a/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp b/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp
index 07389a5ffb2b8a..2e0841ba02b543 100644
--- a/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp
+++ b/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp
@@ -419,10 +419,15 @@ int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
OutputFile = std::move(NativeDef->OutputFile);
}
- return writeImportLibrary(OutputFile, OutputPath, Def->Exports, LibMachine,
- /*MinGW=*/false, NativeExports)
- ? 1
- : 0;
+ if (Error E =
+ writeImportLibrary(OutputFile, OutputPath, Def->Exports, LibMachine,
+ /*MinGW=*/false, NativeExports)) {
+ handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
+ llvm::errs() << OutputPath << ": " << EI.message() << "\n";
+ });
+ return 1;
+ }
+ return 0;
}
// If no input files and not told otherwise, silently do nothing to match
diff --git a/llvm/test/tools/llvm-dlltool/arm64ec-invalid-name.test b/llvm/test/tools/llvm-dlltool/arm64ec-invalid-name.test
new file mode 100644
index 00000000000000..9a32a7afd5e200
--- /dev/null
+++ b/llvm/test/tools/llvm-dlltool/arm64ec-invalid-name.test
@@ -0,0 +1,6 @@
+; RUN: not llvm-dlltool -m arm64ec -d %s -l %t.lib 2>&1 | FileCheck %s
+; CHECK: Invalid ARM64EC function name '?func'
+
+LIBRARY test.dll
+EXPORTS
+ ?func
diff --git a/llvm/test/tools/llvm-lib/arm64ec-invalid-name.test b/llvm/test/tools/llvm-lib/arm64ec-invalid-name.test
new file mode 100644
index 00000000000000..696b4c96eed22e
--- /dev/null
+++ b/llvm/test/tools/llvm-lib/arm64ec-invalid-name.test
@@ -0,0 +1,6 @@
+; RUN: not llvm-lib -machine:arm64ec -def:%s -out:%t.lib 2>&1 | FileCheck %s
+; CHECK: Invalid ARM64EC function name '?func'
+
+LIBRARY test.dll
+EXPORTS
+ ?func
More information about the llvm-commits
mailing list