| 1 | //===- RelocationResolver.cpp ------------------------------------*- C++ -*-===// |
| 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 | // This file defines utilities to resolve relocations in object files. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Object/RelocationResolver.h" |
| 14 | #include "llvm/ADT/Twine.h" |
| 15 | #include "llvm/BinaryFormat/COFF.h" |
| 16 | #include "llvm/BinaryFormat/ELF.h" |
| 17 | #include "llvm/BinaryFormat/MachO.h" |
| 18 | #include "llvm/BinaryFormat/Wasm.h" |
| 19 | #include "llvm/Object/ELFObjectFile.h" |
| 20 | #include "llvm/Object/ObjectFile.h" |
| 21 | #include "llvm/Object/SymbolicFile.h" |
| 22 | #include "llvm/Support/Casting.h" |
| 23 | #include "llvm/Support/Error.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/TargetParser/Triple.h" |
| 26 | #include <cassert> |
| 27 | |
| 28 | namespace llvm { |
| 29 | namespace object { |
| 30 | |
| 31 | static int64_t getELFAddend(RelocationRef R) { |
| 32 | Expected<int64_t> AddendOrErr = ELFRelocationRef(R).getAddend(); |
| 33 | handleAllErrors(E: AddendOrErr.takeError(), Handlers: [](const ErrorInfoBase &EI) { |
| 34 | report_fatal_error(reason: Twine(EI.message())); |
| 35 | }); |
| 36 | return *AddendOrErr; |
| 37 | } |
| 38 | |
| 39 | static bool supportsX86_64(uint64_t Type) { |
| 40 | switch (Type) { |
| 41 | case ELF::R_X86_64_NONE: |
| 42 | case ELF::R_X86_64_64: |
| 43 | case ELF::R_X86_64_DTPOFF32: |
| 44 | case ELF::R_X86_64_DTPOFF64: |
| 45 | case ELF::R_X86_64_PC32: |
| 46 | case ELF::R_X86_64_PC64: |
| 47 | case ELF::R_X86_64_32: |
| 48 | case ELF::R_X86_64_32S: |
| 49 | return true; |
| 50 | default: |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static uint64_t resolveX86_64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 56 | uint64_t LocData, int64_t Addend) { |
| 57 | switch (Type) { |
| 58 | case ELF::R_X86_64_NONE: |
| 59 | return LocData; |
| 60 | case ELF::R_X86_64_64: |
| 61 | case ELF::R_X86_64_DTPOFF32: |
| 62 | case ELF::R_X86_64_DTPOFF64: |
| 63 | return S + Addend; |
| 64 | case ELF::R_X86_64_PC32: |
| 65 | case ELF::R_X86_64_PC64: |
| 66 | return S + Addend - Offset; |
| 67 | case ELF::R_X86_64_32: |
| 68 | case ELF::R_X86_64_32S: |
| 69 | return (S + Addend) & 0xFFFFFFFF; |
| 70 | default: |
| 71 | llvm_unreachable("Invalid relocation type" ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static bool supportsAArch64(uint64_t Type) { |
| 76 | switch (Type) { |
| 77 | case ELF::R_AARCH64_ABS32: |
| 78 | case ELF::R_AARCH64_ABS64: |
| 79 | case ELF::R_AARCH64_PREL16: |
| 80 | case ELF::R_AARCH64_PREL32: |
| 81 | case ELF::R_AARCH64_PREL64: |
| 82 | case ELF::R_AARCH64_TLS_DTPREL64: |
| 83 | return true; |
| 84 | default: |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static uint64_t resolveAArch64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 90 | uint64_t /*LocData*/, int64_t Addend) { |
| 91 | switch (Type) { |
| 92 | case ELF::R_AARCH64_ABS32: |
| 93 | return (S + Addend) & 0xFFFFFFFF; |
| 94 | case ELF::R_AARCH64_ABS64: |
| 95 | case ELF::R_AARCH64_TLS_DTPREL64: |
| 96 | return S + Addend; |
| 97 | case ELF::R_AARCH64_PREL16: |
| 98 | return (S + Addend - Offset) & 0xFFFF; |
| 99 | case ELF::R_AARCH64_PREL32: |
| 100 | return (S + Addend - Offset) & 0xFFFFFFFF; |
| 101 | case ELF::R_AARCH64_PREL64: |
| 102 | return S + Addend - Offset; |
| 103 | default: |
| 104 | llvm_unreachable("Invalid relocation type" ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static bool supportsBPF(uint64_t Type) { |
| 109 | switch (Type) { |
| 110 | case ELF::R_BPF_64_ABS32: |
| 111 | case ELF::R_BPF_64_ABS64: |
| 112 | return true; |
| 113 | default: |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static uint64_t resolveBPF(uint64_t Type, uint64_t Offset, uint64_t S, |
| 119 | uint64_t LocData, int64_t /*Addend*/) { |
| 120 | switch (Type) { |
| 121 | case ELF::R_BPF_64_ABS32: |
| 122 | return (S + LocData) & 0xFFFFFFFF; |
| 123 | case ELF::R_BPF_64_ABS64: |
| 124 | return S + LocData; |
| 125 | default: |
| 126 | llvm_unreachable("Invalid relocation type" ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static bool supportsMips64(uint64_t Type) { |
| 131 | switch (Type) { |
| 132 | case ELF::R_MIPS_32: |
| 133 | case ELF::R_MIPS_64: |
| 134 | case ELF::R_MIPS_TLS_DTPREL64: |
| 135 | case ELF::R_MIPS_PC32: |
| 136 | return true; |
| 137 | default: |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static uint64_t resolveMips64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 143 | uint64_t /*LocData*/, int64_t Addend) { |
| 144 | switch (Type) { |
| 145 | case ELF::R_MIPS_32: |
| 146 | return (S + Addend) & 0xFFFFFFFF; |
| 147 | case ELF::R_MIPS_64: |
| 148 | return S + Addend; |
| 149 | case ELF::R_MIPS_TLS_DTPREL64: |
| 150 | return S + Addend - 0x8000; |
| 151 | case ELF::R_MIPS_PC32: |
| 152 | return S + Addend - Offset; |
| 153 | default: |
| 154 | llvm_unreachable("Invalid relocation type" ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static bool supportsMSP430(uint64_t Type) { |
| 159 | switch (Type) { |
| 160 | case ELF::R_MSP430_32: |
| 161 | case ELF::R_MSP430_16_BYTE: |
| 162 | return true; |
| 163 | default: |
| 164 | return false; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static uint64_t resolveMSP430(uint64_t Type, uint64_t Offset, uint64_t S, |
| 169 | uint64_t /*LocData*/, int64_t Addend) { |
| 170 | switch (Type) { |
| 171 | case ELF::R_MSP430_32: |
| 172 | return (S + Addend) & 0xFFFFFFFF; |
| 173 | case ELF::R_MSP430_16_BYTE: |
| 174 | return (S + Addend) & 0xFFFF; |
| 175 | default: |
| 176 | llvm_unreachable("Invalid relocation type" ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | static bool supportsPPC64(uint64_t Type) { |
| 181 | switch (Type) { |
| 182 | case ELF::R_PPC64_ADDR32: |
| 183 | case ELF::R_PPC64_ADDR64: |
| 184 | case ELF::R_PPC64_REL32: |
| 185 | case ELF::R_PPC64_REL64: |
| 186 | return true; |
| 187 | default: |
| 188 | return false; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static uint64_t resolvePPC64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 193 | uint64_t /*LocData*/, int64_t Addend) { |
| 194 | switch (Type) { |
| 195 | case ELF::R_PPC64_ADDR32: |
| 196 | return (S + Addend) & 0xFFFFFFFF; |
| 197 | case ELF::R_PPC64_ADDR64: |
| 198 | return S + Addend; |
| 199 | case ELF::R_PPC64_REL32: |
| 200 | return (S + Addend - Offset) & 0xFFFFFFFF; |
| 201 | case ELF::R_PPC64_REL64: |
| 202 | return S + Addend - Offset; |
| 203 | default: |
| 204 | llvm_unreachable("Invalid relocation type" ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static bool supportsSystemZ(uint64_t Type) { |
| 209 | switch (Type) { |
| 210 | case ELF::R_390_32: |
| 211 | case ELF::R_390_64: |
| 212 | return true; |
| 213 | default: |
| 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static uint64_t resolveSystemZ(uint64_t Type, uint64_t Offset, uint64_t S, |
| 219 | uint64_t /*LocData*/, int64_t Addend) { |
| 220 | switch (Type) { |
| 221 | case ELF::R_390_32: |
| 222 | return (S + Addend) & 0xFFFFFFFF; |
| 223 | case ELF::R_390_64: |
| 224 | return S + Addend; |
| 225 | default: |
| 226 | llvm_unreachable("Invalid relocation type" ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static bool supportsSparc64(uint64_t Type) { |
| 231 | switch (Type) { |
| 232 | case ELF::R_SPARC_32: |
| 233 | case ELF::R_SPARC_64: |
| 234 | case ELF::R_SPARC_UA32: |
| 235 | case ELF::R_SPARC_UA64: |
| 236 | return true; |
| 237 | default: |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static uint64_t resolveSparc64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 243 | uint64_t /*LocData*/, int64_t Addend) { |
| 244 | switch (Type) { |
| 245 | case ELF::R_SPARC_32: |
| 246 | case ELF::R_SPARC_64: |
| 247 | case ELF::R_SPARC_UA32: |
| 248 | case ELF::R_SPARC_UA64: |
| 249 | return S + Addend; |
| 250 | default: |
| 251 | llvm_unreachable("Invalid relocation type" ); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /// Returns true if \c Obj is an AMDGPU code object based solely on the value |
| 256 | /// of e_machine. |
| 257 | /// |
| 258 | /// AMDGPU code objects with an e_machine of EF_AMDGPU_MACH_NONE do not |
| 259 | /// identify their arch as either r600 or amdgcn, but we can still handle |
| 260 | /// their relocations. When we identify an ELF object with an UnknownArch, |
| 261 | /// we use isAMDGPU to check for this case. |
| 262 | static bool isAMDGPU(const ObjectFile &Obj) { |
| 263 | if (const auto *ELFObj = dyn_cast<ELFObjectFileBase>(Val: &Obj)) |
| 264 | return ELFObj->getEMachine() == ELF::EM_AMDGPU; |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | static bool supportsAmdgpu(uint64_t Type) { |
| 269 | switch (Type) { |
| 270 | case ELF::R_AMDGPU_ABS32: |
| 271 | case ELF::R_AMDGPU_ABS64: |
| 272 | return true; |
| 273 | default: |
| 274 | return false; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static uint64_t resolveAmdgpu(uint64_t Type, uint64_t Offset, uint64_t S, |
| 279 | uint64_t LocData, int64_t Addend) { |
| 280 | assert((LocData == 0 || Addend == 0) && |
| 281 | "one of LocData and Addend must be 0" ); |
| 282 | switch (Type) { |
| 283 | case ELF::R_AMDGPU_ABS32: |
| 284 | case ELF::R_AMDGPU_ABS64: |
| 285 | return S + LocData + Addend; |
| 286 | default: |
| 287 | llvm_unreachable("Invalid relocation type" ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static bool supportsX86(uint64_t Type) { |
| 292 | switch (Type) { |
| 293 | case ELF::R_386_NONE: |
| 294 | case ELF::R_386_32: |
| 295 | case ELF::R_386_PC32: |
| 296 | return true; |
| 297 | default: |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static uint64_t resolveX86(uint64_t Type, uint64_t Offset, uint64_t S, |
| 303 | uint64_t LocData, int64_t /*Addend*/) { |
| 304 | switch (Type) { |
| 305 | case ELF::R_386_NONE: |
| 306 | return LocData; |
| 307 | case ELF::R_386_32: |
| 308 | return S + LocData; |
| 309 | case ELF::R_386_PC32: |
| 310 | return S - Offset + LocData; |
| 311 | default: |
| 312 | llvm_unreachable("Invalid relocation type" ); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static bool supportsPPC32(uint64_t Type) { |
| 317 | switch (Type) { |
| 318 | case ELF::R_PPC_ADDR32: |
| 319 | case ELF::R_PPC_REL32: |
| 320 | return true; |
| 321 | default: |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static uint64_t resolvePPC32(uint64_t Type, uint64_t Offset, uint64_t S, |
| 327 | uint64_t /*LocData*/, int64_t Addend) { |
| 328 | switch (Type) { |
| 329 | case ELF::R_PPC_ADDR32: |
| 330 | return (S + Addend) & 0xFFFFFFFF; |
| 331 | case ELF::R_PPC_REL32: |
| 332 | return (S + Addend - Offset) & 0xFFFFFFFF; |
| 333 | } |
| 334 | llvm_unreachable("Invalid relocation type" ); |
| 335 | } |
| 336 | |
| 337 | static bool supportsARM(uint64_t Type) { |
| 338 | switch (Type) { |
| 339 | case ELF::R_ARM_ABS32: |
| 340 | case ELF::R_ARM_REL32: |
| 341 | return true; |
| 342 | default: |
| 343 | return false; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | static uint64_t resolveARM(uint64_t Type, uint64_t Offset, uint64_t S, |
| 348 | uint64_t LocData, int64_t Addend) { |
| 349 | // Support both RELA and REL relocations. The caller is responsible |
| 350 | // for supplying the correct values for LocData and Addend, i.e. |
| 351 | // Addend == 0 for REL and LocData == 0 for RELA. |
| 352 | assert((LocData == 0 || Addend == 0) && |
| 353 | "one of LocData and Addend must be 0" ); |
| 354 | switch (Type) { |
| 355 | case ELF::R_ARM_ABS32: |
| 356 | return (S + LocData + Addend) & 0xFFFFFFFF; |
| 357 | case ELF::R_ARM_REL32: |
| 358 | return (S + LocData + Addend - Offset) & 0xFFFFFFFF; |
| 359 | } |
| 360 | llvm_unreachable("Invalid relocation type" ); |
| 361 | } |
| 362 | |
| 363 | static bool supportsAVR(uint64_t Type) { |
| 364 | switch (Type) { |
| 365 | case ELF::R_AVR_16: |
| 366 | case ELF::R_AVR_32: |
| 367 | return true; |
| 368 | default: |
| 369 | return false; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | static uint64_t resolveAVR(uint64_t Type, uint64_t Offset, uint64_t S, |
| 374 | uint64_t /*LocData*/, int64_t Addend) { |
| 375 | switch (Type) { |
| 376 | case ELF::R_AVR_16: |
| 377 | return (S + Addend) & 0xFFFF; |
| 378 | case ELF::R_AVR_32: |
| 379 | return (S + Addend) & 0xFFFFFFFF; |
| 380 | default: |
| 381 | llvm_unreachable("Invalid relocation type" ); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static bool supportsLanai(uint64_t Type) { |
| 386 | return Type == ELF::R_LANAI_32; |
| 387 | } |
| 388 | |
| 389 | static uint64_t resolveLanai(uint64_t Type, uint64_t Offset, uint64_t S, |
| 390 | uint64_t /*LocData*/, int64_t Addend) { |
| 391 | if (Type == ELF::R_LANAI_32) |
| 392 | return (S + Addend) & 0xFFFFFFFF; |
| 393 | llvm_unreachable("Invalid relocation type" ); |
| 394 | } |
| 395 | |
| 396 | static bool supportsMips32(uint64_t Type) { |
| 397 | switch (Type) { |
| 398 | case ELF::R_MIPS_32: |
| 399 | case ELF::R_MIPS_TLS_DTPREL32: |
| 400 | return true; |
| 401 | default: |
| 402 | return false; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static uint64_t resolveMips32(uint64_t Type, uint64_t Offset, uint64_t S, |
| 407 | uint64_t LocData, int64_t /*Addend*/) { |
| 408 | // FIXME: Take in account implicit addends to get correct results. |
| 409 | if (Type == ELF::R_MIPS_32) |
| 410 | return (S + LocData) & 0xFFFFFFFF; |
| 411 | if (Type == ELF::R_MIPS_TLS_DTPREL32) |
| 412 | return (S + LocData) & 0xFFFFFFFF; |
| 413 | llvm_unreachable("Invalid relocation type" ); |
| 414 | } |
| 415 | |
| 416 | static bool supportsSparc32(uint64_t Type) { |
| 417 | switch (Type) { |
| 418 | case ELF::R_SPARC_32: |
| 419 | case ELF::R_SPARC_UA32: |
| 420 | return true; |
| 421 | default: |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static uint64_t resolveSparc32(uint64_t Type, uint64_t Offset, uint64_t S, |
| 427 | uint64_t LocData, int64_t Addend) { |
| 428 | if (Type == ELF::R_SPARC_32 || Type == ELF::R_SPARC_UA32) |
| 429 | return S + Addend; |
| 430 | return LocData; |
| 431 | } |
| 432 | |
| 433 | static bool supportsHexagon(uint64_t Type) { |
| 434 | return Type == ELF::R_HEX_32; |
| 435 | } |
| 436 | |
| 437 | static uint64_t resolveHexagon(uint64_t Type, uint64_t Offset, uint64_t S, |
| 438 | uint64_t /*LocData*/, int64_t Addend) { |
| 439 | if (Type == ELF::R_HEX_32) |
| 440 | return S + Addend; |
| 441 | llvm_unreachable("Invalid relocation type" ); |
| 442 | } |
| 443 | |
| 444 | static bool supportsRISCV(uint64_t Type) { |
| 445 | switch (Type) { |
| 446 | case ELF::R_RISCV_NONE: |
| 447 | case ELF::R_RISCV_32: |
| 448 | case ELF::R_RISCV_32_PCREL: |
| 449 | case ELF::R_RISCV_64: |
| 450 | case ELF::R_RISCV_SET6: |
| 451 | case ELF::R_RISCV_SET8: |
| 452 | case ELF::R_RISCV_SUB6: |
| 453 | case ELF::R_RISCV_ADD8: |
| 454 | case ELF::R_RISCV_SUB8: |
| 455 | case ELF::R_RISCV_SET16: |
| 456 | case ELF::R_RISCV_ADD16: |
| 457 | case ELF::R_RISCV_SUB16: |
| 458 | case ELF::R_RISCV_SET32: |
| 459 | case ELF::R_RISCV_ADD32: |
| 460 | case ELF::R_RISCV_SUB32: |
| 461 | case ELF::R_RISCV_ADD64: |
| 462 | case ELF::R_RISCV_SUB64: |
| 463 | // Because the unrelocated value generated by .uleb128 A-B (used by |
| 464 | // loclists/rnglists) is meaningful, DebugInfoDWARF does not inspect the |
| 465 | // relocations. We declare support for the two relocation types without an |
| 466 | // (unreachable) implementation. |
| 467 | case ELF::R_RISCV_SET_ULEB128: |
| 468 | case ELF::R_RISCV_SUB_ULEB128: |
| 469 | return true; |
| 470 | default: |
| 471 | return false; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | static uint64_t resolveRISCV(uint64_t Type, uint64_t Offset, uint64_t S, |
| 476 | uint64_t LocData, int64_t Addend) { |
| 477 | int64_t RA = Addend; |
| 478 | uint64_t A = LocData; |
| 479 | switch (Type) { |
| 480 | case ELF::R_RISCV_NONE: |
| 481 | return LocData; |
| 482 | case ELF::R_RISCV_32: |
| 483 | return (S + RA) & 0xFFFFFFFF; |
| 484 | case ELF::R_RISCV_32_PCREL: |
| 485 | return (S + RA - Offset) & 0xFFFFFFFF; |
| 486 | case ELF::R_RISCV_64: |
| 487 | return S + RA; |
| 488 | case ELF::R_RISCV_SET6: |
| 489 | return (A & 0xC0) | ((S + RA) & 0x3F); |
| 490 | case ELF::R_RISCV_SUB6: |
| 491 | return (A & 0xC0) | (((A & 0x3F) - (S + RA)) & 0x3F); |
| 492 | case ELF::R_RISCV_SET8: |
| 493 | return (S + RA) & 0xFF; |
| 494 | case ELF::R_RISCV_ADD8: |
| 495 | return (A + (S + RA)) & 0xFF; |
| 496 | case ELF::R_RISCV_SUB8: |
| 497 | return (A - (S + RA)) & 0xFF; |
| 498 | case ELF::R_RISCV_SET16: |
| 499 | return (S + RA) & 0xFFFF; |
| 500 | case ELF::R_RISCV_ADD16: |
| 501 | return (A + (S + RA)) & 0xFFFF; |
| 502 | case ELF::R_RISCV_SUB16: |
| 503 | return (A - (S + RA)) & 0xFFFF; |
| 504 | case ELF::R_RISCV_SET32: |
| 505 | return (S + RA) & 0xFFFFFFFF; |
| 506 | case ELF::R_RISCV_ADD32: |
| 507 | return (A + (S + RA)) & 0xFFFFFFFF; |
| 508 | case ELF::R_RISCV_SUB32: |
| 509 | return (A - (S + RA)) & 0xFFFFFFFF; |
| 510 | case ELF::R_RISCV_ADD64: |
| 511 | return (A + (S + RA)); |
| 512 | case ELF::R_RISCV_SUB64: |
| 513 | return (A - (S + RA)); |
| 514 | default: |
| 515 | llvm_unreachable("Invalid relocation type" ); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static bool supportsCSKY(uint64_t Type) { |
| 520 | switch (Type) { |
| 521 | case ELF::R_CKCORE_NONE: |
| 522 | case ELF::R_CKCORE_ADDR32: |
| 523 | case ELF::R_CKCORE_PCREL32: |
| 524 | return true; |
| 525 | default: |
| 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static uint64_t resolveCSKY(uint64_t Type, uint64_t Offset, uint64_t S, |
| 531 | uint64_t LocData, int64_t Addend) { |
| 532 | switch (Type) { |
| 533 | case ELF::R_CKCORE_NONE: |
| 534 | return LocData; |
| 535 | case ELF::R_CKCORE_ADDR32: |
| 536 | return (S + Addend) & 0xFFFFFFFF; |
| 537 | case ELF::R_CKCORE_PCREL32: |
| 538 | return (S + Addend - Offset) & 0xFFFFFFFF; |
| 539 | default: |
| 540 | llvm_unreachable("Invalid relocation type" ); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | static bool supportsLoongArch(uint64_t Type) { |
| 545 | switch (Type) { |
| 546 | case ELF::R_LARCH_NONE: |
| 547 | case ELF::R_LARCH_32: |
| 548 | case ELF::R_LARCH_32_PCREL: |
| 549 | case ELF::R_LARCH_64: |
| 550 | case ELF::R_LARCH_ADD6: |
| 551 | case ELF::R_LARCH_SUB6: |
| 552 | case ELF::R_LARCH_ADD8: |
| 553 | case ELF::R_LARCH_SUB8: |
| 554 | case ELF::R_LARCH_ADD16: |
| 555 | case ELF::R_LARCH_SUB16: |
| 556 | case ELF::R_LARCH_ADD32: |
| 557 | case ELF::R_LARCH_SUB32: |
| 558 | case ELF::R_LARCH_ADD64: |
| 559 | case ELF::R_LARCH_SUB64: |
| 560 | return true; |
| 561 | default: |
| 562 | return false; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | static uint64_t resolveLoongArch(uint64_t Type, uint64_t Offset, uint64_t S, |
| 567 | uint64_t LocData, int64_t Addend) { |
| 568 | switch (Type) { |
| 569 | case ELF::R_LARCH_NONE: |
| 570 | return LocData; |
| 571 | case ELF::R_LARCH_32: |
| 572 | return (S + Addend) & 0xFFFFFFFF; |
| 573 | case ELF::R_LARCH_32_PCREL: |
| 574 | return (S + Addend - Offset) & 0xFFFFFFFF; |
| 575 | case ELF::R_LARCH_64: |
| 576 | return S + Addend; |
| 577 | case ELF::R_LARCH_ADD6: |
| 578 | return (LocData & 0xC0) | ((LocData + S + Addend) & 0x3F); |
| 579 | case ELF::R_LARCH_SUB6: |
| 580 | return (LocData & 0xC0) | ((LocData - (S + Addend)) & 0x3F); |
| 581 | case ELF::R_LARCH_ADD8: |
| 582 | return (LocData + (S + Addend)) & 0xFF; |
| 583 | case ELF::R_LARCH_SUB8: |
| 584 | return (LocData - (S + Addend)) & 0xFF; |
| 585 | case ELF::R_LARCH_ADD16: |
| 586 | return (LocData + (S + Addend)) & 0xFFFF; |
| 587 | case ELF::R_LARCH_SUB16: |
| 588 | return (LocData - (S + Addend)) & 0xFFFF; |
| 589 | case ELF::R_LARCH_ADD32: |
| 590 | return (LocData + (S + Addend)) & 0xFFFFFFFF; |
| 591 | case ELF::R_LARCH_SUB32: |
| 592 | return (LocData - (S + Addend)) & 0xFFFFFFFF; |
| 593 | case ELF::R_LARCH_ADD64: |
| 594 | return (LocData + (S + Addend)); |
| 595 | case ELF::R_LARCH_SUB64: |
| 596 | return (LocData - (S + Addend)); |
| 597 | default: |
| 598 | llvm_unreachable("Invalid relocation type" ); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | static bool supportsCOFFX86(uint64_t Type) { |
| 603 | switch (Type) { |
| 604 | case COFF::IMAGE_REL_I386_SECREL: |
| 605 | case COFF::IMAGE_REL_I386_DIR32: |
| 606 | return true; |
| 607 | default: |
| 608 | return false; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S, |
| 613 | uint64_t LocData, int64_t /*Addend*/) { |
| 614 | switch (Type) { |
| 615 | case COFF::IMAGE_REL_I386_SECREL: |
| 616 | case COFF::IMAGE_REL_I386_DIR32: |
| 617 | return (S + LocData) & 0xFFFFFFFF; |
| 618 | default: |
| 619 | llvm_unreachable("Invalid relocation type" ); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static bool supportsCOFFX86_64(uint64_t Type) { |
| 624 | switch (Type) { |
| 625 | case COFF::IMAGE_REL_AMD64_SECREL: |
| 626 | case COFF::IMAGE_REL_AMD64_ADDR64: |
| 627 | return true; |
| 628 | default: |
| 629 | return false; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 634 | uint64_t LocData, int64_t /*Addend*/) { |
| 635 | switch (Type) { |
| 636 | case COFF::IMAGE_REL_AMD64_SECREL: |
| 637 | return (S + LocData) & 0xFFFFFFFF; |
| 638 | case COFF::IMAGE_REL_AMD64_ADDR64: |
| 639 | return S + LocData; |
| 640 | default: |
| 641 | llvm_unreachable("Invalid relocation type" ); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | static bool supportsCOFFARM(uint64_t Type) { |
| 646 | switch (Type) { |
| 647 | case COFF::IMAGE_REL_ARM_SECREL: |
| 648 | case COFF::IMAGE_REL_ARM_ADDR32: |
| 649 | return true; |
| 650 | default: |
| 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S, |
| 656 | uint64_t LocData, int64_t /*Addend*/) { |
| 657 | switch (Type) { |
| 658 | case COFF::IMAGE_REL_ARM_SECREL: |
| 659 | case COFF::IMAGE_REL_ARM_ADDR32: |
| 660 | return (S + LocData) & 0xFFFFFFFF; |
| 661 | default: |
| 662 | llvm_unreachable("Invalid relocation type" ); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | static bool supportsCOFFARM64(uint64_t Type) { |
| 667 | switch (Type) { |
| 668 | case COFF::IMAGE_REL_ARM64_SECREL: |
| 669 | case COFF::IMAGE_REL_ARM64_ADDR64: |
| 670 | return true; |
| 671 | default: |
| 672 | return false; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 677 | uint64_t LocData, int64_t /*Addend*/) { |
| 678 | switch (Type) { |
| 679 | case COFF::IMAGE_REL_ARM64_SECREL: |
| 680 | return (S + LocData) & 0xFFFFFFFF; |
| 681 | case COFF::IMAGE_REL_ARM64_ADDR64: |
| 682 | return S + LocData; |
| 683 | default: |
| 684 | llvm_unreachable("Invalid relocation type" ); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | static bool supportsMachOX86_64(uint64_t Type) { |
| 689 | return Type == MachO::X86_64_RELOC_UNSIGNED; |
| 690 | } |
| 691 | |
| 692 | static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 693 | uint64_t LocData, int64_t /*Addend*/) { |
| 694 | if (Type == MachO::X86_64_RELOC_UNSIGNED) |
| 695 | return S; |
| 696 | llvm_unreachable("Invalid relocation type" ); |
| 697 | } |
| 698 | |
| 699 | static bool supportsWasm32(uint64_t Type) { |
| 700 | switch (Type) { |
| 701 | case wasm::R_WASM_FUNCTION_INDEX_LEB: |
| 702 | case wasm::R_WASM_TABLE_INDEX_SLEB: |
| 703 | case wasm::R_WASM_TABLE_INDEX_I32: |
| 704 | case wasm::R_WASM_MEMORY_ADDR_LEB: |
| 705 | case wasm::R_WASM_MEMORY_ADDR_SLEB: |
| 706 | case wasm::R_WASM_MEMORY_ADDR_I32: |
| 707 | case wasm::R_WASM_TYPE_INDEX_LEB: |
| 708 | case wasm::R_WASM_GLOBAL_INDEX_LEB: |
| 709 | case wasm::R_WASM_FUNCTION_OFFSET_I32: |
| 710 | case wasm::R_WASM_SECTION_OFFSET_I32: |
| 711 | case wasm::R_WASM_TAG_INDEX_LEB: |
| 712 | case wasm::R_WASM_GLOBAL_INDEX_I32: |
| 713 | case wasm::R_WASM_TABLE_NUMBER_LEB: |
| 714 | case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32: |
| 715 | return true; |
| 716 | default: |
| 717 | return false; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | static bool supportsWasm64(uint64_t Type) { |
| 722 | switch (Type) { |
| 723 | case wasm::R_WASM_MEMORY_ADDR_LEB64: |
| 724 | case wasm::R_WASM_MEMORY_ADDR_SLEB64: |
| 725 | case wasm::R_WASM_MEMORY_ADDR_I64: |
| 726 | case wasm::R_WASM_TABLE_INDEX_SLEB64: |
| 727 | case wasm::R_WASM_TABLE_INDEX_I64: |
| 728 | case wasm::R_WASM_FUNCTION_OFFSET_I64: |
| 729 | return true; |
| 730 | default: |
| 731 | return supportsWasm32(Type); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S, |
| 736 | uint64_t LocData, int64_t /*Addend*/) { |
| 737 | switch (Type) { |
| 738 | case wasm::R_WASM_FUNCTION_INDEX_LEB: |
| 739 | case wasm::R_WASM_TABLE_INDEX_SLEB: |
| 740 | case wasm::R_WASM_TABLE_INDEX_I32: |
| 741 | case wasm::R_WASM_MEMORY_ADDR_LEB: |
| 742 | case wasm::R_WASM_MEMORY_ADDR_SLEB: |
| 743 | case wasm::R_WASM_MEMORY_ADDR_I32: |
| 744 | case wasm::R_WASM_TYPE_INDEX_LEB: |
| 745 | case wasm::R_WASM_GLOBAL_INDEX_LEB: |
| 746 | case wasm::R_WASM_FUNCTION_OFFSET_I32: |
| 747 | case wasm::R_WASM_SECTION_OFFSET_I32: |
| 748 | case wasm::R_WASM_TAG_INDEX_LEB: |
| 749 | case wasm::R_WASM_GLOBAL_INDEX_I32: |
| 750 | case wasm::R_WASM_TABLE_NUMBER_LEB: |
| 751 | case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32: |
| 752 | // For wasm section, its offset at 0 -- ignoring Value |
| 753 | return LocData; |
| 754 | default: |
| 755 | llvm_unreachable("Invalid relocation type" ); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S, |
| 760 | uint64_t LocData, int64_t Addend) { |
| 761 | switch (Type) { |
| 762 | case wasm::R_WASM_MEMORY_ADDR_LEB64: |
| 763 | case wasm::R_WASM_MEMORY_ADDR_SLEB64: |
| 764 | case wasm::R_WASM_MEMORY_ADDR_I64: |
| 765 | case wasm::R_WASM_TABLE_INDEX_SLEB64: |
| 766 | case wasm::R_WASM_TABLE_INDEX_I64: |
| 767 | case wasm::R_WASM_FUNCTION_OFFSET_I64: |
| 768 | // For wasm section, its offset at 0 -- ignoring Value |
| 769 | return LocData; |
| 770 | default: |
| 771 | return resolveWasm32(Type, Offset, S, LocData, Addend); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | std::pair<SupportsRelocation, RelocationResolver> |
| 776 | getRelocationResolver(const ObjectFile &Obj) { |
| 777 | if (Obj.isCOFF()) { |
| 778 | switch (Obj.getArch()) { |
| 779 | case Triple::x86_64: |
| 780 | return {supportsCOFFX86_64, resolveCOFFX86_64}; |
| 781 | case Triple::x86: |
| 782 | return {supportsCOFFX86, resolveCOFFX86}; |
| 783 | case Triple::arm: |
| 784 | case Triple::thumb: |
| 785 | return {supportsCOFFARM, resolveCOFFARM}; |
| 786 | case Triple::aarch64: |
| 787 | return {supportsCOFFARM64, resolveCOFFARM64}; |
| 788 | default: |
| 789 | return {nullptr, nullptr}; |
| 790 | } |
| 791 | } else if (Obj.isELF()) { |
| 792 | if (Obj.getBytesInAddress() == 8) { |
| 793 | switch (Obj.getArch()) { |
| 794 | case Triple::x86_64: |
| 795 | return {supportsX86_64, resolveX86_64}; |
| 796 | case Triple::aarch64: |
| 797 | case Triple::aarch64_be: |
| 798 | return {supportsAArch64, resolveAArch64}; |
| 799 | case Triple::bpfel: |
| 800 | case Triple::bpfeb: |
| 801 | return {supportsBPF, resolveBPF}; |
| 802 | case Triple::loongarch64: |
| 803 | return {supportsLoongArch, resolveLoongArch}; |
| 804 | case Triple::mips64el: |
| 805 | case Triple::mips64: |
| 806 | return {supportsMips64, resolveMips64}; |
| 807 | case Triple::ppc64le: |
| 808 | case Triple::ppc64: |
| 809 | return {supportsPPC64, resolvePPC64}; |
| 810 | case Triple::systemz: |
| 811 | return {supportsSystemZ, resolveSystemZ}; |
| 812 | case Triple::sparcv9: |
| 813 | return {supportsSparc64, resolveSparc64}; |
| 814 | case Triple::amdgcn: |
| 815 | return {supportsAmdgpu, resolveAmdgpu}; |
| 816 | case Triple::riscv64: |
| 817 | case Triple::riscv64be: |
| 818 | return {supportsRISCV, resolveRISCV}; |
| 819 | default: |
| 820 | if (isAMDGPU(Obj)) |
| 821 | return {supportsAmdgpu, resolveAmdgpu}; |
| 822 | return {nullptr, nullptr}; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | // 32-bit object file |
| 827 | assert(Obj.getBytesInAddress() == 4 && |
| 828 | "Invalid word size in object file" ); |
| 829 | |
| 830 | switch (Obj.getArch()) { |
| 831 | case Triple::x86: |
| 832 | return {supportsX86, resolveX86}; |
| 833 | case Triple::ppcle: |
| 834 | case Triple::ppc: |
| 835 | return {supportsPPC32, resolvePPC32}; |
| 836 | case Triple::arm: |
| 837 | case Triple::armeb: |
| 838 | return {supportsARM, resolveARM}; |
| 839 | case Triple::avr: |
| 840 | return {supportsAVR, resolveAVR}; |
| 841 | case Triple::lanai: |
| 842 | return {supportsLanai, resolveLanai}; |
| 843 | case Triple::loongarch32: |
| 844 | return {supportsLoongArch, resolveLoongArch}; |
| 845 | case Triple::mipsel: |
| 846 | case Triple::mips: |
| 847 | return {supportsMips32, resolveMips32}; |
| 848 | case Triple::msp430: |
| 849 | return {supportsMSP430, resolveMSP430}; |
| 850 | case Triple::sparc: |
| 851 | return {supportsSparc32, resolveSparc32}; |
| 852 | case Triple::hexagon: |
| 853 | return {supportsHexagon, resolveHexagon}; |
| 854 | case Triple::r600: |
| 855 | return {supportsAmdgpu, resolveAmdgpu}; |
| 856 | case Triple::riscv32: |
| 857 | case Triple::riscv32be: |
| 858 | return {supportsRISCV, resolveRISCV}; |
| 859 | case Triple::csky: |
| 860 | return {supportsCSKY, resolveCSKY}; |
| 861 | default: |
| 862 | if (isAMDGPU(Obj)) |
| 863 | return {supportsAmdgpu, resolveAmdgpu}; |
| 864 | return {nullptr, nullptr}; |
| 865 | } |
| 866 | } else if (Obj.isMachO()) { |
| 867 | if (Obj.getArch() == Triple::x86_64) |
| 868 | return {supportsMachOX86_64, resolveMachOX86_64}; |
| 869 | return {nullptr, nullptr}; |
| 870 | } else if (Obj.isWasm()) { |
| 871 | if (Obj.getArch() == Triple::wasm32) |
| 872 | return {supportsWasm32, resolveWasm32}; |
| 873 | if (Obj.getArch() == Triple::wasm64) |
| 874 | return {supportsWasm64, resolveWasm64}; |
| 875 | return {nullptr, nullptr}; |
| 876 | } |
| 877 | |
| 878 | llvm_unreachable("Invalid object file" ); |
| 879 | } |
| 880 | |
| 881 | uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, |
| 882 | uint64_t S, uint64_t LocData) { |
| 883 | if (const ObjectFile *Obj = R.getObject()) { |
| 884 | int64_t Addend = 0; |
| 885 | if (Obj->isELF()) { |
| 886 | auto GetRelSectionType = [&]() -> unsigned { |
| 887 | if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Val: Obj)) |
| 888 | return Elf32LEObj->getRelSection(Rel: R.getRawDataRefImpl())->sh_type; |
| 889 | if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Val: Obj)) |
| 890 | return Elf64LEObj->getRelSection(Rel: R.getRawDataRefImpl())->sh_type; |
| 891 | if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Val: Obj)) |
| 892 | return Elf32BEObj->getRelSection(Rel: R.getRawDataRefImpl())->sh_type; |
| 893 | auto *Elf64BEObj = cast<ELF64BEObjectFile>(Val: Obj); |
| 894 | return Elf64BEObj->getRelSection(Rel: R.getRawDataRefImpl())->sh_type; |
| 895 | }; |
| 896 | |
| 897 | if (GetRelSectionType() == ELF::SHT_RELA || |
| 898 | GetRelSectionType() == ELF::SHT_CREL) { |
| 899 | Addend = getELFAddend(R); |
| 900 | // LoongArch and RISCV relocations use both LocData and Addend. |
| 901 | if (Obj->getArch() != Triple::loongarch32 && |
| 902 | Obj->getArch() != Triple::loongarch64 && |
| 903 | Obj->getArch() != Triple::riscv32 && |
| 904 | Obj->getArch() != Triple::riscv64 && |
| 905 | Obj->getArch() != Triple::riscv32be && |
| 906 | Obj->getArch() != Triple::riscv64be) |
| 907 | LocData = 0; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | return Resolver(R.getType(), R.getOffset(), S, LocData, Addend); |
| 912 | } |
| 913 | |
| 914 | // Sometimes the caller might want to use its own specific implementation of |
| 915 | // the resolver function. E.g. this is used by LLD when it resolves debug |
| 916 | // relocations and assumes that all of them have the same computation (S + A). |
| 917 | // The relocation R has no owner object in this case and we don't need to |
| 918 | // provide Type and Offset fields. It is also assumed the DataRefImpl.p |
| 919 | // contains the addend, provided by the caller. |
| 920 | return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData, |
| 921 | R.getRawDataRefImpl().p); |
| 922 | } |
| 923 | |
| 924 | } // namespace object |
| 925 | } // namespace llvm |
| 926 | |