Coverage Report

Created: 2018-10-23 15:26

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Arch/Hexagon.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Hexagon.cpp -------------------------------------------------------===//
2
//
3
//                             The LLVM Linker
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 "InputFiles.h"
11
#include "Symbols.h"
12
#include "SyntheticSections.h"
13
#include "Target.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"
18
19
using namespace llvm;
20
using namespace llvm::object;
21
using namespace llvm::support::endian;
22
using namespace llvm::ELF;
23
using namespace lld;
24
using namespace lld::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
  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;
37
};
38
} // namespace
39
40
4
Hexagon::Hexagon() {
41
4
  PltRel = R_HEX_JMP_SLOT;
42
4
  RelativeRel = R_HEX_RELATIVE;
43
4
  GotRel = R_HEX_GLOB_DAT;
44
4
  GotEntrySize = 4;
45
4
  // The zero'th GOT entry is reserved for the address of _DYNAMIC.  The
46
4
  // next 3 are reserved for the dynamic loader.
47
4
  GotPltHeaderEntriesNum = 4;
48
4
  GotPltEntrySize = 4;
49
4
50
4
  PltEntrySize = 16;
51
4
  PltHeaderSize = 32;
52
4
53
4
  // Hexagon Linux uses 64K pages by default.
54
4
  DefaultMaxPageSize = 0x10000;
55
4
  NoneRel = R_HEX_NONE;
56
4
}
57
58
4
uint32_t Hexagon::calcEFlags() const {
59
4
  assert(!ObjectFiles.empty());
60
4
61
4
  // The architecture revision must always be equal to or greater than
62
4
  // greatest revision in the list of inputs.
63
4
  uint32_t Ret = 0;
64
6
  for (InputFile *F : ObjectFiles) {
65
6
    uint32_t EFlags = cast<ObjFile<ELF32LE>>(F)->getObj().getHeader()->e_flags;
66
6
    if (EFlags > Ret)
67
5
      Ret = EFlags;
68
6
  }
69
4
  return Ret;
70
4
}
71
72
103
static uint32_t applyMask(uint32_t Mask, uint32_t Data) {
73
103
  uint32_t Result = 0;
74
103
  size_t Off = 0;
75
103
76
3.39k
  for (size_t Bit = 0; Bit != 32; 
++Bit3.29k
) {
77
3.29k
    uint32_t ValBit = (Data >> Off) & 1;
78
3.29k
    uint32_t MaskBit = (Mask >> Bit) & 1;
79
3.29k
    if (MaskBit) {
80
1.78k
      Result |= (ValBit << Bit);
81
1.78k
      ++Off;
82
1.78k
    }
83
3.29k
  }
84
103
  return Result;
85
103
}
86
87
RelExpr Hexagon::getRelExpr(RelType Type, const Symbol &S,
88
101
                            const uint8_t *Loc) const {
89
101
  switch (Type) {
90
101
  case R_HEX_B9_PCREL:
91
7
  case R_HEX_B9_PCREL_X:
92
7
  case R_HEX_B13_PCREL:
93
7
  case R_HEX_B15_PCREL:
94
7
  case R_HEX_B15_PCREL_X:
95
7
  case R_HEX_6_PCREL_X:
96
7
  case R_HEX_32_PCREL:
97
7
    return R_PC;
98
11
  case R_HEX_B22_PCREL:
99
11
  case R_HEX_PLT_B22_PCREL:
100
11
  case R_HEX_B22_PCREL_X:
101
11
  case R_HEX_B32_PCREL_X:
102
11
    return R_PLT_PC;
103
11
  case R_HEX_GOT_11_X:
104
4
  case R_HEX_GOT_16_X:
105
4
  case R_HEX_GOT_32_6_X:
106
4
    return R_HEXAGON_GOT;
107
79
  default:
108
79
    return R_ABS;
109
101
  }
110
101
}
111
112
30
static uint32_t findMaskR6(uint32_t Insn) {
113
30
  // There are (arguably too) many relocation masks for the DSP's
114
30
  // R_HEX_6_X type.  The table below is used to select the correct mask
115
30
  // for the given instruction.
116
30
  struct InstructionMask {
117
30
    uint32_t CmpMask;
118
30
    uint32_t RelocMask;
119
30
  };
120
30
121
30
  static const InstructionMask R6[] = {
122
30
      {0x38000000, 0x0000201f}, {0x39000000, 0x0000201f},
123
30
      {0x3e000000, 0x00001f80}, {0x3f000000, 0x00001f80},
124
30
      {0x40000000, 0x000020f8}, {0x41000000, 0x000007e0},
125
30
      {0x42000000, 0x000020f8}, {0x43000000, 0x000007e0},
126
30
      {0x44000000, 0x000020f8}, {0x45000000, 0x000007e0},
127
30
      {0x46000000, 0x000020f8}, {0x47000000, 0x000007e0},
128
30
      {0x6a000000, 0x00001f80}, {0x7c000000, 0x001f2000},
129
30
      {0x9a000000, 0x00000f60}, {0x9b000000, 0x00000f60},
130
30
      {0x9c000000, 0x00000f60}, {0x9d000000, 0x00000f60},
131
30
      {0x9f000000, 0x001f0100}, {0xab000000, 0x0000003f},
132
30
      {0xad000000, 0x0000003f}, {0xaf000000, 0x00030078},
133
30
      {0xd7000000, 0x006020e0}, {0xd8000000, 0x006020e0},
134
30
      {0xdb000000, 0x006020e0}, {0xdf000000, 0x006020e0}};
135
30
136
30
  // Duplex forms have a fixed mask and parse bits 15:14 are always
137
30
  // zero.  Non-duplex insns will always have at least one bit set in the
138
30
  // parse field.
139
30
  if ((0xC000 & Insn) == 0x0)
140
1
    return 0x03f00000;
141
29
142
29
  for (InstructionMask I : R6)
143
390
    if ((0xff000000 & Insn) == I.CmpMask)
144
29
      return I.RelocMask;
145
29
146
29
  error("unrecognized instruction for R_HEX_6 relocation: 0x" +
147
0
        utohexstr(Insn));
148
0
  return 0;
149
29
}
150
151
3
static uint32_t findMaskR8(uint32_t Insn) {
152
3
  if ((0xff000000 & Insn) == 0xde000000)
153
1
    return 0x00e020e8;
154
2
  if ((0xff000000 & Insn) == 0x3c000000)
155
1
    return 0x0000207f;
156
1
  return 0x00001fe0;
157
1
}
158
159
3
static uint32_t findMaskR11(uint32_t Insn) {
160
3
  if ((0xff000000 & Insn) == 0xa1000000)
161
1
    return 0x060020ff;
162
2
  return 0x06003fe0;
163
2
}
164
165
5
static uint32_t findMaskR16(uint32_t Insn) {
166
5
  if ((0xff000000 & Insn) == 0x48000000)
167
1
    return 0x061f20ff;
168
4
  if ((0xff000000 & Insn) == 0x49000000)
169
1
    return 0x061f3fe0;
170
3
  if ((0xff000000 & Insn) == 0x78000000)
171
1
    return 0x00df3fe0;
172
2
  if ((0xff000000 & Insn) == 0xb0000000)
173
2
    return 0x0fe03fe0;
174
0
175
0
  error("unrecognized instruction for R_HEX_16_X relocation: 0x" +
176
0
        utohexstr(Insn));
177
0
  return 0;
178
0
}
179
180
105
static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
181
182
105
void Hexagon::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
183
105
  switch (Type) {
184
105
  case R_HEX_NONE:
185
0
    break;
186
105
  case R_HEX_6_PCREL_X:
187
30
  case R_HEX_6_X:
188
30
    or32le(Loc, applyMask(findMaskR6(read32le(Loc)), Val));
189
30
    break;
190
30
  case R_HEX_8_X:
191
3
    or32le(Loc, applyMask(findMaskR8(read32le(Loc)), Val));
192
3
    break;
193
30
  case R_HEX_9_X:
194
1
    or32le(Loc, applyMask(0x00003fe0, Val & 0x3f));
195
1
    break;
196
30
  case R_HEX_10_X:
197
1
    or32le(Loc, applyMask(0x00203fe0, Val & 0x3f));
198
1
    break;
199
30
  case R_HEX_11_X:
200
3
  case R_HEX_GOT_11_X:
201
3
    or32le(Loc, applyMask(findMaskR11(read32le(Loc)), Val & 0x3f));
202
3
    break;
203
3
  case R_HEX_12_X:
204
1
    or32le(Loc, applyMask(0x000007e0, Val));
205
1
    break;
206
5
  case R_HEX_16_X: // These relocs only have 6 effective bits.
207
5
  case R_HEX_GOT_16_X:
208
5
    or32le(Loc, applyMask(findMaskR16(read32le(Loc)), Val & 0x3f));
209
5
    break;
210
5
  case R_HEX_32:
211
2
  case R_HEX_32_PCREL:
212
2
    or32le(Loc, Val);
213
2
    break;
214
40
  case R_HEX_32_6_X:
215
40
  case R_HEX_GOT_32_6_X:
216
40
    or32le(Loc, applyMask(0x0fff3fff, Val >> 6));
217
40
    break;
218
40
  case R_HEX_B9_PCREL:
219
1
    or32le(Loc, applyMask(0x003000fe, Val >> 2));
220
1
    break;
221
40
  case R_HEX_B9_PCREL_X:
222
0
    or32le(Loc, applyMask(0x003000fe, Val & 0x3f));
223
0
    break;
224
40
  case R_HEX_B13_PCREL:
225
1
    or32le(Loc, applyMask(0x00202ffe, Val >> 2));
226
1
    break;
227
40
  case R_HEX_B15_PCREL:
228
1
    or32le(Loc, applyMask(0x00df20fe, Val >> 2));
229
1
    break;
230
40
  case R_HEX_B15_PCREL_X:
231
1
    or32le(Loc, applyMask(0x00df20fe, Val & 0x3f));
232
1
    break;
233
40
  case R_HEX_B22_PCREL:
234
2
  case R_HEX_PLT_B22_PCREL:
235
2
    or32le(Loc, applyMask(0x1ff3ffe, Val >> 2));
236
2
    break;
237
3
  case R_HEX_B22_PCREL_X:
238
3
    or32le(Loc, applyMask(0x1ff3ffe, Val & 0x3f));
239
3
    break;
240
8
  case R_HEX_B32_PCREL_X:
241
8
    or32le(Loc, applyMask(0x0fff3fff, Val >> 6));
242
8
    break;
243
2
  case R_HEX_HI16:
244
1
    or32le(Loc, applyMask(0x00c03fff, Val >> 16));
245
1
    break;
246
2
  case R_HEX_LO16:
247
1
    or32le(Loc, applyMask(0x00c03fff, Val));
248
1
    break;
249
2
  default:
250
0
    error(getErrorLocation(Loc) + "unrecognized reloc " + toString(Type));
251
0
    break;
252
105
  }
