1 //===-- Hexagon.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
12 #include "SyntheticSections.h"
14 #include "lld/Common/ErrorHandler.h"
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/Object/ELF.h"
17 #include "llvm/Support/Endian.h"
20 using namespace llvm::object;
21 using namespace llvm::support::endian;
22 using namespace llvm::ELF;
24 using namespace lld::elf;
27 class Hexagon final : public TargetInfo {
30 uint32_t calcEFlags() const override;
31 RelExpr getRelExpr(RelType Type, const Symbol &S,
32 const uint8_t *Loc) const override;
33 void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
34 void writePltHeader(uint8_t *Buf) const override;
35 void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
36 int32_t Index, unsigned RelOff) const override;
41 PltRel = R_HEX_JMP_SLOT;
42 RelativeRel = R_HEX_RELATIVE;
43 GotRel = R_HEX_GLOB_DAT;
45 // The zero'th GOT entry is reserved for the address of _DYNAMIC. The
46 // next 3 are reserved for the dynamic loader.
47 GotPltHeaderEntriesNum = 4;
53 // Hexagon Linux uses 64K pages by default.
54 DefaultMaxPageSize = 0x10000;
58 uint32_t Hexagon::calcEFlags() const {
59 assert(!ObjectFiles.empty());
61 // The architecture revision must always be equal to or greater than
62 // greatest revision in the list of inputs.
64 for (InputFile *F : ObjectFiles) {
65 uint32_t EFlags = cast<ObjFile<ELF32LE>>(F)->getObj().getHeader()->e_flags;
72 static uint32_t applyMask(uint32_t Mask, uint32_t Data) {
76 for (size_t Bit = 0; Bit != 32; ++Bit) {
77 uint32_t ValBit = (Data >> Off) & 1;
78 uint32_t MaskBit = (Mask >> Bit) & 1;
80 Result |= (ValBit << Bit);
87 RelExpr Hexagon::getRelExpr(RelType Type, const Symbol &S,
88 const uint8_t *Loc) const {
91 case R_HEX_B9_PCREL_X:
94 case R_HEX_B15_PCREL_X:
99 case R_HEX_PLT_B22_PCREL:
100 case R_HEX_B22_PCREL_X:
101 case R_HEX_B32_PCREL_X:
105 case R_HEX_GOT_32_6_X:
106 return R_HEXAGON_GOT;
112 static uint32_t findMaskR6(uint32_t Insn) {
113 // There are (arguably too) many relocation masks for the DSP's
114 // R_HEX_6_X type. The table below is used to select the correct mask
115 // for the given instruction.
116 struct InstructionMask {
121 static const InstructionMask R6[] = {
122 {0x38000000, 0x0000201f}, {0x39000000, 0x0000201f},
123 {0x3e000000, 0x00001f80}, {0x3f000000, 0x00001f80},
124 {0x40000000, 0x000020f8}, {0x41000000, 0x000007e0},
125 {0x42000000, 0x000020f8}, {0x43000000, 0x000007e0},
126 {0x44000000, 0x000020f8}, {0x45000000, 0x000007e0},
127 {0x46000000, 0x000020f8}, {0x47000000, 0x000007e0},
128 {0x6a000000, 0x00001f80}, {0x7c000000, 0x001f2000},
129 {0x9a000000, 0x00000f60}, {0x9b000000, 0x00000f60},
130 {0x9c000000, 0x00000f60}, {0x9d000000, 0x00000f60},
131 {0x9f000000, 0x001f0100}, {0xab000000, 0x0000003f},
132 {0xad000000, 0x0000003f}, {0xaf000000, 0x00030078},
133 {0xd7000000, 0x006020e0}, {0xd8000000, 0x006020e0},
134 {0xdb000000, 0x006020e0}, {0xdf000000, 0x006020e0}};
136 // Duplex forms have a fixed mask and parse bits 15:14 are always
137 // zero. Non-duplex insns will always have at least one bit set in the
139 if ((0xC000 & Insn) == 0x0)
142 for (InstructionMask I : R6)
143 if ((0xff000000 & Insn) == I.CmpMask)
146 error("unrecognized instruction for R_HEX_6 relocation: 0x" +
151 static uint32_t findMaskR8(uint32_t Insn) {
152 if ((0xff000000 & Insn) == 0xde000000)
154 if ((0xff000000 & Insn) == 0x3c000000)
159 static uint32_t findMaskR11(uint32_t Insn) {
160 if ((0xff000000 & Insn) == 0xa1000000)
165 static uint32_t findMaskR16(uint32_t Insn) {
166 if ((0xff000000 & Insn) == 0x48000000)
168 if ((0xff000000 & Insn) == 0x49000000)
170 if ((0xff000000 & Insn) == 0x78000000)
172 if ((0xff000000 & Insn) == 0xb0000000)
175 error("unrecognized instruction for R_HEX_16_X relocation: 0x" +
180 static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
182 void Hexagon::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
186 case R_HEX_6_PCREL_X:
188 or32le(Loc, applyMask(findMaskR6(read32le(Loc)), Val));
191 or32le(Loc, applyMask(findMaskR8(read32le(Loc)), Val));
194 or32le(Loc, applyMask(0x00003fe0, Val & 0x3f));
197 or32le(Loc, applyMask(0x00203fe0, Val & 0x3f));
201 or32le(Loc, applyMask(findMaskR11(read32le(Loc)), Val & 0x3f));
204 or32le(Loc, applyMask(0x000007e0, Val));
206 case R_HEX_16_X: // These relocs only have 6 effective bits.
208 or32le(Loc, applyMask(findMaskR16(read32le(Loc)), Val & 0x3f));
215 case R_HEX_GOT_32_6_X:
216 or32le(Loc, applyMask(0x0fff3fff, Val >> 6));
219 or32le(Loc, applyMask(0x003000fe, Val >> 2));
221 case R_HEX_B9_PCREL_X:
222 or32le(Loc, applyMask(0x003000fe, Val & 0x3f));
224 case R_HEX_B13_PCREL:
225 or32le(Loc, applyMask(0x00202ffe, Val >> 2));
227 case R_HEX_B15_PCREL:
228 or32le(Loc, applyMask(0x00df20fe, Val >> 2));
230 case R_HEX_B15_PCREL_X:
231 or32le(Loc, applyMask(0x00df20fe, Val & 0x3f));
233 case R_HEX_B22_PCREL:
234 case R_HEX_PLT_B22_PCREL:
235 or32le(Loc, applyMask(0x1ff3ffe, Val >> 2));
237 case R_HEX_B22_PCREL_X:
238 or32le(Loc, applyMask(0x1ff3ffe, Val & 0x3f));
240 case R_HEX_B32_PCREL_X:
241 or32le(Loc, applyMask(0x0fff3fff, Val >> 6));
244 or32le(Loc, applyMask(0x00c03fff, Val >> 16));
247 or32le(Loc, applyMask(0x00c03fff, Val));
250 error(getErrorLocation(Loc) + "unrecognized reloc " + toString(Type));
255 void Hexagon::writePltHeader(uint8_t *Buf) const {
256 const uint8_t PltData[] = {
257 0x00, 0x40, 0x00, 0x00, // { immext (#0)
258 0x1c, 0xc0, 0x49, 0x6a, // r28 = add (pc, ##GOT0@PCREL) } # @GOT0
259 0x0e, 0x42, 0x9c, 0xe2, // { r14 -= add (r28, #16) # offset of GOTn
260 0x4f, 0x40, 0x9c, 0x91, // r15 = memw (r28 + #8) # object ID at GOT2
261 0x3c, 0xc0, 0x9c, 0x91, // r28 = memw (r28 + #4) }# dynamic link at GOT1
262 0x0e, 0x42, 0x0e, 0x8c, // { r14 = asr (r14, #2) # index of PLTn
263 0x00, 0xc0, 0x9c, 0x52, // jumpr r28 } # call dynamic linker
264 0x0c, 0xdb, 0x00, 0x54, // trap0(#0xdb) # bring plt0 into 16byte alignment
266 memcpy(Buf, PltData, sizeof(PltData));
268 // Offset from PLT0 to the GOT.
269 uint64_t Off = In.GotPlt->getVA() - In.Plt->getVA();
270 relocateOne(Buf, R_HEX_B32_PCREL_X, Off);
271 relocateOne(Buf + 4, R_HEX_6_PCREL_X, Off);
274 void Hexagon::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
275 uint64_t PltEntryAddr, int32_t Index,
276 unsigned RelOff) const {
277 const uint8_t Inst[] = {
278 0x00, 0x40, 0x00, 0x00, // { immext (#0)
279 0x0e, 0xc0, 0x49, 0x6a, // r14 = add (pc, ##GOTn@PCREL) }
280 0x1c, 0xc0, 0x8e, 0x91, // r28 = memw (r14)
281 0x00, 0xc0, 0x9c, 0x52, // jumpr r28
283 memcpy(Buf, Inst, sizeof(Inst));
285 relocateOne(Buf, R_HEX_B32_PCREL_X, GotPltEntryAddr - PltEntryAddr);
286 relocateOne(Buf + 4, R_HEX_6_PCREL_X, GotPltEntryAddr - PltEntryAddr);
289 TargetInfo *elf::getHexagonTargetInfo() {
290 static Hexagon Target;