1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/moduleloader.h>
7 #include <linux/vmalloc.h>
8 #include <linux/slab.h>
10 #include <linux/string.h>
11 #include <linux/kernel.h>
12 #include <linux/spinlock.h>
13 #include <asm/pgtable.h>
15 #if defined(__CSKYABIV2__)
16 #define IS_BSR32(hi16, lo16) (((hi16) & 0xFC00) == 0xE000)
17 #define IS_JSRI32(hi16, lo16) ((hi16) == 0xEAE0)
19 #define CHANGE_JSRI_TO_LRW(addr) do { \
20 *(uint16_t *)(addr) = (*(uint16_t *)(addr) & 0xFF9F) | 0x001a; \
21 *((uint16_t *)(addr) + 1) = *((uint16_t *)(addr) + 1) & 0xFFFF; \
24 #define SET_JSR32_R26(addr) do { \
25 *(uint16_t *)(addr) = 0xE8Fa; \
26 *((uint16_t *)(addr) + 1) = 0x0000; \
30 int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
31 unsigned int symindex, unsigned int relsec, struct module *me)
34 Elf32_Rela *rel = (void *) sechdrs[relsec].sh_addr;
38 #if defined(__CSKYABIV2__)
39 uint16_t *location_tmp;
42 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
43 /* This is where to make the change */
44 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
46 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
47 + ELF32_R_SYM(rel[i].r_info);
49 switch (ELF32_R_TYPE(rel[i].r_info)) {
51 /* We add the value into the location given */
52 *location = rel[i].r_addend + sym->st_value;
55 /* Add the value, subtract its postition */
56 *location = rel[i].r_addend + sym->st_value
59 case R_CSKY_PCRELJSR_IMM11BY2:
61 case R_CSKY_PCRELJSR_IMM26BY2:
62 #if defined(__CSKYABIV2__)
63 location_tmp = (uint16_t *)location;
64 if (IS_BSR32(*location_tmp, *(location_tmp + 1)))
67 if (IS_JSRI32(*location_tmp, *(location_tmp + 1))) {
68 /* jsri 0x... --> lrw r26, 0x... */
69 CHANGE_JSRI_TO_LRW(location);
70 /* lsli r0, r0 --> jsr r26 */
71 SET_JSR32_R26(location + 1);
75 case R_CSKY_ADDR_HI16:
76 temp = ((short *)location) + 1;
78 ((rel[i].r_addend + sym->st_value) >> 16);
80 case R_CSKY_ADDR_LO16:
81 temp = ((short *)location) + 1;
83 ((rel[i].r_addend + sym->st_value) & 0xffff);
86 pr_err("module %s: Unknown relocation: %u\n",
87 me->name, ELF32_R_TYPE(rel[i].r_info));