^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #ifdef CONFIG_CPU_CK810
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define IS_BSR32(hi16, lo16) (((hi16) & 0xFC00) == 0xE000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define IS_JSRI32(hi16, lo16) ((hi16) == 0xEAE0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define CHANGE_JSRI_TO_LRW(addr) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *(uint16_t *)(addr) = (*(uint16_t *)(addr) & 0xFF9F) | 0x001a; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *((uint16_t *)(addr) + 1) = *((uint16_t *)(addr) + 1) & 0xFFFF; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define SET_JSR32_R26(addr) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *(uint16_t *)(addr) = 0xE8Fa; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *((uint16_t *)(addr) + 1) = 0x0000; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static void jsri_2_lrw_jsr(uint32_t *location)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) uint16_t *location_tmp = (uint16_t *)location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (IS_BSR32(*location_tmp, *(location_tmp + 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (IS_JSRI32(*location_tmp, *(location_tmp + 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* jsri 0x... --> lrw r26, 0x... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) CHANGE_JSRI_TO_LRW(location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* lsli r0, r0 --> jsr r26 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) SET_JSR32_R26(location + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void inline jsri_2_lrw_jsr(uint32_t *location)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned int symindex, unsigned int relsec, struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) Elf32_Rela *rel = (void *) sechdrs[relsec].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) Elf32_Sym *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) uint32_t *location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) short *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* This is where to make the change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) + rel[i].r_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) + ELF32_R_SYM(rel[i].r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) switch (ELF32_R_TYPE(rel[i].r_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) case R_CSKY_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* We add the value into the location given */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *location = rel[i].r_addend + sym->st_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) case R_CSKY_PC32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* Add the value, subtract its postition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *location = rel[i].r_addend + sym->st_value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) - (uint32_t)location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case R_CSKY_PCRELJSR_IMM11BY2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) case R_CSKY_PCRELJSR_IMM26BY2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) jsri_2_lrw_jsr(location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) case R_CSKY_ADDR_HI16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) temp = ((short *)location) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *temp = (short)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ((rel[i].r_addend + sym->st_value) >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) case R_CSKY_ADDR_LO16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) temp = ((short *)location) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *temp = (short)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ((rel[i].r_addend + sym->st_value) & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pr_err("module %s: Unknown relocation: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) me->name, ELF32_R_TYPE(rel[i].r_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }