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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/string.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/pgtable.h> |
Russell King | 37efe64 | 2008-12-01 11:53:07 +0000 | [diff] [blame] | 24 | #include <asm/sections.h> |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 25 | #include <asm/unwind.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #ifdef CONFIG_XIP_KERNEL |
| 28 | /* |
| 29 | * The XIP kernel text is mapped in the module area for modules and |
| 30 | * some other stuff to work without any indirect relocations. |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 31 | * 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] | 32 | * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off. |
| 33 | */ |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 34 | #undef MODULES_VADDR |
Russell King | 37efe64 | 2008-12-01 11:53:07 +0000 | [diff] [blame] | 35 | #define MODULES_VADDR (((unsigned long)_etext + ~PGDIR_MASK) & PGDIR_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
Hyok S. Choi | 6a570b2 | 2006-09-26 17:37:07 +0900 | [diff] [blame] | 38 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | void *module_alloc(unsigned long size) |
| 40 | { |
| 41 | struct vm_struct *area; |
| 42 | |
| 43 | size = PAGE_ALIGN(size); |
| 44 | if (!size) |
| 45 | return NULL; |
| 46 | |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 47 | area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if (!area) |
| 49 | return NULL; |
| 50 | |
Russell King | 8ec5366 | 2008-09-07 17:16:54 +0100 | [diff] [blame] | 51 | return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
Hyok S. Choi | 6a570b2 | 2006-09-26 17:37:07 +0900 | [diff] [blame] | 53 | #else /* CONFIG_MMU */ |
| 54 | void *module_alloc(unsigned long size) |
| 55 | { |
| 56 | return size == 0 ? NULL : vmalloc(size); |
| 57 | } |
| 58 | #endif /* !CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | void module_free(struct module *module, void *region) |
| 61 | { |
| 62 | vfree(region); |
| 63 | } |
| 64 | |
| 65 | int module_frob_arch_sections(Elf_Ehdr *hdr, |
| 66 | Elf_Shdr *sechdrs, |
| 67 | char *secstrings, |
| 68 | struct module *mod) |
| 69 | { |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 70 | #ifdef CONFIG_ARM_UNWIND |
| 71 | Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum; |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 72 | struct arm_unwind_mapping *maps = mod->arch.map; |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 73 | |
| 74 | for (s = sechdrs; s < sechdrs_end; s++) { |
Phil Carmody | 5793432 | 2010-08-19 15:10:24 +0100 | [diff] [blame] | 75 | char const *secname = secstrings + s->sh_name; |
| 76 | |
| 77 | if (strcmp(".ARM.exidx.init.text", secname) == 0) |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 78 | maps[ARM_SEC_INIT].unw_sec = s; |
Phil Carmody | 5793432 | 2010-08-19 15:10:24 +0100 | [diff] [blame] | 79 | else if (strcmp(".ARM.exidx.devinit.text", secname) == 0) |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 80 | maps[ARM_SEC_DEVINIT].unw_sec = s; |
Phil Carmody | 5793432 | 2010-08-19 15:10:24 +0100 | [diff] [blame] | 81 | else if (strcmp(".ARM.exidx", secname) == 0) |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 82 | maps[ARM_SEC_CORE].unw_sec = s; |
Phil Carmody | 5793432 | 2010-08-19 15:10:24 +0100 | [diff] [blame] | 83 | else if (strcmp(".init.text", secname) == 0) |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 84 | maps[ARM_SEC_INIT].sec_text = s; |
Phil Carmody | 5793432 | 2010-08-19 15:10:24 +0100 | [diff] [blame] | 85 | else if (strcmp(".devinit.text", secname) == 0) |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 86 | maps[ARM_SEC_DEVINIT].sec_text = s; |
Phil Carmody | 5793432 | 2010-08-19 15:10:24 +0100 | [diff] [blame] | 87 | else if (strcmp(".text", secname) == 0) |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 88 | maps[ARM_SEC_CORE].sec_text = s; |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 89 | } |
| 90 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, |
| 96 | unsigned int relindex, struct module *module) |
| 97 | { |
| 98 | Elf32_Shdr *symsec = sechdrs + symindex; |
| 99 | Elf32_Shdr *relsec = sechdrs + relindex; |
| 100 | Elf32_Shdr *dstsec = sechdrs + relsec->sh_info; |
| 101 | Elf32_Rel *rel = (void *)relsec->sh_addr; |
| 102 | unsigned int i; |
| 103 | |
| 104 | for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) { |
| 105 | unsigned long loc; |
| 106 | Elf32_Sym *sym; |
| 107 | s32 offset; |
Catalin Marinas | b749315 | 2010-06-21 15:11:38 +0100 | [diff] [blame] | 108 | #ifdef CONFIG_THUMB2_KERNEL |
Catalin Marinas | adca6dc | 2009-07-24 12:32:59 +0100 | [diff] [blame] | 109 | u32 upper, lower, sign, j1, j2; |
Catalin Marinas | b749315 | 2010-06-21 15:11:38 +0100 | [diff] [blame] | 110 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | offset = ELF32_R_SYM(rel->r_info); |
| 113 | if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) { |
| 114 | printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n", |
| 115 | module->name, relindex, i); |
| 116 | return -ENOEXEC; |
| 117 | } |
| 118 | |
| 119 | sym = ((Elf32_Sym *)symsec->sh_addr) + offset; |
| 120 | |
| 121 | if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) { |
| 122 | printk(KERN_ERR "%s: out of bounds relocation, " |
| 123 | "section %d reloc %d offset %d size %d\n", |
| 124 | module->name, relindex, i, rel->r_offset, |
| 125 | dstsec->sh_size); |
| 126 | return -ENOEXEC; |
| 127 | } |
| 128 | |
| 129 | loc = dstsec->sh_addr + rel->r_offset; |
| 130 | |
| 131 | switch (ELF32_R_TYPE(rel->r_info)) { |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 132 | case R_ARM_NONE: |
| 133 | /* ignore */ |
| 134 | break; |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | case R_ARM_ABS32: |
| 137 | *(u32 *)loc += sym->st_value; |
| 138 | break; |
| 139 | |
| 140 | case R_ARM_PC24: |
Daniel Jacobowitz | c2e2611 | 2005-12-14 22:04:22 +0000 | [diff] [blame] | 141 | case R_ARM_CALL: |
| 142 | case R_ARM_JUMP24: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | offset = (*(u32 *)loc & 0x00ffffff) << 2; |
| 144 | if (offset & 0x02000000) |
| 145 | offset -= 0x04000000; |
| 146 | |
| 147 | offset += sym->st_value - loc; |
| 148 | if (offset & 3 || |
Kevin Welton | c5f1250 | 2007-05-08 22:05:25 +0100 | [diff] [blame] | 149 | offset <= (s32)0xfe000000 || |
| 150 | offset >= (s32)0x02000000) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | printk(KERN_ERR |
| 152 | "%s: relocation out of range, section " |
| 153 | "%d reloc %d sym '%s'\n", module->name, |
| 154 | relindex, i, strtab + sym->st_name); |
| 155 | return -ENOEXEC; |
| 156 | } |
| 157 | |
| 158 | offset >>= 2; |
| 159 | |
| 160 | *(u32 *)loc &= 0xff000000; |
| 161 | *(u32 *)loc |= offset & 0x00ffffff; |
| 162 | break; |
| 163 | |
Daniel Silverstone | 4731f8b | 2009-03-20 11:11:43 +0100 | [diff] [blame] | 164 | case R_ARM_V4BX: |
| 165 | /* Preserve Rm and the condition code. Alter |
| 166 | * other bits to re-code instruction as |
| 167 | * MOV PC,Rm. |
| 168 | */ |
| 169 | *(u32 *)loc &= 0xf000000f; |
| 170 | *(u32 *)loc |= 0x01a0f000; |
| 171 | break; |
| 172 | |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 173 | case R_ARM_PREL31: |
| 174 | offset = *(u32 *)loc + sym->st_value - loc; |
| 175 | *(u32 *)loc = offset & 0x7fffffff; |
| 176 | break; |
| 177 | |
Paul Gortmaker | ae51e60 | 2009-05-07 16:18:40 +0100 | [diff] [blame] | 178 | case R_ARM_MOVW_ABS_NC: |
| 179 | case R_ARM_MOVT_ABS: |
| 180 | offset = *(u32 *)loc; |
| 181 | offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff); |
| 182 | offset = (offset ^ 0x8000) - 0x8000; |
| 183 | |
| 184 | offset += sym->st_value; |
| 185 | if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS) |
| 186 | offset >>= 16; |
| 187 | |
| 188 | *(u32 *)loc &= 0xfff0f000; |
| 189 | *(u32 *)loc |= ((offset & 0xf000) << 4) | |
| 190 | (offset & 0x0fff); |
| 191 | break; |
| 192 | |
Catalin Marinas | b749315 | 2010-06-21 15:11:38 +0100 | [diff] [blame] | 193 | #ifdef CONFIG_THUMB2_KERNEL |
Catalin Marinas | adca6dc | 2009-07-24 12:32:59 +0100 | [diff] [blame] | 194 | case R_ARM_THM_CALL: |
| 195 | case R_ARM_THM_JUMP24: |
| 196 | upper = *(u16 *)loc; |
| 197 | lower = *(u16 *)(loc + 2); |
| 198 | |
| 199 | /* |
| 200 | * 25 bit signed address range (Thumb-2 BL and B.W |
| 201 | * instructions): |
| 202 | * S:I1:I2:imm10:imm11:0 |
| 203 | * where: |
| 204 | * S = upper[10] = offset[24] |
| 205 | * I1 = ~(J1 ^ S) = offset[23] |
| 206 | * I2 = ~(J2 ^ S) = offset[22] |
| 207 | * imm10 = upper[9:0] = offset[21:12] |
| 208 | * imm11 = lower[10:0] = offset[11:1] |
| 209 | * J1 = lower[13] |
| 210 | * J2 = lower[11] |
| 211 | */ |
| 212 | sign = (upper >> 10) & 1; |
| 213 | j1 = (lower >> 13) & 1; |
| 214 | j2 = (lower >> 11) & 1; |
| 215 | offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) | |
| 216 | ((~(j2 ^ sign) & 1) << 22) | |
| 217 | ((upper & 0x03ff) << 12) | |
| 218 | ((lower & 0x07ff) << 1); |
| 219 | if (offset & 0x01000000) |
| 220 | offset -= 0x02000000; |
| 221 | offset += sym->st_value - loc; |
| 222 | |
| 223 | /* only Thumb addresses allowed (no interworking) */ |
| 224 | if (!(offset & 1) || |
| 225 | offset <= (s32)0xff000000 || |
| 226 | offset >= (s32)0x01000000) { |
| 227 | printk(KERN_ERR |
| 228 | "%s: relocation out of range, section " |
| 229 | "%d reloc %d sym '%s'\n", module->name, |
| 230 | relindex, i, strtab + sym->st_name); |
| 231 | return -ENOEXEC; |
| 232 | } |
| 233 | |
| 234 | sign = (offset >> 24) & 1; |
| 235 | j1 = sign ^ (~(offset >> 23) & 1); |
| 236 | j2 = sign ^ (~(offset >> 22) & 1); |
| 237 | *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) | |
| 238 | ((offset >> 12) & 0x03ff)); |
| 239 | *(u16 *)(loc + 2) = (u16)((lower & 0xd000) | |
| 240 | (j1 << 13) | (j2 << 11) | |
| 241 | ((offset >> 1) & 0x07ff)); |
Catalin Marinas | adca6dc | 2009-07-24 12:32:59 +0100 | [diff] [blame] | 242 | break; |
| 243 | |
Catalin Marinas | 8dd4774 | 2010-06-21 15:10:37 +0100 | [diff] [blame] | 244 | case R_ARM_THM_MOVW_ABS_NC: |
| 245 | case R_ARM_THM_MOVT_ABS: |
| 246 | upper = *(u16 *)loc; |
| 247 | lower = *(u16 *)(loc + 2); |
| 248 | |
| 249 | /* |
| 250 | * MOVT/MOVW instructions encoding in Thumb-2: |
| 251 | * |
| 252 | * i = upper[10] |
| 253 | * imm4 = upper[3:0] |
| 254 | * imm3 = lower[14:12] |
| 255 | * imm8 = lower[7:0] |
| 256 | * |
| 257 | * imm16 = imm4:i:imm3:imm8 |
| 258 | */ |
| 259 | offset = ((upper & 0x000f) << 12) | |
| 260 | ((upper & 0x0400) << 1) | |
| 261 | ((lower & 0x7000) >> 4) | (lower & 0x00ff); |
| 262 | offset = (offset ^ 0x8000) - 0x8000; |
| 263 | offset += sym->st_value; |
| 264 | |
| 265 | if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS) |
| 266 | offset >>= 16; |
| 267 | |
| 268 | *(u16 *)loc = (u16)((upper & 0xfbf0) | |
| 269 | ((offset & 0xf000) >> 12) | |
| 270 | ((offset & 0x0800) >> 1)); |
| 271 | *(u16 *)(loc + 2) = (u16)((lower & 0x8f00) | |
| 272 | ((offset & 0x0700) << 4) | |
| 273 | (offset & 0x00ff)); |
| 274 | break; |
Catalin Marinas | b749315 | 2010-06-21 15:11:38 +0100 | [diff] [blame] | 275 | #endif |
Catalin Marinas | 8dd4774 | 2010-06-21 15:10:37 +0100 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | default: |
| 278 | printk(KERN_ERR "%s: unknown relocation: %u\n", |
| 279 | module->name, ELF32_R_TYPE(rel->r_info)); |
| 280 | return -ENOEXEC; |
| 281 | } |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | int |
| 287 | apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab, |
| 288 | unsigned int symindex, unsigned int relsec, struct module *module) |
| 289 | { |
| 290 | printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n", |
| 291 | module->name); |
| 292 | return -ENOEXEC; |
| 293 | } |
| 294 | |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 295 | #ifdef CONFIG_ARM_UNWIND |
| 296 | static void register_unwind_tables(struct module *mod) |
| 297 | { |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 298 | int i; |
| 299 | for (i = 0; i < ARM_SEC_MAX; ++i) { |
| 300 | struct arm_unwind_mapping *map = &mod->arch.map[i]; |
| 301 | if (map->unw_sec && map->sec_text) |
| 302 | map->unwind = unwind_table_add(map->unw_sec->sh_addr, |
| 303 | map->unw_sec->sh_size, |
| 304 | map->sec_text->sh_addr, |
| 305 | map->sec_text->sh_size); |
| 306 | } |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static void unregister_unwind_tables(struct module *mod) |
| 310 | { |
Phil Carmody | e5f7772 | 2010-08-19 15:16:37 +0100 | [diff] [blame^] | 311 | int i = ARM_SEC_MAX; |
| 312 | while (--i >= 0) |
| 313 | unwind_table_del(mod->arch.map[i].unwind); |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 314 | } |
| 315 | #else |
| 316 | static inline void register_unwind_tables(struct module *mod) { } |
| 317 | static inline void unregister_unwind_tables(struct module *mod) { } |
| 318 | #endif |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | int |
| 321 | module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, |
| 322 | struct module *module) |
| 323 | { |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 324 | register_unwind_tables(module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | module_arch_cleanup(struct module *mod) |
| 330 | { |
Catalin Marinas | 2e1926e | 2009-02-11 13:09:54 +0100 | [diff] [blame] | 331 | unregister_unwind_tables(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |