1 //===-- AVRELFObjectWriter.cpp - AVR ELF Writer ---------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "MCTargetDesc/AVRFixupKinds.h" 10 #include "MCTargetDesc/AVRMCExpr.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/MCObjectWriter.h" 17 #include "llvm/MC/MCSection.h" 18 #include "llvm/MC/MCValue.h" 19 #include "llvm/Support/ErrorHandling.h" 20 21 namespace llvm { 22 23 /// Writes AVR machine code into an ELF32 object file. 24 class AVRELFObjectWriter : public MCELFObjectTargetWriter { 25 public: 26 AVRELFObjectWriter(uint8_t OSABI); 27 28 virtual ~AVRELFObjectWriter() = default; 29 30 unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 31 const MCFixup &Fixup, bool IsPCRel) const override; 32 }; 33 34 AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI) 35 : MCELFObjectTargetWriter(false, OSABI, ELF::EM_AVR, true) {} 36 37 unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, 38 const MCFixup &Fixup, 39 bool IsPCRel) const { 40 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant(); 41 switch ((unsigned)Fixup.getKind()) { 42 case FK_Data_1: 43 switch (Modifier) { 44 default: 45 llvm_unreachable("Unsupported Modifier"); 46 case MCSymbolRefExpr::VK_None: 47 return ELF::R_AVR_8; 48 case MCSymbolRefExpr::VK_AVR_DIFF8: 49 return ELF::R_AVR_DIFF8; 50 case MCSymbolRefExpr::VK_AVR_LO8: 51 return ELF::R_AVR_8_LO8; 52 case MCSymbolRefExpr::VK_AVR_HI8: 53 return ELF::R_AVR_8_HI8; 54 case MCSymbolRefExpr::VK_AVR_HLO8: 55 return ELF::R_AVR_8_HLO8; 56 } 57 case FK_Data_4: 58 switch (Modifier) { 59 default: 60 llvm_unreachable("Unsupported Modifier"); 61 case MCSymbolRefExpr::VK_None: 62 return ELF::R_AVR_32; 63 case MCSymbolRefExpr::VK_AVR_DIFF32: 64 return ELF::R_AVR_DIFF32; 65 } 66 case FK_Data_2: 67 switch (Modifier) { 68 default: 69 llvm_unreachable("Unsupported Modifier"); 70 case MCSymbolRefExpr::VK_None: 71 return ELF::R_AVR_16; 72 case MCSymbolRefExpr::VK_AVR_NONE: 73 case MCSymbolRefExpr::VK_AVR_PM: 74 return ELF::R_AVR_16_PM; 75 case MCSymbolRefExpr::VK_AVR_DIFF16: 76 return ELF::R_AVR_DIFF16; 77 } 78 case AVR::fixup_32: 79 return ELF::R_AVR_32; 80 case AVR::fixup_7_pcrel: 81 return ELF::R_AVR_7_PCREL; 82 case AVR::fixup_13_pcrel: 83 return ELF::R_AVR_13_PCREL; 84 case AVR::fixup_16: 85 return ELF::R_AVR_16; 86 case AVR::fixup_16_pm: 87 return ELF::R_AVR_16_PM; 88 case AVR::fixup_lo8_ldi: 89 return ELF::R_AVR_LO8_LDI; 90 case AVR::fixup_hi8_ldi: 91 return ELF::R_AVR_HI8_LDI; 92 case AVR::fixup_hh8_ldi: 93 return ELF::R_AVR_HH8_LDI; 94 case AVR::fixup_lo8_ldi_neg: 95 return ELF::R_AVR_LO8_LDI_NEG; 96 case AVR::fixup_hi8_ldi_neg: 97 return ELF::R_AVR_HI8_LDI_NEG; 98 case AVR::fixup_hh8_ldi_neg: 99 return ELF::R_AVR_HH8_LDI_NEG; 100 case AVR::fixup_lo8_ldi_pm: 101 return ELF::R_AVR_LO8_LDI_PM; 102 case AVR::fixup_hi8_ldi_pm: 103 return ELF::R_AVR_HI8_LDI_PM; 104 case AVR::fixup_hh8_ldi_pm: 105 return ELF::R_AVR_HH8_LDI_PM; 106 case AVR::fixup_lo8_ldi_pm_neg: 107 return ELF::R_AVR_LO8_LDI_PM_NEG; 108 case AVR::fixup_hi8_ldi_pm_neg: 109 return ELF::R_AVR_HI8_LDI_PM_NEG; 110 case AVR::fixup_hh8_ldi_pm_neg: 111 return ELF::R_AVR_HH8_LDI_PM_NEG; 112 case AVR::fixup_call: 113 return ELF::R_AVR_CALL; 114 case AVR::fixup_ldi: 115 return ELF::R_AVR_LDI; 116 case AVR::fixup_6: 117 return ELF::R_AVR_6; 118 case AVR::fixup_6_adiw: 119 return ELF::R_AVR_6_ADIW; 120 case AVR::fixup_ms8_ldi: 121 return ELF::R_AVR_MS8_LDI; 122 case AVR::fixup_ms8_ldi_neg: 123 return ELF::R_AVR_MS8_LDI_NEG; 124 case AVR::fixup_lo8_ldi_gs: 125 return ELF::R_AVR_LO8_LDI_GS; 126 case AVR::fixup_hi8_ldi_gs: 127 return ELF::R_AVR_HI8_LDI_GS; 128 case AVR::fixup_8: 129 return ELF::R_AVR_8; 130 case AVR::fixup_8_lo8: 131 return ELF::R_AVR_8_LO8; 132 case AVR::fixup_8_hi8: 133 return ELF::R_AVR_8_HI8; 134 case AVR::fixup_8_hlo8: 135 return ELF::R_AVR_8_HLO8; 136 case AVR::fixup_diff8: 137 return ELF::R_AVR_DIFF8; 138 case AVR::fixup_diff16: 139 return ELF::R_AVR_DIFF16; 140 case AVR::fixup_diff32: 141 return ELF::R_AVR_DIFF32; 142 case AVR::fixup_lds_sts_16: 143 return ELF::R_AVR_LDS_STS_16; 144 case AVR::fixup_port6: 145 return ELF::R_AVR_PORT6; 146 case AVR::fixup_port5: 147 return ELF::R_AVR_PORT5; 148 default: 149 llvm_unreachable("invalid fixup kind!"); 150 } 151 } 152 153 std::unique_ptr<MCObjectTargetWriter> createAVRELFObjectWriter(uint8_t OSABI) { 154 return std::make_unique<AVRELFObjectWriter>(OSABI); 155 } 156 157 } // end of namespace llvm 158