[PATCH] D42223: X86MachObjectWriter: Allow subtractors with undefined targets.

Joe Groff via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 17 18:36:07 PST 2018


jckarter created this revision.
jckarter added reviewers: lhames, lgerbarg.
Herald added a subscriber: llvm-commits.

This appears to work and be supported by ld64 on Darwin. The subtraction is represented symbolically in the Mach-O object format, and ld64 relocates the offset when the target is a weak or coalesced definition and appears to still do so when the target is undefined. (Possibly it even works with an undefined base, but I don't need that for my use case and didn't thoroughly test it.)


Repository:
  rL LLVM

https://reviews.llvm.org/D42223

Files:
  lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
  test/MC/MachO/bad-darwin-x86_64-diff-relocs.s
  test/MC/X86/macho-reloc-errors-x86_64.s


Index: test/MC/X86/macho-reloc-errors-x86_64.s
===================================================================
--- test/MC/X86/macho-reloc-errors-x86_64.s
+++ test/MC/X86/macho-reloc-errors-x86_64.s
@@ -13,7 +13,7 @@
 // CHECK-ERROR: 4:9: error: unsupported subtraction of qualified symbol
 // CHECK-ERROR: 5:9: error: unsupported pc-relative relocation of difference
 // CHECK-ERROR: 6:9: error: unsupported relocation with identical base
-// CHECK-ERROR: 7:9: error: unsupported relocation with subtraction expression, symbol 'thing' can not be undefined in a subtraction expression
+// CHECK-ERROR: 7:9: error: unsupported relocation with subtraction expression, symbol 'thing2' can not be undefined in a subtraction expression
 // CHECK-ERROR: 8:9: error: unsupported symbol modifier in relocation
 // CHECK-ERROR: 9:9: error: unsupported symbol modifier in branch relocation
 // CHECK-ERROR: 10:9: error: TLVP symbol modifier should have been rip-rel
Index: test/MC/MachO/bad-darwin-x86_64-diff-relocs.s
===================================================================
--- test/MC/MachO/bad-darwin-x86_64-diff-relocs.s
+++ test/MC/MachO/bad-darwin-x86_64-diff-relocs.s
@@ -8,6 +8,6 @@
 .long (_Y+4)-_b
 // CHECK-ERROR: error: unsupported relocation with subtraction expression, symbol '_b' can not be undefined in a subtraction expression
 
+// Undefined target is OK if the base is defined.
 _Z:
 .long (_a+4)-_Z
-// CHECK-ERROR: error: unsupported relocation with subtraction expression, symbol '_a' can not be undefined in a subtraction expression
Index: lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
===================================================================
--- lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
+++ lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
@@ -183,18 +183,19 @@
       return;
     }
 
-    // A subtraction expression where either symbol is undefined is a
+    // A subtraction expression where the base is undefined is a
     // non-relocatable expression.
-    if (A->isUndefined() || B->isUndefined()) {
-      StringRef Name = A->isUndefined() ? A->getName() : B->getName();
+    if (B->isUndefined()) {
+      StringRef Name = B->getName();
       Asm.getContext().reportError(Fixup.getLoc(),
         "unsupported relocation with subtraction expression, symbol '" +
         Name + "' can not be undefined in a subtraction expression");
       return;
     }
 
-    Value += Writer->getSymbolAddress(*A, Layout) -
-             (!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, Layout));
+    if (!A->isUndefined())
+      Value += Writer->getSymbolAddress(*A, Layout) -
+               (!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, Layout));
     Value -= Writer->getSymbolAddress(*B, Layout) -
              (!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, Layout));
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42223.130342.patch
Type: text/x-patch
Size: 2842 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180118/f3bec10e/attachment.bin>


More information about the llvm-commits mailing list