253
105
}
254
255
1
void Hexagon::writePltHeader(uint8_t *Buf) const {
256
1
  const uint8_t PltData[] = {
257
1
      0x00, 0x40, 0x00, 0x00, // { immext (#0)
258
1
      0x1c, 0xc0, 0x49, 0x6a, //   r28 = add (pc, ##GOT0@PCREL) } # @GOT0
259
1
      0x0e, 0x42, 0x9c, 0xe2, // { r14 -= add (r28, #16)  # offset of GOTn
260
1
      0x4f, 0x40, 0x9c, 0x91, //   r15 = memw (r28 + #8)  # object ID at GOT2
261
1
      0x3c, 0xc0, 0x9c, 0x91, //   r28 = memw (r28 + #4) }# dynamic link at GOT1
262
1
      0x0e, 0x42, 0x0e, 0x8c, // { r14 = asr (r14, #2)    # index of PLTn
263
1
      0x00, 0xc0, 0x9c, 0x52, //   jumpr r28 }            # call dynamic linker
264
1
      0x0c, 0xdb, 0x00, 0x54, // trap0(#0xdb) # bring plt0 into 16byte alignment
265
1
  };
266
1
  memcpy(Buf, PltData, sizeof(PltData));
267
1
268
1
  // Offset from PLT0 to the GOT.
269
1
  uint64_t Off = In.GotPlt->getVA() - In.Plt->getVA();
270
1
  relocateOne(Buf, R_HEX_B32_PCREL_X, Off);
271
1
  relocateOne(Buf + 4, R_HEX_6_PCREL_X, Off);
272
1
}
273
274
void Hexagon::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
275
                       uint64_t PltEntryAddr, int32_t Index,
276
1
                       unsigned RelOff) const {
277
1
  const uint8_t Inst[] = {
278
1
      0x00, 0x40, 0x00, 0x00, // { immext (#0)
279
1
      0x0e, 0xc0, 0x49, 0x6a, //   r14 = add (pc, ##GOTn@PCREL) }
280
1
      0x1c, 0xc0, 0x8e, 0x91, // r28 = memw (r14)
281
1
      0x00, 0xc0, 0x9c, 0x52, // jumpr r28
282
1
  };
283
1
  memcpy(Buf, Inst, sizeof(Inst));
284
1
285
1
  relocateOne(Buf, R_HEX_B32_PCREL_X, GotPltEntryAddr - PltEntryAddr);
286
1
  relocateOne(Buf + 4, R_HEX_6_PCREL_X, GotPltEntryAddr - PltEntryAddr);
287
1
}
288
289
4
TargetInfo *elf::getHexagonTargetInfo() {
290
4
  static Hexagon Target;
291
4
  return &Target;
292
4
}