[lld] [LLD][COFF] Validate import library machine type. (PR #102738)

Jacek Caban via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 10 06:05:41 PDT 2024


https://github.com/cjacek updated https://github.com/llvm/llvm-project/pull/102738

>From b8fb205c641b00df58fc33a6ad426d0ada36a65e Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Fri, 9 Aug 2024 21:05:58 +0200
Subject: [PATCH] [LLD][COFF] Validate import library machine type.

---
 lld/COFF/InputFiles.cpp        |  7 +++++++
 lld/COFF/InputFiles.h          |  1 +
 lld/test/COFF/implib-machine.s | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+)
 create mode 100644 lld/test/COFF/implib-machine.s

diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index 8b21e12152f4b..f9a22b0a7a5f0 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -987,6 +987,13 @@ void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
 ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
     : InputFile(ctx, ImportKind, m), live(!ctx.config.doGC), thunkLive(live) {}
 
+MachineTypes ImportFile::getMachineType() const {
+  uint16_t machine =
+      reinterpret_cast<const coff_import_header *>(mb.getBufferStart())
+          ->Machine;
+  return MachineTypes(machine);
+}
+
 void ImportFile::parse() {
   const auto *hdr =
       reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
diff --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h
index dd034e5cb43ee..a332ac87b265e 100644
--- a/lld/COFF/InputFiles.h
+++ b/lld/COFF/InputFiles.h
@@ -344,6 +344,7 @@ class ImportFile : public InputFile {
   explicit ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m);
 
   static bool classof(const InputFile *f) { return f->kind() == ImportKind; }
+  MachineTypes getMachineType() const override;
 
   Symbol *impSym = nullptr;
   Symbol *thunkSym = nullptr;
diff --git a/lld/test/COFF/implib-machine.s b/lld/test/COFF/implib-machine.s
new file mode 100644
index 0000000000000..32deff0fc25f8
--- /dev/null
+++ b/lld/test/COFF/implib-machine.s
@@ -0,0 +1,32 @@
+# REQUIRES: x86
+# RUN: split-file %s %t.dir
+# RUN: llvm-lib -machine:i386 -out:%t.dir/test32.lib -def:%t.dir/test32.def
+# RUN: llvm-lib -machine:amd64 -out:%t.dir/test64.lib -def:%t.dir/test64.def
+# RUN: llvm-mc -triple i686-windows-msvc %t.dir/test.s -filetype=obj -o %t.dir/test32.obj
+# RUN: llvm-mc -triple x86_64-windows-msvc %t.dir/test.s -filetype=obj -o %t.dir/test64.obj
+
+# RUN: not lld-link -dll -noentry -out:%t32.dll %t.dir/test32.obj %t.dir/test64.lib 2>&1 | FileCheck --check-prefix=ERR32 %s
+# ERR32: error: test.dll: machine type x64 conflicts with x86
+
+# RUN: not lld-link -dll -noentry -out:%t64.dll %t.dir/test64.obj %t.dir/test32.lib 2>&1 | FileCheck --check-prefix=ERR64 %s
+# ERR64: error: test.dll: machine type x86 conflicts with x64
+
+#--- test.s
+        .def     @feat.00;
+        .scl    3;
+        .type   0;
+        .endef
+        .globl  @feat.00
+ at feat.00 = 1
+        .data
+        .rva __imp__test
+
+#--- test32.def
+NAME test.dll
+EXPORTS
+         test DATA
+
+#--- test64.def
+NAME test.dll
+EXPORTS
+         _test DATA



More information about the llvm-commits mailing list