1 //===-- AVRELFObjectWriter.cpp - AVR ELF Writer ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/AVRFixupKinds.h"
11 #include "MCTargetDesc/AVRMCTargetDesc.h"
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"
23 /// Writes AVR machine code into an ELF32 object file.
24 class AVRELFObjectWriter : public MCELFObjectTargetWriter {
26 AVRELFObjectWriter(uint8_t OSABI);
28 virtual ~AVRELFObjectWriter() {}
30 unsigned getRelocType(MCContext &Ctx,
31 const MCValue &Target,
33 bool IsPCRel) const override;
36 AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
37 : MCELFObjectTargetWriter(false, OSABI, ELF::EM_AVR, true) {}
39 unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx,
40 const MCValue &Target,
43 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
44 switch ((unsigned) Fixup.getKind()) {
48 llvm_unreachable("Unsupported Modifier");
49 case MCSymbolRefExpr::VK_None:
51 case MCSymbolRefExpr::VK_AVR_DIFF8:
52 return ELF::R_AVR_DIFF8;
53 case MCSymbolRefExpr::VK_AVR_LO8:
54 return ELF::R_AVR_8_LO8;
55 case MCSymbolRefExpr::VK_AVR_HI8:
56 return ELF::R_AVR_8_HI8;
57 case MCSymbolRefExpr::VK_AVR_HLO8:
58 return ELF::R_AVR_8_HLO8;
63 llvm_unreachable("Unsupported Modifier");
64 case MCSymbolRefExpr::VK_None:
66 case MCSymbolRefExpr::VK_AVR_DIFF32:
67 return ELF::R_AVR_DIFF32;
72 llvm_unreachable("Unsupported Modifier");
73 case MCSymbolRefExpr::VK_None:
75 case MCSymbolRefExpr::VK_AVR_NONE:
76 return ELF::R_AVR_16_PM;
77 case MCSymbolRefExpr::VK_AVR_DIFF16:
78 return ELF::R_AVR_DIFF16;
82 case AVR::fixup_7_pcrel:
83 return ELF::R_AVR_7_PCREL;
84 case AVR::fixup_13_pcrel:
85 return ELF::R_AVR_13_PCREL;
88 case AVR::fixup_16_pm:
89 return ELF::R_AVR_16_PM;
90 case AVR::fixup_lo8_ldi:
91 return ELF::R_AVR_LO8_LDI;
92 case AVR::fixup_hi8_ldi:
93 return ELF::R_AVR_HI8_LDI;
94 case AVR::fixup_hh8_ldi:
95 return ELF::R_AVR_HH8_LDI;
96 case AVR::fixup_lo8_ldi_neg:
97 return ELF::R_AVR_LO8_LDI_NEG;
98 case AVR::fixup_hi8_ldi_neg:
99 return ELF::R_AVR_HI8_LDI_NEG;
100 case AVR::fixup_hh8_ldi_neg:
101 return ELF::R_AVR_HH8_LDI_NEG;
102 case AVR::fixup_lo8_ldi_pm:
103 return ELF::R_AVR_LO8_LDI_PM;
104 case AVR::fixup_hi8_ldi_pm:
105 return ELF::R_AVR_HI8_LDI_PM;
106 case AVR::fixup_hh8_ldi_pm:
107 return ELF::R_AVR_HH8_LDI_PM;
108 case AVR::fixup_lo8_ldi_pm_neg:
109 return ELF::R_AVR_LO8_LDI_PM_NEG;
110 case AVR::fixup_hi8_ldi_pm_neg:
111 return ELF::R_AVR_HI8_LDI_PM_NEG;
112 case AVR::fixup_hh8_ldi_pm_neg:
113 return ELF::R_AVR_HH8_LDI_PM_NEG;
114 case AVR::fixup_call:
115 return ELF::R_AVR_CALL;
117 return ELF::R_AVR_LDI;
120 case AVR::fixup_6_adiw:
121 return ELF::R_AVR_6_ADIW;
122 case AVR::fixup_ms8_ldi:
123 return ELF::R_AVR_MS8_LDI;
124 case AVR::fixup_ms8_ldi_neg:
125 return ELF::R_AVR_MS8_LDI_NEG;
126 case AVR::fixup_lo8_ldi_gs:
127 return ELF::R_AVR_LO8_LDI_GS;
128 case AVR::fixup_hi8_ldi_gs:
129 return ELF::R_AVR_HI8_LDI_GS;
132 case AVR::fixup_8_lo8:
133 return ELF::R_AVR_8_LO8;
134 case AVR::fixup_8_hi8:
135 return ELF::R_AVR_8_HI8;
136 case AVR::fixup_8_hlo8:
137 return ELF::R_AVR_8_HLO8;
138 case AVR::fixup_diff8:
139 return ELF::R_AVR_DIFF8;
140 case AVR::fixup_diff16:
141 return ELF::R_AVR_DIFF16;
142 case AVR::fixup_diff32:
143 return ELF::R_AVR_DIFF32;
144 case AVR::fixup_lds_sts_16:
145 return ELF::R_AVR_LDS_STS_16;
146 case AVR::fixup_port6:
147 return ELF::R_AVR_PORT6;
148 case AVR::fixup_port5:
149 return ELF::R_AVR_PORT5;
151 llvm_unreachable("invalid fixup kind!");
155 std::unique_ptr<MCObjectTargetWriter> createAVRELFObjectWriter(uint8_t OSABI) {
156 return make_unique<AVRELFObjectWriter>(OSABI);
159 } // end of namespace llvm