1/* 2 * arch/score/kernel/module.c 3 * 4 * Score Processor version. 5 * 6 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd. 7 * Chen Liqin <liqin.chen@sunplusct.com> 8 * Lennox Wu <lennox.wu@sunplusct.com> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, see the file COPYING, or write 22 * to the Free Software Foundation, Inc., 23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 */ 25 26#include <linux/moduleloader.h> 27#include <linux/module.h> 28#include <linux/vmalloc.h> 29 30void *module_alloc(unsigned long size) 31{ 32 return size ? vmalloc(size) : NULL; 33} 34 35/* Free memory returned from module_alloc */ 36void module_free(struct module *mod, void *module_region) 37{ 38 vfree(module_region); 39} 40 41int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 42 char *secstrings, struct module *mod) 43{ 44 return 0; 45} 46 47int apply_relocate(Elf_Shdr *sechdrs, const char *strtab, 48 unsigned int symindex, unsigned int relindex, 49 struct module *me) 50{ 51 Elf32_Shdr *symsec = sechdrs + symindex; 52 Elf32_Shdr *relsec = sechdrs + relindex; 53 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info; 54 Elf32_Rel *rel = (void *)relsec->sh_addr; 55 unsigned int i; 56 57 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) { 58 unsigned long loc; 59 Elf32_Sym *sym; 60 s32 r_offset; 61 62 r_offset = ELF32_R_SYM(rel->r_info); 63 if ((r_offset < 0) || 64 (r_offset > (symsec->sh_size / sizeof(Elf32_Sym)))) { 65 printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n", 66 me->name, relindex, i); 67 return -ENOEXEC; 68 } 69 70 sym = ((Elf32_Sym *)symsec->sh_addr) + r_offset; 71 72 if ((rel->r_offset < 0) || 73 (rel->r_offset > dstsec->sh_size - sizeof(u32))) { 74 printk(KERN_ERR "%s: out of bounds relocation, " 75 "section %d reloc %d offset %d size %d\n", 76 me->name, relindex, i, rel->r_offset, 77 dstsec->sh_size); 78 return -ENOEXEC; 79 } 80 81 loc = dstsec->sh_addr + rel->r_offset; 82 switch (ELF32_R_TYPE(rel->r_info)) { 83 case R_SCORE_NONE: 84 break; 85 case R_SCORE_ABS32: 86 *(unsigned long *)loc += sym->st_value; 87 break; 88 case R_SCORE_HI16: 89 break; 90 case R_SCORE_LO16: { 91 unsigned long hi16_offset, offset; 92 unsigned long uvalue; 93 unsigned long temp, temp_hi; 94 temp_hi = *((unsigned long *)loc - 1); 95 temp = *(unsigned long *)loc; 96 97 hi16_offset = (((((temp_hi) >> 16) & 0x3) << 15) | 98 ((temp_hi) & 0x7fff)) >> 1; 99 offset = ((temp >> 16 & 0x03) << 15) | 100 ((temp & 0x7fff) >> 1); 101 offset = (hi16_offset << 16) | (offset & 0xffff); 102 uvalue = sym->st_value + offset; 103 hi16_offset = (uvalue >> 16) << 1; 104 105 temp_hi = ((temp_hi) & (~(0x37fff))) | 106 (hi16_offset & 0x7fff) | 107 ((hi16_offset << 1) & 0x30000); 108 *((unsigned long *)loc - 1) = temp_hi; 109 110 offset = (uvalue & 0xffff) << 1; 111 temp = (temp & (~(0x37fff))) | (offset & 0x7fff) | 112 ((offset << 1) & 0x30000); 113 *(unsigned long *)loc = temp; 114 break; 115 } 116 case R_SCORE_24: { 117 unsigned long hi16_offset, offset; 118 unsigned long uvalue; 119 unsigned long temp; 120 121 temp = *(unsigned long *)loc; 122 offset = (temp & 0x03FF7FFE); 123 hi16_offset = (offset & 0xFFFF0000); 124 offset = (hi16_offset | ((offset & 0xFFFF) << 1)) >> 2; 125 126 uvalue = (sym->st_value + offset) >> 1; 127 uvalue = uvalue & 0x00ffffff; 128 129 temp = (temp & 0xfc008001) | 130 ((uvalue << 2) & 0x3ff0000) | 131 ((uvalue & 0x3fff) << 1); 132 *(unsigned long *)loc = temp; 133 break; 134 } 135 default: 136 printk(KERN_ERR "%s: unknown relocation: %u\n", 137 me->name, ELF32_R_TYPE(rel->r_info)); 138 return -ENOEXEC; 139 } 140 } 141 142 return 0; 143} 144 145int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 146 unsigned int symindex, unsigned int relsec, 147 struct module *me) 148{ 149 return 0; 150} 151 152/* Given an address, look for it in the module exception tables. */ 153const struct exception_table_entry *search_module_dbetables(unsigned long addr) 154{ 155 return NULL; 156} 157 158/* Put in dbe list if necessary. */ 159int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, 160 struct module *me) 161{ 162 return 0; 163} 164 165void module_arch_cleanup(struct module *mod) {} 166