2011-12-24 10:14:02 +08:00
|
|
|
//===-- X86WinCOFFObjectWriter.cpp - X86 Win COFF Writer ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/X86FixupKinds.h"
|
|
|
|
#include "MCTargetDesc/X86MCTargetDesc.h"
|
2013-04-11 07:28:17 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
2011-12-24 10:14:02 +08:00
|
|
|
#include "llvm/MC/MCWinCOFFObjectWriter.h"
|
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class MCObjectWriter;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
|
|
|
|
public:
|
2014-04-08 10:15:13 +08:00
|
|
|
X86WinCOFFObjectWriter(bool Is64Bit);
|
2015-04-11 10:11:45 +08:00
|
|
|
~X86WinCOFFObjectWriter() override;
|
2011-12-24 10:14:02 +08:00
|
|
|
|
2014-03-10 13:29:18 +08:00
|
|
|
unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
|
2015-01-22 12:03:32 +08:00
|
|
|
bool IsCrossSection,
|
|
|
|
const MCAsmBackend &MAB) const override;
|
2011-12-24 10:14:02 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2011-12-24 10:14:02 +08:00
|
|
|
|
2014-04-08 10:15:13 +08:00
|
|
|
X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
|
|
|
|
: MCWinCOFFObjectTargetWriter(Is64Bit ? COFF::IMAGE_FILE_MACHINE_AMD64
|
|
|
|
: COFF::IMAGE_FILE_MACHINE_I386) {}
|
2011-12-24 10:14:02 +08:00
|
|
|
|
|
|
|
X86WinCOFFObjectWriter::~X86WinCOFFObjectWriter() {}
|
|
|
|
|
2013-04-11 07:28:17 +08:00
|
|
|
unsigned X86WinCOFFObjectWriter::getRelocType(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
2015-01-22 12:03:32 +08:00
|
|
|
bool IsCrossSection,
|
|
|
|
const MCAsmBackend &MAB) const {
|
2013-04-11 07:28:17 +08:00
|
|
|
unsigned FixupKind = IsCrossSection ? FK_PCRel_4 : Fixup.getKind();
|
|
|
|
|
|
|
|
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
|
|
|
|
MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
|
|
|
|
|
2014-04-08 10:15:13 +08:00
|
|
|
if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
|
|
|
|
switch (FixupKind) {
|
|
|
|
case FK_PCRel_4:
|
|
|
|
case X86::reloc_riprel_4byte:
|
|
|
|
case X86::reloc_riprel_4byte_movq_load:
|
2016-05-31 04:18:53 +08:00
|
|
|
case X86::reloc_riprel_4byte_relax:
|
|
|
|
case X86::reloc_riprel_4byte_relax_rex:
|
2014-04-08 10:15:13 +08:00
|
|
|
return COFF::IMAGE_REL_AMD64_REL32;
|
|
|
|
case FK_Data_4:
|
|
|
|
case X86::reloc_signed_4byte:
|
|
|
|
if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
|
|
|
|
return COFF::IMAGE_REL_AMD64_ADDR32NB;
|
2016-01-20 07:05:27 +08:00
|
|
|
if (Modifier == MCSymbolRefExpr::VK_SECREL)
|
|
|
|
return COFF::IMAGE_REL_AMD64_SECREL;
|
2014-04-08 10:15:13 +08:00
|
|
|
return COFF::IMAGE_REL_AMD64_ADDR32;
|
|
|
|
case FK_Data_8:
|
2011-12-24 10:14:02 +08:00
|
|
|
return COFF::IMAGE_REL_AMD64_ADDR64;
|
2014-04-08 10:15:13 +08:00
|
|
|
case FK_SecRel_2:
|
|
|
|
return COFF::IMAGE_REL_AMD64_SECTION;
|
|
|
|
case FK_SecRel_4:
|
|
|
|
return COFF::IMAGE_REL_AMD64_SECREL;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported relocation type");
|
|
|
|
}
|
|
|
|
} else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
|
|
|
|
switch (FixupKind) {
|
|
|
|
case FK_PCRel_4:
|
|
|
|
case X86::reloc_riprel_4byte:
|
|
|
|
case X86::reloc_riprel_4byte_movq_load:
|
|
|
|
return COFF::IMAGE_REL_I386_REL32;
|
|
|
|
case FK_Data_4:
|
|
|
|
case X86::reloc_signed_4byte:
|
|
|
|
if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
|
|
|
|
return COFF::IMAGE_REL_I386_DIR32NB;
|
2016-01-20 07:05:27 +08:00
|
|
|
if (Modifier == MCSymbolRefExpr::VK_SECREL)
|
|
|
|
return COFF::IMAGE_REL_AMD64_SECREL;
|
2014-04-08 10:15:13 +08:00
|
|
|
return COFF::IMAGE_REL_I386_DIR32;
|
|
|
|
case FK_SecRel_2:
|
|
|
|
return COFF::IMAGE_REL_I386_SECTION;
|
|
|
|
case FK_SecRel_4:
|
|
|
|
return COFF::IMAGE_REL_I386_SECREL;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported relocation type");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
llvm_unreachable("Unsupported COFF machine type.");
|
2011-12-24 10:14:02 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 06:14:34 +08:00
|
|
|
MCObjectWriter *llvm::createX86WinCOFFObjectWriter(raw_pwrite_stream &OS,
|
2011-12-24 10:14:02 +08:00
|
|
|
bool Is64Bit) {
|
|
|
|
MCWinCOFFObjectTargetWriter *MOTW = new X86WinCOFFObjectWriter(Is64Bit);
|
|
|
|
return createWinCOFFObjectWriter(MOTW, OS);
|
|
|
|
}
|