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 SPARCV9::SPARCV9() {
35   CopyRel = R_SPARC_COPY;
36   GotRel = R_SPARC_GLOB_DAT;
37   NoneRel = R_SPARC_NONE;
38   PltRel = R_SPARC_JMP_SLOT;
39   RelativeRel = R_SPARC_RELATIVE;
40   SymbolicRel = R_SPARC_64;
41   PltEntrySize = 32;
42   PltHeaderSize = 4 * PltEntrySize;
43 
44   DefaultCommonPageSize = 8192;
45   DefaultMaxPageSize = 0x100000;
46   DefaultImageBase = 0x100000;
47 }
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 void SPARCV9::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
78   switch (Type) {
79   case R_SPARC_32:
80   case R_SPARC_UA32:
81     // V-word32
82     checkUInt(Loc, Val, 32, Type);
83     write32be(Loc, Val);
84     break;
85   case R_SPARC_DISP32:
86     // V-disp32
87     checkInt(Loc, Val, 32, Type);
88     write32be(Loc, Val);
89     break;
90   case R_SPARC_WDISP30:
91   case R_SPARC_WPLT30:
92     // V-disp30
93     checkInt(Loc, Val, 32, Type);
94     write32be(Loc, (read32be(Loc) & ~0x3fffffff) | ((Val >> 2) & 0x3fffffff));
95     break;
96   case R_SPARC_22:
97     // V-imm22
98     checkUInt(Loc, Val, 22, Type);
99     write32be(Loc, (read32be(Loc) & ~0x003fffff) | (Val & 0x003fffff));
100     break;
101   case R_SPARC_GOT22:
102   case R_SPARC_PC22:
103     // T-imm22
104     write32be(Loc, (read32be(Loc) & ~0x003fffff) | ((Val >> 10) & 0x003fffff));
105     break;
106   case R_SPARC_WDISP19:
107     // V-disp19
108     checkInt(Loc, Val, 21, Type);
109     write32be(Loc, (read32be(Loc) & ~0x0007ffff) | ((Val >> 2) & 0x0007ffff));
110     break;
111   case R_SPARC_GOT10:
112   case R_SPARC_PC10:
113     // T-simm10
114     write32be(Loc, (read32be(Loc) & ~0x000003ff) | (Val & 0x000003ff));
115     break;
116   case R_SPARC_64:
117   case R_SPARC_UA64:
118     // V-xword64
119     write64be(Loc, Val);
120     break;
121   default:
122     llvm_unreachable("unknown relocation");
123   }
124 }
125 
126 void SPARCV9::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
127                        uint64_t PltEntryAddr, int32_t Index,
128                        unsigned RelOff) const {
129   const uint8_t PltData[] = {
130       0x03, 0x00, 0x00, 0x00, // sethi   (. - .PLT0), %g1
131       0x30, 0x68, 0x00, 0x00, // ba,a    %xcc, .PLT1
132       0x01, 0x00, 0x00, 0x00, // nop
133       0x01, 0x00, 0x00, 0x00, // nop
134       0x01, 0x00, 0x00, 0x00, // nop
135       0x01, 0x00, 0x00, 0x00, // nop
136       0x01, 0x00, 0x00, 0x00, // nop
137       0x01, 0x00, 0x00, 0x00  // nop
138   };
139   memcpy(Buf, PltData, sizeof(PltData));
140 
141   uint64_t Off = PltHeaderSize + PltEntrySize * Index;
142   relocateOne(Buf, R_SPARC_22, Off);
143   relocateOne(Buf + 4, R_SPARC_WDISP19, -(Off + 4 - PltEntrySize));
144 }
145 
146 TargetInfo *elf::getSPARCV9TargetInfo() {
147   static SPARCV9 Target;
148   return &Target;
149 }
150