Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/kernel/module.c |
| 3 | * |
| 4 | * Copyright (C) 2002 Russell King. |
Hyok S. Choi | 6a570b2 | 2006-09-26 17:37:07 +0900 | [diff] [blame] | 5 | * Modified for nommu by Hyok S. Choi |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Module allocation method suggested by Andi Kleen. |
| 12 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Russell King | f339ab3 | 2005-10-28 14:29:43 +0100 | [diff] [blame] | 14 | #include <linux/moduleloader.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Andrea Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 16 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/elf.h> |
| 18 | #include <linux/vmalloc.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/string.h> |
| 22 | |
| 23 | #include <asm/pgtable.h> |
Russell King | 37efe64 | 2008-12-01 11:53:07 +0000 | [diff] [blame] | 24 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #ifdef CONFIG_XIP_KERNEL |
| 27 | /* |
| 28 | * The XIP kernel text is mapped in the module area for modules and |
| 29 | * some other stuff to work without any indirect relocations. |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 30 | * MODULES_VADDR is redefined here and not in asm/memory.h to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off. |
| 32 | */ |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 33 | #undef MODULES_VADDR |
Russell King | 37efe64 | 2008-12-01 11:53:07 +0000 | [diff] [blame] | 34 | #define MODULES_VADDR (((unsigned long)_etext + ~PGDIR_MASK) & PGDIR_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #endif |
| 36 | |
Hyok S. Choi | 6a570b2 | 2006-09-26 17:37:07 +0900 | [diff] [blame] | 37 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | void *module_alloc(unsigned long size) |
| 39 | { |
| 40 | struct vm_struct *area; |
| 41 | |
| 42 | size = PAGE_ALIGN(size); |
| 43 | if (!size) |
| 44 | return NULL; |
| 45 | |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 46 | area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | if (!area) |
| 48 | return NULL; |
| 49 | |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 50 | return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
Hyok S. Choi | 6a570b2 | 2006-09-26 17:37:07 +0900 | [diff] [blame] | 52 | #else /* CONFIG_MMU */ |
| 53 | void *module_alloc(unsigned long size) |
| 54 | { |
| 55 | return size == 0 ? NULL : vmalloc(size); |
| 56 | } |
| 57 | #endif /* !CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | void module_free(struct module *module, void *region) |
| 60 | { |
| 61 | vfree(region); |
| 62 | } |
| 63 | |
| 64 | int module_frob_arch_sections(Elf_Ehdr *hdr, |
| 65 | Elf_Shdr *sechdrs, |
| 66 | char *secstrings, |
| 67 | struct module *mod) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int |
| 73 | apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, |
| 74 | unsigned int relindex, struct module *module) |
| 75 | { |
| 76 | Elf32_Shdr *symsec = sechdrs + symindex; |
| 77 | Elf32_Shdr *relsec = sechdrs + relindex; |
| 78 | Elf32_Shdr *dstsec = sechdrs + relsec->sh_info; |
| 79 | Elf32_Rel *rel = (void *)relsec->sh_addr; |
| 80 | unsigned int i; |
| 81 | |
| 82 | for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) { |
| 83 | unsigned long loc; |
| 84 | Elf32_Sym *sym; |
| 85 | s32 offset; |
| 86 | |
| 87 | offset = ELF32_R_SYM(rel->r_info); |
| 88 | if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) { |
| 89 | printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n", |
| 90 | module->name, relindex, i); |
| 91 | return -ENOEXEC; |
| 92 | } |
| 93 | |
| 94 | sym = ((Elf32_Sym *)symsec->sh_addr) + offset; |
| 95 | |
| 96 | if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) { |
| 97 | printk(KERN_ERR "%s: out of bounds relocation, " |
| 98 | "section %d reloc %d offset %d size %d\n", |
| 99 | module->name, relindex, i, rel->r_offset, |
| 100 | dstsec->sh_size); |
| 101 | return -ENOEXEC; |
| 102 | } |
| 103 | |
| 104 | loc = dstsec->sh_addr + rel->r_offset; |
| 105 | |
| 106 | switch (ELF32_R_TYPE(rel->r_info)) { |
| 107 | case R_ARM_ABS32: |
| 108 | *(u32 *)loc += sym->st_value; |
| 109 | break; |
| 110 | |
| 111 | case R_ARM_PC24: |
Daniel Jacobowitz | c2e2611 | 2005-12-14 22:04:22 +0000 | [diff] [blame] | 112 | case R_ARM_CALL: |
| 113 | case R_ARM_JUMP24: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | offset = (*(u32 *)loc & 0x00ffffff) << 2; |
| 115 | if (offset & 0x02000000) |
| 116 | offset -= 0x04000000; |
| 117 | |
| 118 | offset += sym->st_value - loc; |
| 119 | if (offset & 3 || |
Kevin Welton | c5f1250 | 2007-05-08 22:05:25 +0100 | [diff] [blame] | 120 | offset <= (s32)0xfe000000 || |
| 121 | offset >= (s32)0x02000000) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | printk(KERN_ERR |
| 123 | "%s: relocation out of range, section " |
| 124 | "%d reloc %d sym '%s'\n", module->name, |
| 125 | relindex, i, strtab + sym->st_name); |
| 126 | return -ENOEXEC; |
| 127 | } |
| 128 | |
| 129 | offset >>= 2; |
| 130 | |
| 131 | *(u32 *)loc &= 0xff000000; |
| 132 | *(u32 *)loc |= offset & 0x00ffffff; |
| 133 | break; |
| 134 | |
Daniel Silverstone | 4731f8b | 2009-03-20 11:11:43 +0100 | [diff] [blame^] | 135 | case R_ARM_V4BX: |
| 136 | /* Preserve Rm and the condition code. Alter |
| 137 | * other bits to re-code instruction as |
| 138 | * MOV PC,Rm. |
| 139 | */ |
| 140 | *(u32 *)loc &= 0xf000000f; |
| 141 | *(u32 *)loc |= 0x01a0f000; |
| 142 | break; |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | default: |
| 145 | printk(KERN_ERR "%s: unknown relocation: %u\n", |
| 146 | module->name, ELF32_R_TYPE(rel->r_info)); |
| 147 | return -ENOEXEC; |
| 148 | } |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int |
| 154 | apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab, |
| 155 | unsigned int symindex, unsigned int relsec, struct module *module) |
| 156 | { |
| 157 | printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n", |
| 158 | module->name); |
| 159 | return -ENOEXEC; |
| 160 | } |
| 161 | |
| 162 | int |
| 163 | module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, |
| 164 | struct module *module) |
| 165 | { |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | module_arch_cleanup(struct module *mod) |
| 171 | { |
| 172 | } |