[lld] r219093 - PE/COFF: add a check to ensure that we dont mix up architectures

Saleem Abdulrasool compnerd at compnerd.org
Sun Oct 5 16:43:59 PDT 2014


Author: compnerd
Date: Sun Oct  5 18:43:59 2014
New Revision: 219093

URL: http://llvm.org/viewvc/llvm-project?rev=219093&view=rev
Log:
PE/COFF: add a check to ensure that we dont mix up architectures

Previously, we would not check the target machine type and the module (object)
machine type.  Add a check to ensure that we do not attempt to use an object
file with a different target architecture.

This change identified a couple of tests which were incorrectly mixing up
architecture types, using x86 input for a x64 target.  Adjust the tests
appropriately.  The renaming of the input and the architectures covers the
changes to the existing tests.

One significant change to the existing tests is that the newly added test input
for x64 uses the correct user label prefix for X64.

Added:
    lld/trunk/test/pecoff/Inputs/vars-main-x64.obj.yaml
      - copied, changed from r219089, lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml
    lld/trunk/test/pecoff/Inputs/vars-main-x86.obj.yaml
      - copied, changed from r219089, lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml
    lld/trunk/test/pecoff/conflicting-machine.test
Removed:
    lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml
Modified:
    lld/trunk/include/lld/Core/Error.h
    lld/trunk/lib/Core/Error.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
    lld/trunk/test/pecoff/delayimport.test
    lld/trunk/test/pecoff/dynamic.test
    lld/trunk/test/pecoff/importlib.test
    lld/trunk/test/pecoff/options.test

Modified: lld/trunk/include/lld/Core/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Error.h?rev=219093&r1=219092&r2=219093&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/Error.h (original)
+++ lld/trunk/include/lld/Core/Error.h Sun Oct  5 18:43:59 2014
@@ -28,6 +28,7 @@ enum class NativeReaderError {
   file_malformed,
   unknown_chunk_type,
   memory_error,
+  conflicting_target_machine,
 };
 
 inline std::error_code make_error_code(NativeReaderError e) {

Modified: lld/trunk/lib/Core/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/Error.cpp?rev=219093&r1=219092&r2=219093&view=diff
==============================================================================
--- lld/trunk/lib/Core/Error.cpp (original)
+++ lld/trunk/lib/Core/Error.cpp Sun Oct  5 18:43:59 2014
@@ -38,6 +38,8 @@ public:
       return "out of memory";
     case NativeReaderError::unknown_chunk_type:
       return "unknown chunk type";
+    case NativeReaderError::conflicting_target_machine:
+      return "conflicting target machine";
     }
     llvm_unreachable("An enumerator of NativeReaderError does not have a "
                      "message defined.");

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp?rev=219093&r1=219092&r2=219093&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp Sun Oct  5 18:43:59 2014
@@ -71,6 +71,7 @@ public:
   std::error_code parse();
   StringRef getLinkerDirectives() const { return _directives; }
   bool isCompatibleWithSEH() const { return _compatibleWithSEH; }
