module.h 2.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
 */
#ifndef _ASM_MODULE_H
#define _ASM_MODULE_H

#include <asm/inst.h>
#include <asm-generic/module.h>

#define RELA_STACK_DEPTH 16

struct mod_section {
14
	int shndx;
15 16 17 18 19
	int num_entries;
	int max_entries;
};

struct mod_arch_specific {
20
	struct mod_section got;
21 22
	struct mod_section plt;
	struct mod_section plt_idx;
23 24 25

	/* For CONFIG_DYNAMIC_FTRACE */
	struct plt_entry *ftrace_trampolines;
26 27
};

28 29 30 31
struct got_entry {
	Elf_Addr symbol_addr;
};

32 33 34 35 36 37 38 39
struct plt_entry {
	u32 inst_lu12iw;
	u32 inst_lu32id;
	u32 inst_lu52id;
	u32 inst_jirl;
};

struct plt_idx_entry {
40
	Elf_Addr symbol_addr;
41 42
};

43 44
Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
45 46 47 48 49

static inline struct got_entry emit_got_entry(Elf_Addr val)
{
	return (struct got_entry) { val };
}
50 51 52 53 54

static inline struct plt_entry emit_plt_entry(unsigned long val)
{
	u32 lu12iw, lu32id, lu52id, jirl;

55
	lu12iw = larch_insn_gen_lu12iw(LOONGARCH_GPR_T1, ADDR_IMM(val, LU12IW));
56 57 58 59 60 61 62 63 64 65 66 67
	lu32id = larch_insn_gen_lu32id(LOONGARCH_GPR_T1, ADDR_IMM(val, LU32ID));
	lu52id = larch_insn_gen_lu52id(LOONGARCH_GPR_T1, LOONGARCH_GPR_T1, ADDR_IMM(val, LU52ID));
	jirl = larch_insn_gen_jirl(0, LOONGARCH_GPR_T1, 0, (val & 0xfff));

	return (struct plt_entry) { lu12iw, lu32id, lu52id, jirl };
}

static inline struct plt_idx_entry emit_plt_idx_entry(unsigned long val)
{
	return (struct plt_idx_entry) { val };
}

68
static inline int get_plt_idx(unsigned long val, Elf_Shdr *sechdrs, const struct mod_section *sec)
69 70
{
	int i;
71
	struct plt_idx_entry *plt_idx = (struct plt_idx_entry *)sechdrs[sec->shndx].sh_addr;
72 73 74 75 76 77 78 79 80 81

	for (i = 0; i < sec->num_entries; i++) {
		if (plt_idx[i].symbol_addr == val)
			return i;
	}

	return -1;
}

static inline struct plt_entry *get_plt_entry(unsigned long val,
82 83 84
					      Elf_Shdr *sechdrs,
					      const struct mod_section *sec_plt,
					      const struct mod_section *sec_plt_idx)
85
{
86 87
	int plt_idx = get_plt_idx(val, sechdrs, sec_plt_idx);
	struct plt_entry *plt = (struct plt_entry *)sechdrs[sec_plt->shndx].sh_addr;
88 89 90 91 92 93 94

	if (plt_idx < 0)
		return NULL;

	return plt + plt_idx;
}

95
static inline struct got_entry *get_got_entry(Elf_Addr val,
96
					      Elf_Shdr *sechdrs,
97 98 99
					      const struct mod_section *sec)
{
	int i;
100
	struct got_entry *got = (struct got_entry *)sechdrs[sec->shndx].sh_addr;
101 102 103 104 105 106 107

	for (i = 0; i < sec->num_entries; i++)
		if (got[i].symbol_addr == val)
			return &got[i];
	return NULL;
}

108
#endif /* _ASM_MODULE_H */