From 6f1c76b0b9fef8bf20e63766ce55363b11bc21be Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 22 Jun 2017 17:25:35 +0000 Subject: [PATCH] Add a common error checking for some invalid expressions. This refactors a bit of duplicated code and fixes an assertion failure on ELF. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306035 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/ELFObjectWriter.cpp | 3 --- lib/MC/MCAssembler.cpp | 17 +++++++++++++---- .../X86/MCTargetDesc/X86MachObjectWriter.cpp | 3 +-- test/MC/AArch64/label-arithmetic-diags-darwin.s | 4 ++-- test/MC/ELF/bad-expr.s | 6 +++++- test/MC/X86/macho-reloc-errors-x86_64.s | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index 4d139132df4..30f35782680 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -633,9 +633,6 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm, MCContext &Ctx = Asm.getContext(); if (const MCSymbolRefExpr *RefB = Target.getSymB()) { - assert(RefB->getKind() == MCSymbolRefExpr::VK_None && - "Should not have constructed this"); - // Let A, B and C being the components of Target and R be the location of // the fixup. If the fixup is not pcrel, we want to compute (A - B + C). // If it is pcrel, we want to compute (A - B + C - R). diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index 8ccdd3f5f41..758c4c708f3 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -193,14 +193,23 @@ bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, // FIXME: This code has some duplication with recordRelocation. We should // probably merge the two into a single callback that tries to evaluate a // fixup and records a relocation if one is needed. + + // On error claim to have completely evaluated the fixup, to prevent any + // further processing from being done. const MCExpr *Expr = Fixup.getValue(); + MCContext &Ctx = getContext(); + Value = 0; if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) { - getContext().reportError(Fixup.getLoc(), "expected relocatable expression"); - // Claim to have completely evaluated the fixup, to prevent any further - // processing from being done. - Value = 0; + Ctx.reportError(Fixup.getLoc(), "expected relocatable expression"); return true; } + if (const MCSymbolRefExpr *RefB = Target.getSymB()) { + if (RefB->getKind() != MCSymbolRefExpr::VK_None) { + Ctx.reportError(Fixup.getLoc(), + "unsupported subtraction of qualified symbol"); + return true; + } + } bool IsPCRel = Backend.getFixupKindInfo( Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel; diff --git a/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp b/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp index 4097ef224d5..caf98bffb80 100644 --- a/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp +++ b/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp @@ -153,8 +153,7 @@ void X86MachObjectWriter::RecordX86_64Relocation( const MCSymbol *B_Base = Asm.getAtom(*B); // Neither symbol can be modified. - if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None || - Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None) { + if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None) { Asm.getContext().reportError(Fixup.getLoc(), "unsupported relocation of modified symbol"); return; diff --git a/test/MC/AArch64/label-arithmetic-diags-darwin.s b/test/MC/AArch64/label-arithmetic-diags-darwin.s index 96b00cd4e3e..e32db7c125b 100644 --- a/test/MC/AArch64/label-arithmetic-diags-darwin.s +++ b/test/MC/AArch64/label-arithmetic-diags-darwin.s @@ -15,10 +15,10 @@ Lend: add w0, w1, #(Lend - var@TLVPPAGEOFF) cmp w0, #(Lend - var@TLVPPAGEOFF) - // CHECK: error: unknown AArch64 fixup kind! + // CHECK: error: unsupported subtraction of qualified symbol // CHECK-NEXT: add w0, w1, #(Lend - var@TLVPPAGEOFF) // CHECK-NEXT: ^ - // CHECK: error: unknown AArch64 fixup kind! + // CHECK: error: unsupported subtraction of qualified symbol // CHECK-NEXT: cmp w0, #(Lend - var@TLVPPAGEOFF) // CHECK-NEXT: ^ diff --git a/test/MC/ELF/bad-expr.s b/test/MC/ELF/bad-expr.s index 1cad919fee3..59809700d4a 100644 --- a/test/MC/ELF/bad-expr.s +++ b/test/MC/ELF/bad-expr.s @@ -1,8 +1,12 @@ // RUN: not llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o /dev/null 2>%t // RUN: FileCheck --input-file=%t %s -// CHECK: symbol '__executable_start' can not be undefined in a subtraction expression .data x: +// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: symbol '__executable_start' can not be undefined in a subtraction expression .quad x-__executable_start + +// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: unsupported subtraction of qualified symbol + .long bar - foo@got +foo: diff --git a/test/MC/X86/macho-reloc-errors-x86_64.s b/test/MC/X86/macho-reloc-errors-x86_64.s index 05f77c495b2..4498d03338f 100644 --- a/test/MC/X86/macho-reloc-errors-x86_64.s +++ b/test/MC/X86/macho-reloc-errors-x86_64.s @@ -10,7 +10,7 @@ mov %rax, thing@TLVP // CHECK-ERROR: 3:9: error: 32-bit absolute addressing is not supported in 64-bit mode -// CHECK-ERROR: 4:9: error: unsupported relocation of modified symbol +// 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 -- 2.50.1