| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <byteswap.h> |
| 5 |
#include <elf.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include "platform.h" // IWYU pragma: associated |
| 9 |
|
| 10 |
/* |
| 11 |
* The ABI specification can be found at: |
| 12 |
* https://github.com/riscv-non-isa/riscv-elf-psabi-doc |
| 13 |
*/ |
| 14 |
|
| 15 |
static struct drgn_error drgn_invalid_rel = { |
| 16 |
.code = DRGN_ERROR_OTHER, |
| 17 |
.message = "invalid relocation type for SHT_REL", |
| 18 |
}; |
| 19 |
|
| 20 |
static struct drgn_error * |
| 21 |
apply_elf_reloc_riscv(const struct drgn_relocating_section *relocating, |
| 22 |
uint64_t r_offset, uint32_t r_type, const int64_t *r_addend, |
| 23 |
uint64_t sym_value) |
| 24 |
{ |
| 25 |
switch (r_type) { |
| 26 |
case R_RISCV_NONE: |
| 27 |
return NULL; |
| 28 |
case R_RISCV_32: |
| 29 |
return drgn_reloc_add32(relocating, r_offset, r_addend, |
| 30 |
sym_value); |
| 31 |
case R_RISCV_64: |
| 32 |
return drgn_reloc_add64(relocating, r_offset, r_addend, |
| 33 |
sym_value); |
| 34 |
#define CASE_R_RISCV_ADD_SUB(bits) \ |
| 35 |
case R_RISCV_ADD##bits: { \ |
| 36 |
if (!r_addend) \ |
| 37 |
return &drgn_invalid_rel; \ |
| 38 |
uint##bits##_t value; \ |
| 39 |
if (r_offset > relocating->buf_size || \ |
| 40 |
relocating->buf_size - r_offset < sizeof(value)) \ |
| 41 |
return &drgn_invalid_relocation_offset; \ |
| 42 |
memcpy(&value, relocating->buf + r_offset, sizeof(value)); \ |
| 43 |
if (relocating->bswap) \ |
| 44 |
value = bswap_##bits(value); \ |
| 45 |
value += sym_value + *r_addend; \ |
| 46 |
if (relocating->bswap) \ |
| 47 |
value = bswap_##bits(value); \ |
| 48 |
memcpy(relocating->buf + r_offset, &value, sizeof(value)); \ |
| 49 |
return NULL; \ |
| 50 |
} \ |
| 51 |
case R_RISCV_SUB##bits: { \ |
| 52 |
if (!r_addend) \ |
| 53 |
return &drgn_invalid_rel; \ |
| 54 |
uint##bits##_t value; \ |
| 55 |
if (r_offset > relocating->buf_size || \ |
| 56 |
relocating->buf_size - r_offset < sizeof(value)) \ |
| 57 |
return &drgn_invalid_relocation_offset; \ |
| 58 |
memcpy(&value, relocating->buf + r_offset, sizeof(value)); \ |
| 59 |
if (relocating->bswap) \ |
| 60 |
value = bswap_##bits(value); \ |
| 61 |
value -= sym_value + *r_addend; \ |
| 62 |
if (relocating->bswap) \ |
| 63 |
value = bswap_##bits(value); \ |
| 64 |
memcpy(relocating->buf + r_offset, &value, sizeof(value)); \ |
| 65 |
return NULL; \ |
| 66 |
} |
| 67 |
#define bswap_8(x) (x) |
| 68 |
CASE_R_RISCV_ADD_SUB(8) |
| 69 |
#undef bswap_8 |
| 70 |
CASE_R_RISCV_ADD_SUB(16) |
| 71 |
CASE_R_RISCV_ADD_SUB(32) |
| 72 |
CASE_R_RISCV_ADD_SUB(64) |
| 73 |
#undef CASE_R_RISCV_ADD_SUB |
| 74 |
case R_RISCV_SUB6: { |
| 75 |
if (!r_addend) |
| 76 |
return &drgn_invalid_rel; |
| 77 |
uint8_t value; |
| 78 |
if (r_offset > relocating->buf_size || |
| 79 |
relocating->buf_size - r_offset < sizeof(value)) |
| 80 |
return &drgn_invalid_relocation_offset; |
| 81 |
memcpy(&value, relocating->buf + r_offset, sizeof(value)); |
| 82 |
value = ((value & 0xc0) | |
| 83 |
(((value & 0x3f) - (sym_value + *r_addend)) & 0x3f)); |
| 84 |
memcpy(relocating->buf + r_offset, &value, sizeof(value)); |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
case R_RISCV_SET6: { |
| 88 |
if (!r_addend) |
| 89 |
return &drgn_invalid_rel; |
| 90 |
uint8_t value; |
| 91 |
if (r_offset > relocating->buf_size || |
| 92 |
relocating->buf_size - r_offset < sizeof(value)) |
| 93 |
return &drgn_invalid_relocation_offset; |
| 94 |
memcpy(&value, relocating->buf + r_offset, sizeof(value)); |
| 95 |
value = (value & 0xc0) | ((sym_value + *r_addend) & 0x3f); |
| 96 |
memcpy(relocating->buf + r_offset, &value, sizeof(value)); |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
case R_RISCV_SET8: |
| 100 |
return drgn_reloc_add8(relocating, r_offset, r_addend, |
| 101 |
sym_value); |
| 102 |
case R_RISCV_SET16: |
| 103 |
return drgn_reloc_add16(relocating, r_offset, r_addend, |
| 104 |
sym_value); |
| 105 |
case R_RISCV_SET32: |
| 106 |
return drgn_reloc_add32(relocating, r_offset, r_addend, |
| 107 |
sym_value); |
| 108 |
default: |
| 109 |
return DRGN_UNKNOWN_RELOCATION_TYPE(r_type); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
const struct drgn_architecture_info arch_info_riscv64 = { |
| 114 |
.name = "RISC-V 64", |
| 115 |
.arch = DRGN_ARCH_RISCV64, |
| 116 |
.default_flags = (DRGN_PLATFORM_IS_64_BIT | |
| 117 |
DRGN_PLATFORM_IS_LITTLE_ENDIAN), |
| 118 |
.scalar_alignment = { 1, 2, 4, 8, 16 }, |
| 119 |
.register_by_name = drgn_register_by_name_unknown, |
| 120 |
.apply_elf_reloc = apply_elf_reloc_riscv, |
| 121 |
}; |
| 122 |
|
| 123 |
const struct drgn_architecture_info arch_info_riscv32 = { |
| 124 |
.name = "RISC-V 32", |
| 125 |
.arch = DRGN_ARCH_RISCV32, |
| 126 |
.scalar_alignment = { 1, 2, 4, 8, 16 }, |
| 127 |
.default_flags = DRGN_PLATFORM_IS_LITTLE_ENDIAN, |
| 128 |
.register_by_name = drgn_register_by_name_unknown, |
| 129 |
.apply_elf_reloc = apply_elf_reloc_riscv, |
| 130 |
}; |