1 //===-- AVRELFObjectWriter.cpp - AVR 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/AVRFixupKinds.h" 11 #include "MCTargetDesc/AVRMCTargetDesc.h" 12 13 #include "llvm/MC/MCAssembler.h" 14 #include "llvm/MC/MCELFObjectWriter.h" 15 #include "llvm/MC/MCExpr.h" 16 #include "llvm/MC/MCSection.h" 17 #include "llvm/MC/MCValue.h" 18 #include "llvm/Support/ErrorHandling.h" 19 20 namespace llvm { 21 22 /// Writes AVR machine code into an ELF32 object file. 23 class AVRELFObjectWriter : public MCELFObjectTargetWriter { 24 public: 25 AVRELFObjectWriter(uint8_t OSABI); 26 27 virtual ~AVRELFObjectWriter() {} 28 29 unsigned getRelocType(MCContext &Ctx, 30 const MCValue &Target, 31 const MCFixup &Fixup, 32 bool IsPCRel) const override; 33 }; 34 35 AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI) 36 : MCELFObjectTargetWriter(false, OSABI, ELF::EM_AVR, true, false) {} 37 38 unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx, 39 const MCValue &Target, 40 const MCFixup &Fixup, 41 bool IsPCRel) const { 42 switch ((unsigned) Fixup.getKind()) { 43 case FK_Data_1: 44 case FK_Data_4: 45 llvm_unreachable("unsupported relocation type"); 46 case FK_Data_2: 47 return ELF::R_AVR_16_PM; 48 case AVR::fixup_32: 49 return ELF::R_AVR_32; 50 case AVR::fixup_7_pcrel: 51 return ELF::R_AVR_7_PCREL; 52 case AVR::fixup_13_pcrel: 53 return ELF::R_AVR_13_PCREL; 54 case AVR::fixup_16: 55 return ELF::R_AVR_16; 56 case AVR::fixup_16_pm: 57 return ELF::R_AVR_16_PM; 58 case AVR::fixup_lo8_ldi: 59 return ELF::R_AVR_LO8_LDI; 60 case AVR::fixup_hi8_ldi: 61 return ELF::R_AVR_HI8_LDI; 62 case AVR::fixup_hh8_ldi: 63 return ELF::R_AVR_HH8_LDI; 64 case AVR::fixup_lo8_ldi_neg: 65 return ELF::R_AVR_LO8_LDI_NEG; 66 case AVR::fixup_hi8_ldi_neg: 67 return ELF::R_AVR_HI8_LDI_NEG; 68 case AVR::fixup_hh8_ldi_neg: 69 return ELF::R_AVR_HH8_LDI_NEG; 70 case AVR::fixup_lo8_ldi_pm: 71 return ELF::R_AVR_LO8_LDI_PM; 72 case AVR::fixup_hi8_ldi_pm: 73 return ELF::R_AVR_HI8_LDI_PM; 74 case AVR::fixup_hh8_ldi_pm: 75 return ELF::R_AVR_HH8_LDI_PM; 76 case AVR::fixup_lo8_ldi_pm_neg: 77 return ELF::R_AVR_LO8_LDI_PM_NEG; 78 case AVR::fixup_hi8_ldi_pm_neg: 79 return ELF::R_AVR_HI8_LDI_PM_NEG; 80 case AVR::fixup_hh8_ldi_pm_neg: 81 return ELF::R_AVR_HH8_LDI_PM_NEG; 82 case AVR::fixup_call: 83 return ELF::R_AVR_CALL; 84 case AVR::fixup_ldi: 85 return ELF::R_AVR_LDI; 86 case AVR::fixup_6: 87 return ELF::R_AVR_6; 88 case AVR::fixup_6_adiw: 89 return ELF::R_AVR_6_ADIW; 90 case AVR::fixup_ms8_ldi: 91 return ELF::R_AVR_MS8_LDI; 92 case AVR::fixup_ms8_ldi_neg: 93 return ELF::R_AVR_MS8_LDI_NEG; 94 case AVR::fixup_lo8_ldi_gs: 95 return ELF::R_AVR_LO8_LDI_GS; 96 case AVR::fixup_hi8_ldi_gs: 97 return ELF::R_AVR_HI8_LDI_GS; 98 case AVR::fixup_8: 99 return ELF::R_AVR_8; 100 case AVR::fixup_8_lo8: 101 return ELF::R_AVR_8_LO8; 102 case AVR::fixup_8_hi8: 103 return ELF::R_AVR_8_HI8; 104 case AVR::fixup_8_hlo8: 105 return ELF::R_AVR_8_HLO8; 106 case AVR::fixup_sym_diff: 107 return ELF::R_AVR_SYM_DIFF; 108 case AVR::fixup_16_ldst: 109 return ELF::R_AVR_16_LDST; 110 case AVR::fixup_lds_sts_16: 111 return ELF::R_AVR_LDS_STS_16; 112 case AVR::fixup_port6: 113 return ELF::R_AVR_PORT6; 114 case AVR::fixup_port5: 115 return ELF::R_AVR_PORT5; 116 default: 117 llvm_unreachable("invalid fixup kind!"); 118 } 119 } 120 121 MCObjectWriter *createAVRELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI) { 122 MCELFObjectTargetWriter *MOTW = new AVRELFObjectWriter(OSABI); 123 return createELFObjectWriter(MOTW, OS, true); 124 } 125 126 } // end of namespace llvm 127 128