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, const Symbol &sym,
29                 uint64_t pltEntryAddr) const override;
30   void relocate(uint8_t *loc, const Relocation &rel,
31                 uint64_t val) const override;
32 };
33 } // namespace
34 
SPARCV9()35 SPARCV9::SPARCV9() {
36   copyRel = R_SPARC_COPY;
37   gotRel = R_SPARC_GLOB_DAT;
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 
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const49 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   case R_SPARC_H44:
57   case R_SPARC_M44:
58   case R_SPARC_L44:
59   case R_SPARC_HH22:
60   case R_SPARC_HM10:
61   case R_SPARC_LM22:
62   case R_SPARC_HI22:
63   case R_SPARC_LO10:
64     return R_ABS;
65   case R_SPARC_PC10:
66   case R_SPARC_PC22:
67   case R_SPARC_DISP32:
68   case R_SPARC_WDISP30:
69     return R_PC;
70   case R_SPARC_GOT10:
71     return R_GOT_OFF;
72   case R_SPARC_GOT22:
73     return R_GOT_OFF;
74   case R_SPARC_WPLT30:
75     return R_PLT_PC;
76   case R_SPARC_NONE:
77     return R_NONE;
78   case R_SPARC_TLS_LE_HIX22:
79   case R_SPARC_TLS_LE_LOX10:
80     return R_TPREL;
81   default:
82     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
83           ") against symbol " + toString(s));
84     return R_NONE;
85   }
86 }
87 
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const88 void SPARCV9::relocate(uint8_t *loc, const Relocation &rel,
89                        uint64_t val) const {
90   switch (rel.type) {
91   case R_SPARC_32:
92   case R_SPARC_UA32:
93     // V-word32
94     checkUInt(loc, val, 32, rel);
95     write32be(loc, val);
96     break;
97   case R_SPARC_DISP32:
98     // V-disp32
99     checkInt(loc, val, 32, rel);
100     write32be(loc, val);
101     break;
102   case R_SPARC_WDISP30:
103   case R_SPARC_WPLT30:
104     // V-disp30
105     checkInt(loc, val, 32, rel);
106     write32be(loc, (read32be(loc) & ~0x3fffffff) | ((val >> 2) & 0x3fffffff));
107     break;
108   case R_SPARC_22:
109     // V-imm22
110     checkUInt(loc, val, 22, rel);
111     write32be(loc, (read32be(loc) & ~0x003fffff) | (val & 0x003fffff));
112     break;
113   case R_SPARC_GOT22:
114   case R_SPARC_PC22:
115   case R_SPARC_LM22:
116     // T-imm22
117     write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
118     break;
119   case R_SPARC_HI22:
120     // V-imm22
121     checkUInt(loc, val >> 10, 22, rel);
122     write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 10) & 0x003fffff));
123     break;
124   case R_SPARC_WDISP19:
125     // V-disp19
126     checkInt(loc, val, 21, rel);
127     write32be(loc, (read32be(loc) & ~0x0007ffff) | ((val >> 2) & 0x0007ffff));
128     break;
129   case R_SPARC_GOT10:
130   case R_SPARC_PC10:
131     // T-simm10
132     write32be(loc, (read32be(loc) & ~0x000003ff) | (val & 0x000003ff));
133     break;
134   case R_SPARC_LO10:
135     // T-simm13
136     write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff));
137     break;
138   case R_SPARC_64:
139   case R_SPARC_UA64:
140     // V-xword64
141     write64be(loc, val);
142     break;
143   case R_SPARC_HH22:
144     // V-imm22
145     checkUInt(loc, val >> 42, 22, rel);
146     write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 42) & 0x003fffff));
147     break;
148   case R_SPARC_HM10:
149     // T-simm13
150     write32be(loc, (read32be(loc) & ~0x00001fff) | ((val >> 32) & 0x000003ff));
151     break;
152   case R_SPARC_H44:
153     // V-imm22
154     checkUInt(loc, val >> 22, 22, rel);
155     write32be(loc, (read32be(loc) & ~0x003fffff) | ((val >> 22) & 0x003fffff));
156     break;
157   case R_SPARC_M44:
158     // T-imm10
159     write32be(loc, (read32be(loc) & ~0x000003ff) | ((val >> 12) & 0x000003ff));
160     break;
161   case R_SPARC_L44:
162     // T-imm13
163     write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x00000fff));
164     break;
165   case R_SPARC_TLS_LE_HIX22:
166     // T-imm22
167     write32be(loc, (read32be(loc) & ~0x003fffff) | ((~val >> 10) & 0x003fffff));
168     break;
169   case R_SPARC_TLS_LE_LOX10:
170     // T-simm13
171     write32be(loc, (read32be(loc) & ~0x00001fff) | (val & 0x000003ff) | 0x1C00);
172     break;
173   default:
174     llvm_unreachable("unknown relocation");
175   }
176 }
177 
writePlt(uint8_t * buf,const Symbol &,uint64_t pltEntryAddr) const178 void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
179                        uint64_t pltEntryAddr) const {
180   const uint8_t pltData[] = {
181       0x03, 0x00, 0x00, 0x00, // sethi   (. - .PLT0), %g1
182       0x30, 0x68, 0x00, 0x00, // ba,a    %xcc, .PLT1
183       0x01, 0x00, 0x00, 0x00, // nop
184       0x01, 0x00, 0x00, 0x00, // nop
185       0x01, 0x00, 0x00, 0x00, // nop
186       0x01, 0x00, 0x00, 0x00, // nop
187       0x01, 0x00, 0x00, 0x00, // nop
188       0x01, 0x00, 0x00, 0x00  // nop
189   };
190   memcpy(buf, pltData, sizeof(pltData));
191 
192   uint64_t off = pltEntryAddr - in.plt->getVA();
193   relocateNoSym(buf, R_SPARC_22, off);
194   relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
195 }
196 
getSPARCV9TargetInfo()197 TargetInfo *elf::getSPARCV9TargetInfo() {
198   static SPARCV9 target;
199   return ⌖
200 }
201