|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 XiaoMi Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + */ |
| 5 | + |
| 6 | +#include "aot_reloc.h" |
| 7 | + |
| 8 | +#define R_RISCV_CALL 18 |
| 9 | + |
| 10 | +static SymbolMap target_sym_map[] = { REG_COMMON_SYMBOLS }; |
| 11 | + |
| 12 | +void |
| 13 | +get_current_target(char *target_buf, uint32 target_buf_size) |
| 14 | +{ |
| 15 | + snprintf(target_buf, target_buf_size, "riscv"); |
| 16 | +} |
| 17 | + |
| 18 | +uint32 |
| 19 | +get_plt_item_size() |
| 20 | +{ |
| 21 | + return 16; |
| 22 | +} |
| 23 | + |
| 24 | +SymbolMap * |
| 25 | +get_target_symbol_map(uint32 *sym_num) |
| 26 | +{ |
| 27 | + *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap); |
| 28 | + return target_sym_map; |
| 29 | +} |
| 30 | + |
| 31 | +uint32 |
| 32 | +get_plt_table_size() |
| 33 | +{ |
| 34 | + return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); |
| 35 | +} |
| 36 | + |
| 37 | +void |
| 38 | +init_plt_table(uint8 *plt) |
| 39 | +{} |
| 40 | + |
| 41 | +bool |
| 42 | +apply_relocation(AOTModule *module, |
| 43 | + uint8 *target_section_addr, |
| 44 | + uint32 target_section_size, |
| 45 | + uint64 reloc_offset, |
| 46 | + uint64 reloc_addend, |
| 47 | + uint32 reloc_type, |
| 48 | + void *symbol_addr, |
| 49 | + int32 symbol_index, |
| 50 | + char *error_buf, |
| 51 | + uint32 error_buf_size) |
| 52 | +{ |
| 53 | + uint8 *P = target_section_addr + reloc_offset; |
| 54 | + uint32 hi20, lo12; |
| 55 | + |
| 56 | + hi20 = (uint32)((unsigned long)symbol_addr & 0xfffff000); |
| 57 | + lo12 = (uint32)(((unsigned long)symbol_addr & 0xfff) << 20); |
| 58 | + |
| 59 | + switch (reloc_type) { |
| 60 | + case R_RISCV_CALL: |
| 61 | + if ((uint32)(uintptr_t)symbol_addr != (uintptr_t)symbol_addr) { |
| 62 | + if (error_buf != NULL) { |
| 63 | + snprintf( |
| 64 | + error_buf, error_buf_size, |
| 65 | + "Jump address exceeds 32-bit address space (0-4GB)."); |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + /* lui t0, hi20 */ |
| 71 | + *(uint32 *)(P + 0) = (uint32)hi20 | 0x2b7; |
| 72 | + /* jalr ra, lo12(t0) */ |
| 73 | + *(uint32 *)(P + 4) = (uint32)lo12 | 0x280e7; |
| 74 | + break; |
| 75 | + |
| 76 | + default: |
| 77 | + if (error_buf != NULL) |
| 78 | + snprintf(error_buf, error_buf_size, |
| 79 | + "Load relocation section failed: " |
| 80 | + "invalid relocation type %ld.", |
| 81 | + reloc_type); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + return true; |
| 86 | +} |
0 commit comments