Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Kernel module help for SH. |
| 2 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 3 | SHcompact version by Kaz Kojima and Paul Mundt. |
| 4 | |
| 5 | SHmedia bits: |
| 6 | |
| 7 | Copyright 2004 SuperH (UK) Ltd |
| 8 | Author: Richard Curnow |
| 9 | |
| 10 | Based on the sh version, and on code from the sh64-specific parts of |
| 11 | modutils, originally written by Richard Curnow and Ben Gaster. |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | This program is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation; either version 2 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | */ |
| 27 | #include <linux/moduleloader.h> |
| 28 | #include <linux/elf.h> |
| 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/fs.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/kernel.h> |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame^] | 33 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | void *module_alloc(unsigned long size) |
| 36 | { |
| 37 | if (size == 0) |
| 38 | return NULL; |
| 39 | return vmalloc(size); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /* Free memory returned from module_alloc */ |
| 44 | void module_free(struct module *mod, void *module_region) |
| 45 | { |
| 46 | vfree(module_region); |
| 47 | /* FIXME: If module_region == mod->init_region, trim exception |
| 48 | table entries. */ |
| 49 | } |
| 50 | |
| 51 | /* We don't need anything special. */ |
| 52 | int module_frob_arch_sections(Elf_Ehdr *hdr, |
| 53 | Elf_Shdr *sechdrs, |
| 54 | char *secstrings, |
| 55 | struct module *mod) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int apply_relocate_add(Elf32_Shdr *sechdrs, |
| 61 | const char *strtab, |
| 62 | unsigned int symindex, |
| 63 | unsigned int relsec, |
| 64 | struct module *me) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; |
| 68 | Elf32_Sym *sym; |
| 69 | Elf32_Addr relocation; |
| 70 | uint32_t *location; |
| 71 | uint32_t value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 73 | pr_debug("Applying relocate section %u to %u\n", relsec, |
| 74 | sechdrs[relsec].sh_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { |
| 76 | /* This is where to make the change */ |
| 77 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 78 | + rel[i].r_offset; |
| 79 | /* This is the symbol it is referring to. Note that all |
| 80 | undefined symbols have been resolved. */ |
| 81 | sym = (Elf32_Sym *)sechdrs[symindex].sh_addr |
| 82 | + ELF32_R_SYM(rel[i].r_info); |
| 83 | relocation = sym->st_value + rel[i].r_addend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 85 | #ifdef CONFIG_SUPERH64 |
| 86 | /* For text addresses, bit2 of the st_other field indicates |
| 87 | * whether the symbol is SHmedia (1) or SHcompact (0). If |
| 88 | * SHmedia, the LSB of the symbol needs to be asserted |
| 89 | * for the CPU to be in SHmedia mode when it starts executing |
| 90 | * the branch target. */ |
| 91 | relocation |= (sym->st_other & 4); |
| 92 | #endif |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | switch (ELF32_R_TYPE(rel[i].r_info)) { |
| 95 | case R_SH_DIR32: |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame^] | 96 | value = get_unaligned(location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | value += relocation; |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame^] | 98 | put_unaligned(value, location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | break; |
| 100 | case R_SH_REL32: |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 101 | relocation = (relocation - (Elf32_Addr) location); |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame^] | 102 | value = get_unaligned(location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | value += relocation; |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame^] | 104 | put_unaligned(value, location); |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 105 | break; |
| 106 | case R_SH_IMM_LOW16: |
| 107 | *location = (*location & ~0x3fffc00) | |
| 108 | ((relocation & 0xffff) << 10); |
| 109 | break; |
| 110 | case R_SH_IMM_MEDLOW16: |
| 111 | *location = (*location & ~0x3fffc00) | |
| 112 | (((relocation >> 16) & 0xffff) << 10); |
| 113 | break; |
| 114 | case R_SH_IMM_LOW16_PCREL: |
| 115 | relocation -= (Elf32_Addr) location; |
| 116 | *location = (*location & ~0x3fffc00) | |
| 117 | ((relocation & 0xffff) << 10); |
| 118 | break; |
| 119 | case R_SH_IMM_MEDLOW16_PCREL: |
| 120 | relocation -= (Elf32_Addr) location; |
| 121 | *location = (*location & ~0x3fffc00) | |
| 122 | (((relocation >> 16) & 0xffff) << 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | break; |
| 124 | default: |
| 125 | printk(KERN_ERR "module %s: Unknown relocation: %u\n", |
| 126 | me->name, ELF32_R_TYPE(rel[i].r_info)); |
| 127 | return -ENOEXEC; |
| 128 | } |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int apply_relocate(Elf32_Shdr *sechdrs, |
| 134 | const char *strtab, |
| 135 | unsigned int symindex, |
| 136 | unsigned int relsec, |
| 137 | struct module *me) |
| 138 | { |
| 139 | printk(KERN_ERR "module %s: REL RELOCATION unsupported\n", |
| 140 | me->name); |
| 141 | return -ENOEXEC; |
| 142 | } |
| 143 | |
| 144 | int module_finalize(const Elf_Ehdr *hdr, |
| 145 | const Elf_Shdr *sechdrs, |
| 146 | struct module *me) |
| 147 | { |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | void module_arch_cleanup(struct module *mod) |
| 152 | { |
| 153 | } |