Kuninori Morimoto | 5933f6d | 2018-12-28 00:32:24 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* Kernel module help for SH. |
| 3 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 4 | SHcompact version by Kaz Kojima and Paul Mundt. |
| 5 | |
| 6 | SHmedia bits: |
| 7 | |
| 8 | Copyright 2004 SuperH (UK) Ltd |
| 9 | Author: Richard Curnow |
| 10 | |
| 11 | Based on the sh version, and on code from the sh64-specific parts of |
| 12 | modutils, originally written by Richard Curnow and Ben Gaster. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | #include <linux/moduleloader.h> |
| 15 | #include <linux/elf.h> |
| 16 | #include <linux/vmalloc.h> |
Paul Mundt | 3108cf0 | 2008-08-04 13:32:04 +0900 | [diff] [blame] | 17 | #include <linux/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/fs.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/kernel.h> |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 21 | #include <asm/unaligned.h> |
Matt Fleming | a6a2f2a | 2009-10-09 23:20:54 +0100 | [diff] [blame] | 22 | #include <asm/dwarf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | int apply_relocate_add(Elf32_Shdr *sechdrs, |
| 25 | const char *strtab, |
| 26 | unsigned int symindex, |
| 27 | unsigned int relsec, |
| 28 | struct module *me) |
| 29 | { |
| 30 | unsigned int i; |
| 31 | Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; |
| 32 | Elf32_Sym *sym; |
| 33 | Elf32_Addr relocation; |
| 34 | uint32_t *location; |
| 35 | uint32_t value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 37 | pr_debug("Applying relocate section %u to %u\n", relsec, |
| 38 | sechdrs[relsec].sh_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { |
| 40 | /* This is where to make the change */ |
| 41 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 42 | + rel[i].r_offset; |
| 43 | /* This is the symbol it is referring to. Note that all |
| 44 | undefined symbols have been resolved. */ |
| 45 | sym = (Elf32_Sym *)sechdrs[symindex].sh_addr |
| 46 | + ELF32_R_SYM(rel[i].r_info); |
| 47 | relocation = sym->st_value + rel[i].r_addend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 49 | #ifdef CONFIG_SUPERH64 |
| 50 | /* For text addresses, bit2 of the st_other field indicates |
| 51 | * whether the symbol is SHmedia (1) or SHcompact (0). If |
| 52 | * SHmedia, the LSB of the symbol needs to be asserted |
| 53 | * for the CPU to be in SHmedia mode when it starts executing |
| 54 | * the branch target. */ |
Paul Mundt | 7cd0378 | 2009-05-09 18:03:37 +0900 | [diff] [blame] | 55 | relocation |= !!(sym->st_other & 4); |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 56 | #endif |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | switch (ELF32_R_TYPE(rel[i].r_info)) { |
Paul Mundt | 78207ff | 2011-05-23 17:09:30 +0900 | [diff] [blame] | 59 | case R_SH_NONE: |
| 60 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | case R_SH_DIR32: |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 62 | value = get_unaligned(location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | value += relocation; |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 64 | put_unaligned(value, location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | break; |
| 66 | case R_SH_REL32: |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 67 | relocation = (relocation - (Elf32_Addr) location); |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 68 | value = get_unaligned(location); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | value += relocation; |
Harvey Harrison | 1f9d294 | 2008-05-28 16:38:17 -0700 | [diff] [blame] | 70 | put_unaligned(value, location); |
Paul Mundt | 1cb80fc | 2007-11-20 15:16:25 +0900 | [diff] [blame] | 71 | break; |
| 72 | case R_SH_IMM_LOW16: |
| 73 | *location = (*location & ~0x3fffc00) | |
| 74 | ((relocation & 0xffff) << 10); |
| 75 | break; |
| 76 | case R_SH_IMM_MEDLOW16: |
| 77 | *location = (*location & ~0x3fffc00) | |
| 78 | (((relocation >> 16) & 0xffff) << 10); |
| 79 | break; |
| 80 | case R_SH_IMM_LOW16_PCREL: |
| 81 | relocation -= (Elf32_Addr) location; |
| 82 | *location = (*location & ~0x3fffc00) | |
| 83 | ((relocation & 0xffff) << 10); |
| 84 | break; |
| 85 | case R_SH_IMM_MEDLOW16_PCREL: |
| 86 | relocation -= (Elf32_Addr) location; |
| 87 | *location = (*location & ~0x3fffc00) | |
| 88 | (((relocation >> 16) & 0xffff) << 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | break; |
| 90 | default: |
| 91 | printk(KERN_ERR "module %s: Unknown relocation: %u\n", |
| 92 | me->name, ELF32_R_TYPE(rel[i].r_info)); |
| 93 | return -ENOEXEC; |
| 94 | } |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | int module_finalize(const Elf_Ehdr *hdr, |
| 100 | const Elf_Shdr *sechdrs, |
| 101 | struct module *me) |
| 102 | { |
Paul Mundt | 5a3abba | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 103 | int ret = 0; |
Matt Fleming | a6a2f2a | 2009-10-09 23:20:54 +0100 | [diff] [blame] | 104 | |
Paul Mundt | 5a3abba | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 105 | ret |= module_dwarf_finalize(hdr, sechdrs, me); |
Matt Fleming | a6a2f2a | 2009-10-09 23:20:54 +0100 | [diff] [blame] | 106 | |
Paul Mundt | 5a3abba | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 107 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void module_arch_cleanup(struct module *mod) |
| 111 | { |
Paul Mundt | 5a3abba | 2009-10-13 13:32:19 +0900 | [diff] [blame] | 112 | module_dwarf_cleanup(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |