Fix a bug in darwin's 32-bit X86 handling of evaluating fixups.
authorKevin Enderby <enderby@apple.com>
Wed, 4 Dec 2013 23:36:24 +0000 (23:36 +0000)
committerKevin Enderby <enderby@apple.com>
Wed, 4 Dec 2013 23:36:24 +0000 (23:36 +0000)
Where it would use a scattered relocation entry but falls back to a
normal relocation entry because the FixupOffset is more than 24-bits.

The bug is in the X86MachObjectWriter::RecordScatteredRelocation() where
it changes reference parameter FixedValue but then returns false to indicate
it did not create a scattered relocation entry.  The fix is simply to save the
original value of the parameter FixedValue at the start of the method and
restore it if we are returning false in that case.

rdar://15526046

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

lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
test/MC/MachO/x86_32-scattered-reloc-fallback.s [new file with mode: 0644]

index eb7c0b1a996597373b5945a0840b15f49ade94f2..0f16621b852feb346770ea126afb7d210d97cb18 100644 (file)
@@ -362,6 +362,7 @@ bool X86MachObjectWriter::RecordScatteredRelocation(MachObjectWriter *Writer,
                                                     MCValue Target,
                                                     unsigned Log2Size,
                                                     uint64_t &FixedValue) {
+  uint64_t OriginalFixedValue = FixedValue;
   uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
   unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
   unsigned Type = MachO::GENERIC_RELOC_VANILLA;
@@ -431,8 +432,10 @@ bool X86MachObjectWriter::RecordScatteredRelocation(MachObjectWriter *Writer,
     // symbol, things can go badly.
     //
     // Required for 'as' compatibility.
-    if (FixupOffset > 0xffffff)
+    if (FixupOffset > 0xffffff) {
+      FixedValue = OriginalFixedValue;
       return false;
+    }
   }
 
   MachO::any_relocation_info MRE;
diff --git a/test/MC/MachO/x86_32-scattered-reloc-fallback.s b/test/MC/MachO/x86_32-scattered-reloc-fallback.s
new file mode 100644 (file)
index 0000000..3de52b4
--- /dev/null
@@ -0,0 +1,27 @@
+// RUN: llvm-mc -triple i386-apple-darwin9 %s -filetype=obj -o - | macho-dump --dump-section-data | FileCheck %s
+
+// rdar://15526046
+
+.text
+.globl _main
+_main:
+       .space 0x01020f55, 0x90
+bug:
+       movl  $0, _key64b_9+4
+.section __TEXT, __padding
+       .space 0x515b91, 0
+.data
+       .space 0xa70, 0
+.globl _key64b_9
+_key64b_9:
+       .long 1
+       .long 2
+
+// The movl instruction above should produce this encoding where the address
+// of _key64b_9 is at 0x01537560.  This is testing falling back from using a
+// scattered relocation to a normal relocation because the offset from the
+// start of the section is more than 24-bits.  But need to get the item to
+// be relocated, in this case _key64b_9+4, value correct in the instruction.
+// 01020f55    c7056475530100000000    movl    $0x0, 0x1537564
+
+// CHECK:   90c70564 75530100 000000')