1 // SPDX-License-Identifier: GPL-2.0 !! 1 /* 2 // Copyright (C) 2018 Hangzhou C-SKY Microsyst !! 2 * This file is subject to the terms and conditions of the GNU General Public >> 3 * License. See the file COPYING in the main directory of this archive >> 4 * for more details. >> 5 */ 3 6 4 #include <linux/moduleloader.h> 7 #include <linux/moduleloader.h> 5 #include <linux/elf.h> 8 #include <linux/elf.h> 6 #include <linux/mm.h> << 7 #include <linux/vmalloc.h> 9 #include <linux/vmalloc.h> 8 #include <linux/slab.h> << 9 #include <linux/fs.h> 10 #include <linux/fs.h> 10 #include <linux/string.h> 11 #include <linux/string.h> 11 #include <linux/kernel.h> 12 #include <linux/kernel.h> 12 #include <linux/spinlock.h> << 13 13 14 #ifdef CONFIG_CPU_CK810 !! 14 #if 0 15 #define IS_BSR32(hi16, lo16) (((hi1 !! 15 #define DEBUGP(fmt, ...) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 16 #define IS_JSRI32(hi16, lo16) ((hi16 !! 16 #else 17 !! 17 #define DEBUGP(fmt, ...) no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 18 #define CHANGE_JSRI_TO_LRW(addr) do { !! 18 #endif 19 *(uint16_t *)(addr) = (*(uint16_t *)(a !! 19 20 *((uint16_t *)(addr) + 1) = *((uint16_ !! 20 #ifdef CONFIG_MODULES 21 } while (0) << 22 << 23 #define SET_JSR32_R26(addr) do { << 24 *(uint16_t *)(addr) = 0xE8Fa; << 25 *((uint16_t *)(addr) + 1) = 0x0000; << 26 } while (0) << 27 21 28 static void jsri_2_lrw_jsr(uint32_t *location) !! 22 int apply_relocate(Elf32_Shdr *sechdrs, >> 23 const char *strtab, >> 24 unsigned int symindex, >> 25 unsigned int relsec, >> 26 struct module *me) 29 { 27 { 30 uint16_t *location_tmp = (uint16_t *)l !! 28 unsigned int i; >> 29 Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr; >> 30 Elf32_Sym *sym; >> 31 uint32_t *location; 31 32 32 if (IS_BSR32(*location_tmp, *(location !! 33 DEBUGP("Applying relocate section %u to %u\n", relsec, 33 return; !! 34 sechdrs[relsec].sh_info); >> 35 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { >> 36 /* This is where to make the change */ >> 37 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr >> 38 + rel[i].r_offset; >> 39 /* This is the symbol it is referring to. Note that all >> 40 undefined symbols have been resolved. */ >> 41 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr >> 42 + ELF32_R_SYM(rel[i].r_info); 34 43 35 if (IS_JSRI32(*location_tmp, *(locatio !! 44 switch (ELF32_R_TYPE(rel[i].r_info)) { 36 /* jsri 0x... --> lrw r26, 0x !! 45 case R_68K_32: 37 CHANGE_JSRI_TO_LRW(location); !! 46 /* We add the value into the location given */ 38 /* lsli r0, r0 --> jsr r26 */ !! 47 *location += sym->st_value; 39 SET_JSR32_R26(location + 1); !! 48 break; >> 49 case R_68K_PC32: >> 50 /* Add the value, subtract its position */ >> 51 *location += sym->st_value - (uint32_t)location; >> 52 break; >> 53 default: >> 54 pr_err("module %s: Unknown relocation: %u\n", me->name, >> 55 ELF32_R_TYPE(rel[i].r_info)); >> 56 return -ENOEXEC; >> 57 } 40 } 58 } >> 59 return 0; 41 } 60 } 42 #else << 43 static inline void jsri_2_lrw_jsr(uint32_t *lo << 44 { << 45 return; << 46 } << 47 #endif << 48 61 49 int apply_relocate_add(Elf32_Shdr *sechdrs, co !! 62 int apply_relocate_add(Elf32_Shdr *sechdrs, 50 unsigned int symindex, unsigne !! 63 const char *strtab, >> 64 unsigned int symindex, >> 65 unsigned int relsec, >> 66 struct module *me) 51 { 67 { 52 unsigned int i; 68 unsigned int i; 53 Elf32_Rela *rel = (void *) sechdr !! 69 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; 54 Elf32_Sym *sym; !! 70 Elf32_Sym *sym; 55 uint32_t *location; !! 71 uint32_t *location; 56 short *temp; << 57 72 >> 73 DEBUGP("Applying relocate_add section %u to %u\n", relsec, >> 74 sechdrs[relsec].sh_info); 58 for (i = 0; i < sechdrs[relsec].sh_siz 75 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 59 /* This is where to make the c 76 /* This is where to make the change */ 60 location = (void *)sechdrs[sec 77 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 61 !! 78 + rel[i].r_offset; >> 79 /* This is the symbol it is referring to. Note that all >> 80 undefined symbols have been resolved. */ 62 sym = (Elf32_Sym *)sechdrs[sym 81 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr 63 !! 82 + ELF32_R_SYM(rel[i].r_info); 64 83 65 switch (ELF32_R_TYPE(rel[i].r_ 84 switch (ELF32_R_TYPE(rel[i].r_info)) { 66 case R_CSKY_32: !! 85 case R_68K_32: 67 /* We add the value in 86 /* We add the value into the location given */ 68 *location = rel[i].r_a 87 *location = rel[i].r_addend + sym->st_value; 69 break; 88 break; 70 case R_CSKY_PC32: !! 89 case R_68K_PC32: 71 /* Add the value, subt 90 /* Add the value, subtract its position */ 72 *location = rel[i].r_a !! 91 *location = rel[i].r_addend + sym->st_value - (uint32_t)location; 73 << 74 break; << 75 case R_CSKY_PCRELJSR_IMM11BY2: << 76 break; << 77 case R_CSKY_PCRELJSR_IMM26BY2: << 78 jsri_2_lrw_jsr(locatio << 79 break; << 80 case R_CSKY_ADDR_HI16: << 81 temp = ((short *)loca << 82 *temp = (short) << 83 ((rel[i].r_add << 84 break; << 85 case R_CSKY_ADDR_LO16: << 86 temp = ((short *)loca << 87 *temp = (short) << 88 ((rel[i].r_add << 89 break; 92 break; 90 default: 93 default: 91 pr_err("module %s: Unk !! 94 pr_err("module %s: Unknown relocation: %u\n", me->name, 92 me->name, ELF3 !! 95 ELF32_R_TYPE(rel[i].r_info)); 93 return -ENOEXEC; 96 return -ENOEXEC; 94 } 97 } 95 } 98 } 96 return 0; 99 return 0; >> 100 } >> 101 >> 102 int module_finalize(const Elf_Ehdr *hdr, >> 103 const Elf_Shdr *sechdrs, >> 104 struct module *mod) >> 105 { >> 106 module_fixup(mod, mod->arch.fixup_start, mod->arch.fixup_end); >> 107 return 0; >> 108 } >> 109 >> 110 #endif /* CONFIG_MODULES */ >> 111 >> 112 void module_fixup(struct module *mod, struct m68k_fixup_info *start, >> 113 struct m68k_fixup_info *end) >> 114 { >> 115 #ifdef CONFIG_MMU >> 116 struct m68k_fixup_info *fixup; >> 117 >> 118 for (fixup = start; fixup < end; fixup++) { >> 119 switch (fixup->type) { >> 120 case m68k_fixup_memoffset: >> 121 *(u32 *)fixup->addr = m68k_memoffset; >> 122 break; >> 123 case m68k_fixup_vnode_shift: >> 124 *(u16 *)fixup->addr += m68k_virt_to_node_shift; >> 125 break; >> 126 } >> 127 } >> 128 #endif 97 } 129 } 98 130
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.