1336821Sdim//===-- Hexagon.cpp -------------------------------------------------------===//
2336821Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6336821Sdim//
7336821Sdim//===----------------------------------------------------------------------===//
8336821Sdim
9336821Sdim#include "InputFiles.h"
10336821Sdim#include "Symbols.h"
11344779Sdim#include "SyntheticSections.h"
12336821Sdim#include "Target.h"
13336821Sdim#include "lld/Common/ErrorHandler.h"
14336821Sdim#include "llvm/BinaryFormat/ELF.h"
15336821Sdim#include "llvm/Object/ELF.h"
16336821Sdim#include "llvm/Support/Endian.h"
17336821Sdim
18336821Sdimusing namespace llvm;
19336821Sdimusing namespace llvm::object;
20336821Sdimusing namespace llvm::support::endian;
21336821Sdimusing namespace llvm::ELF;
22336821Sdim
23360784Sdimnamespace lld {
24360784Sdimnamespace elf {
25360784Sdim
26336821Sdimnamespace {
27336821Sdimclass Hexagon final : public TargetInfo {
28336821Sdimpublic:
29344779Sdim  Hexagon();
30336821Sdim  uint32_t calcEFlags() const override;
31353358Sdim  RelExpr getRelExpr(RelType type, const Symbol &s,
32353358Sdim                     const uint8_t *loc) const override;
33360784Sdim  RelType getDynRel(RelType type) const override;
34353358Sdim  void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
35353358Sdim  void writePltHeader(uint8_t *buf) const override;
36360784Sdim  void writePlt(uint8_t *buf, const Symbol &sym,
37360784Sdim                uint64_t pltEntryAddr) const override;
38336821Sdim};
39336821Sdim} // namespace
40336821Sdim
41344779SdimHexagon::Hexagon() {
42353358Sdim  pltRel = R_HEX_JMP_SLOT;
43353358Sdim  relativeRel = R_HEX_RELATIVE;
44353358Sdim  gotRel = R_HEX_GLOB_DAT;
45353358Sdim  symbolicRel = R_HEX_32;
46353358Sdim
47344779Sdim  // The zero'th GOT entry is reserved for the address of _DYNAMIC.  The
48344779Sdim  // next 3 are reserved for the dynamic loader.
49353358Sdim  gotPltHeaderEntriesNum = 4;
50336821Sdim
51353358Sdim  pltEntrySize = 16;
52353358Sdim  pltHeaderSize = 32;
53344779Sdim
54344779Sdim  // Hexagon Linux uses 64K pages by default.
55353358Sdim  defaultMaxPageSize = 0x10000;
56353358Sdim  noneRel = R_HEX_NONE;
57360784Sdim  tlsGotRel = R_HEX_TPREL_32;
58344779Sdim}
59344779Sdim
60344779Sdimuint32_t Hexagon::calcEFlags() const {
61353358Sdim  assert(!objectFiles.empty());
62344779Sdim
63344779Sdim  // The architecture revision must always be equal to or greater than
64344779Sdim  // greatest revision in the list of inputs.
65353358Sdim  uint32_t ret = 0;
66353358Sdim  for (InputFile *f : objectFiles) {
67353358Sdim    uint32_t eflags = cast<ObjFile<ELF32LE>>(f)->getObj().getHeader()->e_flags;
68353358Sdim    if (eflags > ret)
69353358Sdim      ret = eflags;
70344779Sdim  }
71353358Sdim  return ret;
72344779Sdim}
73344779Sdim
74353358Sdimstatic uint32_t applyMask(uint32_t mask, uint32_t data) {
75353358Sdim  uint32_t result = 0;
76353358Sdim  size_t off = 0;
77336821Sdim
78353358Sdim  for (size_t bit = 0; bit != 32; ++bit) {
79353358Sdim    uint32_t valBit = (data >> off) & 1;
80353358Sdim    uint32_t maskBit = (mask >> bit) & 1;
81353358Sdim    if (maskBit) {
82353358Sdim      result |= (valBit << bit);
83353358Sdim      ++off;
84336821Sdim    }
85336821Sdim  }
86353358Sdim  return result;
87336821Sdim}
88336821Sdim
89353358SdimRelExpr Hexagon::getRelExpr(RelType type, const Symbol &s,
90353358Sdim                            const uint8_t *loc) const {
91353358Sdim  switch (type) {
92360784Sdim  case R_HEX_NONE:
93360784Sdim    return R_NONE;
94360784Sdim  case R_HEX_6_X:
95360784Sdim  case R_HEX_8_X:
96360784Sdim  case R_HEX_9_X:
97360784Sdim  case R_HEX_10_X:
98360784Sdim  case R_HEX_11_X:
99360784Sdim  case R_HEX_12_X:
100360784Sdim  case R_HEX_16_X:
101360784Sdim  case R_HEX_32:
102360784Sdim  case R_HEX_32_6_X:
103360784Sdim  case R_HEX_HI16:
104360784Sdim  case R_HEX_LO16:
105360784Sdim    return R_ABS;
106344779Sdim  case R_HEX_B9_PCREL:
107344779Sdim  case R_HEX_B13_PCREL:
108336821Sdim  case R_HEX_B15_PCREL:
109344779Sdim  case R_HEX_6_PCREL_X:
110344779Sdim  case R_HEX_32_PCREL:
111344779Sdim    return R_PC;
112360784Sdim  case R_HEX_B9_PCREL_X:
113360784Sdim  case R_HEX_B15_PCREL_X:
114336821Sdim  case R_HEX_B22_PCREL:
115344779Sdim  case R_HEX_PLT_B22_PCREL:
116336821Sdim  case R_HEX_B22_PCREL_X:
117336821Sdim  case R_HEX_B32_PCREL_X:
118344779Sdim    return R_PLT_PC;
119360784Sdim  case R_HEX_IE_32_6_X:
120360784Sdim  case R_HEX_IE_16_X:
121360784Sdim  case R_HEX_IE_HI16:
122360784Sdim  case R_HEX_IE_LO16:
123360784Sdim    return R_GOT;
124360784Sdim  case R_HEX_GOTREL_11_X:
125360784Sdim  case R_HEX_GOTREL_16_X:
126360784Sdim  case R_HEX_GOTREL_32_6_X:
127360784Sdim  case R_HEX_GOTREL_HI16:
128360784Sdim  case R_HEX_GOTREL_LO16:
129360784Sdim    return R_GOTPLTREL;
130344779Sdim  case R_HEX_GOT_11_X:
131344779Sdim  case R_HEX_GOT_16_X:
132344779Sdim  case R_HEX_GOT_32_6_X:
133360784Sdim    return R_GOTPLT;
134360784Sdim  case R_HEX_IE_GOT_11_X:
135360784Sdim  case R_HEX_IE_GOT_16_X:
136360784Sdim  case R_HEX_IE_GOT_32_6_X:
137360784Sdim  case R_HEX_IE_GOT_HI16:
138360784Sdim  case R_HEX_IE_GOT_LO16:
139360784Sdim    config->hasStaticTlsModel = true;
140360784Sdim    return R_GOTPLT;
141360784Sdim  case R_HEX_TPREL_11_X:
142360784Sdim  case R_HEX_TPREL_16:
143360784Sdim  case R_HEX_TPREL_16_X:
144360784Sdim  case R_HEX_TPREL_32_6_X:
145360784Sdim  case R_HEX_TPREL_HI16:
146360784Sdim  case R_HEX_TPREL_LO16:
147360784Sdim    return R_TLS;
148336821Sdim  default:
149360784Sdim    error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
150360784Sdim          ") against symbol " + toString(s));
151360784Sdim    return R_NONE;
152336821Sdim  }
153336821Sdim}
154336821Sdim
155353358Sdimstatic uint32_t findMaskR6(uint32_t insn) {
156344779Sdim  // There are (arguably too) many relocation masks for the DSP's
157344779Sdim  // R_HEX_6_X type.  The table below is used to select the correct mask
158344779Sdim  // for the given instruction.
159344779Sdim  struct InstructionMask {
160353358Sdim    uint32_t cmpMask;
161353358Sdim    uint32_t relocMask;
162344779Sdim  };
163344779Sdim
164353358Sdim  static const InstructionMask r6[] = {
165344779Sdim      {0x38000000, 0x0000201f}, {0x39000000, 0x0000201f},
166344779Sdim      {0x3e000000, 0x00001f80}, {0x3f000000, 0x00001f80},
167344779Sdim      {0x40000000, 0x000020f8}, {0x41000000, 0x000007e0},
168344779Sdim      {0x42000000, 0x000020f8}, {0x43000000, 0x000007e0},
169344779Sdim      {0x44000000, 0x000020f8}, {0x45000000, 0x000007e0},
170344779Sdim      {0x46000000, 0x000020f8}, {0x47000000, 0x000007e0},
171344779Sdim      {0x6a000000, 0x00001f80}, {0x7c000000, 0x001f2000},
172344779Sdim      {0x9a000000, 0x00000f60}, {0x9b000000, 0x00000f60},
173344779Sdim      {0x9c000000, 0x00000f60}, {0x9d000000, 0x00000f60},
174344779Sdim      {0x9f000000, 0x001f0100}, {0xab000000, 0x0000003f},
175344779Sdim      {0xad000000, 0x0000003f}, {0xaf000000, 0x00030078},
176344779Sdim      {0xd7000000, 0x006020e0}, {0xd8000000, 0x006020e0},
177344779Sdim      {0xdb000000, 0x006020e0}, {0xdf000000, 0x006020e0}};
178344779Sdim
179344779Sdim  // Duplex forms have a fixed mask and parse bits 15:14 are always
180344779Sdim  // zero.  Non-duplex insns will always have at least one bit set in the
181344779Sdim  // parse field.
182353358Sdim  if ((0xC000 & insn) == 0x0)
183344779Sdim    return 0x03f00000;
184344779Sdim
185353358Sdim  for (InstructionMask i : r6)
186353358Sdim    if ((0xff000000 & insn) == i.cmpMask)
187353358Sdim      return i.relocMask;
188344779Sdim
189344779Sdim  error("unrecognized instruction for R_HEX_6 relocation: 0x" +
190353358Sdim        utohexstr(insn));
191344779Sdim  return 0;
192344779Sdim}
193344779Sdim
194353358Sdimstatic uint32_t findMaskR8(uint32_t insn) {
195353358Sdim  if ((0xff000000 & insn) == 0xde000000)
196344779Sdim    return 0x00e020e8;
197353358Sdim  if ((0xff000000 & insn) == 0x3c000000)
198344779Sdim    return 0x0000207f;
199344779Sdim  return 0x00001fe0;
200344779Sdim}
201344779Sdim
202353358Sdimstatic uint32_t findMaskR11(uint32_t insn) {
203353358Sdim  if ((0xff000000 & insn) == 0xa1000000)
204344779Sdim    return 0x060020ff;
205344779Sdim  return 0x06003fe0;
206344779Sdim}
207344779Sdim
208353358Sdimstatic uint32_t findMaskR16(uint32_t insn) {
209353358Sdim  if ((0xff000000 & insn) == 0x48000000)
210344779Sdim    return 0x061f20ff;
211353358Sdim  if ((0xff000000 & insn) == 0x49000000)
212344779Sdim    return 0x061f3fe0;
213353358Sdim  if ((0xff000000 & insn) == 0x78000000)
214344779Sdim    return 0x00df3fe0;
215353358Sdim  if ((0xff000000 & insn) == 0xb0000000)
216344779Sdim    return 0x0fe03fe0;
217344779Sdim
218344779Sdim  error("unrecognized instruction for R_HEX_16_X relocation: 0x" +
219353358Sdim        utohexstr(insn));
220344779Sdim  return 0;
221344779Sdim}
222344779Sdim
223353358Sdimstatic void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); }
224336821Sdim
225353358Sdimvoid Hexagon::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
226353358Sdim  switch (type) {
227336821Sdim  case R_HEX_NONE:
228336821Sdim    break;
229344779Sdim  case R_HEX_6_PCREL_X:
230344779Sdim  case R_HEX_6_X:
231353358Sdim    or32le(loc, applyMask(findMaskR6(read32le(loc)), val));
232344779Sdim    break;
233344779Sdim  case R_HEX_8_X:
234353358Sdim    or32le(loc, applyMask(findMaskR8(read32le(loc)), val));
235344779Sdim    break;
236344779Sdim  case R_HEX_9_X:
237353358Sdim    or32le(loc, applyMask(0x00003fe0, val & 0x3f));
238344779Sdim    break;
239344779Sdim  case R_HEX_10_X:
240353358Sdim    or32le(loc, applyMask(0x00203fe0, val & 0x3f));
241344779Sdim    break;
242344779Sdim  case R_HEX_11_X:
243360784Sdim  case R_HEX_IE_GOT_11_X:
244344779Sdim  case R_HEX_GOT_11_X:
245360784Sdim  case R_HEX_GOTREL_11_X:
246360784Sdim  case R_HEX_TPREL_11_X:
247353358Sdim    or32le(loc, applyMask(findMaskR11(read32le(loc)), val & 0x3f));
248344779Sdim    break;
249337151Sdim  case R_HEX_12_X:
250353358Sdim    or32le(loc, applyMask(0x000007e0, val));
251337151Sdim    break;
252344779Sdim  case R_HEX_16_X: // These relocs only have 6 effective bits.
253360784Sdim  case R_HEX_IE_16_X:
254360784Sdim  case R_HEX_IE_GOT_16_X:
255344779Sdim  case R_HEX_GOT_16_X:
256360784Sdim  case R_HEX_GOTREL_16_X:
257360784Sdim  case R_HEX_TPREL_16_X:
258353358Sdim    or32le(loc, applyMask(findMaskR16(read32le(loc)), val & 0x3f));
259344779Sdim    break;
260360784Sdim  case R_HEX_TPREL_16:
261360784Sdim    or32le(loc, applyMask(findMaskR16(read32le(loc)), val & 0xffff));
262360784Sdim    break;
263344779Sdim  case R_HEX_32:
264344779Sdim  case R_HEX_32_PCREL:
265353358Sdim    or32le(loc, val);
266344779Sdim    break;
267337151Sdim  case R_HEX_32_6_X:
268344779Sdim  case R_HEX_GOT_32_6_X:
269360784Sdim  case R_HEX_GOTREL_32_6_X:
270360784Sdim  case R_HEX_IE_GOT_32_6_X:
271360784Sdim  case R_HEX_IE_32_6_X:
272360784Sdim  case R_HEX_TPREL_32_6_X:
273353358Sdim    or32le(loc, applyMask(0x0fff3fff, val >> 6));
274337151Sdim    break;
275344779Sdim  case R_HEX_B9_PCREL:
276360784Sdim    checkInt(loc, val, 11, type);
277353358Sdim    or32le(loc, applyMask(0x003000fe, val >> 2));
278344779Sdim    break;
279344779Sdim  case R_HEX_B9_PCREL_X:
280353358Sdim    or32le(loc, applyMask(0x003000fe, val & 0x3f));
281344779Sdim    break;
282344779Sdim  case R_HEX_B13_PCREL:
283360784Sdim    checkInt(loc, val, 15, type);
284353358Sdim    or32le(loc, applyMask(0x00202ffe, val >> 2));
285344779Sdim    break;
286336821Sdim  case R_HEX_B15_PCREL:
287360784Sdim    checkInt(loc, val, 17, type);
288353358Sdim    or32le(loc, applyMask(0x00df20fe, val >> 2));
289336821Sdim    break;
290336821Sdim  case R_HEX_B15_PCREL_X:
291353358Sdim    or32le(loc, applyMask(0x00df20fe, val & 0x3f));
292336821Sdim    break;
293336821Sdim  case R_HEX_B22_PCREL:
294344779Sdim  case R_HEX_PLT_B22_PCREL:
295360784Sdim    checkInt(loc, val, 22, type);
296353358Sdim    or32le(loc, applyMask(0x1ff3ffe, val >> 2));
297336821Sdim    break;
298336821Sdim  case R_HEX_B22_PCREL_X:
299353358Sdim    or32le(loc, applyMask(0x1ff3ffe, val & 0x3f));
300336821Sdim    break;
301336821Sdim  case R_HEX_B32_PCREL_X:
302353358Sdim    or32le(loc, applyMask(0x0fff3fff, val >> 6));
303336821Sdim    break;
304360784Sdim  case R_HEX_GOTREL_HI16:
305344779Sdim  case R_HEX_HI16:
306360784Sdim  case R_HEX_IE_GOT_HI16:
307360784Sdim  case R_HEX_IE_HI16:
308360784Sdim  case R_HEX_TPREL_HI16:
309353358Sdim    or32le(loc, applyMask(0x00c03fff, val >> 16));
310344779Sdim    break;
311360784Sdim  case R_HEX_GOTREL_LO16:
312344779Sdim  case R_HEX_LO16:
313360784Sdim  case R_HEX_IE_GOT_LO16:
314360784Sdim  case R_HEX_IE_LO16:
315360784Sdim  case R_HEX_TPREL_LO16:
316353358Sdim    or32le(loc, applyMask(0x00c03fff, val));
317344779Sdim    break;
318336821Sdim  default:
319360784Sdim    llvm_unreachable("unknown relocation");
320336821Sdim  }
321336821Sdim}
322336821Sdim
323353358Sdimvoid Hexagon::writePltHeader(uint8_t *buf) const {
324353358Sdim  const uint8_t pltData[] = {
325344779Sdim      0x00, 0x40, 0x00, 0x00, // { immext (#0)
326344779Sdim      0x1c, 0xc0, 0x49, 0x6a, //   r28 = add (pc, ##GOT0@PCREL) } # @GOT0
327344779Sdim      0x0e, 0x42, 0x9c, 0xe2, // { r14 -= add (r28, #16)  # offset of GOTn
328344779Sdim      0x4f, 0x40, 0x9c, 0x91, //   r15 = memw (r28 + #8)  # object ID at GOT2
329344779Sdim      0x3c, 0xc0, 0x9c, 0x91, //   r28 = memw (r28 + #4) }# dynamic link at GOT1
330344779Sdim      0x0e, 0x42, 0x0e, 0x8c, // { r14 = asr (r14, #2)    # index of PLTn
331344779Sdim      0x00, 0xc0, 0x9c, 0x52, //   jumpr r28 }            # call dynamic linker
332344779Sdim      0x0c, 0xdb, 0x00, 0x54, // trap0(#0xdb) # bring plt0 into 16byte alignment
333344779Sdim  };
334353358Sdim  memcpy(buf, pltData, sizeof(pltData));
335344779Sdim
336344779Sdim  // Offset from PLT0 to the GOT.
337353358Sdim  uint64_t off = in.gotPlt->getVA() - in.plt->getVA();
338353358Sdim  relocateOne(buf, R_HEX_B32_PCREL_X, off);
339353358Sdim  relocateOne(buf + 4, R_HEX_6_PCREL_X, off);
340344779Sdim}
341344779Sdim
342360784Sdimvoid Hexagon::writePlt(uint8_t *buf, const Symbol &sym,
343360784Sdim                       uint64_t pltEntryAddr) const {
344353358Sdim  const uint8_t inst[] = {
345344779Sdim      0x00, 0x40, 0x00, 0x00, // { immext (#0)
346344779Sdim      0x0e, 0xc0, 0x49, 0x6a, //   r14 = add (pc, ##GOTn@PCREL) }
347344779Sdim      0x1c, 0xc0, 0x8e, 0x91, // r28 = memw (r14)
348344779Sdim      0x00, 0xc0, 0x9c, 0x52, // jumpr r28
349344779Sdim  };
350353358Sdim  memcpy(buf, inst, sizeof(inst));
351344779Sdim
352360784Sdim  uint64_t gotPltEntryAddr = sym.getGotPltVA();
353353358Sdim  relocateOne(buf, R_HEX_B32_PCREL_X, gotPltEntryAddr - pltEntryAddr);
354353358Sdim  relocateOne(buf + 4, R_HEX_6_PCREL_X, gotPltEntryAddr - pltEntryAddr);
355344779Sdim}
356344779Sdim
357360784SdimRelType Hexagon::getDynRel(RelType type) const {
358360784Sdim  if (type == R_HEX_32)
359360784Sdim    return type;
360360784Sdim  return R_HEX_NONE;
361360784Sdim}
362360784Sdim
363360784SdimTargetInfo *getHexagonTargetInfo() {
364353358Sdim  static Hexagon target;
365353358Sdim  return &target;
366336821Sdim}
367360784Sdim
368360784Sdim} // namespace elf
369360784Sdim} // namespace lld
370