1 //===-- MSP430ELFObjectWriter.cpp - MSP430 ELF Writer ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "MCTargetDesc/MSP430FixupKinds.h"
11 #include "MCTargetDesc/MSP430MCTargetDesc.h"
12 
13 #include "MCTargetDesc/MSP430MCTargetDesc.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/ErrorHandling.h"
19 
20 using namespace llvm;
21 
22 namespace {
23 class MSP430ELFObjectWriter : public MCELFObjectTargetWriter {
24 public:
MSP430ELFObjectWriter(uint8_t OSABI)25   MSP430ELFObjectWriter(uint8_t OSABI)
26     : MCELFObjectTargetWriter(false, OSABI, ELF::EM_MSP430,
27                               /*HasRelocationAddend*/ true) {}
28 
~MSP430ELFObjectWriter()29   ~MSP430ELFObjectWriter() override {}
30 
31 protected:
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const32   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
33                         const MCFixup &Fixup, bool IsPCRel) const override {
34     // Translate fixup kind to ELF relocation type.
35     switch ((unsigned)Fixup.getKind()) {
36     case FK_Data_1:                   return ELF::R_MSP430_8;
37     case FK_Data_2:                   return ELF::R_MSP430_16_BYTE;
38     case FK_Data_4:                   return ELF::R_MSP430_32;
39     case MSP430::fixup_32:            return ELF::R_MSP430_32;
40     case MSP430::fixup_10_pcrel:      return ELF::R_MSP430_10_PCREL;
41     case MSP430::fixup_16:            return ELF::R_MSP430_16;
42     case MSP430::fixup_16_pcrel:      return ELF::R_MSP430_16_PCREL;
43     case MSP430::fixup_16_byte:       return ELF::R_MSP430_16_BYTE;
44     case MSP430::fixup_16_pcrel_byte: return ELF::R_MSP430_16_PCREL_BYTE;
45     case MSP430::fixup_2x_pcrel:      return ELF::R_MSP430_2X_PCREL;
46     case MSP430::fixup_rl_pcrel:      return ELF::R_MSP430_RL_PCREL;
47     case MSP430::fixup_8:             return ELF::R_MSP430_8;
48     case MSP430::fixup_sym_diff:      return ELF::R_MSP430_SYM_DIFF;
49     default:
50       llvm_unreachable("Invalid fixup kind");
51     }
52   }
53 };
54 } // end of anonymous namespace
55 
56 std::unique_ptr<MCObjectTargetWriter>
createMSP430ELFObjectWriter(uint8_t OSABI)57 llvm::createMSP430ELFObjectWriter(uint8_t OSABI) {
58   return llvm::make_unique<MSP430ELFObjectWriter>(OSABI);
59 }
60