[llvm] r306082 - COFF: Produce an error on invalid pcrel relocs.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 22 21:07:44 PDT 2017
Author: rafael
Date: Thu Jun 22 23:07:44 2017
New Revision: 306082
URL: http://llvm.org/viewvc/llvm-project?rev=306082&view=rev
Log:
COFF: Produce an error on invalid pcrel relocs.
X86_64 COFF only has support for 32 bit pcrel relocations. Produce an
error on all others.
Note that gnu as has extended the relocation values to support
this. It is not clear if we should support the gnu extension.
Added:
llvm/trunk/test/MC/COFF/cross-section-relative-err.s
Modified:
llvm/trunk/include/llvm/MC/MCWinCOFFObjectWriter.h
llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp
llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp
llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp
llvm/trunk/test/MC/COFF/cross-section-relative.s
Modified: llvm/trunk/include/llvm/MC/MCWinCOFFObjectWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCWinCOFFObjectWriter.h?rev=306082&r1=306081&r2=306082&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCWinCOFFObjectWriter.h (original)
+++ llvm/trunk/include/llvm/MC/MCWinCOFFObjectWriter.h Thu Jun 22 23:07:44 2017
@@ -30,8 +30,8 @@ class raw_pwrite_stream;
virtual ~MCWinCOFFObjectTargetWriter() = default;
unsigned getMachine() const { return Machine; }
- virtual unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
- bool IsCrossSection,
+ virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
+ const MCFixup &Fixup, bool IsCrossSection,
const MCAsmBackend &MAB) const = 0;
virtual bool recordRelocation(const MCFixup &) const { return true; }
};
Modified: llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp?rev=306082&r1=306081&r2=306082&view=diff
==============================================================================
--- llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp Thu Jun 22 23:07:44 2017
@@ -781,8 +781,8 @@ void WinCOFFObjectWriter::recordRelocati
++Reloc.Symb->Relocations;
Reloc.Data.VirtualAddress += Fixup.getOffset();
- Reloc.Data.Type =
- TargetObjectWriter->getRelocType(Target, Fixup, SymB, Asm.getBackend());
+ Reloc.Data.Type = TargetObjectWriter->getRelocType(
+ Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
// FIXME: Can anyone explain what this does other than adjust for the size
// of the offset?
Modified: llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp?rev=306082&r1=306081&r2=306082&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp (original)
+++ llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp Thu Jun 22 23:07:44 2017
@@ -33,8 +33,8 @@ public:
~ARMWinCOFFObjectWriter() override = default;
- unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
- bool IsCrossSection,
+ unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
+ const MCFixup &Fixup, bool IsCrossSection,
const MCAsmBackend &MAB) const override;
bool recordRelocation(const MCFixup &) const override;
@@ -42,7 +42,8 @@ public:
} // end anonymous namespace
-unsigned ARMWinCOFFObjectWriter::getRelocType(const MCValue &Target,
+unsigned ARMWinCOFFObjectWriter::getRelocType(MCContext &Ctx,
+ const MCValue &Target,
const MCFixup &Fixup,
bool IsCrossSection,
const MCAsmBackend &MAB) const {
Modified: llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp?rev=306082&r1=306081&r2=306082&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp (original)
+++ llvm/trunk/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp Thu Jun 22 23:07:44 2017
@@ -10,6 +10,7 @@
#include "MCTargetDesc/X86FixupKinds.h"
#include "MCTargetDesc/X86MCTargetDesc.h"
#include "llvm/BinaryFormat/COFF.h"
+#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCValue.h"
@@ -25,8 +26,8 @@ public:
X86WinCOFFObjectWriter(bool Is64Bit);
~X86WinCOFFObjectWriter() override = default;
- unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
- bool IsCrossSection,
+ unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
+ const MCFixup &Fixup, bool IsCrossSection,
const MCAsmBackend &MAB) const override;
};
@@ -36,11 +37,19 @@ X86WinCOFFObjectWriter::X86WinCOFFObject
: MCWinCOFFObjectTargetWriter(Is64Bit ? COFF::IMAGE_FILE_MACHINE_AMD64
: COFF::IMAGE_FILE_MACHINE_I386) {}
-unsigned X86WinCOFFObjectWriter::getRelocType(const MCValue &Target,
+unsigned X86WinCOFFObjectWriter::getRelocType(MCContext &Ctx,
+ const MCValue &Target,
const MCFixup &Fixup,
bool IsCrossSection,
const MCAsmBackend &MAB) const {
- unsigned FixupKind = IsCrossSection ? FK_PCRel_4 : Fixup.getKind();
+ unsigned FixupKind = Fixup.getKind();
+ if (IsCrossSection) {
+ if (FixupKind != FK_Data_4) {
+ Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
+ return COFF::IMAGE_REL_AMD64_ADDR32;
+ }
+ FixupKind = FK_PCRel_4;
+ }
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Added: llvm/trunk/test/MC/COFF/cross-section-relative-err.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/COFF/cross-section-relative-err.s?rev=306082&view=auto
==============================================================================
--- llvm/trunk/test/MC/COFF/cross-section-relative-err.s (added)
+++ llvm/trunk/test/MC/COFF/cross-section-relative-err.s Thu Jun 22 23:07:44 2017
@@ -0,0 +1,12 @@
+// RUN: not llvm-mc -filetype=obj -triple x86_64-pc-win32 %s -o /dev/null 2>&1 | FileCheck %s
+
+
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: Cannot represent this expression
+ .byte foo - .
+
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: Cannot represent this expression
+ .short foo - .
+
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: Cannot represent this expression
+ .quad foo - .
+
Modified: llvm/trunk/test/MC/COFF/cross-section-relative.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/COFF/cross-section-relative.s?rev=306082&r1=306081&r2=306082&view=diff
==============================================================================
--- llvm/trunk/test/MC/COFF/cross-section-relative.s (original)
+++ llvm/trunk/test/MC/COFF/cross-section-relative.s Thu Jun 22 23:07:44 2017
@@ -27,17 +27,14 @@ g4:
.globl t1 # @t1
.align 8
t1:
- .quad (g3-t1)+4
.globl t2 # @t2
.align 8
t2:
- .quad g3-t2
.globl t3 # @t3
.align 8
t3:
- .quad (g3-t3)-4
.globl t4 # @t4
.align 4
@@ -78,10 +75,9 @@ t6:
// READOBJ-NEXT: IMAGE_SCN_MEM_WRITE (0x80000000)
// READOBJ-NEXT: ]
// READOBJ-NEXT: SectionData (
-// READOBJ-NEXT: 0000: 08000000 00000000 04000000 00000000 |
-// READOBJ-NEXT: 0010: 00000000 00000000 04000000 00000000 |
-// READOBJ-NEXT: 0020: 01020000 00000000 00010000 00000000 |
-// READOBJ-NEXT: 0030: 04000000 00000000 04000000 |
+// READOBJ-NEXT: 0000: 04000000 00000000 00000000 00000000 |
+// READOBJ-NEXT: 0010: 01020000 00000000 00010000 00000000 |
+// READOBJ-NEXT: 0020: 04000000 00000000 04000000 |
// READOBJ-NEXT: )
// READOBJ-NEXT: }
// READOBJ-NEXT: ]
@@ -93,32 +89,17 @@ t6:
// READOBJ-NEXT: Symbol: g3
// READOBJ-NEXT: }
// READOBJ-NEXT: Relocation {
-// READOBJ-NEXT: Offset: 0x8
-// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
-// READOBJ-NEXT: Symbol: g3
-// READOBJ-NEXT: }
-// READOBJ-NEXT: Relocation {
-// READOBJ-NEXT: Offset: 0x10
-// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
-// READOBJ-NEXT: Symbol: g3
-// READOBJ-NEXT: }
-// READOBJ-NEXT: Relocation {
-// READOBJ-NEXT: Offset: 0x18
-// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
-// READOBJ-NEXT: Symbol: g3
-// READOBJ-NEXT: }
-// READOBJ-NEXT: Relocation {
-// READOBJ-NEXT: Offset: 0x1C
+// READOBJ-NEXT: Offset: 0x4
// READOBJ-NEXT: Type: IMAGE_REL_AMD64_ADDR32NB (3)
// READOBJ-NEXT: Symbol: g3
// READOBJ-NEXT: }
// READOBJ-NEXT: Relocation {
-// READOBJ-NEXT: Offset: 0x30
+// READOBJ-NEXT: Offset: 0x20
// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
// READOBJ-NEXT: Symbol: g3
// READOBJ-NEXT: }
// READOBJ-NEXT: Relocation {
-// READOBJ-NEXT: Offset: 0x38
+// READOBJ-NEXT: Offset: 0x28
// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
// READOBJ-NEXT: Symbol: foobar
// READOBJ-NEXT: }
More information about the llvm-commits
mailing list