[ifs] Print an error when llvm-ifs is supplied with an unknown arch
authorHaowei Wu <haowei@google.com>
Wed, 22 Mar 2023 23:41:47 +0000 (16:41 -0700)
committerHaowei Wu <haowei@google.com>
Mon, 10 Apr 2023 23:02:04 +0000 (16:02 -0700)
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

llvm/lib/InterfaceStub/IFSHandler.cpp
llvm/test/tools/llvm-ifs/ifs-read-invalid-arch.test [new file with mode: 0644]
llvm/test/tools/llvm-ifs/write-stub.test
llvm/tools/llvm-ifs/llvm-ifs.cpp

index a7be08a..aa5817d 100644 (file)
@@ -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 (file)
index 0000000..3c25243
--- /dev/null
@@ -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
index 5444035..44c194b 100644 (file)
@@ -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]]'
index 92ca519..0d2744f 100644 (file)
@@ -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());