Skip to content

Commit 17a3045

Browse files
committed
Implement relocator for riscv
Signed-off-by: Huang Qi <[email protected]>
1 parent 6b4f8d3 commit 17a3045

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,8 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
10341034
/* Create each data section */
10351035
for (i = 0; i < module->data_section_count; i++) {
10361036
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
1037-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
1037+
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
1038+
|| defined(BUILD_TARGET_RISCV64)
10381039
/* aot code and data in x86_64 must be in range 0 to 2G due to
10391040
relocation for R_X86_64_32/32S/PC32 */
10401041
int map_flags = MMAP_MAP_32BIT;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

core/iwasm/aot/iwasm_aot.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ elseif (WAMR_BUILD_TARGET STREQUAL "MIPS")
2323
set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_mips.c)
2424
elseif (WAMR_BUILD_TARGET STREQUAL "XTENSA")
2525
set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_xtensa.c)
26+
elseif (WAMR_BUILD_TARGET MATCHES "RISCV*")
27+
set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_riscv.c)
2628
else ()
2729
message (FATAL_ERROR "Build target isn't set")
2830
endif ()

0 commit comments

Comments
 (0)