^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Kernel module support for Nios II.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2004 Microtronix Datacom Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by Wentao Xu <xuwentao@microtronix.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2001, 2003 Rusty Russell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Public License. See the file COPYING in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Modules should NOT be allocated with kmalloc for (obvious) reasons.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * But we do it for now to avoid relocation issues. CALL26/PCREL26 cannot reach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * from 0x80000000 (vmalloc area) to 0xc00000000 (kernel) (kmalloc returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * addresses in 0xc0000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) void *module_alloc(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Free memory returned from module_alloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) void module_memfree(void *module_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) kfree(module_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int symindex, unsigned int relsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) pr_debug("Applying relocate section %u to %u\n", relsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sechdrs[relsec].sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* This is where to make the change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) uint32_t word;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) uint32_t *loc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) = ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) + rela[i].r_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* This is the symbol it is referring to. Note that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) undefined symbols have been resolved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) Elf32_Sym *sym
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) = ((Elf32_Sym *)sechdrs[symindex].sh_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) + ELF32_R_SYM(rela[i].r_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) uint32_t v = sym->st_value + rela[i].r_addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pr_debug("reltype %d 0x%x name:<%s>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ELF32_R_TYPE(rela[i].r_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) rela[i].r_offset, strtab + sym->st_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) switch (ELF32_R_TYPE(rela[i].r_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case R_NIOS2_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) case R_NIOS2_BFD_RELOC_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) *loc += v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) case R_NIOS2_PCREL16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) v -= (uint32_t)loc + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if ((int32_t)v > 0x7fff ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) (int32_t)v < -(int32_t)0x8000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) pr_err("module %s: relocation overflow\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) word = *loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) (word & 0x3f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) case R_NIOS2_CALL26:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (v & 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pr_err("module %s: dangerous relocation\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if ((v >> 28) != ((uint32_t)loc >> 28)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) pr_err("module %s: relocation overflow\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mod->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *loc = (*loc & 0x3f) | ((v >> 2) << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case R_NIOS2_HI16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) word = *loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *loc = ((((word >> 22) << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ((v >> 16) & 0xffff)) << 6) | (word & 0x3f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case R_NIOS2_LO16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) word = *loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) (word & 0x3f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) case R_NIOS2_HIADJ16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) Elf32_Addr word2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) word = *loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *loc = ((((word >> 22) << 16) | word2) << 6) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) (word & 0x3f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pr_err("module %s: Unknown reloc: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) mod->name, ELF32_R_TYPE(rela[i].r_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) flush_cache_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }