1 //===- RISCV.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 "Target.h" 12 13 using namespace llvm; 14 using namespace llvm::object; 15 using namespace llvm::support::endian; 16 using namespace llvm::ELF; 17 using namespace lld; 18 using namespace lld::elf; 19 20 namespace { 21 22 class RISCV final : public TargetInfo { 23 public: 24 RISCV(); 25 virtual uint32_t calcEFlags() const override; 26 RelExpr getRelExpr(RelType Type, const Symbol &S, 27 const uint8_t *Loc) const override; 28 void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override; 29 }; 30 31 } // end anonymous namespace 32 33 RISCV::RISCV() { NoneRel = R_RISCV_NONE; } 34 35 static uint32_t getEFlags(InputFile *F) { 36 if (Config->Is64) 37 return cast<ObjFile<ELF64LE>>(F)->getObj().getHeader()->e_flags; 38 return cast<ObjFile<ELF32LE>>(F)->getObj().getHeader()->e_flags; 39 } 40 41 uint32_t RISCV::calcEFlags() const { 42 assert(!ObjectFiles.empty()); 43 44 uint32_t Target = getEFlags(ObjectFiles.front()); 45 46 for (InputFile *F : ObjectFiles) { 47 uint32_t EFlags = getEFlags(F); 48 if (EFlags & EF_RISCV_RVC) 49 Target |= EF_RISCV_RVC; 50 51 if ((EFlags & EF_RISCV_FLOAT_ABI) != (Target & EF_RISCV_FLOAT_ABI)) 52 error(toString(F) + 53 ": cannot link object files with different floating-point ABI"); 54 55 if ((EFlags & EF_RISCV_RVE) != (Target & EF_RISCV_RVE)) 56 error(toString(F) + 57 ": cannot link object files with different EF_RISCV_RVE"); 58 } 59 60 return Target; 61 } 62 63 RelExpr RISCV::getRelExpr(const RelType Type, const Symbol &S, 64 const uint8_t *Loc) const { 65 switch (Type) { 66 case R_RISCV_JAL: 67 case R_RISCV_BRANCH: 68 case R_RISCV_CALL: 69 case R_RISCV_PCREL_HI20: 70 case R_RISCV_RVC_BRANCH: 71 case R_RISCV_RVC_JUMP: 72 case R_RISCV_32_PCREL: 73 return R_PC; 74 case R_RISCV_PCREL_LO12_I: 75 case R_RISCV_PCREL_LO12_S: 76 return R_RISCV_PC_INDIRECT; 77 case R_RISCV_RELAX: 78 case R_RISCV_ALIGN: 79 return R_HINT; 80 default: 81 return R_ABS; 82 } 83 } 84 85 // Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63. 86 static uint32_t extractBits(uint64_t V, uint32_t Begin, uint32_t End) { 87 return (V & ((1ULL << (Begin + 1)) - 1)) >> End; 88 } 89 90 void RISCV::relocateOne(uint8_t *Loc, const RelType Type, 91 const uint64_t Val) const { 92 switch (Type) { 93 case R_RISCV_32: 94 write32le(Loc, Val); 95 return; 96 case R_RISCV_64: 97 write64le(Loc, Val); 98 return; 99 100 case R_RISCV_RVC_BRANCH: { 101 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 8, Type); 102 checkAlignment(Loc, Val, 2, Type); 103 uint16_t Insn = read16le(Loc) & 0xE383; 104 uint16_t Imm8 = extractBits(Val, 8, 8) << 12; 105 uint16_t Imm4_3 = extractBits(Val, 4, 3) << 10; 106 uint16_t Imm7_6 = extractBits(Val, 7, 6) << 5; 107 uint16_t Imm2_1 = extractBits(Val, 2, 1) << 3; 108 uint16_t Imm5 = extractBits(Val, 5, 5) << 2; 109 Insn |= Imm8 | Imm4_3 | Imm7_6 | Imm2_1 | Imm5; 110 111 write16le(Loc, Insn); 112 return; 113 } 114 115 case R_RISCV_RVC_JUMP: { 116 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 11, Type); 117 checkAlignment(Loc, Val, 2, Type); 118 uint16_t Insn = read16le(Loc) & 0xE003; 119 uint16_t Imm11 = extractBits(Val, 11, 11) << 12; 120 uint16_t Imm4 = extractBits(Val, 4, 4) << 11; 121 uint16_t Imm9_8 = extractBits(Val, 9, 8) << 9; 122 uint16_t Imm10 = extractBits(Val, 10, 10) << 8; 123 uint16_t Imm6 = extractBits(Val, 6, 6) << 7; 124 uint16_t Imm7 = extractBits(Val, 7, 7) << 6; 125 uint16_t Imm3_1 = extractBits(Val, 3, 1) << 3; 126 uint16_t Imm5 = extractBits(Val, 5, 5) << 2; 127 Insn |= Imm11 | Imm4 | Imm9_8 | Imm10 | Imm6 | Imm7 | Imm3_1 | Imm5; 128 129 write16le(Loc, Insn); 130 return; 131 } 132 133 case R_RISCV_RVC_LUI: { 134 int32_t Imm = ((Val + 0x800) >> 12); 135 checkUInt(Loc, Imm, 6, Type); 136 if (Imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0` 137 write16le(Loc, (read16le(Loc) & 0x0F83) | 0x4000); 138 } else { 139 uint16_t Imm17 = extractBits(Val + 0x800, 17, 17) << 12; 140 uint16_t Imm16_12 = extractBits(Val + 0x800, 16, 12) << 2; 141 write16le(Loc, (read16le(Loc) & 0xEF83) | Imm17 | Imm16_12); 142 } 143 return; 144 } 145 146 case R_RISCV_JAL: { 147 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 20, Type); 148 checkAlignment(Loc, Val, 2, Type); 149 150 uint32_t Insn = read32le(Loc) & 0xFFF; 151 uint32_t Imm20 = extractBits(Val, 20, 20) << 31; 152 uint32_t Imm10_1 = extractBits(Val, 10, 1) << 21; 153 uint32_t Imm11 = extractBits(Val, 11, 11) << 20; 154 uint32_t Imm19_12 = extractBits(Val, 19, 12) << 12; 155 Insn |= Imm20 | Imm10_1 | Imm11 | Imm19_12; 156 157 write32le(Loc, Insn); 158 return; 159 } 160 161 case R_RISCV_BRANCH: { 162 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 12, Type); 163 checkAlignment(Loc, Val, 2, Type); 164 165 uint32_t Insn = read32le(Loc) & 0x1FFF07F; 166 uint32_t Imm12 = extractBits(Val, 12, 12) << 31; 167 uint32_t Imm10_5 = extractBits(Val, 10, 5) << 25; 168 uint32_t Imm4_1 = extractBits(Val, 4, 1) << 8; 169 uint32_t Imm11 = extractBits(Val, 11, 11) << 7; 170 Insn |= Imm12 | Imm10_5 | Imm4_1 | Imm11; 171 172 write32le(Loc, Insn); 173 return; 174 } 175 176 // auipc + jalr pair 177 case R_RISCV_CALL: { 178 checkInt(Loc, Val, 32, Type); 179 if (isInt<32>(Val)) { 180 relocateOne(Loc, R_RISCV_PCREL_HI20, Val); 181 relocateOne(Loc + 4, R_RISCV_PCREL_LO12_I, Val); 182 } 183 return; 184 } 185 186 case R_RISCV_PCREL_HI20: 187 case R_RISCV_HI20: { 188 checkInt(Loc, Val, 32, Type); 189 uint32_t Hi = Val + 0x800; 190 write32le(Loc, (read32le(Loc) & 0xFFF) | (Hi & 0xFFFFF000)); 191 return; 192 } 193 194 case R_RISCV_PCREL_LO12_I: 195 case R_RISCV_LO12_I: { 196 checkInt(Loc, Val, 32, Type); 197 uint32_t Hi = Val + 0x800; 198 uint32_t Lo = Val - (Hi & 0xFFFFF000); 199 write32le(Loc, (read32le(Loc) & 0xFFFFF) | ((Lo & 0xFFF) << 20)); 200 return; 201 } 202 203 case R_RISCV_PCREL_LO12_S: 204 case R_RISCV_LO12_S: { 205 checkInt(Loc, Val, 32, Type); 206 uint32_t Hi = Val + 0x800; 207 uint32_t Lo = Val - (Hi & 0xFFFFF000); 208 uint32_t Imm11_5 = extractBits(Lo, 11, 5) << 25; 209 uint32_t Imm4_0 = extractBits(Lo, 4, 0) << 7; 210 write32le(Loc, (read32le(Loc) & 0x1FFF07F) | Imm11_5 | Imm4_0); 211 return; 212 } 213 214 case R_RISCV_ADD8: 215 *Loc += Val; 216 return; 217 case R_RISCV_ADD16: 218 write16le(Loc, read16le(Loc) + Val); 219 return; 220 case R_RISCV_ADD32: 221 write32le(Loc, read32le(Loc) + Val); 222 return; 223 case R_RISCV_ADD64: 224 write64le(Loc, read64le(Loc) + Val); 225 return; 226 case R_RISCV_SUB6: 227 *Loc = (*Loc & 0xc0) | (((*Loc & 0x3f) - Val) & 0x3f); 228 return; 229 case R_RISCV_SUB8: 230 *Loc -= Val; 231 return; 232 case R_RISCV_SUB16: 233 write16le(Loc, read16le(Loc) - Val); 234 return; 235 case R_RISCV_SUB32: 236 write32le(Loc, read32le(Loc) - Val); 237 return; 238 case R_RISCV_SUB64: 239 write64le(Loc, read64le(Loc) - Val); 240 return; 241 case R_RISCV_SET6: 242 *Loc = (*Loc & 0xc0) | (Val & 0x3f); 243 return; 244 case R_RISCV_SET8: 245 *Loc = Val; 246 return; 247 case R_RISCV_SET16: 248 write16le(Loc, Val); 249 return; 250 case R_RISCV_SET32: 251 case R_RISCV_32_PCREL: 252 write32le(Loc, Val); 253 return; 254 255 case R_RISCV_ALIGN: 256 case R_RISCV_RELAX: 257 return; // Ignored (for now) 258 case R_RISCV_NONE: 259 return; // Do nothing 260 261 // These are handled by the dynamic linker 262 case R_RISCV_RELATIVE: 263 case R_RISCV_COPY: 264 case R_RISCV_JUMP_SLOT: 265 // GP-relative relocations are only produced after relaxation, which 266 // we don't support for now 267 case R_RISCV_GPREL_I: 268 case R_RISCV_GPREL_S: 269 default: 270 error(getErrorLocation(Loc) + 271 "unimplemented relocation: " + toString(Type)); 272 return; 273 } 274 } 275 276 TargetInfo *elf::getRISCVTargetInfo() { 277 static RISCV Target; 278 return &Target; 279 } 280