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> |
Paul Mundt | 3108cf0 | 2008-08-04 13:32:04 +0900 | [diff] [blame] | 30 | #include <linux/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/fs.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/kernel.h> |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 34 | #include <asm/unaligned.h> |
Matt Fleming | a6a2f2a | 2009-10-09 23:20:54 +0100 | [diff] [blame] | 35 | #include <asm/dwarf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | int apply_relocate_add(Elf32_Shdr *sechdrs, |
| 38 | const char *strtab, |
| 39 | unsigned int symindex, |
| 40 | unsigned int relsec, |
| 41 | struct module *me) |
| 42 | { |
| 43 | unsigned int i; |
| 44 | Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; |
| 45 | Elf32_Sym *sym; |
| 46 | Elf32_Addr relocation; |
| 47 | uint32_t *location; |
| 48 | uint32_t value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 50 | pr_debug("Applying relocate section %u to %u\n", relsec, |
| 51 | sechdrs[relsec].sh_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { |
| 53 | /* This is where to make the change */ |
| 54 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 55 | + rel[i].r_offset; |
| 56 | /* This is the symbol it is referring to. Note that all |
| 57 | undefined symbols have been resolved. */ |
| 58 | sym = (Elf32_Sym *)sechdrs[symindex].sh_addr |
| 59 | + ELF32_R_SYM(rel[i].r_info); |
| 60 | relocation = sym->st_value + rel[i].r_addend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 62 | #ifdef CONFIG_SUPERH64 |
| 63 | /* For text addresses, bit2 of the st_other field indicates |
| 64 | * whether the symbol is SHmedia (1) or SHcompact (0). If |
| 65 | * SHmedia, the LSB of the symbol needs to be asserted |
| 66 | * for the CPU to be in SHmedia mode when it starts executing |
| 67 | * the branch target. */ |
Paul Mundt | 7cd0378 | 2009-05-09 18:03:37 +0900 | [diff] [blame] | 68 | relocation |= !!(sym->st_other & 4); |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 69 | #endif |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | switch (ELF32_R_TYPE(rel[i].r_info)) { |
Paul Mundt | 78207ff | 2011-05-23 17:09:30 +0900 | [diff] [blame] | 72 | case R_SH_NONE: |
| 73 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | case R_SH_DIR32: |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 75 | value = get_unaligned(location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | value += relocation; |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 77 | put_unaligned(value, location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
| 79 | case R_SH_REL32: |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 80 | relocation = (relocation - (Elf32_Addr) location); |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 81 | value = get_unaligned(location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | value += relocation; |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 83 | put_unaligned(value, location); |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 84 | break; |
| 85 | case R_SH_IMM_LOW16: |
| 86 | *location = (*location & ~0x3fffc00) | |
| 87 | ((relocation & 0xffff) << 10); |
| 88 | break; |
| 89 | case R_SH_IMM_MEDLOW16: |
| 90 | *location = (*location & ~0x3fffc00) | |
| 91 | (((relocation >> 16) & 0xffff) << 10); |
| 92 | break; |
| 93 | case R_SH_IMM_LOW16_PCREL: |
| 94 | relocation -= (Elf32_Addr) location; |
| 95 | *location = (*location & ~0x3fffc00) | |
| 96 | ((relocation & 0xffff) << 10); |
| 97 | break; |
| 98 | case R_SH_IMM_MEDLOW16_PCREL: |
| 99 | relocation -= (Elf32_Addr) location; |
| 100 | *location = (*location & ~0x3fffc00) | |
| 101 | (((relocation >> 16) & 0xffff) << 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | break; |
| 103 | default: |
| 104 | printk(KERN_ERR "module %s: Unknown relocation: %u\n", |
| 105 | me->name, ELF32_R_TYPE(rel[i].r_info)); |
| 106 | return -ENOEXEC; |
| 107 | } |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | int module_finalize(const Elf_Ehdr *hdr, |
| 113 | const Elf_Shdr *sechdrs, |
| 114 | struct module *me) |
| 115 | { |
Paul Mundt | 5a3abba7 | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 116 | int ret = 0; |
Matt Fleming | a6a2f2a | 2009-10-09 23:20:54 +0100 | [diff] [blame] | 117 | |
Paul Mundt | 5a3abba7 | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 118 | ret |= module_dwarf_finalize(hdr, sechdrs, me); |
Matt Fleming | a6a2f2a | 2009-10-09 23:20:54 +0100 | [diff] [blame] | 119 | |
Paul Mundt | 5a3abba7 | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 120 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void module_arch_cleanup(struct module *mod) |
| 124 | { |
Paul Mundt | 5a3abba7 | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 125 | module_dwarf_cleanup(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |