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

Jacek Caban via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 10 02:25:23 PDT 2024


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

This depends on  #102737 and #102736.

Check import library machine type instead of ignoring it. This matches MSVC link.exe's behavior. It will be also useful for ARM64EC, which needs to handle multiple machine types (ARM64EC and x86_64 for pure EC, additionally aarch64 for ARM64X) differently, depending on the type

>From ff413e24769dbabc96269b199b8fffd436458e8a Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Fri, 9 Aug 2024 20:25:48 +0200
Subject: [PATCH 1/3] [LLD][NFC] Don't use x64 import library for x86 target in
 safeseh-md tests.

Use llvm-lib to generate input library instead of a binary blob.
---
 lld/test/COFF/Inputs/except_handler3.lib | Bin 1364 -> 0 bytes
 lld/test/COFF/safeseh-md.s               |  12 ++++++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)
 delete mode 100644 lld/test/COFF/Inputs/except_handler3.lib

diff --git a/lld/test/COFF/Inputs/except_handler3.lib b/lld/test/COFF/Inputs/except_handler3.lib
deleted file mode 100644
index fdc51ed7328555ededd6e03d629840b1bb487a44..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 1364
zcmcIk%}&BV5FUPvrt#pxt4U4Nc%smjf`*t9F_DNsu$-4lNg%0)5DwhD at d!SIPr;M;
z7+!H^yIb2*Lx^#c*_rwF+wIKGbWdyUY&gDAG&Meo5jU`-v#ORX$rvaAum_Nt1LUqz
zz5w!{B#bp5yPclH8h1~(y=K?x^w?-I98KqJ?7n*bXl4Lo?S8AZNrblqGvGP*{q_TE
z)SWu+oV-jKOF1#I6SQP<k=)YEswvTdk~pj2`M&m7P?J|DJ`;7Lw``d~AjXvpXrOq@
zEM7Oj2g*LT016Y&ox5kcaN!sY4`m^M9BLbiE5Wk_%z5w95!wcIm4LW=h%<ee&I;xn
z9HEuxUdk;Ijjxi<pk!ll5;B$uk0>}@#Awnk8X>0~_=M27lrHMvM(Q!-8NL7QKTR*m
zHsN|(6-#<W at TrQFKR_2Hu)-=Dd;uq_Vof?iQxy|X=u#zU(0N%n!THyJOVLbyx7aH(
z*Pjl=-#r<7Sfc}6qYjJ?8*JHuVReb;M at eWCF9C5kp5HlQTb~F;vy7ecELSC+UbXca
Rvslu{FGK{JuT5I#e*^9C_SOIZ

diff --git a/lld/test/COFF/safeseh-md.s b/lld/test/COFF/safeseh-md.s
index d065af872b8f90..fda09fda64ede4 100644
--- a/lld/test/COFF/safeseh-md.s
+++ b/lld/test/COFF/safeseh-md.s
@@ -1,12 +1,15 @@
 # REQUIRES: x86
-# RUN: llvm-mc -triple i686-windows-msvc %s -filetype=obj -o %t.obj
-# RUN: lld-link %t.obj %S/Inputs/except_handler3.lib -safeseh -out:%t.exe -opt:noref -entry:main
+# RUN: split-file %s %t.dir
+# RUN: llvm-mc -triple i686-windows-msvc %t.dir/safeseh-md.s -filetype=obj -o %t.obj
+# RUN: llvm-lib -machine:x86 -out:%t.dir/except_handler3.lib -def:%t.dir/except_handler3.def
+# RUN: lld-link %t.obj %t.dir/except_handler3.lib -safeseh -out:%t.exe -opt:noref -entry:main
 # RUN: llvm-readobj --coff-load-config %t.exe | FileCheck %s
 
 # CHECK: SEHTable [
 # CHECK-NEXT: 0x
 # CHECK-NEXT: ]
 
+#--- safeseh-md.s
         .def     @feat.00;
         .scl    3;
         .type   0;
@@ -33,3 +36,8 @@ __load_config_used:
         .fill 60, 1, 0
         .long ___safe_se_handler_table
         .long ___safe_se_handler_count
+
+#--- except_handler3.def
+NAME except_handler3.dll
+EXPORTS
+        _except_handler3

>From 4068e72243b40ffaa60ae8f6ecdd9646bf53be42 Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Fri, 9 Aug 2024 20:29:15 +0200
Subject: [PATCH 2/3] [LLD][NFC] Make InputFile::getMachineType const.

---
 lld/COFF/InputFiles.cpp |  6 +++---
 lld/COFF/InputFiles.h   | 10 ++++++----
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index fdbe90390f5cac..8b21e12152f4b5 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -724,7 +724,7 @@ std::optional<Symbol *> ObjFile::createDefined(
   return createRegular(sym);
 }
 
-MachineTypes ObjFile::getMachineType() {
+MachineTypes ObjFile::getMachineType() const {
   if (coffObj)
     return static_cast<MachineTypes>(coffObj->getMachine());
   return IMAGE_FILE_MACHINE_UNKNOWN;
@@ -1139,7 +1139,7 @@ void BitcodeFile::parseLazy() {
       ctx.symtab.addLazyObject(this, sym.getName());
 }
 
-MachineTypes BitcodeFile::getMachineType() {
+MachineTypes BitcodeFile::getMachineType() const {
   switch (Triple(obj->getTargetTriple()).getArch()) {
   case Triple::x86_64:
     return AMD64;
@@ -1220,7 +1220,7 @@ void DLLFile::parse() {
   }
 }
 
-MachineTypes DLLFile::getMachineType() {
+MachineTypes DLLFile::getMachineType() const {
   if (coffObj)
     return static_cast<MachineTypes>(coffObj->getMachine());
   return IMAGE_FILE_MACHINE_UNKNOWN;
diff --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h
index cabd87ba673e3c..dd034e5cb43ee7 100644
--- a/lld/COFF/InputFiles.h
+++ b/lld/COFF/InputFiles.h
@@ -82,7 +82,9 @@ class InputFile {
   virtual void parse() = 0;
 
   // Returns the CPU type this file was compiled to.
-  virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
+  virtual MachineTypes getMachineType() const {
+    return IMAGE_FILE_MACHINE_UNKNOWN;
+  }
 
   MemoryBufferRef mb;
 
@@ -133,7 +135,7 @@ class ObjFile : public InputFile {
   static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
   void parse() override;
   void parseLazy();
-  MachineTypes getMachineType() override;
+  MachineTypes getMachineType() const override;
   ArrayRef<Chunk *> getChunks() { return chunks; }
   ArrayRef<SectionChunk *> getDebugChunks() { return debugChunks; }
   ArrayRef<SectionChunk *> getSXDataChunks() { return sxDataChunks; }
@@ -376,7 +378,7 @@ class BitcodeFile : public InputFile {
   ~BitcodeFile();
   static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
   ArrayRef<Symbol *> getSymbols() { return symbols; }
-  MachineTypes getMachineType() override;
+  MachineTypes getMachineType() const override;
   void parseLazy();
   std::unique_ptr<llvm::lto::InputFile> obj;
 
@@ -393,7 +395,7 @@ class DLLFile : public InputFile {
       : InputFile(ctx, DLLKind, m) {}
   static bool classof(const InputFile *f) { return f->kind() == DLLKind; }
   void parse() override;
-  MachineTypes getMachineType() override;
+  MachineTypes getMachineType() const override;
 
   struct Symbol {
     StringRef dllName;

>From 64be2e27918d6fedd3daeda96213656f9a773bb6 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 3/3] [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 8b21e12152f4b5..f9a22b0a7a5f0f 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 dd034e5cb43ee7..a332ac87b265e6 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 00000000000000..32deff0fc25f89
--- /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