loongarch: Add support for ELF psABI v1.00 relocations

This patch adds support of the stack-based LoongArch relocations
throughout GRUB, including tools, dynamic linkage, and support for
conversion of ELF relocations into PE ones. A stack machine is required
to handle these per the spec [1] (see the R_LARCH_SOP types), of which
a simple implementation is included.

These relocations are produced by binutils 2.38 and 2.39, while the newer
v2.00 relocs require more recent toolchain (binutils 2.40+ & gcc 13+, or
LLVM 16+). GCC 13 has not been officially released as of early 2023, so
support for v1.00 relocs are expected to stay relevant for a while.

[1] https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_relocations

Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
This commit is contained in:
Wentao Guan
2024-08-25 15:59:19 +08:00
parent 19c95139b9
commit a12fcf90ed
7 changed files with 522 additions and 3 deletions

View File

@@ -44,6 +44,7 @@
#include <grub/arm/reloc.h>
#include <grub/arm64/reloc.h>
#include <grub/ia64/reloc.h>
#include <grub/loongarch64/reloc.h>
#include <grub/osdep/hostfile.h>
#include <grub/util/install.h>
#include <grub/util/mkimage.h>
@@ -784,6 +785,8 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
struct grub_ia64_trampoline *tr = (void *) (pe_target + tramp_off);
grub_uint64_t *gpptr = (void *) (pe_target + got_off);
unsigned unmatched_adr_got_page = 0;
struct grub_loongarch64_stack stack;
grub_loongarch64_stack_init (&stack);
#define MASK19 ((1 << 19) - 1)
#else
grub_uint32_t *tr = (void *) (pe_target + tramp_off);
@@ -1187,6 +1190,31 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
}
break;
}
case EM_LOONGARCH:
{
sym_addr += addend;
switch (ELF_R_TYPE (info))
{
case R_LARCH_64:
*target = grub_host_to_target64 (grub_target_to_host64 (*target) + sym_addr);
break;
case R_LARCH_MARK_LA:
break;
case R_LARCH_SOP_PUSH_PCREL:
case R_LARCH_SOP_PUSH_PLT_PCREL:
grub_loongarch64_sop_push (&stack, sym_addr
-(target_section_addr
+offset
+image_target->vaddr_offset));
break;
GRUB_LOONGARCH64_RELOCATION (&stack, target, sym_addr)
default:
grub_util_error (_("relocation 0x%x is not implemented yet"),
(unsigned int) ELF_R_TYPE (info));
break;
}
break;
}
#endif
#if defined(MKIMAGE_ELF32)
case EM_ARM:
@@ -1734,6 +1762,57 @@ translate_relocation_pe (struct translate_context *ctx,
break;
}
break;
case EM_LOONGARCH:
#if defined(MKIMAGE_ELF64)
switch (ELF_R_TYPE (info))
{
case R_LARCH_64:
{
ctx->current_address = add_fixup_entry (&ctx->lst,
GRUB_PE32_REL_BASED_DIR64,
addr, 0, ctx->current_address,
image_target);
}
break;
case R_LARCH_MARK_LA:
{
ctx->current_address = add_fixup_entry (&ctx->lst,
GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA,
addr, 0, ctx->current_address,
image_target);
}
break;
/* Relative relocations do not require fixup entries. */
case R_LARCH_NONE:
case R_LARCH_SOP_PUSH_PCREL:
case R_LARCH_SOP_PUSH_ABSOLUTE:
case R_LARCH_SOP_PUSH_PLT_PCREL:
case R_LARCH_SOP_SUB:
case R_LARCH_SOP_SL:
case R_LARCH_SOP_SR:
case R_LARCH_SOP_ADD:
case R_LARCH_SOP_AND:
case R_LARCH_SOP_IF_ELSE:
case R_LARCH_SOP_POP_32_S_10_5:
case R_LARCH_SOP_POP_32_U_10_12:
case R_LARCH_SOP_POP_32_S_10_12:
case R_LARCH_SOP_POP_32_S_10_16:
case R_LARCH_SOP_POP_32_S_10_16_S2:
case R_LARCH_SOP_POP_32_S_5_20:
case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
grub_util_info (" %s: not adding fixup: 0x%08x : 0x%08x",
__FUNCTION__,
(unsigned int) addr,
(unsigned int) ctx->current_address);
break;
default:
grub_util_error (_("relocation 0x%x is not implemented yet"),
(unsigned int) ELF_R_TYPE (info));
break;
}
#endif /* defined(MKIMAGE_ELF64) */
break;
#if defined(MKIMAGE_ELF64)
case EM_MIPS:
switch (ELF_R_TYPE (info))

View File

@@ -119,6 +119,32 @@ struct grub_module_verifier_arch archs[] = {
R_AARCH64_PREL32,
-1
} },
{ "loongarch64", 8, 0, EM_LOONGARCH, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
R_LARCH_NONE,
R_LARCH_64,
R_LARCH_MARK_LA,
R_LARCH_SOP_PUSH_PCREL,
R_LARCH_SOP_PUSH_ABSOLUTE,
R_LARCH_SOP_PUSH_PLT_PCREL,
R_LARCH_SOP_SUB,
R_LARCH_SOP_SL,
R_LARCH_SOP_SR,
R_LARCH_SOP_ADD,
R_LARCH_SOP_AND,
R_LARCH_SOP_IF_ELSE,
R_LARCH_SOP_POP_32_S_10_5,
R_LARCH_SOP_POP_32_U_10_12,
R_LARCH_SOP_POP_32_S_10_12,
R_LARCH_SOP_POP_32_S_10_16,
R_LARCH_SOP_POP_32_S_10_16_S2,
R_LARCH_SOP_POP_32_S_5_20,
R_LARCH_SOP_POP_32_S_0_5_10_16_S2,
R_LARCH_SOP_POP_32_S_0_10_10_16_S2,
-1
}, (int[]){
-1
}
},
{ "mips64el", 8, 0, EM_MIPS, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
R_MIPS_64,
R_MIPS_32,