[llvm] 420526f - [ifs] Print an error when llvm-ifs is supplied with an unknown arch
Haowei Wu via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 10 16:18:34 PDT 2023
Author: Haowei Wu
Date: 2023-04-10T16:02:04-07:00
New Revision: 420526fb22f35df4750063ef4783b3774eaa371a
URL: https://github.com/llvm/llvm-project/commit/420526fb22f35df4750063ef4783b3774eaa371a
DIFF: https://github.com/llvm/llvm-project/commit/420526fb22f35df4750063ef4783b3774eaa371a.diff
LOG: [ifs] Print an error when llvm-ifs is supplied with an unknown arch
This patch makes llvm-ifs print an error when the arch string from
'--arch' flag or the IFS file 'Arch' field is unknown.
Differential Revision: https://reviews.llvm.org/D146672
Added:
llvm/test/tools/llvm-ifs/ifs-read-invalid-arch.test
Modified:
llvm/lib/InterfaceStub/IFSHandler.cpp
llvm/test/tools/llvm-ifs/write-stub.test
llvm/tools/llvm-ifs/llvm-ifs.cpp
Removed:
################################################################################
diff --git a/llvm/lib/InterfaceStub/IFSHandler.cpp b/llvm/lib/InterfaceStub/IFSHandler.cpp
index a7be08acb4587..aa5817dceed54 100644
--- a/llvm/lib/InterfaceStub/IFSHandler.cpp
+++ b/llvm/lib/InterfaceStub/IFSHandler.cpp
@@ -193,8 +193,13 @@ Expected<std::unique_ptr<IFSStub>> ifs::readIFSFromBuffer(StringRef Buf) {
"IFS version " + Stub->IfsVersion.getAsString() + " is unsupported.",
std::make_error_code(std::errc::invalid_argument));
if (Stub->Target.ArchString) {
- Stub->Target.Arch =
+ uint16_t eMachine =
ELF::convertArchNameToEMachine(*Stub->Target.ArchString);
+ if (eMachine == ELF::EM_NONE)
+ return createStringError(
+ std::make_error_code(std::errc::invalid_argument),
+ "IFS arch '" + *Stub->Target.ArchString + "' is unsupported");
+ Stub->Target.Arch = eMachine;
}
return std::move(Stub);
}
diff --git a/llvm/test/tools/llvm-ifs/ifs-read-invalid-arch.test b/llvm/test/tools/llvm-ifs/ifs-read-invalid-arch.test
new file mode 100644
index 0000000000000..3c25243eb3cd8
--- /dev/null
+++ b/llvm/test/tools/llvm-ifs/ifs-read-invalid-arch.test
@@ -0,0 +1,15 @@
+# RUN: not llvm-ifs --output-ifs=- %s 2>&1 | FileCheck %s
+
+--- !ifs-v1
+SoName: somelib.so
+IfsVersion: 3.0
+Target: { ObjectFormat: ELF, Arch: RiscV64, Endianness: little, BitWidth: 64 }
+Symbols:
+ - { Name: foo, Type: Func }
+ - { Name: bar, Type: Object, Size: 42 }
+ - { Name: baz, Type: Object, Size: 8 }
+ - { Name: not, Type: Object, Size: 128, Undefined: true }
+ - { Name: nor, Type: Func, Undefined: true }
+...
+
+# CHECK: error: IFS arch 'RiscV64' is unsupported
diff --git a/llvm/test/tools/llvm-ifs/write-stub.test b/llvm/test/tools/llvm-ifs/write-stub.test
index 5444035ca370d..44c194b77abc7 100644
--- a/llvm/test/tools/llvm-ifs/write-stub.test
+++ b/llvm/test/tools/llvm-ifs/write-stub.test
@@ -38,6 +38,8 @@
# RUN: llvm-ifs --output-elf=- --target=riscv64-linux-gnu %s | llvm-readelf -h - | FileCheck %s --check-prefix=MACHINE
+# RUN: not llvm-ifs --output-elf=- --arch=riscv64 --endianness=little --bitwidth=64 %s 2>&1 | FileCheck %s -DMSG=riscv64 --check-prefix=ARCHERR
+
--- !ifs-v1
IfsVersion: 3.0
NeededLibs:
@@ -207,3 +209,5 @@ Symbols:
# CONFLICTERR: error: Supplied [[MSG]] conflicts with the text stub
# MACHINE: Machine: RISC-V
+
+# ARCHERR: error: unknown arch '[[MSG]]'
diff --git a/llvm/tools/llvm-ifs/llvm-ifs.cpp b/llvm/tools/llvm-ifs/llvm-ifs.cpp
index 92ca519e4f274..0d2744f9a33b0 100644
--- a/llvm/tools/llvm-ifs/llvm-ifs.cpp
+++ b/llvm/tools/llvm-ifs/llvm-ifs.cpp
@@ -333,9 +333,13 @@ static DriverConfig parseArgs(int argc, char *const *argv) {
if (!Config.OutputFormat)
OptionNotFound("--output-format", A->getValue());
}
- if (const opt::Arg *A = Args.getLastArg(OPT_arch_EQ))
- Config.OverrideArch = ELF::convertArchNameToEMachine(A->getValue());
-
+ if (const opt::Arg *A = Args.getLastArg(OPT_arch_EQ)) {
+ uint16_t eMachine = ELF::convertArchNameToEMachine(A->getValue());
+ if (eMachine == ELF::EM_NONE) {
+ fatalError(Twine("unknown arch '") + A->getValue() + "'");
+ }
+ Config.OverrideArch = eMachine;
+ }
if (const opt::Arg *A = Args.getLastArg(OPT_bitwidth_EQ)) {
size_t Width;
llvm::StringRef S(A->getValue());
More information about the llvm-commits
mailing list