Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Arch/SPARCV9.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SPARCV9.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/Support/Endian.h"
15
16
using namespace llvm;
17
using namespace llvm::support::endian;
18
using namespace llvm::ELF;
19
using namespace lld;
20
using namespace lld::elf;
21
22
namespace {
23
class SPARCV9 final : public TargetInfo {
24
public:
25
  SPARCV9();
26
  RelExpr getRelExpr(RelType Type, const Symbol &S,
27
                     const uint8_t *Loc) const override;
28
  void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
29
                int32_t Index, unsigned RelOff) const override;
30
  void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
31
};
32
} // namespace
33
34
1
SPARCV9::SPARCV9() {
35
1
  CopyRel = R_SPARC_COPY;
36
1
  GotRel = R_SPARC_GLOB_DAT;
37
1
  NoneRel = R_SPARC_NONE;
38
1
  PltRel = R_SPARC_JMP_SLOT;
39
1
  RelativeRel = R_SPARC_RELATIVE;
40
1
  GotEntrySize = 8;
41
1
  PltEntrySize = 32;
42
1
  PltHeaderSize = 4 * PltEntrySize;
43
1
44
1
  DefaultCommonPageSize = 8192;
45
1
  DefaultMaxPageSize = 0x100000;
46
1
  DefaultImageBase = 0x100000;
47
1
}
48
49
RelExpr SPARCV9::getRelExpr(RelType Type, const Symbol &S,
50
                            const uint8_t *Loc) const {
51
  switch (Type) {
52
  case R_SPARC_32:
53
  case R_SPARC_UA32:
54
  case R_SPARC_64:
55
  case R_SPARC_UA64:
56
    return R_ABS;
57
  case R_SPARC_PC10:
58
  case R_SPARC_PC22:
59
  case R_SPARC_DISP32:
60
  case R_SPARC_WDISP30:
61
    return R_PC;
62
  case R_SPARC_GOT10:
63
    return R_GOT_OFF;
64
  case R_SPARC_GOT22:
65
    return R_GOT_OFF;
66
  case R_SPARC_WPLT30:
67
    return R_PLT_PC;
68
  case R_SPARC_NONE:
69
    return R_NONE;
70
  default:
71
    error(getErrorLocation(Loc) + "unknown relocation (" + Twine(Type) +
72
          ") against symbol " + toString(S));
73
    return R_NONE;
74
  }
75
}
76
77
0
void SPARCV9::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
78
0
  switch (Type) {
79
0
  case R_SPARC_32:
80
0
  case R_SPARC_UA32:
81
0
    // V-word32
82
0
    checkUInt(Loc, Val, 32, Type);
83
0
    write32be(Loc, Val);
84
0
    break;
85
0
  case R_SPARC_DISP32:
86
0
    // V-disp32
87
0
    checkInt(Loc, Val, 32, Type);
88
0
    write32be(Loc, Val);
89
0
    break;
90
0
  case R_SPARC_WDISP30:
91
0
  case R_SPARC_WPLT30:
92
0
    // V-disp30
93
0
    checkInt(Loc, Val, 32, Type);
94
0
    write32be(Loc, (read32be(Loc) & ~0x3fffffff) | ((Val >> 2) & 0x3fffffff));
95
0
    break;
96
0
  case R_SPARC_22:
97
0
    // V-imm22
98
0
    checkUInt(Loc, Val, 22, Type);
99
0
    write32be(Loc, (read32be(Loc) & ~0x003fffff) | (Val & 0x003fffff));
100
0
    break;
101
0
  case R_SPARC_GOT22:
102
0
  case R_SPARC_PC22:
103
0
    // T-imm22
104
0
    write32be(Loc, (read32be(Loc) & ~0x003fffff) | ((Val >> 10) & 0x003fffff));
105
0
    break;
106
0
  case R_SPARC_WDISP19:
107
0
    // V-disp19
108
0
    checkInt(Loc, Val, 21, Type);
109
0
    write32be(Loc, (read32be(Loc) & ~0x0007ffff) | ((Val >> 2) & 0x0007ffff));
110
0
    break;
111
0
  case R_SPARC_GOT10:
112
0
  case R_SPARC_PC10:
113
0
    // T-simm10
114
0
    write32be(Loc, (read32be(Loc) & ~0x000003ff) | (Val & 0x000003ff));
115
0
    break;
116
0
  case R_SPARC_64:
117
0
  case R_SPARC_UA64:
118
0
  case R_SPARC_GLOB_DAT:
119
0
    // V-xword64
120
0
    write64be(Loc, Val);
121
0
    break;
122
0
  default:
123
0
    llvm_unreachable("unknown relocation");
124
0
  }
125
0
}
126
127
void SPARCV9::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
128
                       uint64_t PltEntryAddr, int32_t Index,
129
0
                       unsigned RelOff) const {
130
0
  const uint8_t PltData[] = {
131
0
      0x03, 0x00, 0x00, 0x00, // sethi   (. - .PLT0), %g1
132
0
      0x30, 0x68, 0x00, 0x00, // ba,a    %xcc, .PLT1
133
0
      0x01, 0x00, 0x00, 0x00, // nop
134
0
      0x01, 0x00, 0x00, 0x00, // nop
135
0
      0x01, 0x00, 0x00, 0x00, // nop
136
0
      0x01, 0x00, 0x00, 0x00, // nop
137
0
      0x01, 0x00, 0x00, 0x00, // nop
138
0
      0x01, 0x00, 0x00, 0x00  // nop
139
0
  };
140
0
  memcpy(Buf, PltData, sizeof(PltData));
141
0
142
0
  uint64_t Off = PltHeaderSize + PltEntrySize * Index;
143
0
  relocateOne(Buf, R_SPARC_22, Off);
144
0
  relocateOne(Buf + 4, R_SPARC_WDISP19, -(Off + 4 - PltEntrySize));
145
0
}
146
147
1
TargetInfo *elf::getSPARCV9TargetInfo() {
148
1
  static SPARCV9 Target;
149
1
  return &Target;
150
1
}