Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loongarch64 #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.luo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Loongarch64

指令参考 https://www.kernel.org/doc/html/latest/translations/zh_CN/arch/loongarch/introduction.html

我使用了bl指令,没有使用jirl指令 计算offset的时候花了点时间,需要注意先移位然后高低位互换的情况,主要是我使用的编译器8.3,提示我很多指令不支持,导致无法很好的研究指令特性
6 changes: 6 additions & 0 deletions src/linux/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ int injector__collect_libc_information(injector_t *injector)
injector->sys_munmap = 215;
break;
#endif
case EM_LOONGARCH:
injector->arch = ARCH_LOONGARCH_64;
injector->sys_mmap = 222;
injector->sys_mprotect = 226;
injector->sys_munmap = 215;
break;
default:
injector__set_errmsg("Unknown target process architecture: 0x%04x", ehdr.e_machine);
rv = INJERR_UNSUPPORTED_TARGET;
Expand Down
3 changes: 3 additions & 0 deletions src/linux/injector_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ typedef enum {
ARCH_POWERPC,
ARCH_RISCV_64,
ARCH_RISCV_32,
ARCH_LOONGARCH_64,
} arch_t;

typedef union {
Expand All @@ -105,6 +106,8 @@ typedef union {
uint32_t u32[2];
#elif defined(__riscv)
uint32_t u32[2];
#elif defined(__loongarch64)
uint32_t u32[2];
#endif
long dummy;
} code_t;
Expand Down
2 changes: 1 addition & 1 deletion src/linux/ptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
#include "injector_internal.h"

#if defined(__aarch64__) || defined(__riscv)
#if defined(__aarch64__) || defined(__riscv) || defined(__loongarch64)
#define USE_REGSET
#include <elf.h> /* for NT_PRSTATUS */
#include <sys/uio.h> /* for struct iovec */
Expand Down
41 changes: 41 additions & 0 deletions src/linux/remote_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ static void print_regs(const injector_t *injector, const struct user_regs_struct
typedef uint64_t user_reg_t;
#elif defined(__riscv)
typedef unsigned long user_reg_t;
#elif defined(__loongarch64)
typedef uint64_t user_reg_t;
#elif defined(__LP64__) && !defined(MUSL_LIBC)
typedef unsigned long long user_reg_t;
#elif defined(__i386__)
Expand Down Expand Up @@ -425,6 +427,23 @@ int injector__call_syscall(const injector_t *injector, intptr_t *retval, long sy
reg_return = &regs.a0;
break;
#endif

#if defined(__loongarch64)
case ARCH_LOONGARCH_64:
code.u32[0] = 0x002b0000; /* syscall */
code.u32[1] = 0x002a0000; /* break */
code_size = 2 * 4;
regs.pc = injector->code_addr;
regs.gpr[4+0] = arg1;
regs.gpr[4+1] = arg2;
regs.gpr[4+2] = arg3;
regs.gpr[4+3] = arg4;
regs.gpr[4+4] = arg5;
regs.gpr[4+5] = arg6;
regs.gpr[4+7] = syscall_number;
reg_return = &regs.gpr[4];
break;
#endif
default:
injector__set_errmsg("Unexpected architecture: %s", injector__arch2name(injector->arch));
return INJERR_UNSUPPORTED_TARGET;
Expand Down Expand Up @@ -712,6 +731,28 @@ int injector__call_function_va_list(const injector_t *injector, intptr_t *retval
reg_return = &regs.a0;
break;
#endif
#if defined(__loongarch64)
case ARCH_LOONGARCH_64:
{
unsigned int offset = function_addr - injector->code_addr;

offset = (offset & 0x3FFFFFF) >> 2;
code.u32[0] = 0x54000000 | ((offset & 0xFFFF) << 10)| ((offset & 0xFF0000) >> 16);
}
code.u32[1] = 0x002a0000; /* break */
code_size = 2 * 4;
regs.pc = injector->code_addr;
regs.gpr[22] = injector->stack + injector->stack_size - 32; // SP
regs.gpr[4+0] = arg1;
regs.gpr[4+1] = arg2;
regs.gpr[4+2] = arg3;
regs.gpr[4+3] = arg4;
regs.gpr[4+4] = arg5;
regs.gpr[4+5] = arg6;
reg_return = &regs.gpr[4];
break;
#endif

default:
injector__set_errmsg("Unexpected architecture: %s", injector__arch2name(injector->arch));
return -1;
Expand Down
2 changes: 2 additions & 0 deletions src/linux/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const char *injector__arch2name(arch_t arch)
return "RISC-V 64";
case ARCH_RISCV_32:
return "RISC-V 32";
case ARCH_LOONGARCH_64:
return "LOONGARCH 64";
}
return "?";
}