1 //===-- HexagonELFObjectWriter.cpp - Hexagon Target Descriptions ----------===//
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 "Hexagon.h"
11 #include "MCTargetDesc/HexagonFixupKinds.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
18
19 #define DEBUG_TYPE "hexagon-elf-writer"
20
21 using namespace llvm;
22 using namespace Hexagon;
23
24 namespace {
25
26 class HexagonELFObjectWriter : public MCELFObjectTargetWriter {
27 private:
28 StringRef CPU;
29
30 public:
31 HexagonELFObjectWriter(uint8_t OSABI, StringRef C);
32
33 unsigned getRelocType(MCContext &Ctx, MCValue const &Target,
34 MCFixup const &Fixup, bool IsPCRel) const override;
35 };
36 }
37
HexagonELFObjectWriter(uint8_t OSABI,StringRef C)38 HexagonELFObjectWriter::HexagonELFObjectWriter(uint8_t OSABI, StringRef C)
39 : MCELFObjectTargetWriter(/*Is64bit*/ false, OSABI, ELF::EM_HEXAGON,
40 /*HasRelocationAddend*/ true),
41 CPU(C) {}
42
getRelocType(MCContext & Ctx,MCValue const & Target,MCFixup const & Fixup,bool IsPCRel) const43 unsigned HexagonELFObjectWriter::getRelocType(MCContext &Ctx,
44 MCValue const &Target,
45 MCFixup const &Fixup,
46 bool IsPCRel) const {
47 MCSymbolRefExpr::VariantKind Variant = Target.getAccessVariant();
48 switch ((unsigned)Fixup.getKind()) {
49 default:
50 report_fatal_error("Unrecognized relocation type");
51 break;
52 case FK_Data_4:
53 switch(Variant) {
54 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
55 return ELF::R_HEX_DTPREL_32;
56 case MCSymbolRefExpr::VariantKind::VK_GOT:
57 return ELF::R_HEX_GOT_32;
58 case MCSymbolRefExpr::VariantKind::VK_GOTREL:
59 return ELF::R_HEX_GOTREL_32;
60 case MCSymbolRefExpr::VariantKind::VK_Hexagon_GD_GOT:
61 return ELF::R_HEX_GD_GOT_32;
62 case MCSymbolRefExpr::VariantKind::VK_Hexagon_IE:
63 return ELF::R_HEX_IE_32;
64 case MCSymbolRefExpr::VariantKind::VK_Hexagon_IE_GOT:
65 return ELF::R_HEX_IE_GOT_32;
66 case MCSymbolRefExpr::VariantKind::VK_Hexagon_LD_GOT:
67 return ELF::R_HEX_LD_GOT_32;
68 case MCSymbolRefExpr::VariantKind::VK_Hexagon_PCREL:
69 return ELF::R_HEX_32_PCREL;
70 case MCSymbolRefExpr::VariantKind::VK_TPREL:
71 return ELF::R_HEX_TPREL_32;
72 case MCSymbolRefExpr::VariantKind::VK_None:
73 return IsPCRel ? ELF::R_HEX_32_PCREL : ELF::R_HEX_32;
74 default:
75 report_fatal_error("Unrecognized variant type");
76 };
77 case FK_PCRel_4:
78 return ELF::R_HEX_32_PCREL;
79 case FK_Data_2:
80 switch(Variant) {
81 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
82 return ELF::R_HEX_DTPREL_16;
83 case MCSymbolRefExpr::VariantKind::VK_GOT:
84 return ELF::R_HEX_GOT_16;
85 case MCSymbolRefExpr::VariantKind::VK_Hexagon_GD_GOT:
86 return ELF::R_HEX_GD_GOT_16;
87 case MCSymbolRefExpr::VariantKind::VK_Hexagon_IE_GOT:
88 return ELF::R_HEX_IE_GOT_16;
89 case MCSymbolRefExpr::VariantKind::VK_Hexagon_LD_GOT:
90 return ELF::R_HEX_LD_GOT_16;
91 case MCSymbolRefExpr::VariantKind::VK_TPREL:
92 return ELF::R_HEX_TPREL_16;
93 case MCSymbolRefExpr::VariantKind::VK_None:
94 return ELF::R_HEX_16;
95 default:
96 report_fatal_error("Unrecognized variant type");
97 };
98 case FK_Data_1:
99 return ELF::R_HEX_8;
100 case fixup_Hexagon_B22_PCREL:
101 return ELF::R_HEX_B22_PCREL;
102 case fixup_Hexagon_B15_PCREL:
103 return ELF::R_HEX_B15_PCREL;
104 case fixup_Hexagon_B7_PCREL:
105 return ELF::R_HEX_B7_PCREL;
106 case fixup_Hexagon_LO16:
107 return ELF::R_HEX_LO16;
108 case fixup_Hexagon_HI16:
109 return ELF::R_HEX_HI16;
110 case fixup_Hexagon_32:
111 return ELF::R_HEX_32;
112 case fixup_Hexagon_16:
113 return ELF::R_HEX_16;
114 case fixup_Hexagon_8:
115 return ELF::R_HEX_8;
116 case fixup_Hexagon_GPREL16_0:
117 return ELF::R_HEX_GPREL16_0;
118 case fixup_Hexagon_GPREL16_1:
119 return ELF::R_HEX_GPREL16_1;
120 case fixup_Hexagon_GPREL16_2:
121 return ELF::R_HEX_GPREL16_2;
122 case fixup_Hexagon_GPREL16_3:
123 return ELF::R_HEX_GPREL16_3;
124 case fixup_Hexagon_HL16:
125 return ELF::R_HEX_HL16;
126 case fixup_Hexagon_B13_PCREL:
127 return ELF::R_HEX_B13_PCREL;
128 case fixup_Hexagon_B9_PCREL:
129 return ELF::R_HEX_B9_PCREL;
130 case fixup_Hexagon_B32_PCREL_X:
131 return ELF::R_HEX_B32_PCREL_X;
132 case fixup_Hexagon_32_6_X:
133 return ELF::R_HEX_32_6_X;
134 case fixup_Hexagon_B22_PCREL_X:
135 return ELF::R_HEX_B22_PCREL_X;
136 case fixup_Hexagon_B15_PCREL_X:
137 return ELF::R_HEX_B15_PCREL_X;
138 case fixup_Hexagon_B13_PCREL_X:
139 return ELF::R_HEX_B13_PCREL_X;
140 case fixup_Hexagon_B9_PCREL_X:
141 return ELF::R_HEX_B9_PCREL_X;
142 case fixup_Hexagon_B7_PCREL_X:
143 return ELF::R_HEX_B7_PCREL_X;
144 case fixup_Hexagon_16_X:
145 return ELF::R_HEX_16_X;
146 case fixup_Hexagon_12_X:
147 return ELF::R_HEX_12_X;
148 case fixup_Hexagon_11_X:
149 return ELF::R_HEX_11_X;
150 case fixup_Hexagon_10_X:
151 return ELF::R_HEX_10_X;
152 case fixup_Hexagon_9_X:
153 return ELF::R_HEX_9_X;
154 case fixup_Hexagon_8_X:
155 return ELF::R_HEX_8_X;
156 case fixup_Hexagon_7_X:
157 return ELF::R_HEX_7_X;
158 case fixup_Hexagon_6_X:
159 return ELF::R_HEX_6_X;
160 case fixup_Hexagon_32_PCREL:
161 return ELF::R_HEX_32_PCREL;
162 case fixup_Hexagon_COPY:
163 return ELF::R_HEX_COPY;
164 case fixup_Hexagon_GLOB_DAT:
165 return ELF::R_HEX_GLOB_DAT;
166 case fixup_Hexagon_JMP_SLOT:
167 return ELF::R_HEX_JMP_SLOT;
168 case fixup_Hexagon_RELATIVE:
169 return ELF::R_HEX_RELATIVE;
170 case fixup_Hexagon_PLT_B22_PCREL:
171 return ELF::R_HEX_PLT_B22_PCREL;
172 case fixup_Hexagon_GOTREL_LO16:
173 return ELF::R_HEX_GOTREL_LO16;
174 case fixup_Hexagon_GOTREL_HI16:
175 return ELF::R_HEX_GOTREL_HI16;
176 case fixup_Hexagon_GOTREL_32:
177 return ELF::R_HEX_GOTREL_32;
178 case fixup_Hexagon_GOT_LO16:
179 return ELF::R_HEX_GOT_LO16;
180 case fixup_Hexagon_GOT_HI16:
181 return ELF::R_HEX_GOT_HI16;
182 case fixup_Hexagon_GOT_32:
183 return ELF::R_HEX_GOT_32;
184 case fixup_Hexagon_GOT_16:
185 return ELF::R_HEX_GOT_16;
186 case fixup_Hexagon_DTPMOD_32:
187 return ELF::R_HEX_DTPMOD_32;
188 case fixup_Hexagon_DTPREL_LO16:
189 return ELF::R_HEX_DTPREL_LO16;
190 case fixup_Hexagon_DTPREL_HI16:
191 return ELF::R_HEX_DTPREL_HI16;
192 case fixup_Hexagon_DTPREL_32:
193 return ELF::R_HEX_DTPREL_32;
194 case fixup_Hexagon_DTPREL_16:
195 return ELF::R_HEX_DTPREL_16;
196 case fixup_Hexagon_GD_PLT_B22_PCREL:
197 return ELF::R_HEX_GD_PLT_B22_PCREL;
198 case fixup_Hexagon_LD_PLT_B22_PCREL:
199 return ELF::R_HEX_LD_PLT_B22_PCREL;
200 case fixup_Hexagon_GD_GOT_LO16:
201 return ELF::R_HEX_GD_GOT_LO16;
202 case fixup_Hexagon_GD_GOT_HI16:
203 return ELF::R_HEX_GD_GOT_HI16;
204 case fixup_Hexagon_GD_GOT_32:
205 return ELF::R_HEX_GD_GOT_32;
206 case fixup_Hexagon_GD_GOT_16:
207 return ELF::R_HEX_GD_GOT_16;
208 case fixup_Hexagon_LD_GOT_LO16:
209 return ELF::R_HEX_LD_GOT_LO16;
210 case fixup_Hexagon_LD_GOT_HI16:
211 return ELF::R_HEX_LD_GOT_HI16;
212 case fixup_Hexagon_LD_GOT_32:
213 return ELF::R_HEX_LD_GOT_32;
214 case fixup_Hexagon_LD_GOT_16:
215 return ELF::R_HEX_LD_GOT_16;
216 case fixup_Hexagon_IE_LO16:
217 return ELF::R_HEX_IE_LO16;
218 case fixup_Hexagon_IE_HI16:
219 return ELF::R_HEX_IE_HI16;
220 case fixup_Hexagon_IE_32:
221 return ELF::R_HEX_IE_32;
222 case fixup_Hexagon_IE_GOT_LO16:
223 return ELF::R_HEX_IE_GOT_LO16;
224 case fixup_Hexagon_IE_GOT_HI16:
225 return ELF::R_HEX_IE_GOT_HI16;
226 case fixup_Hexagon_IE_GOT_32:
227 return ELF::R_HEX_IE_GOT_32;
228 case fixup_Hexagon_IE_GOT_16:
229 return ELF::R_HEX_IE_GOT_16;
230 case fixup_Hexagon_TPREL_LO16:
231 return ELF::R_HEX_TPREL_LO16;
232 case fixup_Hexagon_TPREL_HI16:
233 return ELF::R_HEX_TPREL_HI16;
234 case fixup_Hexagon_TPREL_32:
235 return ELF::R_HEX_TPREL_32;
236 case fixup_Hexagon_TPREL_16:
237 return ELF::R_HEX_TPREL_16;
238 case fixup_Hexagon_6_PCREL_X:
239 return ELF::R_HEX_6_PCREL_X;
240 case fixup_Hexagon_GOTREL_32_6_X:
241 return ELF::R_HEX_GOTREL_32_6_X;
242 case fixup_Hexagon_GOTREL_16_X:
243 return ELF::R_HEX_GOTREL_16_X;
244 case fixup_Hexagon_GOTREL_11_X:
245 return ELF::R_HEX_GOTREL_11_X;
246 case fixup_Hexagon_GOT_32_6_X:
247 return ELF::R_HEX_GOT_32_6_X;
248 case fixup_Hexagon_GOT_16_X:
249 return ELF::R_HEX_GOT_16_X;
250 case fixup_Hexagon_GOT_11_X:
251 return ELF::R_HEX_GOT_11_X;
252 case fixup_Hexagon_DTPREL_32_6_X:
253 return ELF::R_HEX_DTPREL_32_6_X;
254 case fixup_Hexagon_DTPREL_16_X:
255 return ELF::R_HEX_DTPREL_16_X;
256 case fixup_Hexagon_DTPREL_11_X:
257 return ELF::R_HEX_DTPREL_11_X;
258 case fixup_Hexagon_GD_GOT_32_6_X:
259 return ELF::R_HEX_GD_GOT_32_6_X;
260 case fixup_Hexagon_GD_GOT_16_X:
261 return ELF::R_HEX_GD_GOT_16_X;
262 case fixup_Hexagon_GD_GOT_11_X:
263 return ELF::R_HEX_GD_GOT_11_X;
264 case fixup_Hexagon_LD_GOT_32_6_X:
265 return ELF::R_HEX_LD_GOT_32_6_X;
266 case fixup_Hexagon_LD_GOT_16_X:
267 return ELF::R_HEX_LD_GOT_16_X;
268 case fixup_Hexagon_LD_GOT_11_X:
269 return ELF::R_HEX_LD_GOT_11_X;
270 case fixup_Hexagon_IE_32_6_X:
271 return ELF::R_HEX_IE_32_6_X;
272 case fixup_Hexagon_IE_16_X:
273 return ELF::R_HEX_IE_16_X;
274 case fixup_Hexagon_IE_GOT_32_6_X:
275 return ELF::R_HEX_IE_GOT_32_6_X;
276 case fixup_Hexagon_IE_GOT_16_X:
277 return ELF::R_HEX_IE_GOT_16_X;
278 case fixup_Hexagon_IE_GOT_11_X:
279 return ELF::R_HEX_IE_GOT_11_X;
280 case fixup_Hexagon_TPREL_32_6_X:
281 return ELF::R_HEX_TPREL_32_6_X;
282 case fixup_Hexagon_TPREL_16_X:
283 return ELF::R_HEX_TPREL_16_X;
284 case fixup_Hexagon_TPREL_11_X:
285 return ELF::R_HEX_TPREL_11_X;
286 case fixup_Hexagon_23_REG:
287 return ELF::R_HEX_23_REG;
288 case fixup_Hexagon_27_REG:
289 return ELF::R_HEX_27_REG;
290 case fixup_Hexagon_GD_PLT_B22_PCREL_X:
291 return ELF::R_HEX_GD_PLT_B22_PCREL_X;
292 case fixup_Hexagon_GD_PLT_B32_PCREL_X:
293 return ELF::R_HEX_GD_PLT_B32_PCREL_X;
294 case fixup_Hexagon_LD_PLT_B22_PCREL_X:
295 return ELF::R_HEX_LD_PLT_B22_PCREL_X;
296 case fixup_Hexagon_LD_PLT_B32_PCREL_X:
297 return ELF::R_HEX_LD_PLT_B32_PCREL_X;
298 }
299 }
300
301 std::unique_ptr<MCObjectTargetWriter>
createHexagonELFObjectWriter(uint8_t OSABI,StringRef CPU)302 llvm::createHexagonELFObjectWriter(uint8_t OSABI, StringRef CPU) {
303 return llvm::make_unique<HexagonELFObjectWriter>(OSABI, CPU);
304 }
305