xref: /linux/arch/arm/kernel/module.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2  *  linux/arch/arm/kernel/module.c
3  *
4  *  Copyright (C) 2002 Russell King.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Module allocation method suggested by Andi Kleen.
11  */
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/moduleloader.h>
15 #include <linux/kernel.h>
16 #include <linux/elf.h>
17 #include <linux/vmalloc.h>
18 #include <linux/slab.h>
19 #include <linux/fs.h>
20 #include <linux/string.h>
21 
22 #include <asm/pgtable.h>
23 
24 #ifdef CONFIG_XIP_KERNEL
25 /*
26  * The XIP kernel text is mapped in the module area for modules and
27  * some other stuff to work without any indirect relocations.
28  * MODULE_START is redefined here and not in asm/memory.h to avoid
29  * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
30  */
31 extern void _etext;
32 #undef MODULE_START
33 #define MODULE_START	(((unsigned long)&_etext + ~PGDIR_MASK) & PGDIR_MASK)
34 #endif
35 
36 void *module_alloc(unsigned long size)
37 {
38 	struct vm_struct *area;
39 
40 	size = PAGE_ALIGN(size);
41 	if (!size)
42 		return NULL;
43 
44 	area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
45 	if (!area)
46 		return NULL;
47 
48 	return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
49 }
50 
51 void module_free(struct module *module, void *region)
52 {
53 	vfree(region);
54 }
55 
56 int module_frob_arch_sections(Elf_Ehdr *hdr,
57 			      Elf_Shdr *sechdrs,
58 			      char *secstrings,
59 			      struct module *mod)
60 {
61 	return 0;
62 }
63 
64 int
65 apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
66 	       unsigned int relindex, struct module *module)
67 {
68 	Elf32_Shdr *symsec = sechdrs + symindex;
69 	Elf32_Shdr *relsec = sechdrs + relindex;
70 	Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
71 	Elf32_Rel *rel = (void *)relsec->sh_addr;
72 	unsigned int i;
73 
74 	for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
75 		unsigned long loc;
76 		Elf32_Sym *sym;
77 		s32 offset;
78 
79 		offset = ELF32_R_SYM(rel->r_info);
80 		if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
81 			printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
82 				module->name, relindex, i);
83 			return -ENOEXEC;
84 		}
85 
86 		sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
87 
88 		if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
89 			printk(KERN_ERR "%s: out of bounds relocation, "
90 				"section %d reloc %d offset %d size %d\n",
91 				module->name, relindex, i, rel->r_offset,
92 				dstsec->sh_size);
93 			return -ENOEXEC;
94 		}
95 
96 		loc = dstsec->sh_addr + rel->r_offset;
97 
98 		switch (ELF32_R_TYPE(rel->r_info)) {
99 		case R_ARM_ABS32:
100 			*(u32 *)loc += sym->st_value;
101 			break;
102 
103 		case R_ARM_PC24:
104 		case R_ARM_CALL:
105 		case R_ARM_JUMP24:
106 			offset = (*(u32 *)loc & 0x00ffffff) << 2;
107 			if (offset & 0x02000000)
108 				offset -= 0x04000000;
109 
110 			offset += sym->st_value - loc;
111 			if (offset & 3 ||
112 			    offset <= (s32)0xfc000000 ||
113 			    offset >= (s32)0x04000000) {
114 				printk(KERN_ERR
115 				       "%s: relocation out of range, section "
116 				       "%d reloc %d sym '%s'\n", module->name,
117 				       relindex, i, strtab + sym->st_name);
118 				return -ENOEXEC;
119 			}
120 
121 			offset >>= 2;
122 
123 			*(u32 *)loc &= 0xff000000;
124 			*(u32 *)loc |= offset & 0x00ffffff;
125 			break;
126 
127 		default:
128 			printk(KERN_ERR "%s: unknown relocation: %u\n",
129 			       module->name, ELF32_R_TYPE(rel->r_info));
130 			return -ENOEXEC;
131 		}
132 	}
133 	return 0;
134 }
135 
136 int
137 apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
138 		   unsigned int symindex, unsigned int relsec, struct module *module)
139 {
140 	printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
141 	       module->name);
142 	return -ENOEXEC;
143 }
144 
145 int
146 module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
147 		struct module *module)
148 {
149 	return 0;
150 }
151 
152 void
153 module_arch_cleanup(struct module *mod)
154 {
155 }
156