MC: check machine magic when applying offset adjustments
authorSaleem Abdulrasool <compnerd@compnerd.org>
Sun, 13 Apr 2014 20:47:55 +0000 (20:47 +0000)
committerSaleem Abdulrasool <compnerd@compnerd.org>
Sun, 13 Apr 2014 20:47:55 +0000 (20:47 +0000)
The values for the relocation type can (and do) overlap across various
architectures.  When performing an adjustment of the emitted relocation in the
final object file, check that the file magic matches the target for which the
relocation type is valid (e.g. a I386 relocation is only applied to an X86
object file, and an AMD64 relocation is only applied to an X86_64 object file).
This was noticed while adding support for ARM WinCOFF object file emission.

A test case for this is not really possible as the values for REL32 do not
overlap on I386 and AMD64, which is why this was never noticed in practice.  The
ARM WinCOFF emission is not yet ready to merge into the tree.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206138 91177308-0d34-0410-b5e6-96231b3b80d8

lib/MC/WinCOFFObjectWriter.cpp

index f51bac8f4ddad22d75c973a696f29ac64957db58..335986dac5399ef265e5963fd17e592b819c1ce9 100644 (file)
@@ -739,8 +739,10 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
 
   // FIXME: Can anyone explain what this does other than adjust for the size
   // of the offset?
-  if (Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32 ||
-      Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32)
+  if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
+       Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
+      (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
+       Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
     FixedValue += 4;
 
   coff_section->Relocations.push_back(Reloc);