+  llvm::COFF::MachineTypes getMachineType() { return _machineType; }
 
   const atom_collection<DefinedAtom> &defined() const override {
     return _definedAtoms;
@@ -190,6 +191,7 @@ private:
   _definedAtomLocations;
 
   uint64_t _ordinal;
+  llvm::COFF::MachineTypes _machineType;
 };
 
 class BumpPtrStringSaver : public llvm::cl::StringSaver {
@@ -290,7 +292,8 @@ DefinedAtom::Merge getMerge(const coff_a
 
 FileCOFF::FileCOFF(std::unique_ptr<MemoryBuffer> mb, std::error_code &ec)
     : File(mb->getBufferIdentifier(), kindObject), _mb(std::move(mb)),
-      _compatibleWithSEH(false), _ordinal(0) {
+      _compatibleWithSEH(false), _ordinal(0),
+      _machineType(llvm::COFF::MT_Invalid) {
   auto binaryOrErr = llvm::object::createBinary(_mb->getMemBufferRef());
   if ((ec = binaryOrErr.getError()))
     return;
@@ -303,6 +306,8 @@ FileCOFF::FileCOFF(std::unique_ptr<Memor
   }
   bin.release();
 
+  _machineType = static_cast<llvm::COFF::MachineTypes>(_obj->getMachine());
+
   // Read .drectve section if exists.
   ArrayRef<uint8_t> directives;
   if ((ec = getSectionContents(".drectve", directives)))
@@ -951,6 +956,18 @@ StringRef FileCOFF::ArrayRefToString(Arr
   return StringRef(*contents).trim();
 }
 
+StringRef getMachineName(llvm::COFF::MachineTypes Type) {
+  switch (Type) {
+  default: llvm_unreachable("unsupported machine type");
+  case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
+    return "ARM";
+  case llvm::COFF::IMAGE_FILE_MACHINE_I386:
+    return "X86";
+  case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
+    return "X64";
+  }
+}
+
 class COFFObjectReader : public Reader {
 public:
   COFFObjectReader(PECOFFLinkingContext &ctx) : _ctx(ctx) {}
@@ -970,6 +987,15 @@ public:
     if (ec)
       return ec;
 
+    if (file->getMachineType() != llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
+        file->getMachineType() != _ctx.getMachineType()) {
+      llvm::errs() << "module machine type '"
+                   << getMachineName(file->getMachineType())
+                   << "' conflicts with target machine type '"
+                   << getMachineName(_ctx.getMachineType()) << "'\n";
+      return NativeReaderError::conflicting_target_machine;
+    }
+
     // The set to contain the symbols specified as arguments of
     // /INCLUDE option.
     std::set<StringRef> undefinedSymbols;

Copied: lld/trunk/test/pecoff/Inputs/vars-main-x64.obj.yaml (from r219089, lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml)
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars-main-x64.obj.yaml?p2=lld/trunk/test/pecoff/Inputs/vars-main-x64.obj.yaml&p1=lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml&r1=219089&r2=219093&rev=219093&view=diff
==============================================================================
--- lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/vars-main-x64.obj.yaml Sun Oct  5 18:43:59 2014
@@ -1,6 +1,6 @@
 ---
 header:
-  Machine:         IMAGE_FILE_MACHINE_I386
+  Machine:         IMAGE_FILE_MACHINE_AMD64
   Characteristics: [  ]
 sections:
   - Name:            .text
@@ -9,14 +9,14 @@ sections:
     SectionData:     558BEC56FF15000000008B0D000000008B3103F0FF150000000003C65E5DC3
     Relocations:
       - VirtualAddress:  6
-        SymbolName:      __imp__fn
-        Type:            IMAGE_REL_I386_DIR32
+        SymbolName:      __imp_fn
+        Type:            IMAGE_REL_AMD64_ADDR32
       - VirtualAddress:  12
-        SymbolName:      __imp__var
-        Type:            IMAGE_REL_I386_DIR32
+        SymbolName:      __imp_var
+        Type:            IMAGE_REL_AMD64_ADDR32
       - VirtualAddress:  22
-        SymbolName:      __imp___name_with_underscore
-        Type:            IMAGE_REL_I386_DIR32
+        SymbolName:      __imp__name_with_underscore
+        Type:            IMAGE_REL_AMD64_ADDR32
 symbols:
   - Name:            .text
     Value:           0
@@ -30,25 +30,25 @@ symbols:
       NumberOfLinenumbers: 0
       CheckSum:        3595596940
       Number:          0
-  - Name:            __imp__fn
+  - Name:            __imp_fn
     Value:           0
     SectionNumber:   0
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            __imp___name_with_underscore
+  - Name:            __imp__name_with_underscore
     Value:           0
     SectionNumber:   0
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            _main
+  - Name:            main
     Value:           0
     SectionNumber:   1
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            __imp__var
+  - Name:            __imp_var
     Value:           0
     SectionNumber:   0
     SimpleType:      IMAGE_SYM_TYPE_NULL

Copied: lld/trunk/test/pecoff/Inputs/vars-main-x86.obj.yaml (from r219089, lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml)
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars-main-x86.obj.yaml?p2=lld/trunk/test/pecoff/Inputs/vars-main-x86.obj.yaml&p1=lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml&r1=219089&r2=219093&rev=219093&view=diff
==============================================================================
    (empty)

Removed: lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml?rev=219092&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml (removed)
@@ -1,57 +0,0 @@
----
-header:
-  Machine:         IMAGE_FILE_MACHINE_I386
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       16
-    SectionData:     558BEC56FF15000000008B0D000000008B3103F0FF150000000003C65E5DC3
-    Relocations:
-      - VirtualAddress:  6
-        SymbolName:      __imp__fn
-        Type:            IMAGE_REL_I386_DIR32
-      - VirtualAddress:  12
-        SymbolName:      __imp__var
-        Type:            IMAGE_REL_I386_DIR32
-      - VirtualAddress:  22
-        SymbolName:      __imp___name_with_underscore
-        Type:            IMAGE_REL_I386_DIR32
-symbols:
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          31
-      NumberOfRelocations: 3
-      NumberOfLinenumbers: 0
-      CheckSum:        3595596940
-      Number:          0
-  - Name:            __imp__fn
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            __imp___name_with_underscore
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            _main
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            __imp__var
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...

Added: lld/trunk/test/pecoff/conflicting-machine.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/conflicting-machine.test?rev=219093&view=auto
==============================================================================
--- lld/trunk/test/pecoff/conflicting-machine.test (added)
+++ lld/trunk/test/pecoff/conflicting-machine.test Sun Oct  5 18:43:59 2014
@@ -0,0 +1,6 @@
+# RUN: yaml2obj %p/Inputs/vars-main-x64.obj.yaml > %t-x64.obj
+
+# RUN: not lld -flavor link /machine:x86 /out:%t.exe /entry:main %t-x64.obj 2>&1 \
+# RUN:    | FileCheck %s
+
+CHECK: module machine type 'X64' conflicts with target machine type 'X86'

Modified: lld/trunk/test/pecoff/delayimport.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/delayimport.test?rev=219093&r1=219092&r2=219093&view=diff
==============================================================================
--- lld/trunk/test/pecoff/delayimport.test (original)
+++ lld/trunk/test/pecoff/delayimport.test Sun Oct  5 18:43:59 2014
@@ -1,12 +1,13 @@
-# RUN: yaml2obj %p/Inputs/vars-main.obj.yaml > %t.obj
+# RUN: yaml2obj %p/Inputs/vars-main-x86.obj.yaml > %t-x86.obj
+# RUN: yaml2obj %p/Inputs/vars-main-x64.obj.yaml > %t-x64.obj
 #
-# RUN: not lld -flavor link /out:%t1.exe /subsystem:console /entry:main \
-# RUN:   /delayload:vars.dll -- %t.obj %p/Inputs/vars.lib >& %t.log
-# RUN: FileCheck -check-prefix=X86 %s < %t.log
+# RUN: not lld -flavor link /out:%t.exe /subsystem:console /entry:main \
+# RUN:   /delayload:vars.dll -- %t-x86.obj %p/Inputs/vars.lib 2>&1 \
+# RUN:   | FileCheck -check-prefix=X86 %s
 #
-# RUN: not lld -flavor link /out:%t1.exe /subsystem:console /entry:main \
-# RUN:   /machine:x64 /delayload:vars.dll -- %t.obj %p/Inputs/vars.lib >& %t.log
-# RUN: FileCheck -check-prefix=X64 %s < %t.log
+# RUN: not lld -flavor link /out:%t.exe /subsystem:console /entry:main \
+# RUN:   /machine:x64 /delayload:vars.dll -- %t-x64.obj %p/Inputs/vars.lib 2>&1 \
+# RUN:   | FileCheck -check-prefix=X64 %s
 
 X86: Undefined symbol: {{.*}} ___delayLoadHelper2 at 8
 X64: Undefined symbol: {{.*}} __delayLoadHelper2

Modified: lld/trunk/test/pecoff/dynamic.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/dynamic.test?rev=219093&r1=219092&r2=219093&view=diff
==============================================================================
--- lld/trunk/test/pecoff/dynamic.test (original)
+++ lld/trunk/test/pecoff/dynamic.test Sun Oct  5 18:43:59 2014
@@ -1,4 +1,4 @@
-# RUN: yaml2obj %p/Inputs/vars-main.obj.yaml > %t.obj
+# RUN: yaml2obj %p/Inputs/vars-main-x86.obj.yaml > %t.obj
 #
 # RUN: lld -flavor link /out:%t.exe /subsystem:console /entry:main /opt:noref \
 # RUN:    -- %t.obj %p/Inputs/vars.lib

Modified: lld/trunk/test/pecoff/importlib.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/importlib.test?rev=219093&r1=219092&r2=219093&view=diff
==============================================================================
--- lld/trunk/test/pecoff/importlib.test (original)
+++ lld/trunk/test/pecoff/importlib.test Sun Oct  5 18:43:59 2014
@@ -1,7 +1,7 @@
 # Verify that lld can handle .lib files. "main.obj" refers "var" and
 # "fn" defined in "vars.lib".
 #
-# RUN: yaml2obj %p/Inputs/vars-main.obj.yaml > %t.obj
+# RUN: yaml2obj %p/Inputs/vars-main-x86.obj.yaml > %t.obj
 #
 # RUN: lld -flavor link /out:%t1.exe /subsystem:console /entry:main /opt:noref \
 # RUN:   -- %t.obj %p/Inputs/vars.lib

Modified: lld/trunk/test/pecoff/options.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/options.test?rev=219093&r1=219092&r2=219093&view=diff
==============================================================================
--- lld/trunk/test/pecoff/options.test (original)
+++ lld/trunk/test/pecoff/options.test Sun Oct  5 18:43:59 2014
@@ -1,38 +1,40 @@
 # Tests for miscellaneous command line options.
-#
-# RUN: yaml2obj %p/Inputs/nop.obj.yaml > %t.obj
 
-# RUN: lld -flavor link /align:8192 /out:%t1.exe /entry:start \
-# RUN:   /subsystem:console -- %t.obj
-# RUN: llvm-readobj -file-headers %t1.exe | FileCheck -check-prefix=ALIGN %s
+# RUN: yaml2obj %p/Inputs/nop.obj.yaml > %t-x86.obj
+# RUN: yaml2obj %p/Inputs/nop64.obj.yaml > %t-x64.obj
+
+# RUN: lld -flavor link /align:8192 /out:%t.exe /entry:start \
+# RUN:   /subsystem:console -- %t-x86.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=ALIGN %s
 ALIGN: SectionAlignment: 8192
 
-# RUN: lld -flavor link /allowbind:no /out:%t2.exe /entry:start \
-# RUN:   /subsystem:console -- %t.obj
-# RUN: llvm-readobj -file-headers %t2.exe | FileCheck -check-prefix=NOBIND %s
+# RUN: lld -flavor link /allowbind:no /out:%t.exe /entry:start \
+# RUN:   /subsystem:console -- %t-x86.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=NOBIND %s
 NOBIND: IMAGE_DLL_CHARACTERISTICS_NO_BIND
 
-# RUN: lld -flavor link /allowisolation:no /out:%t3.exe /entry:start \
-# RUN:   /subsystem:console -- %t.obj
-# RUN: llvm-readobj -file-headers %t3.exe | FileCheck -check-prefix=NOISO %s
+# RUN: lld -flavor link /allowisolation:no /out:%t.exe /entry:start \
+# RUN:   /subsystem:console -- %t-x86.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=NOISO %s
 NOISO: IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION
 
-# RUN: lld -flavor link /swaprun:cd /out:%t4.exe /entry:start \
-# RUN:   /subsystem:console -- %t.obj
-# RUN: llvm-readobj -file-headers %t4.exe | FileCheck -check-prefix=RUNCD %s
+# RUN: lld -flavor link /swaprun:cd /out:%t.exe /entry:start \
+# RUN:   /subsystem:console -- %t-x86.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=RUNCD %s
 RUNCD: IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
 
-# RUN: lld -flavor link /swaprun:net /out:%t5.exe /entry:start \
-# RUN:   /subsystem:console -- %t.obj
-# RUN: llvm-readobj -file-headers %t5.exe | FileCheck -check-prefix=RUNNET %s
+# RUN: lld -flavor link /swaprun:net /out:%t.exe /entry:start \
+# RUN:   /subsystem:console -- %t-x86.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=RUNNET %s
 RUNNET: IMAGE_FILE_NET_RUN_FROM_SWAP
 
-# RUN: lld -flavor link /machine:x64 /force /highentropyva /out:%t6.exe \
-# RUN:   /entry:start /subsystem:console -- %t.obj
-# RUN: llvm-readobj -file-headers %t6.exe | FileCheck -check-prefix=ENT %s
+# RUN: lld -flavor link /machine:x64 /force /highentropyva /out:%t.exe \
+# RUN:   /entry:start /subsystem:console -- %t-x64.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=ENT %s
 ENT: IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA
 
-# RUN: lld -flavor link /machine:x64 /force /highentropyva:no /out:%t7.exe \
-# RUN:   /entry:start /subsystem:console -- %t.obj
-# RUN: llvm-readobj -file-headers %t7.exe | FileCheck -check-prefix=NOENT %s
+# RUN: lld -flavor link /machine:x64 /force /highentropyva:no /out:%t.exe \
+# RUN:   /entry:start /subsystem:console -- %t-x64.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=NOENT %s
 NOENT-NOT: IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA
+





More information about the llvm-commits mailing list