1 //===-- Hexagon.cpp -------------------------------------------------------===//
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 "InputFiles.h"
10 #include "Symbols.h"
11 #include "SyntheticSections.h"
12 #include "Target.h"
13 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/Object/ELF.h"
16 #include "llvm/Support/Endian.h"
17
18 using namespace llvm;
19 using namespace llvm::object;
20 using namespace llvm::support::endian;
21 using namespace llvm::ELF;
22
23 namespace lld {
24 namespace elf {
25
26 namespace {
27 class Hexagon final : public TargetInfo {
28 public:
29 Hexagon();
30 uint32_t calcEFlags() const override;
31 RelExpr getRelExpr(RelType type, const Symbol &s,
32 const uint8_t *loc) const override;
33 RelType getDynRel(RelType type) const override;
34 void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
35 void writePltHeader(uint8_t *buf) const override;
36 void writePlt(uint8_t *buf, const Symbol &sym,
37 uint64_t pltEntryAddr) const override;
38 };
39 } // namespace
40
Hexagon()41 Hexagon::Hexagon() {
42 pltRel = R_HEX_JMP_SLOT;
43 relativeRel = R_HEX_RELATIVE;
44 gotRel = R_HEX_GLOB_DAT;
45 symbolicRel = R_HEX_32;
46
47 // The zero'th GOT entry is reserved for the address of _DYNAMIC. The
48 // next 3 are reserved for the dynamic loader.
49 gotPltHeaderEntriesNum = 4;
50
51 pltEntrySize = 16;
52 pltHeaderSize = 32;
53
54 // Hexagon Linux uses 64K pages by default.
55 defaultMaxPageSize = 0x10000;
56 noneRel = R_HEX_NONE;
57 tlsGotRel = R_HEX_TPREL_32;
58 }
59
calcEFlags() const60 uint32_t Hexagon::calcEFlags() const {
61 assert(!objectFiles.empty());
62
63 // The architecture revision must always be equal to or greater than
64 // greatest revision in the list of inputs.
65 uint32_t ret = 0;
66 for (InputFile *f : objectFiles) {
67 uint32_t eflags = cast<ObjFile<ELF32LE>>(f)->getObj().getHeader()->e_flags;
68 if (eflags > ret)
69 ret = eflags;
70 }
71 return ret;
72 }
73
applyMask(uint32_t mask,uint32_t data)74 static uint32_t applyMask(uint32_t mask, uint32_t data) {
75 uint32_t result = 0;
76 size_t off = 0;
77
78 for (size_t bit = 0; bit != 32; ++bit) {
79 uint32_t valBit = (data >> off) & 1;
80 uint32_t maskBit = (mask >> bit) & 1;
81 if (maskBit) {
82 result |= (valBit << bit);
83 ++off;
84 }
85 }
86 return result;
87 }
88
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const89 RelExpr Hexagon::getRelExpr(RelType type, const Symbol &s,
90 const uint8_t *loc) const {
91 switch (type) {
92 case R_HEX_NONE:
93 return R_NONE;
94 case R_HEX_6_X:
95 case R_HEX_8_X:
96 case R_HEX_9_X:
97 case R_HEX_10_X:
98 case R_HEX_11_X:
99 case R_HEX_12_X:
100 case R_HEX_16_X:
101 case R_HEX_32:
102 case R_HEX_32_6_X:
103 case R_HEX_HI16:
104 case R_HEX_LO16:
105 return R_ABS;
106 case R_HEX_B9_PCREL:
107 case R_HEX_B13_PCREL:
108 case R_HEX_B15_PCREL:
109 case R_HEX_6_PCREL_X:
110 case R_HEX_32_PCREL:
111 return R_PC;
112 case R_HEX_B9_PCREL_X:
113 case R_HEX_B15_PCREL_X:
114 case R_HEX_B22_PCREL:
115 case R_HEX_PLT_B22_PCREL:
116 case R_HEX_B22_PCREL_X:
117 case R_HEX_B32_PCREL_X:
118 return R_PLT_PC;
119 case R_HEX_IE_32_6_X:
120 case R_HEX_IE_16_X:
121 case R_HEX_IE_HI16:
122 case R_HEX_IE_LO16:
123 return R_GOT;
124 case R_HEX_GOTREL_11_X:
125 case R_HEX_GOTREL_16_X:
126 case R_HEX_GOTREL_32_6_X:
127 case R_HEX_GOTREL_HI16:
128 case R_HEX_GOTREL_LO16:
129 return R_GOTPLTREL;
130 case R_HEX_GOT_11_X:
131 case R_HEX_GOT_16_X:
132 case R_HEX_GOT_32_6_X:
133 return R_GOTPLT;
134 case R_HEX_IE_GOT_11_X:
135 case R_HEX_IE_GOT_16_X:
136 case R_HEX_IE_GOT_32_6_X:
137 case R_HEX_IE_GOT_HI16:
138 case R_HEX_IE_GOT_LO16:
139 config->hasStaticTlsModel = true;
140 return R_GOTPLT;
141 case R_HEX_TPREL_11_X:
142 case R_HEX_TPREL_16:
143 case R_HEX_TPREL_16_X:
144 case R_HEX_TPREL_32_6_X:
145 case R_HEX_TPREL_HI16:
146 case R_HEX_TPREL_LO16:
147 return R_TLS;
148 default:
149 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
150 ") against symbol " + toString(s));
151 return R_NONE;
152 }
153 }
154
findMaskR6(uint32_t insn)155 static uint32_t findMaskR6(uint32_t insn) {
156 // There are (arguably too) many relocation masks for the DSP's
157 // R_HEX_6_X type. The table below is used to select the correct mask
158 // for the given instruction.
159 struct InstructionMask {
160 uint32_t cmpMask;
161 uint32_t relocMask;
162 };
163
164 static const InstructionMask r6[] = {
165 {0x38000000, 0x0000201f}, {0x39000000, 0x0000201f},
166 {0x3e000000, 0x00001f80}, {0x3f000000, 0x00001f80},
167 {0x40000000, 0x000020f8}, {0x41000000, 0x000007e0},
168 {0x42000000, 0x000020f8}, {0x43000000, 0x000007e0},
169 {0x44000000, 0x000020f8}, {0x45000000, 0x000007e0},
170 {0x46000000, 0x000020f8}, {0x47000000, 0x000007e0},
171 {0x6a000000, 0x00001f80}, {0x7c000000, 0x001f2000},
172 {0x9a000000, 0x00000f60}, {0x9b000000, 0x00000f60},
173 {0x9c000000, 0x00000f60}, {0x9d000000, 0x00000f60},
174 {0x9f000000, 0x001f0100}, {0xab000000, 0x0000003f},
175 {0xad000000, 0x0000003f}, {0xaf000000, 0x00030078},
176 {0xd7000000, 0x006020e0}, {0xd8000000, 0x006020e0},
177 {0xdb000000, 0x006020e0}, {0xdf000000, 0x006020e0}};
178
179 // Duplex forms have a fixed mask and parse bits 15:14 are always
180 // zero. Non-duplex insns will always have at least one bit set in the
181 // parse field.
182 if ((0xC000 & insn) == 0x0)
183 return 0x03f00000;
184
185 for (InstructionMask i : r6)
186 if ((0xff000000 & insn) == i.cmpMask)
187 return i.relocMask;
188
189 error("unrecognized instruction for R_HEX_6 relocation: 0x" +
190 utohexstr(insn));
191 return 0;
192 }
193
findMaskR8(uint32_t insn)194 static uint32_t findMaskR8(uint32_t insn) {
195 if ((0xff000000 & insn) == 0xde000000)
196 return 0x00e020e8;
197 if ((0xff000000 & insn) == 0x3c000000)
198 return 0x0000207f;
199 return 0x00001fe0;
200 }
201
findMaskR11(uint32_t insn)202 static uint32_t findMaskR11(uint32_t insn) {
203 if ((0xff000000 & insn) == 0xa1000000)
204 return 0x060020ff;
205 return 0x06003fe0;
206 }
207
findMaskR16(uint32_t insn)208 static uint32_t findMaskR16(uint32_t insn) {
209 if ((0xff000000 & insn) == 0x48000000)
210 return 0x061f20ff;
211 if ((0xff000000 & insn) == 0x49000000)
212 return 0x061f3fe0;
213 if ((0xff000000 & insn) == 0x78000000)
214 return 0x00df3fe0;
215 if ((0xff000000 & insn) == 0xb0000000)
216 return 0x0fe03fe0;
217
218 error("unrecognized instruction for R_HEX_16_X relocation: 0x" +
219 utohexstr(insn));
220 return 0;
221 }
222
or32le(uint8_t * p,int32_t v)223 static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); }
224
relocateOne(uint8_t * loc,RelType type,uint64_t val) const225 void Hexagon::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
226 switch (type) {
227 case R_HEX_NONE:
228 break;
229 case R_HEX_6_PCREL_X:
230 case R_HEX_6_X:
231 or32le(loc, applyMask(findMaskR6(read32le(loc)), val));
232 break;
233 case R_HEX_8_X:
234 or32le(loc, applyMask(findMaskR8(read32le(loc)), val));
235 break;
236 case R_HEX_9_X:
237 or32le(loc, applyMask(0x00003fe0, val & 0x3f));
238 break;
239 case R_HEX_10_X:
240 or32le(loc, applyMask(0x00203fe0, val & 0x3f));
241 break;
242 case R_HEX_11_X:
243 case R_HEX_IE_GOT_11_X:
244 case R_HEX_GOT_11_X:
245 case R_HEX_GOTREL_11_X:
246 case R_HEX_TPREL_11_X:
247 or32le(loc, applyMask(findMaskR11(read32le(loc)), val & 0x3f));
248 break;
249 case R_HEX_12_X:
250 or32le(loc, applyMask(0x000007e0, val));
251 break;
252 case R_HEX_16_X: // These relocs only have 6 effective bits.
253 case R_HEX_IE_16_X:
254 case R_HEX_IE_GOT_16_X:
255 case R_HEX_GOT_16_X:
256 case R_HEX_GOTREL_16_X:
257 case R_HEX_TPREL_16_X:
258 or32le(loc, applyMask(findMaskR16(read32le(loc)), val & 0x3f));
259 break;
260 case R_HEX_TPREL_16:
261 or32le(loc, applyMask(findMaskR16(read32le(loc)), val & 0xffff));
262 break;
263 case R_HEX_32:
264 case R_HEX_32_PCREL:
265 or32le(loc, val);
266 break;
267 case R_HEX_32_6_X:
268 case R_HEX_GOT_32_6_X:
269 case R_HEX_GOTREL_32_6_X:
270 case R_HEX_IE_GOT_32_6_X:
271 case R_HEX_IE_32_6_X:
272 case R_HEX_TPREL_32_6_X:
273 or32le(loc, applyMask(0x0fff3fff, val >> 6));
274 break;
275 case R_HEX_B9_PCREL:
276 checkInt(loc, val, 11, type);
277 or32le(loc, applyMask(0x003000fe, val >> 2));
278 break;
279 case R_HEX_B9_PCREL_X:
280 or32le(loc, applyMask(0x003000fe, val & 0x3f));
281 break;
282 case R_HEX_B13_PCREL:
283 checkInt(loc, val, 15, type);
284 or32le(loc, applyMask(0x00202ffe, val >> 2));
285 break;
286 case R_HEX_B15_PCREL:
287 checkInt(loc, val, 17, type);
288 or32le(loc, applyMask(0x00df20fe, val >> 2));
289 break;
290 case R_HEX_B15_PCREL_X:
291 or32le(loc, applyMask(0x00df20fe, val & 0x3f));
292 break;
293 case R_HEX_B22_PCREL:
294 case R_HEX_PLT_B22_PCREL:
295 checkInt(loc, val, 22, type);
296 or32le(loc, applyMask(0x1ff3ffe, val >> 2));
297 break;
298 case R_HEX_B22_PCREL_X:
299 or32le(loc, applyMask(0x1ff3ffe, val & 0x3f));
300 break;
301 case R_HEX_B32_PCREL_X:
302 or32le(loc, applyMask(0x0fff3fff, val >> 6));
303 break;
304 case R_HEX_GOTREL_HI16:
305 case R_HEX_HI16:
306 case R_HEX_IE_GOT_HI16:
307 case R_HEX_IE_HI16:
308 case R_HEX_TPREL_HI16:
309 or32le(loc, applyMask(0x00c03fff, val >> 16));
310 break;
311 case R_HEX_GOTREL_LO16:
312 case R_HEX_LO16:
313 case R_HEX_IE_GOT_LO16:
314 case R_HEX_IE_LO16:
315 case R_HEX_TPREL_LO16:
316 or32le(loc, applyMask(0x00c03fff, val));
317 break;
318 default:
319 llvm_unreachable("unknown relocation");
320 }
321 }
322
writePltHeader(uint8_t * buf) const323 void Hexagon::writePltHeader(uint8_t *buf) const {
324 const uint8_t pltData[] = {
325 0x00, 0x40, 0x00, 0x00, // { immext (#0)
326 0x1c, 0xc0, 0x49, 0x6a, // r28 = add (pc, ##GOT0@PCREL) } # @GOT0
327 0x0e, 0x42, 0x9c, 0xe2, // { r14 -= add (r28, #16) # offset of GOTn
328 0x4f, 0x40, 0x9c, 0x91, // r15 = memw (r28 + #8) # object ID at GOT2
329 0x3c, 0xc0, 0x9c, 0x91, // r28 = memw (r28 + #4) }# dynamic link at GOT1
330 0x0e, 0x42, 0x0e, 0x8c, // { r14 = asr (r14, #2) # index of PLTn
331 0x00, 0xc0, 0x9c, 0x52, // jumpr r28 } # call dynamic linker
332 0x0c, 0xdb, 0x00, 0x54, // trap0(#0xdb) # bring plt0 into 16byte alignment
333 };
334 memcpy(buf, pltData, sizeof(pltData));
335
336 // Offset from PLT0 to the GOT.
337 uint64_t off = in.gotPlt->getVA() - in.plt->getVA();
338 relocateOne(buf, R_HEX_B32_PCREL_X, off);
339 relocateOne(buf + 4, R_HEX_6_PCREL_X, off);
340 }
341
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const342 void Hexagon::writePlt(uint8_t *buf, const Symbol &sym,
343 uint64_t pltEntryAddr) const {
344 const uint8_t inst[] = {
345 0x00, 0x40, 0x00, 0x00, // { immext (#0)
346 0x0e, 0xc0, 0x49, 0x6a, // r14 = add (pc, ##GOTn@PCREL) }
347 0x1c, 0xc0, 0x8e, 0x91, // r28 = memw (r14)
348 0x00, 0xc0, 0x9c, 0x52, // jumpr r28
349 };
350 memcpy(buf, inst, sizeof(inst));
351
352 uint64_t gotPltEntryAddr = sym.getGotPltVA();
353 relocateOne(buf, R_HEX_B32_PCREL_X, gotPltEntryAddr - pltEntryAddr);
354 relocateOne(buf + 4, R_HEX_6_PCREL_X, gotPltEntryAddr - pltEntryAddr);
355 }
356
getDynRel(RelType type) const357 RelType Hexagon::getDynRel(RelType type) const {
358 if (type == R_HEX_32)
359 return type;
360 return R_HEX_NONE;
361 }
362
getHexagonTargetInfo()363 TargetInfo *getHexagonTargetInfo() {
364 static Hexagon target;
365 return ⌖
366 }
367
368 } // namespace elf
369 } // namespace lld
370