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)); 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: 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