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 "OutputSections.h"
11#include "RelocScan.h"
12#include "Symbols.h"
13#include "SyntheticSections.h"
14#include "Target.h"
15#include "Thunks.h"
16#include "lld/Common/ErrorHandler.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/BinaryFormat/ELF.h"
19#include "llvm/Support/ELFAttributes.h"
20#include "llvm/Support/Endian.h"
21#include "llvm/Support/HexagonAttributeParser.h"
22#include "llvm/Support/HexagonAttributes.h"
23#include "llvm/Support/LEB128.h"
24
25using namespace llvm;
26using namespace llvm::object;
27using namespace llvm::support::endian;
28using namespace llvm::ELF;
29using namespace lld;
30using namespace lld::elf;
31
32namespace {
33class Hexagon final : public TargetInfo {
34public:
35 Hexagon(Ctx &);
36 uint32_t calcEFlags() const override;
37 RelExpr getRelExpr(RelType type, const Symbol &s,
38 const uint8_t *loc) const override;
39 RelType getDynRel(RelType type) const override;
40 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
41 template <class ELFT, class RelTy>
42 void scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels);
43 void scanSection(InputSectionBase &sec) override {
44 elf::scanSection1<Hexagon, ELF32LE>(target&: *this, sec);
45 }
46 bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
47 uint64_t branchAddr, const Symbol &s,
48 int64_t a) const override;
49 bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
50 void relocate(uint8_t *loc, const Relocation &rel,
51 uint64_t val) const override;
52 void writePltHeader(uint8_t *buf) const override;
53 void writePlt(uint8_t *buf, const Symbol &sym,
54 uint64_t pltEntryAddr) const override;
55};
56} // namespace
57
58Hexagon::Hexagon(Ctx &ctx) : TargetInfo(ctx) {
59 pltRel = R_HEX_JMP_SLOT;
60 relativeRel = R_HEX_RELATIVE;
61 gotRel = R_HEX_GLOB_DAT;
62 symbolicRel = R_HEX_32;
63
64 gotBaseSymInGotPlt = true;
65 // The zero'th GOT entry is reserved for the address of _DYNAMIC. The
66 // next 3 are reserved for the dynamic loader.
67 gotPltHeaderEntriesNum = 4;
68
69 pltEntrySize = 16;
70 pltHeaderSize = 32;
71
72 // Hexagon Linux uses 64K pages by default.
73 defaultMaxPageSize = 0x10000;
74 tlsGotRel = R_HEX_TPREL_32;
75 tlsModuleIndexRel = R_HEX_DTPMOD_32;
76 tlsOffsetRel = R_HEX_DTPREL_32;
77
78 needsThunks = true;
79}
80
81uint32_t Hexagon::calcEFlags() const {
82 // The architecture revision must always be equal to or greater than
83 // greatest revision in the list of inputs.
84 std::optional<uint32_t> ret;
85 for (InputFile *f : ctx.objectFiles) {
86 uint32_t eflags = cast<ObjFile<ELF32LE>>(Val: f)->getObj().getHeader().e_flags;
87 if (!ret || eflags > *ret)
88 ret = eflags;
89 }
90 return ret.value_or(/* Default Arch Rev: */ u: EF_HEXAGON_MACH_V68);
91}
92
93static uint32_t applyMask(uint32_t mask, uint32_t data) {
94 uint32_t result = 0;
95 size_t off = 0;
96
97 for (size_t bit = 0; bit != 32; ++bit) {
98 uint32_t valBit = (data >> off) & 1;
99 uint32_t maskBit = (mask >> bit) & 1;
100 if (maskBit) {
101 result |= (valBit << bit);
102 ++off;
103 }
104 }
105 return result;
106}
107
108// Only needed to support relocations used by relocateNonAlloc and
109// preprocessRelocs.
110RelExpr Hexagon::getRelExpr(RelType type, const Symbol &s,
111 const uint8_t *loc) const {
112 switch (type) {
113 case R_HEX_NONE:
114 return R_NONE;
115 case R_HEX_32:
116 return R_ABS;
117 case R_HEX_32_PCREL:
118 return R_PC;
119 default:
120 Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation (" << type.v
121 << ") against symbol " << &s;
122 return R_NONE;
123 }
124}
125
126template <class ELFT, class RelTy>
127void Hexagon::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
128 RelocScan rs(ctx, &sec);
129 sec.relocations.reserve(N: rels.size());
130 for (auto it = rels.begin(); it != rels.end(); ++it) {
131 const RelTy &rel = *it;
132 uint32_t symIdx = rel.getSymbol(false);
133 Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIdx);
134 uint64_t offset = rel.r_offset;
135 RelType type = rel.getType(false);
136 if (sym.isUndefined() && symIdx != 0 &&
137 rs.maybeReportUndefined(sym&: cast<Undefined>(Val&: sym), offset))
138 continue;
139 int64_t addend = rs.getAddend<ELFT>(rel, type);
140 RelExpr expr;
141 // Relocation types that only need a RelExpr set `expr` and break out of
142 // the switch to reach rs.process(). Types that need special handling
143 // (fast-path helpers, TLS) call a handler and use `continue`.
144 switch (type) {
145 case R_HEX_NONE:
146 continue;
147
148 // Absolute relocations:
149 case R_HEX_6_X:
150 case R_HEX_8_X:
151 case R_HEX_9_X:
152 case R_HEX_10_X:
153 case R_HEX_11_X:
154 case R_HEX_12_X:
155 case R_HEX_16_X:
156 case R_HEX_32:
157 case R_HEX_32_6_X:
158 case R_HEX_HI16:
159 case R_HEX_LO16:
160 case R_HEX_DTPREL_32:
161 expr = R_ABS;
162 break;
163
164 // PC-relative relocations:
165 case R_HEX_B9_PCREL:
166 case R_HEX_B13_PCREL:
167 case R_HEX_B15_PCREL:
168 case R_HEX_6_PCREL_X:
169 case R_HEX_32_PCREL:
170 rs.processR_PC(type, offset, addend, sym);
171 continue;
172
173 // PLT-generating relocations:
174 case R_HEX_B9_PCREL_X:
175 case R_HEX_B15_PCREL_X:
176 case R_HEX_B22_PCREL:
177 case R_HEX_PLT_B22_PCREL:
178 case R_HEX_B22_PCREL_X:
179 case R_HEX_B32_PCREL_X:
180 rs.processR_PLT_PC(type, offset, addend, sym);
181 continue;
182 case R_HEX_GD_PLT_B22_PCREL:
183 case R_HEX_GD_PLT_B22_PCREL_X:
184 case R_HEX_GD_PLT_B32_PCREL_X:
185 sym.setFlags(NEEDS_PLT);
186 sec.addReloc(r: {.expr: R_PLT_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
187 continue;
188
189 // GOT-generating relocations:
190 case R_HEX_GOT_11_X:
191 case R_HEX_GOT_16_X:
192 case R_HEX_GOT_32_6_X:
193 ctx.in.gotPlt->hasGotPltOffRel.store(i: true, m: std::memory_order_relaxed);
194 expr = R_GOTPLT;
195 break;
196
197 // GOTREL relocations:
198 case R_HEX_GOTREL_11_X:
199 case R_HEX_GOTREL_16_X:
200 case R_HEX_GOTREL_32_6_X:
201 case R_HEX_GOTREL_HI16:
202 case R_HEX_GOTREL_LO16:
203 ctx.in.gotPlt->hasGotPltOffRel.store(i: true, m: std::memory_order_relaxed);
204 expr = R_GOTPLTREL;
205 break;
206
207 // TLS relocations:
208 case R_HEX_TPREL_11_X:
209 case R_HEX_TPREL_16:
210 case R_HEX_TPREL_16_X:
211 case R_HEX_TPREL_32_6_X:
212 case R_HEX_TPREL_HI16:
213 case R_HEX_TPREL_LO16:
214 if (rs.checkTlsLe(offset, sym, type))
215 continue;
216 expr = R_TPREL;
217 break;
218 case R_HEX_IE_32_6_X:
219 case R_HEX_IE_16_X:
220 case R_HEX_IE_HI16:
221 case R_HEX_IE_LO16:
222 // There is no IE to LE optimization.
223 rs.handleTlsIe<false>(ieExpr: R_GOT, type, offset, addend, sym);
224 continue;
225 case R_HEX_IE_GOT_11_X:
226 case R_HEX_IE_GOT_16_X:
227 case R_HEX_IE_GOT_32_6_X:
228 case R_HEX_IE_GOT_HI16:
229 case R_HEX_IE_GOT_LO16:
230 ctx.in.gotPlt->hasGotPltOffRel.store(i: true, m: std::memory_order_relaxed);
231 rs.handleTlsIe<false>(ieExpr: R_GOTPLT, type, offset, addend, sym);
232 continue;
233 case R_HEX_GD_GOT_11_X:
234 case R_HEX_GD_GOT_16_X:
235 case R_HEX_GD_GOT_32_6_X:
236 sym.setFlags(NEEDS_TLSGD);
237 ctx.in.gotPlt->hasGotPltOffRel.store(i: true, m: std::memory_order_relaxed);
238 sec.addReloc(r: {.expr: R_TLSGD_GOTPLT, .type: type, .offset: offset, .addend: addend, .sym: &sym});
239 continue;
240
241 default:
242 Err(ctx) << getErrorLoc(ctx, loc: sec.content().data() + offset)
243 << "unknown relocation (" << type.v << ") against symbol "
244 << &sym;
245 continue;
246 }
247 rs.process(expr, type, offset, sym, addend);
248 }
249}
250
251// There are (arguably too) many relocation masks for the DSP's
252// R_HEX_6_X type. The table below is used to select the correct mask
253// for the given instruction.
254struct InstructionMask {
255 uint32_t cmpMask;
256 uint32_t relocMask;
257};
258static const InstructionMask r6[] = {
259 {.cmpMask: 0x38000000, .relocMask: 0x0000201f}, {.cmpMask: 0x39000000, .relocMask: 0x0000201f},
260 {.cmpMask: 0x3e000000, .relocMask: 0x00001f80}, {.cmpMask: 0x3f000000, .relocMask: 0x00001f80},
261 {.cmpMask: 0x40000000, .relocMask: 0x000020f8}, {.cmpMask: 0x41000000, .relocMask: 0x000007e0},
262 {.cmpMask: 0x42000000, .relocMask: 0x000020f8}, {.cmpMask: 0x43000000, .relocMask: 0x000007e0},
263 {.cmpMask: 0x44000000, .relocMask: 0x000020f8}, {.cmpMask: 0x45000000, .relocMask: 0x000007e0},
264 {.cmpMask: 0x46000000, .relocMask: 0x000020f8}, {.cmpMask: 0x47000000, .relocMask: 0x000007e0},
265 {.cmpMask: 0x6a000000, .relocMask: 0x00001f80}, {.cmpMask: 0x7c000000, .relocMask: 0x001f2000},
266 {.cmpMask: 0x9a000000, .relocMask: 0x00000f60}, {.cmpMask: 0x9b000000, .relocMask: 0x00000f60},
267 {.cmpMask: 0x9c000000, .relocMask: 0x00000f60}, {.cmpMask: 0x9d000000, .relocMask: 0x00000f60},
268 {.cmpMask: 0x9f000000, .relocMask: 0x001f0100}, {.cmpMask: 0xab000000, .relocMask: 0x0000003f},
269 {.cmpMask: 0xad000000, .relocMask: 0x0000003f}, {.cmpMask: 0xaf000000, .relocMask: 0x00030078},
270 {.cmpMask: 0xd7000000, .relocMask: 0x006020e0}, {.cmpMask: 0xd8000000, .relocMask: 0x006020e0},
271 {.cmpMask: 0xdb000000, .relocMask: 0x006020e0}, {.cmpMask: 0xdf000000, .relocMask: 0x006020e0}};
272
273constexpr uint32_t instParsePacketEnd = 0x0000c000;
274
275static bool isDuplex(uint32_t insn) {
276 // Duplex forms have a fixed mask and parse bits 15:14 are always
277 // zero. Non-duplex insns will always have at least one bit set in the
278 // parse field.
279 return (instParsePacketEnd & insn) == 0;
280}
281
282static uint32_t findMaskR6(Ctx &ctx, uint32_t insn) {
283 if (isDuplex(insn))
284 return 0x03f00000;
285
286 for (InstructionMask i : r6)
287 if ((0xff000000 & insn) == i.cmpMask)
288 return i.relocMask;
289
290 Err(ctx) << "unrecognized instruction for 6_X relocation: 0x"
291 << utohexstr(X: insn, LowerCase: true);
292 return 0;
293}
294
295static uint32_t findMaskR8(uint32_t insn) {
296 if ((0xff000000 & insn) == 0xde000000)
297 return 0x00e020e8;
298 if ((0xff000000 & insn) == 0x3c000000)
299 return 0x0000207f;
300 return 0x00001fe0;
301}
302
303static uint32_t findMaskR11(uint32_t insn) {
304 if (isDuplex(insn))
305 return 0x03f00000;
306 if ((0xff000000 & insn) == 0xa1000000)
307 return 0x060020ff;
308 return 0x06003fe0;
309}
310
311static uint32_t findMaskR16(Ctx &ctx, uint32_t insn) {
312 if (isDuplex(insn))
313 return 0x03f00000;
314
315 // Clear the end-packet-parse bits:
316 insn = insn & ~instParsePacketEnd;
317
318 if ((0xff000000 & insn) == 0x48000000)
319 return 0x061f20ff;
320 if ((0xff000000 & insn) == 0x49000000)
321 return 0x061f3fe0;
322 if ((0xff000000 & insn) == 0x78000000)
323 return 0x00df3fe0;
324 if ((0xff000000 & insn) == 0xb0000000)
325 return 0x0fe03fe0;
326
327 if ((0xff802000 & insn) == 0x74000000)
328 return 0x00001fe0;
329 if ((0xff802000 & insn) == 0x74002000)
330 return 0x00001fe0;
331 if ((0xff802000 & insn) == 0x74800000)
332 return 0x00001fe0;
333 if ((0xff802000 & insn) == 0x74802000)
334 return 0x00001fe0;
335
336 for (InstructionMask i : r6)
337 if ((0xff000000 & insn) == i.cmpMask)
338 return i.relocMask;
339
340 Err(ctx) << "unrecognized instruction for 16_X type: 0x" << utohexstr(X: insn);
341 return 0;
342}
343
344static void or32le(uint8_t *p, int32_t v) { write32le(P: p, V: read32le(P: p) | v); }
345
346bool Hexagon::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
347 int64_t offset = dst - src;
348 switch (type) {
349 case llvm::ELF::R_HEX_B22_PCREL:
350 case llvm::ELF::R_HEX_PLT_B22_PCREL:
351 case llvm::ELF::R_HEX_GD_PLT_B22_PCREL:
352 case llvm::ELF::R_HEX_LD_PLT_B22_PCREL:
353 return llvm::isInt<22>(x: offset >> 2);
354 case llvm::ELF::R_HEX_B15_PCREL:
355 return llvm::isInt<15>(x: offset >> 2);
356 break;
357 case llvm::ELF::R_HEX_B13_PCREL:
358 return llvm::isInt<13>(x: offset >> 2);
359 break;
360 case llvm::ELF::R_HEX_B9_PCREL:
361 return llvm::isInt<9>(x: offset >> 2);
362 default:
363 return true;
364 }
365 llvm_unreachable("unsupported relocation");
366}
367
368bool Hexagon::needsThunk(RelExpr expr, RelType type, const InputFile *file,
369 uint64_t branchAddr, const Symbol &s,
370 int64_t a) const {
371 // Only check branch range for supported branch relocation types
372 switch (type) {
373 case R_HEX_B22_PCREL:
374 case R_HEX_PLT_B22_PCREL:
375 case R_HEX_GD_PLT_B22_PCREL:
376 case R_HEX_LD_PLT_B22_PCREL:
377 case R_HEX_B15_PCREL:
378 case R_HEX_B13_PCREL:
379 case R_HEX_B9_PCREL:
380 return !ctx.target->inBranchRange(type, src: branchAddr, dst: s.getVA(ctx, addend: a));
381 default:
382 return false;
383 }
384}
385
386void Hexagon::relocate(uint8_t *loc, const Relocation &rel,
387 uint64_t val) const {
388 switch (rel.type) {
389 case R_HEX_NONE:
390 break;
391 case R_HEX_6_PCREL_X:
392 case R_HEX_6_X:
393 or32le(p: loc, v: applyMask(mask: findMaskR6(ctx, insn: read32le(P: loc)), data: val));
394 break;
395 case R_HEX_8_X:
396 or32le(p: loc, v: applyMask(mask: findMaskR8(insn: read32le(P: loc)), data: val));
397 break;
398 case R_HEX_9_X:
399 or32le(p: loc, v: applyMask(mask: 0x00003fe0, data: val & 0x3f));
400 break;
401 case R_HEX_10_X:
402 or32le(p: loc, v: applyMask(mask: 0x00203fe0, data: val & 0x3f));
403 break;
404 case R_HEX_11_X:
405 case R_HEX_GD_GOT_11_X:
406 case R_HEX_IE_GOT_11_X:
407 case R_HEX_GOT_11_X:
408 case R_HEX_GOTREL_11_X:
409 case R_HEX_TPREL_11_X:
410 or32le(p: loc, v: applyMask(mask: findMaskR11(insn: read32le(P: loc)), data: val & 0x3f));
411 break;
412 case R_HEX_12_X:
413 or32le(p: loc, v: applyMask(mask: 0x000007e0, data: val));
414 break;
415 case R_HEX_16_X: // These relocs only have 6 effective bits.
416 case R_HEX_IE_16_X:
417 case R_HEX_IE_GOT_16_X:
418 case R_HEX_GD_GOT_16_X:
419 case R_HEX_GOT_16_X:
420 case R_HEX_GOTREL_16_X:
421 case R_HEX_TPREL_16_X:
422 or32le(p: loc, v: applyMask(mask: findMaskR16(ctx, insn: read32le(P: loc)), data: val & 0x3f));
423 break;
424 case R_HEX_TPREL_16:
425 or32le(p: loc, v: applyMask(mask: findMaskR16(ctx, insn: read32le(P: loc)), data: val & 0xffff));
426 break;
427 case R_HEX_32:
428 case R_HEX_32_PCREL:
429 case R_HEX_DTPREL_32:
430 or32le(p: loc, v: val);
431 break;
432 case R_HEX_32_6_X:
433 case R_HEX_GD_GOT_32_6_X:
434 case R_HEX_GOT_32_6_X:
435 case R_HEX_GOTREL_32_6_X:
436 case R_HEX_IE_GOT_32_6_X:
437 case R_HEX_IE_32_6_X:
438 case R_HEX_TPREL_32_6_X:
439 or32le(p: loc, v: applyMask(mask: 0x0fff3fff, data: val >> 6));
440 break;
441 case R_HEX_B9_PCREL:
442 checkInt(ctx, loc, v: val, n: 11, rel);
443 or32le(p: loc, v: applyMask(mask: 0x003000fe, data: val >> 2));
444 break;
445 case R_HEX_B9_PCREL_X:
446 or32le(p: loc, v: applyMask(mask: 0x003000fe, data: val & 0x3f));
447 break;
448 case R_HEX_B13_PCREL:
449 checkInt(ctx, loc, v: val, n: 15, rel);
450 or32le(p: loc, v: applyMask(mask: 0x00202ffe, data: val >> 2));
451 break;
452 case R_HEX_B15_PCREL:
453 checkInt(ctx, loc, v: val, n: 17, rel);
454 or32le(p: loc, v: applyMask(mask: 0x00df20fe, data: val >> 2));
455 break;
456 case R_HEX_B15_PCREL_X:
457 or32le(p: loc, v: applyMask(mask: 0x00df20fe, data: val & 0x3f));
458 break;
459 case R_HEX_B22_PCREL:
460 case R_HEX_GD_PLT_B22_PCREL:
461 case R_HEX_PLT_B22_PCREL:
462 checkInt(ctx, loc, v: val, n: 24, rel);
463 or32le(p: loc, v: applyMask(mask: 0x1ff3ffe, data: val >> 2));
464 break;
465 case R_HEX_B22_PCREL_X:
466 case R_HEX_GD_PLT_B22_PCREL_X:
467 or32le(p: loc, v: applyMask(mask: 0x1ff3ffe, data: val & 0x3f));
468 break;
469 case R_HEX_B32_PCREL_X:
470 case R_HEX_GD_PLT_B32_PCREL_X:
471 or32le(p: loc, v: applyMask(mask: 0x0fff3fff, data: val >> 6));
472 break;
473 case R_HEX_GOTREL_HI16:
474 case R_HEX_HI16:
475 case R_HEX_IE_GOT_HI16:
476 case R_HEX_IE_HI16:
477 case R_HEX_TPREL_HI16:
478 or32le(p: loc, v: applyMask(mask: 0x00c03fff, data: val >> 16));
479 break;
480 case R_HEX_GOTREL_LO16:
481 case R_HEX_LO16:
482 case R_HEX_IE_GOT_LO16:
483 case R_HEX_IE_LO16:
484 case R_HEX_TPREL_LO16:
485 or32le(p: loc, v: applyMask(mask: 0x00c03fff, data: val));
486 break;
487 default:
488 llvm_unreachable("unknown relocation");
489 }
490}
491
492void Hexagon::writePltHeader(uint8_t *buf) const {
493 const uint8_t pltData[] = {
494 0x00, 0x40, 0x00, 0x00, // { immext (#0)
495 0x1c, 0xc0, 0x49, 0x6a, // r28 = add (pc, ##GOT0@PCREL) } # @GOT0
496 0x0e, 0x42, 0x9c, 0xe2, // { r14 -= add (r28, #16) # offset of GOTn
497 0x4f, 0x40, 0x9c, 0x91, // r15 = memw (r28 + #8) # object ID at GOT2
498 0x3c, 0xc0, 0x9c, 0x91, // r28 = memw (r28 + #4) }# dynamic link at GOT1
499 0x0e, 0x42, 0x0e, 0x8c, // { r14 = asr (r14, #2) # index of PLTn
500 0x00, 0xc0, 0x9c, 0x52, // jumpr r28 } # call dynamic linker
501 0x0c, 0xdb, 0x00, 0x54, // trap0(#0xdb) # bring plt0 into 16byte alignment
502 };
503 memcpy(dest: buf, src: pltData, n: sizeof(pltData));
504
505 // Offset from PLT0 to the GOT.
506 uint64_t off = ctx.in.gotPlt->getVA() - ctx.in.plt->getVA();
507 relocateNoSym(loc: buf, type: R_HEX_B32_PCREL_X, val: off);
508 relocateNoSym(loc: buf + 4, type: R_HEX_6_PCREL_X, val: off);
509}
510
511void Hexagon::writePlt(uint8_t *buf, const Symbol &sym,
512 uint64_t pltEntryAddr) const {
513 const uint8_t inst[] = {
514 0x00, 0x40, 0x00, 0x00, // { immext (#0)
515 0x0e, 0xc0, 0x49, 0x6a, // r14 = add (pc, ##GOTn@PCREL) }
516 0x1c, 0xc0, 0x8e, 0x91, // r28 = memw (r14)
517 0x00, 0xc0, 0x9c, 0x52, // jumpr r28
518 };
519 memcpy(dest: buf, src: inst, n: sizeof(inst));
520
521 uint64_t gotPltEntryAddr = sym.getGotPltVA(ctx);
522 relocateNoSym(loc: buf, type: R_HEX_B32_PCREL_X, val: gotPltEntryAddr - pltEntryAddr);
523 relocateNoSym(loc: buf + 4, type: R_HEX_6_PCREL_X, val: gotPltEntryAddr - pltEntryAddr);
524}
525
526RelType Hexagon::getDynRel(RelType type) const {
527 if (type == R_HEX_32)
528 return type;
529 return R_HEX_NONE;
530}
531
532int64_t Hexagon::getImplicitAddend(const uint8_t *buf, RelType type) const {
533 switch (type) {
534 case R_HEX_NONE:
535 case R_HEX_GLOB_DAT:
536 case R_HEX_JMP_SLOT:
537 return 0;
538 case R_HEX_32:
539 case R_HEX_RELATIVE:
540 case R_HEX_DTPMOD_32:
541 case R_HEX_DTPREL_32:
542 case R_HEX_TPREL_32:
543 return SignExtend64<32>(x: read32(ctx, p: buf));
544 default:
545 InternalErr(ctx, buf) << "cannot read addend for relocation " << type;
546 return 0;
547 }
548}
549
550namespace {
551class HexagonAttributesSection final : public SyntheticSection {
552public:
553 HexagonAttributesSection(Ctx &ctx)
554 : SyntheticSection(ctx, ".hexagon.attributes", SHT_HEXAGON_ATTRIBUTES, 0,
555 1) {}
556
557 size_t getSize() const override { return size; }
558 void writeTo(uint8_t *buf) override;
559
560 static constexpr StringRef vendor = "hexagon";
561 DenseMap<unsigned, unsigned> intAttr;
562 size_t size = 0;
563};
564} // namespace
565
566static HexagonAttributesSection *
567mergeAttributesSection(Ctx &ctx,
568 const SmallVector<InputSectionBase *, 0> &sections) {
569 ctx.in.hexagonAttributes = std::make_unique<HexagonAttributesSection>(args&: ctx);
570 auto &merged =
571 static_cast<HexagonAttributesSection &>(*ctx.in.hexagonAttributes);
572
573 // Collect all tags values from attributes section.
574 const auto &attributesTags = HexagonAttrs::getHexagonAttributeTags();
575 for (const InputSectionBase *sec : sections) {
576 HexagonAttributeParser parser;
577 if (Error e = parser.parse(section: sec->content(), endian: llvm::endianness::little))
578 Warn(ctx) << sec << ": " << std::move(e);
579 for (const auto &tag : attributesTags) {
580 switch (HexagonAttrs::AttrType(tag.attr)) {
581 case HexagonAttrs::ARCH:
582 case HexagonAttrs::HVXARCH:
583 if (auto i = parser.getAttributeValue(tag: tag.attr)) {
584 auto r = merged.intAttr.try_emplace(Key: tag.attr, Args&: *i);
585 if (!r.second)
586 if (r.first->second < *i)
587 r.first->second = *i;
588 }
589 continue;
590
591 case HexagonAttrs::HVXIEEEFP:
592 case HexagonAttrs::HVXQFLOAT:
593 case HexagonAttrs::ZREG:
594 case HexagonAttrs::AUDIO:
595 case HexagonAttrs::CABAC:
596 if (auto i = parser.getAttributeValue(tag: tag.attr)) {
597 auto r = merged.intAttr.try_emplace(Key: tag.attr, Args&: *i);
598 if (!r.second && r.first->second != *i) {
599 r.first->second |= *i;
600 }
601 }
602 continue;
603 }
604 }
605 }
606
607 // The total size of headers: format-version [ <section-length> "vendor-name"
608 // [ <file-tag> <size>.
609 size_t size = 5 + merged.vendor.size() + 1 + 5;
610 for (auto &attr : merged.intAttr)
611 if (attr.second != 0)
612 size += getULEB128Size(Value: attr.first) + getULEB128Size(Value: attr.second);
613 merged.size = size;
614 return &merged;
615}
616
617void HexagonAttributesSection::writeTo(uint8_t *buf) {
618 const size_t size = getSize();
619 uint8_t *const end = buf + size;
620 *buf = ELFAttrs::Format_Version;
621 write32(ctx, p: buf + 1, v: size - 1);
622 buf += 5;
623
624 memcpy(dest: buf, src: vendor.data(), n: vendor.size());
625 buf += vendor.size() + 1;
626
627 *buf = ELFAttrs::File;
628 write32(ctx, p: buf + 1, v: end - buf);
629 buf += 5;
630
631 for (auto &attr : intAttr) {
632 if (attr.second == 0)
633 continue;
634 buf += encodeULEB128(Value: attr.first, p: buf);
635 buf += encodeULEB128(Value: attr.second, p: buf);
636 }
637}
638
639void elf::mergeHexagonAttributesSections(Ctx &ctx) {
640 // Find the first input SHT_HEXAGON_ATTRIBUTES; return if not found.
641 size_t place =
642 llvm::find_if(Range&: ctx.inputSections,
643 P: [](auto *s) { return s->type == SHT_HEXAGON_ATTRIBUTES; }) -
644 ctx.inputSections.begin();
645 if (place == ctx.inputSections.size())
646 return;
647
648 // Extract all SHT_HEXAGON_ATTRIBUTES sections into `sections`.
649 SmallVector<InputSectionBase *, 0> sections;
650 llvm::erase_if(C&: ctx.inputSections, P: [&](InputSectionBase *s) {
651 if (s->type != SHT_HEXAGON_ATTRIBUTES)
652 return false;
653 sections.push_back(Elt: s);
654 return true;
655 });
656
657 // Add the merged section.
658 ctx.inputSections.insert(I: ctx.inputSections.begin() + place,
659 Elt: mergeAttributesSection(ctx, sections));
660}
661
662void elf::setHexagonTargetInfo(Ctx &ctx) { ctx.target.reset(p: new Hexagon(ctx)); }
663