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/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
~AVRELFObjectWriter()28 virtual ~AVRELFObjectWriter() {}
29
30 unsigned getRelocType(MCContext &Ctx,
31 const MCValue &Target,
32 const MCFixup &Fixup,
33 bool IsPCRel) const override;
34 };
35
AVRELFObjectWriter(uint8_t OSABI)36 AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
37 : MCELFObjectTargetWriter(false, OSABI, ELF::EM_AVR, true) {}
38
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const39 unsigned AVRELFObjectWriter::getRelocType(MCContext &Ctx,
40 const MCValue &Target,
41 const MCFixup &Fixup,
42 bool IsPCRel) const {
43 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
44 switch ((unsigned) Fixup.getKind()) {
45 case FK_Data_1:
46 switch (Modifier) {
47 default:
48 llvm_unreachable("Unsupported Modifier");
49 case MCSymbolRefExpr::VK_None:
50 return ELF::R_AVR_8;
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;
59 }
60 case FK_Data_4:
61 switch (Modifier) {
62 default:
63 llvm_unreachable("Unsupported Modifier");
64 case MCSymbolRefExpr::VK_None:
65 return ELF::R_AVR_32;
66 case MCSymbolRefExpr::VK_AVR_DIFF32:
67 return ELF::R_AVR_DIFF32;
68 }
69 case FK_Data_2:
70 switch (Modifier) {
71 default:
72 llvm_unreachable("Unsupported Modifier");
73 case MCSymbolRefExpr::VK_None:
74 return ELF::R_AVR_16;
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;
79 }
80 case AVR::fixup_32:
81 return ELF::R_AVR_32;
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;
86 case AVR::fixup_16:
87 return ELF::R_AVR_16;
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;
116 case AVR::fixup_ldi:
117 return ELF::R_AVR_LDI;
118 case AVR::fixup_6:
119 return ELF::R_AVR_6;
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;
130 case AVR::fixup_8:
131 return ELF::R_AVR_8;
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;
150 default:
151 llvm_unreachable("invalid fixup kind!");
152 }
153 }
154
createAVRELFObjectWriter(uint8_t OSABI)155 std::unique_ptr<MCObjectTargetWriter> createAVRELFObjectWriter(uint8_t OSABI) {
156 return make_unique<AVRELFObjectWriter>(OSABI);
157 }
158
159 } // end of namespace llvm
160
161